3
public class menucard 
{
    public static void main (String [] args)throws IOException
    {
        Scanner input= new Scanner(System.in);
        int tea=5,coffee=7,samosa=8,idly=15,biryani=50,talawa=35,item;
        System.out.print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
        int size=input.nextInt();

        for(int i=1;i<=size;i++)
        {
            System.out.print("Order Item No"+i+":");
            int choice=input.nextInt();
            switch(choice)
        {
        case 1:
            System.out.println("TEA : INR "+tea);
            break;
        case 2:
            System.out.println("COFFEE : INR "+coffee);
            break;
        case 3:
            System.out.println("SAMOSA : INR "+samosa);
            break;
        case 4:
            System.out.println("IDLY : INR "+idly);
            break;
        case 5:
            System.out.println("BIRYANI : INR "+biryani);
            break;
        case 6:
            System.out.println("TALAAWA : INR "+talawa);
            break;
            default:
            System.out.println("INVALID ENTRY");
        }

    }
    }
}

出力は次のとおりです。

MENU 
1.TEA     :5
2.COFFEE  :7
3.SAMOSA  :8
4.IDLY    :15
5.BIRYANI :50
6.TALAAWA :35
How many items you want to order:5
Order Item No1:3
SAMOSA : INR 8
Order Item No2:6
TALAAWA : INR 35
Order Item No3:1
TEA : INR 5
Order Item No4:3
SAMOSA : INR 8
Order Item No5:4
IDLY : INR 15

それらのアイテムの価格を追加して請求書を生成する必要があります。

そのアイテムをどのように追加すればよいですか..?

4

7 に答える 7

1

switch ステートメントを使用しないでください。彼らはあなたの人生を難しくするだけです。

私は次のことをしていると思います:

public interface MenuCard {
    void addToMenuItems(MenuItem item);
    List<MenuItem> getMenuItems();
    void renderTo(PrintStream printStream);
}

public interface MenuItem {
    BigDecimal getPrice();
    String getDescription();
}

public interface Order {
    void add(MenuItem item);

    //total price of all ordered items
    BigDecimal getPrice();
}

public static void main(String[] args) {
    Scanner input= new Scanner(System.in);
    MenuCard menuCard = getMenuCard(); //from database or wherever it is stored
    menuCard.renderTo(System.out);
    Order order = createNewOrder();
    int itemIndex = input.nextInt();
    while(itemIndex > 0) {
        order.add(menuCard.getMenuItems().get(itemIndex-1));
        itemIndex = input.nextInt();
    }
    //now generate bill for order
}
于 2013-02-13T18:43:13.620 に答える
0

これがswitch-caseプラスのあるものですenum

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Menucard {
     private static enum Item {
        TEA (5,"Tea"), COFFEE(7,"Coffee"), SAMOSA(8,"Samosa"), IDLY(15,"Idly"), BIRYANI(50,"Biryani"), TALAWA(35,"Talawa");
        private int price;
        private String name;
        private Item(int price, String name) {
            this.price = price;
            this.name = name;
        }
        public int getPrice() {
            return price;
        }
        public String getName(){
            return name;
        }
     };
    public static void main (String [] args)throws IOException
    {
        Scanner input= new Scanner(System.in);
        List<Item> itemList = new ArrayList<Item>();
        float totalAmt = 0;
        System.out.print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
        int size=input.nextInt();
        for(int i=1;i<=size;i++)
        {
            System.out.print("Order Item No"+i+":");
            int choice=input.nextInt();
            Item selectedItem = null;
            switch(choice)
            {
            case 1:
                selectedItem = Item.TEA;
                break;
            case 2:
                selectedItem = Item.COFFEE;
                break;
            case 3:
                selectedItem = Item.SAMOSA;
                break;
            case 4:
                selectedItem = Item.IDLY;
                break;
            case 5:
                selectedItem = Item.BIRYANI;
                break;
            case 6:
                selectedItem = Item.TALAWA;
                break;
            default:
                System.out.println("INVALID ENTRY");
            }
            if(selectedItem != null){
                itemList.add(selectedItem);
                System.out.println(selectedItem.getName() + " Price INR: " + selectedItem.getPrice());
                totalAmt += selectedItem.getPrice();
            }
        }
        System.out.println("Total Bill: " + totalAmt);
    }
}

