如何在java POS中添加项目

时间:2014-10-14 04:07:32

标签: java

    String item1="Burger";
    String item2="Softdrink";
    int burger_price=40, sofdrink_price=20;
    int x=0;
    while(x==0){
    System.out.println("Select item [1] burger [2] sofdrink");
    int select=scan.nextInt();



    switch(select){
    case 1: 
            System.out.println("Enter Quantity: ");
            int qty=scan.nextInt();

            int total=qty*burger_price;
            System.out.println("Item: Burger");
            System.out.println("Quanity: "+qty);
            System.out.println("Total: "+total);
            x=1;
        break;
    case 2:
            System.out.println("Enter Quantity: ");
            int qty2=scan.nextInt();

            int total2=qty2*sofdrink_price;
            System.out.println("Item: Softdrink");
            System.out.println("Quanity: "+qty2);
            System.out.println("Total: "+total2);
            x=1;
        break;
    default: System.out.println("Select [1] and [2] only");
    }
    }//end while`

2 个答案:

答案 0 :(得分:0)

首先,你需要某种循环......

boolean done = false;
do {
    //...
} while (!done);

然后你需要为用户提供一些退出循环的方法

boolean done = false;
do {
    System.out.println("Select item [1] burger [2] sofdrink or [0] when you're done");
    //...
    switch(select){
        case 0:
            done = true;
            break;
    }
} while (!done);

现在,你需要保持一个已经订购的东西的运行记录,因为你只有两个项目,你可以使用两个变量......

int burgerCount = 0;
int softDrinkCount = 0;
boolean done = false;
do {
    //...

现在您可以根据用户选择来增加计数器......

现在,自动地,您需要向用户询问两件事,他们想要什么以及他们想要多少,您可以通过一点点聪明来简化过程......

System.out.println("Select item [1] burger [2] sofdrink or [0] when you're done");
int select=scan.nextInt();
scan.nextLine();
switch (select) {
    case 0:
        done = true;
        break;
    case 1:
    case 2:
        System.out.println("Enter Quantity: ");
        int qty=scan.nextInt();
        scan.nextLine();
        switch (select) {
            case 1:
                burgerCount += qty;
                break;
            case 1:
                softDrinkCount += qty;
                break;
        }
        break;
}
// Show details

请确保在阅读nextLine后致电nextInt,因为缓冲区中仍有回车符;)

然后你可以显示运行计数......

    //...
    System.out.println("You've ordered...");
    double burgerTally = burger_price * burgerCount;
    double drinkTally = burger_price * drinkTally;
    System.out.println(burgerCount + " burgers @ $" + burger_price + " for a sub total of $" + burgerTally);
    System.out.println(softDrinkCount + " soft drinks @ $" + sofdrink_price + " for a sub total of $" + drinkTally);
    System.out.println("For a total of $" + (burgerTally + drinkTally));
} while (!done);

由于代码的布局方式,无论您选择什么,都会显示计数...

现在,如果你有更多的项目,那么一个数组会非常方便,并且会减少你可能需要的所有代码...

答案 1 :(得分:0)

MadProgrammer给出了一个非常好的解释。我把它作为代码放在一起。我尝试对原始代码进行最少的更改,以便您更好地理解。

import java.util.Scanner;

    public class BugerCheese {

        public static void main(String[] args) {
            Scanner scan=new Scanner(System.in);
            int burger_price=40, sofdrink_price=20;
            int qty1=0, qty2=0;
            while(true){
                System.out.println("Select item [1] burger [2] sofdrink");
                int select=scan.nextInt();
                switch(select){
                    case 1: 
                        System.out.println("Enter Quantity: ");
                        qty1 += scan.nextInt();
                        break;
                    case 2:
                        System.out.println("Enter Quantity: ");
                        qty2 += scan.nextInt();
                        break;
                    default: System.out.println("Select [1] and [2] only");
                }
                System.out.println("Enter 9 to add more items. Enter any other key to calculate total");
                if(9!=scan.nextInt()){
                    break;
                }
            }
            if(qty1>0){
                int total=qty1*burger_price;
                System.out.println("Item: Burger");
                System.out.println("Quanity: "+qty1);
                System.out.println("Total: "+total);
            }

            if(qty2>0){
                int total2=qty2*sofdrink_price;
                System.out.println("Item: Softdrink");
                System.out.println("Quanity: "+qty2);
                System.out.println("Total: "+total2);   
            }
            System.out.println("GrandTotal: "+(qty1*burger_price+qty2*sofdrink_price));
        }
    }