Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: switch statement related exercice

1. ## switch statement related exercice

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
"A/a --> Apples"   + "\n" +
"B/b --> Bananas"  + "\n" +
"O/o --> Oranges"  + "\n" +
"P/p --> Potatoes" + "\n" +
"T/t --> Tomatoes" + "\n" +

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

}
}```

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

b
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)```

2. ## Re: switch statement related exercice

Your problem is probably because of how the Scanner class works. There is a problem with calling nextLine after calling other next methods. Read this for an explanation: https://www.geeksforgeeks.org/why-is...ext-functions/

3. ## Re: switch statement related exercice

Originally Posted by Seth Lau.20
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
"A/a --> Apples"   + "\n" +
"B/b --> Bananas"  + "\n" +
"O/o --> Oranges"  + "\n" +
"P/p --> Potatoes" + "\n" +
"T/t --> Tomatoes" + "\n" +

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

}
}```

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

b
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)```

there is a terms exception handling but i have simplified our code very simple way

now you can check the result

i have done with Apple only you should use others; the problem was didn't gave the value of "price " .for specifiq way you have put the price1, price 2 in the different case which was fixed.

```
package com.company;

import java.util.Scanner;

public class Main {
double num;
public static void main(String[] args) {
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
"A/a --> Apples"   + "\n" +
"B/b --> Bananas"  + "\n" +
"O/o --> Oranges"  + "\n" +
"P/p --> Potatoes" + "\n" +
"T/t --> Tomatoes" + "\n" +
code = sc.next().charAt(0);
//price = sc.nextDouble();
System.out.println("how much you want ?1-9kg maximum");
quantity = sc.nextInt();

switch(code){
case 'A':
case 'a':
totAmtDue = quantity * PRICE1;
System.out.println("the total amount due is: " + totAmtDue);
System.out.println("you have paid");
payment = sc.nextInt();
change = payment - totAmtDue;
System.out.println("The change is: " + change);;
break;

default:
System.out.println("You entered an invalid code");
}

}
}```

solving your problem I got stuck with another implementation here. i couldn't able to pass the value of one method to another method. here is the algorithm of the program.

if anyone can give me a code to create a purchasing menu option i will be glad.

4. ## Re: switch statement related exercice

Here's another way to store the data for easy lookup. There's a product code such as 'A' for apple, which can then be used to look up the details of apples.
```import java.util.HashMap;
public static void main(String[] args) {

class Product{
public String name;
public Double price;
Product(String name, Double price){
this.name = name;
this.price = price;
}
public String toString(){
return name + ": \$" + price;
}
}

HashMap<Character, Product> productCodes = new HashMap<Character, Product>();
productCodes.put('A', new Product("Apples", 1.50));
productCodes.put('B', new Product("Bananas", 2.50));
productCodes.put('O', new Product("Oranges", 3.50));
productCodes.put('P', new Product("Potatoes", 4.50));
productCodes.put('T', new Product("Tomatoes", 5.50));

// display prices
System.out.println("Choose product " + productCodes);

} // main