How do I add the numbers I get from the switch chassis pin?

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

    }
    }
}

      

OUTPUT:

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

      

I need to create an invoice by adding the price of this item.

How do I add these elements?

+3


source to share


7 replies


Please don't use switch statements! They just make your life hard.

I am assuming the following:



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
}

      

+1


source


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

    }
}

      



the amount must have the total amount

0


source


Try to enter the code:

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

      

0


source


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

      

Output:

        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

      

I edited the code to print the menu after each case and generated a nice invoice.

0


source


All you have to do is first declare the variable say total_amount ie

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;
    ............................................
    ........................................

      

Now use this variable total_amount in your switch case before the break ie statement

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;
.................
.................

      

and similarly for other cases. Finally, after your cycle, you can print out the total money. Thanks to

0


source


Here is one switch-case

plusenum

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

      

This one without 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);
    }
}

      

0


source


Just take a global static variable, initialize it to zero and keep adding the price value to it in each case.

public static int totalAmount=0;

      

use this in your

public class menucard 
{
public static void main (String [] args)throws IOException
{
public static long totalAmount=0;
    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);
totalAmount+=tea;
        break;
    case 2:
        System.out.println("COFFEE : INR "+coffee);
totalAmount+=tea;
        break;
    case 3:
        System.out.println("SAMOSA : INR "+samosa);
totalAmount+=tea;
        break;
    case 4:
        System.out.println("IDLY : INR "+idly);
totalAmount+=tea;
        break;
    case 5:
        System.out.println("BIRYANI : INR "+biryani);
totalAmount+=tea;
        break;
    case 6:
        System.out.println("TALAAWA : INR "+talawa);
totalAmount+=tea;
        break;
        default:
        System.out.println("INVALID ENTRY");
}

}

System.out.println("Total Amount is "+totalAmount);
}

      

0


source







All Articles