Muvhango runs a small Fruits and Vegetables sphaza shop. He sells oranges, bananas, apples, tomatoes and potatoes. The table 2 below shows the price list for the items sold.
Code Description Unit price(R)
A or a Apples 1.50
B or b Bananas 2.00
O or o Oranges 2.50
P or p Potatoes 6.00
T or t Tomatoes 10.00
Muvhango gives a 10% discount if the total amount due exceeds R20. Create an application for Muvhango that will allow a customer to select an item to buy, enter the quantity and thereafter be provided with the total amount due. The customer must be given an opportunity to make payment and be given change. Use the switch statement to determine the unit price.
. here is my code
package everythingfishyapp; import java.util.Scanner; /** * * @author SETH */ public class EveryThingFishyApp { /** * @param args the command line arguments */ public static void main(String[] args) { // declare variables char code; double dis = 10,totAmtDue,change,price; int payment,quantity; String choice = ""; final double PRICE1 = 1.50, PRICE2 = 2.00,PRICE3 = 2.50,PRICE4 = 6.00,PRICE5 = 10.00; Scanner sc = new Scanner(System.in); //get the user input System.out.print("Please select an item to buy: " + "\n" + "A/a --> Apples" + "\n" + "B/b --> Bananas" + "\n" + "O/o --> Oranges" + "\n" + "P/p --> Potatoes" + "\n" + "T/t --> Tomatoes" + "\n" + "Your choice: "); code = sc.next().charAt(0); price = sc.nextDouble(); quantity = sc.nextInt(); payment = sc.nextInt(); totAmtDue = quantity * price; change = payment - totAmtDue; switch(code){ case'A': case'a': System.out.println("the total amount due is: " + totAmtDue); System.out.println("The change is: " + change);; break; case'B': case'b': System.out.println("the total amount due is: " + totAmtDue); System.out.println("The change is: " + change); break; case'O': case'o': System.out.println("the total amount due is: " + totAmtDue); System.out.println("The change is: " + change); break; case'P': case'p': System.out.println("the total amount due is: " + totAmtDue); System.out.println("The change is: " + change); break; case'T': case't': System.out.println("the total amount due is: " + totAmtDue); System.out.println("The change is: " + change); break; default: System.out.println("You entered an invalid code"); } //display outcome System.out.println(choice); System.out.println("Please enter a quantity: "); System.out.println("Please make a payment: "); } }
here is the output
Please select an item to buy: A/a --> Apples B/b --> Bananas O/o --> Oranges P/p --> Potatoes T/t --> Tomatoes Your choice: a b Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextDouble(Scanner.java:2413) at everythingfishyapp.EveryThingFishyApp.main(EveryThingFishyApp.java:39) Java Result: 1 BUILD SUCCESSFUL (total time: 32 seconds)