これなしswitch-case

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Menucard {
     private static enum Item {
        TEA (5,"Tea"), COFFEE(7,"Coffee"), SAMOSA(8,"Samosa"), IDLY(15,"Idly"), BIRYANI(50,"Biryani"), TALAWA(35,"Talawa");
        private int price;
        private String name;
        private Item(int price, String name) {
            this.price = price;
            this.name = name;
        }
        public int getPrice() {
            return price;
        }
        public String getName(){
            return name;
        }
     };
    public static void main (String [] args)throws IOException
    {
        Scanner input= new Scanner(System.in);
        List<Item> itemList = new ArrayList<Item>();
        float totalAmt = 0;
        System.out.print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
        int size=input.nextInt();
        for(int i=1;i<=size;i++)
        {
            System.out.print("Order Item No"+i+":");
            int choice=input.nextInt();
            Item selectedItem = null;
            if(choice < 1 || choice > 6){
                System.out.println("Invalid selection");
                i--;
                continue;
            }
            selectedItem = Item.values()[choice-1];
            if(selectedItem != null){
                itemList.add(selectedItem);
                System.out.println(selectedItem.getName() + " Price INR: " + selectedItem.getPrice());
                totalAmt += selectedItem.getPrice();
            }
        }
        System.out.println("Total Bill: " + totalAmt);
    }
}
于 2013-02-07T09:03:00.030 に答える
0

あなたがする必要があるのは、最初にtotal_amountと言う変数を宣言することです

