2012-12-01 50 views
-2

所以我的问题直接针对我的作业。在你问之前,是的,我看过其他问题,我已经看过java文档来尝试和帮助我,但我只理解这么多..抽象概念作业

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

创建抽象类的命名餐厅

创建函数/方法调用时,将打印的餐厅的名字。

创建一个名为总价

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

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

创建一个名为麦当劳类,它扩展餐厅

实施所有抽象方法

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

添加一个方法,该方法返回一个名为hasPlayPlace的布尔值。在这个位置有一个playplace

创建一个构造函数,将设置在麦当劳,位置的名称,并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. 
    } 

} 

返回true,所有这一切意义基本上是我要问什么。 定位方法应该怎么办? 这个班级中的吸气和吸气设备有什么用处,我做对了吗?

+1

麦当劳现在有名字!? (当然除了麦当劳以外) – NullUserException

+3

不要使用char作为名字,使用String。字符串文字用“”写成,所以用“麦当劳”而不是“麦当劳”(类似于吉尔曼) – Vertex

+3

这是一个很混乱和令人困惑的问题。你打电话给你的方法怎么样?你问的是什么**问题**。 (超越“有人可以修复看起来错误的一切吗?”)你提到的菜单与任何东西有什么关系? “我可以为一个构造函数使用多个对象吗?”什么意思? – millimoose

回答

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); 
} 
2

1)你的构造器是结构化的罚款,但你应该使用String!而非char S为名称和位置。 A char将只保存一个字符。

2)你可以创建一个类的多个实例:price * 1.06

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

3)您应作为一个百分比,而不是和税收增加的价格。打印总价时,请注意不要更改不含税的价格。

1

除此之外波希米亚指出(名称和位置应该是字符串,而不是字符)的问题:

你的构造函数调用需要的字符串参数报价:

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

和税务计算不正确 - 您需要将总金额乘以税率百分比,而您必须等到实际有总计才能完成计算。

+0

是的,我无意中把“+”当它应该是“*” – RazaHuss

+3

这仍然是不正确的。例如,如果你的总金额是100美元,税额是6%,那么你想要的结果是106美元 - 所以你需要totalPrice + =(totalPrice * tax) – GreyBeardedGeek

+0

是的,这很有道理 – RazaHuss

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中制作一种方法,返回一串我想要的辣酱。但之后它说在测试班内打电话给热点,并返回热点。我还没有创建过这门课,因为我专注于用麦当劳和TacoBell纠正一切。

+0

碰到了答案? – RazaHuss