抽象概念任务

时间:2012-12-01 00:38:13

标签: java implementation abstract

所以我的问题直接适合我的作业。在你问之前,是的,我已经查看过其他问题了,我看过java文档试着帮助我,但我只是理解了很多......

你已成为一名餐厅大亨。你拥有几家快餐连锁店。但是,您现在需要设定一个标准,您的所有快餐连锁店必须遵循这一标准,以使您的软件全面统一。所有餐厅都会有一些相同的规则。

创建一个名为Restaurant

的抽象类

创建一个函数/方法,在调用时打印餐厅的名称。

创建一个名为总价格的抽象函数/方法

创建一个名为菜单项的抽象函数/方法

创建抽象函数/方法名称位置

创建一个名为McDonalds的类,扩展了Restaurant

实施所有抽象方法

添加逻辑,以便总价格方法/功能将给出包含6%税的膳食总价

添加一个返回名为hasPlayPlace的布尔值的方法。当此位置具有游戏场地时,返回true

创建一个构造函数,用于设置Mcdonalds,location和hasPlayPlace的名称

public class McDonalds extends Restaurant {
    private String name;
    private String location;
    private boolean hasPlayPlace;
    Scanner input = new Scanner(System.in);

    public McDonalds (String name, String location, boolean hasPlayPlace) {
        setName(name);
        setLocation(location);
        setHasPlayPlace(hasPlayPlace);
    }

    McDonalds location1 = new McDonalds("McDonalds", "Kirkman", false);
    McDonalds location2 = new McDonalds("McDonalds 2", "International Dr.", true);

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location){
        this.location = location;
    }
    public boolean isHasPlayPlace() {
        return hasPlayPlace;
    }
    public void setHasPlayPlace(boolean hasPlayPlace) {
        this.hasPlayPlace = hasPlayPlace;
    }


    public void totalPrice() {
        double totalPrice = 0;
        double tax = 0.06;
        totalPrice += (totalPrice * tax);       
    }

    public void menuItems() {
            //some syntax is wrong in this method
        double mcChicken = 1;
        double fries = 1.25;
        System.out.println("1. Mc Chicken $1");
        System.out.println("2. Fries $1.25");
        int choice = input.nextInt();
        switch (choice){
        case 1: mcChicken *= tax;
        case 2: fries *= tax;
        }

    }

    public void location() {
        //Don't know what's supposed to go in here.
        //But I've implemented the method as I was supposed to.
    }

}

这一切都有意义基本上是我所要求的。 定位方法应该怎么做? 在这堂课中,吸气剂和制定者有什么用,我做得对吗?

4 个答案:

答案 0 :(得分:2)

名称和位置应为String而不是char

我喜欢在构造函数中调用setter的样式,因为它是一种代码重用形式,特别是如果对这些值进行特殊检查,例如不是null - 调用setter意味着你只能检查它一个地方。

您的代码无法编译,但您已关闭:

McDonalds location1 = new McDonalds("Some name", "Kirkman", true);

您的计算也有点偏差:

double tax = 0.06;
totalPrice *= (tax + 1);

但是,这很危险,因为如果调用两次,它将两次添加税。最好让方法返回含税价格,每次计算它。具有副作用的吸气剂是设计错误。就是这样:

public double getTaxIncPrice() {
    double tax = 0.06;
    return totalPrice * (1 + tax);
}

答案 1 :(得分:2)

1)您的构造函数结构正常,但您应该使用String而不是char来获取名称和位置。 char只能容纳一个字符。

2)您可以创建一个类的多个实例:

McDonalds location1 = new McDonalds("McDonald", "Kirkman", true);
McDonalds location2 = new McDonalds("McDonald2", "Kirkman", false);

3)您应该将税率添加到价格中,而不是总和:price * 1.06。打印总价时,请注意不要更改不含税的价格。

答案 2 :(得分:1)

除了Bohemian指出的问题(名称和位置应该是String,而不是char):

您的构造函数调用将需要String参数的引号:

McDonalds location1 = new McDonalds("McDonald", "Kirkman", true);

并且您的税务计算不正确 - 您需要将总金额乘以税率,并且您必须等到实际总计才能进行计算。

答案 3 :(得分:0)

刚编辑我的代码并提供了问题。到目前为止,你的家伙输入帮助

public String TacoBellSauce(String fire, String hot, String mild) {
    System.out.println("What sauce would you like to have?");
    System.out.println("1. Fire");
    System.out.println("2. Hot");
    System.out.println("3. Mild");
    int choice = input.nextInt();
    switch(choice) {
    case 1:
        return fire;
    case 2:
        return hot;
    case 3:
        return mild;
    }
    return null;
}

这也是我对TacoBell类的方法。我如何在Test类中返回它?它说要在TacoBell中制作一个方法,它返回一串我想要的辣酱。但随后它在测试课程中说要调用hotsauce并返回热门。我没有创造那个课程,因为我专注于用麦当劳和TacoBell纠正一切。

相关问题