煎饼果子系统(面向对象)


package com.example;

public class EggPk extends Pencake {
public EggPk(String name, double value) {
super(name, value);
}

@Override
public void taste() {
    System.out.println("肉松鸡蛋味");
}

}




package com.example;

public class FishPk extends Pencake{

    public FishPk(String name, double value) {
        super(name, value);
    }

    @Override
    public void taste() {
        System.out.println("小鱼干味");
    }
}



package com.example;

public class HotdogPk extends Pencake {
public HotdogPk(String name, double value) {
super(name, value);
}

@Override
public void taste() {
    System.out.println("热狗味");
}

}


package com.example;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    buyPencake();
}

private static void buyPencake() {
    int totalmoney = 0;
    Pencake[] pencakeList = new Pencake[1024];
    int j = 0;
    Scanner scanner = new Scanner(System.in);
    System.out.println("煎饼果子售卖系统:" + "\n" +
            "1:充值金额" + "\n" +
            "2:购买煎饼果子" + "\n" +
            "3:打印账单" + "\n" +
            "4:退出系统");
    while (true) {
        if (scanner.hasNextInt()) {
            String type = scanner.nextLine();
            if (type.equals("1")) {
                System.out.println("请输入您要充值的金额:");
                int czMoney = Integer.valueOf(scanner.nextLine());
                if (czMoney >= 100 && czMoney < 200) {
                    totalmoney += czMoney + 30;
                    System.out.println("充值" + czMoney + "元成功,实际充值" + (czMoney + 30) + "元");
                } else if (czMoney >= 200 && czMoney < 300) {
                    totalmoney += czMoney + 80;
                    System.out.println("充值" + czMoney + "元成功,实际充值" + (czMoney + 80) + "元");
                } else if (czMoney >= 300) {
                    totalmoney += czMoney + 130;
                    System.out.println("充值" + czMoney + "元成功,实际充值" + (czMoney + 130) + "元");
                }
            } else if (type.equals("2")) {
                if (totalmoney <= 0) {
                    System.out.println("不充钱想买啥");
                } else {
                    System.out.println("输入你要买的煎饼果子数量");
                    int pkCount = Integer.valueOf(scanner.nextLine());
                    for (int i = 0; i < pkCount; i++) {
                        System.out.println("输入你要买的煎饼果子种类:");
                        String pkType = scanner.nextLine();
                        if (pkType.equals("1")) {
                            YuanweiPk yuanweiPk = new YuanweiPk("原味", 5);
                            totalmoney -= 5;
                            pencakeList[j] = yuanweiPk;
                            j++;
                            System.out.println("购买原味成功");
                        } else if (pkType.equals("2")) {
                            HotdogPk hotdogPk = new HotdogPk("热狗火腿", 8);
                            totalmoney -= 8;
                            pencakeList[j] = hotdogPk;
                            j++;
                            System.out.println("购买热狗果子成功");
                        } else if (pkType.equals("3")) {
                            FishPk fishPk = new FishPk("小鱼干鸡排", 10);
                            totalmoney -= 10;
                            pencakeList[j] = fishPk;
                            j++;
                            System.out.println("购买小鱼干果子成功");
                        } else if (pkType.equals("4")) {
                            EggPk eggPk = new EggPk("鸡蛋肉松", 7);
                            totalmoney -= 7;
                            pencakeList[j] = eggPk;
                            j++;
                            System.out.println("购买鸡蛋果子成功");
                        }
                    }

                }
            } else if (type.equals("3")) {
                for (int i = 0; i < j; i++) {
                    if (pencakeList[i] instanceof YuanweiPk) {
                        pencakeList[i].print();
                        pencakeList[i].taste();
                    } else if (pencakeList[i] instanceof HotdogPk) {
                        pencakeList[i].print();
                        pencakeList[i].taste();
                    } else if (pencakeList[i] instanceof FishPk) {
                        pencakeList[i].print();
                        pencakeList[i].taste();
                    } else if (pencakeList[i] instanceof EggPk) {
                        pencakeList[i].print();
                        pencakeList[i].taste();
                    }
                }
            } else if (type.equals("4")) {
                System.out.println("欢迎下次光临");
                break;
            } else {
                scanner.next();
                System.out.println("输入错误,请重新输入");
            }
            System.out.println("请重新选择功能:" + "\n" +
                    "1:充值金额" + "\n" +
                    "2:购买煎饼果子" + "\n" +
                    "3:打印账单" + "\n" +
                    "4:退出系统");
        } else {
            scanner.next();
            System.out.println("输入错误,请重新输入");
        }
    }
}

}






package com.example;

public class Pencake {
String name;
double value;

public Pencake(String name, double value) {
    this.name = name;
    this.value = value;
}

public void taste() {

}

public void print() {
    System.out.println(name + value + "钱一个");
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值