In: Computer Science
You are writing a billing program that adds up the items a user enters. Your program should use a loop and prompt the user for the item number for each item in inventory to add to the bill. (You can ask the user for the number of items you will enter and use a counter controlled loop or a sentinel controlled loop that has a stop number. Either way is fine.) Here is the table of items in inventory:
Item Value
1 209.00
2 129.50
3 118.00
4 232.00
(So if your program starts asking the user for the items they are purchasing and they enter item 2 with quantity 2, and item 3 with quantity 1. The bill should be 377.00. On the next run if your program asks the user for items in the loop and they give enter each item with quantity of 1. The bill should be 688.50
[You must use an IF or IF/Else Structure to look up the item value for the item number they give or you are missing an objective on this assignment.] Use Java and NetBeans *.0 version to solve this. Must use a simple loop and nested if statement.
Answer:
import java.util.Scanner; public class CalculateBill { public static void main( String[] args ) { int item,qty; double price=0,total_price=0; char choice; do{ Scanner s=new Scanner(System.in); System.out.println("\nEnter the item no : "); item=s.nextInt(); System.out.println("\nEnter the total no of items : "); qty=s.nextInt(); //Assign each item price in the inventory according to the item no //and multiply the quantity of the items with the price if(item==1) { price=209.00 * qty; } else if(item==2) { price=129.50 * qty; } else if(item==3) { price=118.00 * qty; } else if(item==4) { price=232.00 * qty; } //add the total price in the end total_price+=price; //Ask the user user continuously if they want to add another item or not System.out.println("\nDo you want to add another item? \nPress Y or N"); choice=s.next().charAt(0); } while(choice=='Y' || choice=='y' ); System.out.println("\nYour Total Bill is: "+total_price); } }
Output: