Reputation: 13
The following code consists of a switch case which executes five times asking the customer for the five icecream options ,but the condition is that each time the customer must select a different icecream if he selects the same icecream again it should display an error could someone help me with the above logic please ,or suggest me a better method than switch case.
class Icecreams {
public static void main( String[] args ) throws Exception {
for( int i = 1; i <= 5; i++ ) {
System.out.println("Select an Icecream:\n1.Strawberry\n2.Vanilla\n3.Chocolate\n4.Butterscotch\n5.Black current\n6.Exit");
DataInputStream in = new DataInputStream(System.in);
int n = Integer.parseInt(in.readLine());
switch( n ) {
case 1:
System.out.println("STRAWBERRY-Rs.50");
break;
case 2:
System.out.println("VANILLA-Rs.40");
break;
case 3:
System.out.println("CHOCOLATE-Rs.60");
break;
case 4:
System.out.println("BUTTERSCOTCH-Rs.70");
break;
case 5:
System.out.println("BLACKCURRENT-Rs.40");
break;
case 6:
System.out.println("Exit");
System.exit(0);
break;
default:
System.out.println("Select from the available choice");
break;
}
}
}
}
Upvotes: 1
Views: 774
Reputation: 62
import java.util.Scanner;
public class practice4 {
public static void main(String[] args) {
String I = "Icecream Rs. 10.";
String S = "Sandwitch Rs.5";
String C = "Cake Rs.15";
String Ch = "Chocolate Rs.5";
String L = "Lemon water Rs.12";
int limit = 6;
System.out.println(
"Select your choice from below options of 1 - 5 \n" +
"1 :" + I + "\n2 :" + S + "\n3 :" + C + "\n4 :" + Ch + "\n5 :" + L);
for (int i = 1; i <= limit; i++) {
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("You have chosen a item " + I);
break;
case 2:
System.out.println("You have chosen a item " + S);
break;
case 3:
System.out.println("You have chosen a item " + C);
break;
case 4:
System.out.println("You have chosen a item " + Ch);
break;
case 5:
System.out.println("You have chosen a item " + L);
break;
default:
System.out.println("Select only from the available choice of 1 - 5");
break;
}
}
}
}
Upvotes: 0
Reputation: 1514
You can extend this code to your problem.This solution is easy-looking and short.You should easily understand where to change.
import java.util.ArrayList;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int n = 0, selection;
ArrayList<Integer> selectionsSoFar = new ArrayList<Integer>();
while (n < 5) {
System.out.println("Enter Option:\n");
selection = keyboard.nextInt();
if (selectionsSoFar.contains(selection) || selection <= 0 || selection > 2) {
System.out.println("Error");
} else {
switch (selection) {
case 1:
System.out.println("1");
selectionsSoFar.add(selection);
n++;
break;
case 2:
System.out.println("2");
selectionsSoFar.add(selection);
n++;
break;
}
}
}
keyboard = null;
}
}
Upvotes: 0
Reputation: 5811
Like I said in my comment, you need to save the previous choice. So it may look something like this:
public static void main( String[] args ) throws Exception {
int previousChoice = 0;
for( int i = 1; i <= 5; i++ ) {
System.out.println("Select an Icecream:\n1.Strawberry\n2.Vanilla\n3.Chocolate\n4.Butterscotch\n5.Black current\n6.Exit");
DataInputStream in = new DataInputStream(System.in);
int n = Integer.parseInt(in.readLine());
while (previousChoice == n) {
System.out.println("Can't select the same ice-cream twice in a row, try again.");
n = Integer.parseInt(in.readLine());
}
previousChoice = n; // save previous choice
switch( n ) {
Disclaimer: this only works if you need to avoid selecting the same ice-cream twice in a row. If you need to make sure the choice is always unique, the solution will be different, but it'd be good to clarify it in your question.
Upvotes: 1
Reputation: 17965
You could do this: Put the possible ice creams in a List
, i.e. an ArrayList
, and every time an ice cream is chosen, you remove the corresponding ice cream from that ArrayList
.
You could also use the very same ArrayList
for displaying the menu of choices to the user.
The advantage of using the ArrayList
is that you will not have to modify your program if the selection of ice creams changes in future. As a rule of thumb: Put abstractions in code, detail in data. (Dave Thomas, Andrew Hunt http://pragmatictips.com/38)
You could then, later on, even modify the program to read the ice creams from a file, so you only have to change a text file when the selection of ice creams changes.
Upvotes: 2