java—如何制作一个菜单来选择不同的项目,然后在驱动程序类中计算总计

vulvrdjw  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(148)

我一直在尝试实现一个菜单,用户可以在其中输入他们是要点菜还是三人菜单(饮料、沙拉和三明治)。我不知道如何使用扫描仪,它会调用不同的类。

public class Trio implements MenuItem{
    private String name;
    private double price;

    public Trio(Sandwich s,Salad sl,Drink d) {
        name=""+s.getName()+" / "+sl.getName()+" / "+d.getName()+" Trio";
        price=0;
        double a=s.getPrice(),b=sl.getPrice(),c=d.getPrice();
        if(a<=b&&a<=c)
            price=b+c;
        else if(b<=a&&b<=c)
            price=a+c;
        else if(c<=b&&c<=a)
            price=a+b;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

}
//Create an interface with required methods 
public interface MenuItem {
    String getName();
    double getPrice();
}

我被困在如何调用不同的类与司机。我在考虑允许用户输入他们想要的数字。

public class Driver {

    public static void main(String[] args) {
        //Create a sandwich object with the given data 
        Sandwich sandwich1=new Sandwich("Cheeseburger",2.75);
        Sandwich sandwich2=new Sandwich("Club Sandwich",2.75);
        //Create salad object with the given data 
        Salad salad1=new Salad("Spinach Salad",1.25);
        Salad salad2=new Salad("Coleslaw",1.25);
        //Create Drink object with given data 
        Drink drink1=new Drink("Orange Soda",1.25);
        Drink drink2=new Drink("Cappuccino",3.50);
        //Create a Trio
        Trio t1=new Trio(sandwich1,salad1,drink1);
        //Display its info 
        System.out.println(""+t1.getName()+" and the price is : $"+t1.getPrice());
        Trio t2=new Trio(sandwich2,salad2,drink2);
        System.out.println(""+t2.getName()+" and the price is : $"+t2.getPrice());

        System.out.println("Welcome to Java Restaurant!");
        System.out.println("Type 1 for Orange Soda:");
    }    
}
public class Drink implements MenuItem {

    private String name;
    private double price;

    public Drink(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }
}
public class Salad implements MenuItem {

      private String name;
    private double price;

    public Salad(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String getName() {
        return name;
    }
    @Override
    public double getPrice() {
        return price;
    }
}
public class Sandwich implements MenuItem {

    //declare and privatize sandwich name and price
    private String name;
    private double price;

    //override sandwich class
    public Sandwich(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String getName() {
        return name;
    }
    @Override
    public double getPrice() {
        return price;
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题