public class menucard 
{
public static void main (String [] args)throws IOException
{
    Scanner input= new Scanner(System.in);
    int tea=5,coffee=7,samosa=8,idly=15,biryani=50,talawa=35,item,total_amount=0;
    ............................................
    ........................................

break ステートメントの前に、switch ケースでこの total_mount 変数を使用します。

switch(choice)
    {
    case 1:
        System.out.println("TEA : INR "+tea);
        total_amount = total_amount + tea;
        break;
    case 2:
        System.out.println("COFFEE : INR "+coffee);
        total_amount = total_amount +coffee;
        break;
.................
.................

他の場合も同様です。最後に、for ループの後、合計金額を出力できます。ありがとう

于 2013-02-07T06:16:27.877 に答える
0
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String s = "   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n";
    int tea = 5, coffee = 7, samosa = 8, idly = 15, biryani = 50, talawa = 35;
    System.out
            .print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
    int size = input.nextInt();
    ArrayList<String> itemsList = new ArrayList<String>();
    ArrayList<Integer> price = new ArrayList<Integer>();
    for (int i = 1; i <= size; i++) {
        System.out.print("Order Item No" + i + ":");
        int choice = input.nextInt();
        switch (choice) {
        case 1:

            System.out.println("TEA : INR " + tea);
            itemsList.add("Tea");
            price.add(tea);
            System.out.println(s);
            break;
        case 2:
            System.out.println("COFFEE : INR " + coffee);
            itemsList.add("Coffee");
            price.add(coffee);
            System.out.println(s);
            break;
        case 3:
            System.out.println("SAMOSA : INR " + samosa);
            itemsList.add("Samosa");
            price.add(samosa);
            System.out.println(s);
            break;
        case 4:
            System.out.println("IDLY : INR " + idly);
                            itemsList.add("Idly");
            price.add(idly);
            System.out.println(s);
            break;
        case 5:
            System.out.println("BIRYANI : INR " + biryani);
            itemsList.add("Biryani");
            price.add(biryani);
            System.out.println(s);
            break;
        case 6:
            System.out.println("TALAAWA : INR " + talawa);
            itemsList.add("Talawa");
            price.add(talawa);
            System.out.println(s);
            break;
        default:
            System.out.println("INVALID ENTRY");
        }

    }
    int total = 0;
    System.out.println("Item        Price");
    for (int i = 0; i < itemsList.size(); i++) {
        System.out.print(itemsList.get(i) + "       ");
        System.out.println(price.get(i));
        total = total + price.get(i);

    }
    System.out.println("Total       " + total);
}

出力:

        MENU 
1.TEA     :5
2.COFFEE  :7
3.SAMOSA  :8
4.IDLY    :15
5.BIRYANI :50
6.TALAAWA :35
    How many items you want to order:3
Order Item No1:1
TEA : INR 5
   MENU 
1.TEA     :5
2.COFFEE  :7
3.SAMOSA  :8
4.IDLY    :15
5.BIRYANI :50
6.TALAAWA :35

Order Item No2:2
COFFEE : INR 7
   MENU 
1.TEA     :5
2.COFFEE  :7
3.SAMOSA  :8
4.IDLY    :15
5.BIRYANI :50
6.TALAAWA :35

Order Item No3:3
SAMOSA : INR 8
   MENU 
1.TEA     :5
2.COFFEE  :7
3.SAMOSA  :8
4.IDLY    :15
5.BIRYANI :50
6.TALAAWA :35

Item        Price
Tea         5
Coffee      7
Samosa      8
Total       20

各ケースの実行後にメニューを印刷し、適切な方法で請求書を生成するようにコードを編集しました。

于 2013-02-07T06:13:47.687 に答える
0

以下のコードを試してください:

    import java.io.IOException;
import java.util.Scanner;

public class menucard 
{
    public static void main (String [] args)throws IOException
    {
        int total =0;
        int item = 0;
        Scanner input= new Scanner(System.in);
        int tea=5,coffee=7,samosa=8,idly=15,biryani=50,talawa=35;
        System.out.print("   MENU \n1.TEA     :5\n2.COFFEE  :7\n3.SAMOSA  :8\n4.IDLY    :15\n5.BIRYANI :50\n6.TALAAWA :35\n \tHow many items you want to order:");
        int size=input.nextInt();

        for(int i=1;i<=size;i++)
        {
            System.out.print("Order Item No"+i+":");
            int choice=input.nextInt();
            switch(choice)
        {
            case 1:
            {
                System.out.println("TEA : INR "+tea);
                item++;
                total+=tea;
                display(item, total);
                break;
            }
            case 2:
            {
                System.out.println("COFFEE : INR "+coffee);
                item++;
                total+=coffee;
                display(item, total);
                break;
            }
            case 3:
            {

                System.out.println("SAMOSA : INR "+samosa);
                item++;
                total+=samosa;
                display(item, total);
                break;
            }
            case 4:
            {
                System.out.println("IDLY : INR "+idly);
                item++;
                total+=idly;
                display(item, total);
                break;
            }
            case 5:
            {
                System.out.println("BIRYANI : INR "+biryani);
                item++;
                total=+biryani;
                display(item, total);
                break;
            }
            case 6:
            {
                System.out.println("TALAAWA : INR "+talawa);
                item++;
                total=+talawa;
                display(item, total);
                break;
            }
                default:
                System.out.println("INVALID ENTRY");
            }

    }

    }

    public static void display(int item, int price)
    {
         System.out.println("Total bill for "+item+" = "+price+" INR  ");
    }
}
于 2013-02-07T06:01:06.893 に答える
0
int sum = 0;
for(int i=1;i<=size;i++)
        {
            System.out.print("Order Item No"+i+":");
            int choice=input.nextInt();
            switch(choice)
        {
        case 1:
            System.out.println("TEA : INR "+tea);
            sum +=tea;
            break;
        case 2:
            System.out.println("COFFEE : INR "+coffee);
             sum +=coffee;
            break;
... etc

    }
}

sum には合計が必要です

于 2013-02-07T05:56:44.517 に答える