【面向对象项目之点餐系统】

项目需求

项目结构分析 

最终代码 

匹萨父类:

/**
 * @Auther: themyth
 * 父类:匹萨类
 */
public class Pizza {
    //属性
    private String name;//名称
    private int size;//大小
    private int price;//价格
    //方法
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    //展示匹萨信息:
    public String showPizza(){
        return "匹萨的名字是:"+name+"\n匹萨的大小是:"+size+"寸\n匹萨的价格:"+price+"元";
    }
    //构造器
    public Pizza() {
    }
    public Pizza(String name, int size, int price) {
        this.name = name;
        this.size = size;
        this.price = price;
    }
}

培根匹萨:

/**
 * @Auther: themyth
 */
public class BaconPizza extends Pizza {
    //属性:
    private int weight;
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    //构造器:
    public BaconPizza() {
    }
    public BaconPizza(String name, int size, int price, int weight) {
        super(name, size, price);
        this.weight = weight;
    }
    //重写父类showPizza方法:
    @Override
    public String showPizza() {
        return super.showPizza()+"\n培根的克数是:"+weight+"克";
    }
}

水果匹萨: 

/**
 * @Auther: themyth
 */
public class FruitsPizza extends Pizza{
    //属性:
    private String burdening;
    public String getBurdening() {
        return burdening;
    }
    public void setBurdening(String burdening) {
        this.burdening = burdening;
    }
    //构造器:
    public FruitsPizza() {
    }
    public FruitsPizza(String name, int size, int price, String burdening) {
        super(name, size, price);
        this.burdening = burdening;
    }
    //重写父类showPizza方法:
    @Override
    public String showPizza() {
        return super.showPizza()+"\配料水果:"+burdening;
    }
}

测试类:

import java.util.Scanner;
/**
 * @Auther: themyth
 */
public class Test {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) {
        //选择购买匹萨:
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择你想要购买的匹萨(1.培根匹萨 2.水果匹萨):");
        int choice = sc.nextInt();//选择
        //通过工厂获取匹萨:
        Pizza pizza = PizzaStore.getPizza(choice);
        System.out.println(pizza.showPizza());
    }
}

工厂类:

import java.util.Scanner;
/**
 * @Auther: themyth
 */
public class PizzaStore {
    public static Pizza getPizza(int choice){//父类当作方法返回值,返回的是一个具体的对象--->多态的应用之一
        Scanner sc = new Scanner(System.in);
        Pizza p = null;
        switch (choice){
            case 1:
                {
                    System.out.println("请录入培根的克数:");
                    int weight = sc.nextInt();
                    System.out.println("请录入匹萨的大小:");
                    int size = sc.nextInt();
                    System.out.println("请录入匹萨的价格:");
                    int price = sc.nextInt();
                    //将录入的信息封装为培根匹萨的对象:
                    BaconPizza bp = new BaconPizza("培根匹萨",size,price,weight);
                    p = bp;//将一个具体的培根匹萨对象赋值给p

                                                                                                                                                                                        //可以合成一句话->p = new BaconPizza("培根匹萨",size,price,weight);
                }
                break;//也可以在代码块中
            case 2:
                {
                    System.out.println("请录入你想要加入的水果:");
                    String burdening = sc.next();
                    System.out.println("请录入匹萨的大小:");
                    int size = sc.nextInt();
                    System.out.println("请录入匹萨的价格:");
                    int price = sc.nextInt();
                    //将录入的信息封装为水果匹萨的对象:
                    FruitsPizza fp = new FruitsPizza("水果匹萨",size,price,burdening);
                    p = fp;//将一个具体的水果匹萨对象赋值给p

                    //可以合成一句话->p = new FruitsPizza("水果匹萨",size,price,burdening);
                }
                break;//也可以在代码块中
        }
        return p;//返回一个具体的匹萨对象
    }
}
  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TheMythWS

你的鼓励,我的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值