unreachable statement switch - java

I have a POS program that is not working it says switch(food) unreachable statement
import javax.swing.*;
import java.awt.*;
public class sari{
public static void One()
{
int choice=0,food=0,c=0,con,x=1,y=1,z=1,q=1,trans=0,price=0 ,qty,gtotal=0,ptotal=0,pay,change,total=0,ord;
String order="",bibilhin="",transaction="",A;
ImageIcon welcome = new ImageIcon("welcome.jpg");
ImageIcon chip = new ImageIcon("chip.jpg");
ImageIcon rc = new ImageIcon("rc.jpg");
ImageIcon stick = new ImageIcon("stick.jpg");
ImageIcon pancit = new ImageIcon("pancit.jpg");
ImageIcon jampong = new ImageIcon("jampong.jpg");
ImageIcon chups = new ImageIcon("chups.jpg");
ImageIcon egg = new ImageIcon("egg.jpg");
A=JOptionPane.showInputDialog("Enter your name:"); ImageIcon hansel = new ImageIcon("hansel.jpg");
JOptionPane.showMessageDialog(null,"Sir/Ma`am \n "+A+"\n Welcome\n to Mang Inasal\n Please Choose The\n product you want to buy","Welcome" ,JOptionPane.PLAIN_MESSAGE,welcome);
while(x==1)
{
c=Integer.parseInt(JOptionPane.showInputDialog(" Categories" + "\n" +"[1] Drinks" + "\n" + "[2] Foods"));
if(c==1)
{choice=Integer.parseInt(JOptionPane.showInputDialog("DRINKS" +"\n"
+ "[1] Lemonade" + "\n"
+ "[2] Coca Cola" + "\n"
+ "[3] Sprite" + "\n"
+ "[4] Mountain Dew" + "\n"
+ "[5] Pepsi" + "\n"
+ "[6] Cofee" +"\n"
+ "[7] Hot Choco" + "\n"
+ "[8] Nestie Iced Tea" +"\n"
+ "[9] Exit" +"\n"
+ "Enter Your Choice:"));
if(c==2)
food=Integer.parseInt(JOptionPane.showInputDialog("Food" +"\n"
+ "[1] L" + "\n"
+ "[2] Coca Cola" + "\n"
+ "[3] Sprite" + "\n"
+ "[4] Mountain Dew" + "\n"
+ "[5] Pepsi" + "\n"
+ "[6] Cofee" +"\n"
+ "[7] Hot Choco" + "\n"
+ "[8] Nestie Iced Tea" +"\n"
+ "[9] Exit" +"\n"
+ "Enter Your Choice:"));
}
switch(choice)
{
case 1:
price = 20;
bibilhin= " Lemonade";
break;
case 2:
price = 20;
bibilhin= "Coca Cola ";
break;
case 3:
price =820;
bibilhin = "Sprite";
break;
case 4:
price =205;
bibilhin= "Mountain dew";
break;
case 5:
price =20;
bibilhin= "Pepsi";
break;
case 6:
price =25;
bibilhin= "Cofee ";
break;
case 7:
price =25;
bibilhin= "Hot Choco";
break;
case 8:
price = 20;
bibilhin= "Nestie Ice tea";
break;
case 9:
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"You are about to exit!","Exit",JOptionPane.WARNING_MESSAGE);
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Are you sure you want to exit?\n[1] Yes\n[0] No"));
if(con==1)
System.exit(0);
else if(con==1)
{
JOptionPane.showMessageDialog(null,"THANK YOU!\nGood Bye!","Exit",JOptionPane.ERROR_MESSAGE);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
con=1;
}
}while(con==1);
continue;
default:
JOptionPane.showMessageDialog(null,"Invalid Choice!","ERROR",JOptionPane.ERROR_MESSAGE);
}
do{//do1
qty = Integer.parseInt(JOptionPane.showInputDialog("Quantity:"));
if (qty>0)
break;
else
{
JOptionPane.showMessageDialog(null,"Invalid Input!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(q==1);//end do1
total = price * qty;
ptotal = ptotal+total;
order = order +bibilhin+" "+qty+" "+price+"="+total+"\n";
do {//do2
con = Integer.parseInt(JOptionPane.showInputDialog("Continue?[1] yes [0] no"));
if(con==1)
{
break;
}
else if(con==0)
{
do{
pay = Integer.parseInt(JOptionPane.showInputDialog(order+"Total " +ptotal+"\nEnter Payment:"));
if(pay>=ptotal)
{
change = pay-ptotal;
JOptionPane.showMessageDialog(null," \t Mang Inasal"+
"\n\t 14 T molina st purok 6-B "+
"\n\t Alabang Muntinlupa City "+
"\nOperator: Micko Mendoza"+
"\n\t----------------------------------------------------------------"+
"\n\n\t "+order+"\nTotal "+ptotal+
"\nCash "+pay+
"\nChange "+change+
"\n----------------------------------------------------------------"+
"\n\n \t Thanks For buying!!!!!"+
"\n\t This Serve as an official reciept DTSN:41D983"+
"\n \t For Delivery Dial (519-6936)"+
"\n \t Feedbacks"+
"\n \t micko.mendoza#yahoo.com"
);
y=0;
z=0;
x=1;
gtotal = gtotal+ptotal;
trans++;
order="";
transaction = transaction+"Transaction "+ trans+ " "+ptotal+"\n";//No of transactions
ptotal = 0;
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Next Customer?\n[1] Yes\n[0] No"));
if(con==1)
break;
else if(con==0)
{
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(con==1);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Kulang pera mo");
continue;
}
}while(z==1);//do3
}
else
{
JOptionPane.showMessageDialog(null,"Intiger lang pwede","Continue",JOptionPane.ERROR_MESSAGE);
y=1;
continue;
}
} while(y==1);//do2
break;
switch(food)
{
case 11:
price = 20;
bibilhin= " Lemonade";
break;
case 12:
price = 20;
bibilhin= "Coca Cola ";
break;
case 13:
price =820;
bibilhin = "Sprite";
break;
case 14:
price =205;
bibilhin= "Mountain dew";
break;
case 15:
price =20;
bibilhin= "Pepsi";
break;
case 16:
price =25;
bibilhin= "Cofee ";
break;
case 17:
price =25;
bibilhin= "Hot Choco";
break;
case 18:
price = 20;
bibilhin= "Nestie Ice tea";
break;
case 19:
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"You are about to exit!","Exit",JOptionPane.WARNING_MESSAGE);
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Are you sure you want to exit?\n[1] Yes\n[0] No"));
if(con==1)
System.exit(0);
else if(con==1)
{
JOptionPane.showMessageDialog(null,"THANK YOU!\nGood Bye!","Exit",JOptionPane.ERROR_MESSAGE);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
con=1;
}
}while(con==1);
continue;
default:
JOptionPane.showMessageDialog(null,"Invalid Choice!","ERROR",JOptionPane.ERROR_MESSAGE);
}
do{//do1
qty = Integer.parseInt(JOptionPane.showInputDialog("Quantity:"));
if (qty>0)
break;
else
{
JOptionPane.showMessageDialog(null,"Invalid Input!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(q==1);//end do1
total = price * qty;
ptotal = ptotal+total;
order = order +bibilhin+" "+qty+" "+price+"="+total+"\n";
do {//do2
con = Integer.parseInt(JOptionPane.showInputDialog("Continue?[1] yes [0] no"));
if(con==1)
{
break;
}
else if(con==0)
{
do{
pay = Integer.parseInt(JOptionPane.showInputDialog(order+"Total " +ptotal+"\nEnter Payment:"));
if(pay>=ptotal)
{
change = pay-ptotal;
JOptionPane.showMessageDialog(null," \t Mang Inasal"+
"\n\t 14 T molina st purok 6-B "+
"\n\t Alabang Muntinlupa City "+
"\nOperator: Micko Mendoza"+
"\n\t----------------------------------------------------------------"+
"\n\n\t "+order+"\nTotal "+ptotal+
"\nCash "+pay+
"\nChange "+change+
"\n----------------------------------------------------------------"+
"\n\n \t Thanks For buying!!!!!"+
"\n\t This Serve as an official reciept DTSN:41D983"+
"\n \t For Delivery Dial (519-6936)"+
"\n \t Feedbacks"+
"\n \t micko.mendoza#yahoo.com"
);
y=0;
z=0;
x=1;
gtotal = gtotal+ptotal;
trans++;
order="";
transaction = transaction+"Transaction "+ trans+ " "+ptotal+"\n";//No of transactions
ptotal = 0;
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Next Customer?\n[1] Yes\n[0] No"));
if(con==1)
break;
else if(con==0)
{
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(con==1);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Kulang pera mo");
continue;
}
}while(z==1);//do3
}
else
{
JOptionPane.showMessageDialog(null,"Intiger lang pwede","Continue",JOptionPane.ERROR_MESSAGE);
y=1;
continue;
}
} while(y==1);//do2
continue;
}
}
}

You have an unconditional break right before the switch in question.
As an aside, I think this is the perfect time to learn about functions and how to use them to make code more modular.

comment the break statement
import javax.swing.;
import java.awt.;
public class Test{
public static void main(String[] args)
{
int choice=0,food=0,c=0,con,x=1,y=1,z=1,q=1,trans=0,price=0 ,qty,gtotal=0,ptotal=0,pay,change,total=0,ord;
String order="",bibilhin="",transaction="",A;
ImageIcon welcome = new ImageIcon("welcome.jpg");
ImageIcon chip = new ImageIcon("chip.jpg");
ImageIcon rc = new ImageIcon("rc.jpg");
ImageIcon stick = new ImageIcon("stick.jpg");
ImageIcon pancit = new ImageIcon("pancit.jpg");
ImageIcon jampong = new ImageIcon("jampong.jpg");
ImageIcon chups = new ImageIcon("chups.jpg");
ImageIcon egg = new ImageIcon("egg.jpg");
A=JOptionPane.showInputDialog("Enter your name:"); ImageIcon hansel = new ImageIcon("hansel.jpg");
JOptionPane.showMessageDialog(null,"Sir/Ma`am \n "+A+"\n Welcome\n to Mang Inasal\n Please Choose The\n product you want to buy","Welcome" ,JOptionPane.PLAIN_MESSAGE,welcome);
while(x==1)
{
c=Integer.parseInt(JOptionPane.showInputDialog(" Categories" + "\n" +"[1] Drinks" + "\n" + "[2] Foods"));
if(c==1)
{choice=Integer.parseInt(JOptionPane.showInputDialog("DRINKS" +"\n"
+ "[1] Lemonade" + "\n"
+ "[2] Coca Cola" + "\n"
+ "[3] Sprite" + "\n"
+ "[4] Mountain Dew" + "\n"
+ "[5] Pepsi" + "\n"
+ "[6] Cofee" +"\n"
+ "[7] Hot Choco" + "\n"
+ "[8] Nestie Iced Tea" +"\n"
+ "[9] Exit" +"\n"
+ "Enter Your Choice:"));
if(c==2)
food=Integer.parseInt(JOptionPane.showInputDialog("Food" +"\n"
+ "[1] L" + "\n"
+ "[2] Coca Cola" + "\n"
+ "[3] Sprite" + "\n"
+ "[4] Mountain Dew" + "\n"
+ "[5] Pepsi" + "\n"
+ "[6] Cofee" +"\n"
+ "[7] Hot Choco" + "\n"
+ "[8] Nestie Iced Tea" +"\n"
+ "[9] Exit" +"\n"
+ "Enter Your Choice:"));
}
switch(choice)
{
case 1:
price = 20;
bibilhin= " Lemonade";
break;
case 2:
price = 20;
bibilhin= "Coca Cola ";
break;
case 3:
price =820;
bibilhin = "Sprite";
break;
case 4:
price =205;
bibilhin= "Mountain dew";
break;
case 5:
price =20;
bibilhin= "Pepsi";
break;
case 6:
price =25;
bibilhin= "Cofee ";
break;
case 7:
price =25;
bibilhin= "Hot Choco";
break;
case 8:
price = 20;
bibilhin= "Nestie Ice tea";
break;
case 9:
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"You are about to exit!","Exit",JOptionPane.WARNING_MESSAGE);
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Are you sure you want to exit?\n[1] Yes\n[0] No"));
if(con==1)
System.exit(0);
else if(con==1)
{
JOptionPane.showMessageDialog(null,"THANK YOU!\nGood Bye!","Exit",JOptionPane.ERROR_MESSAGE);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
con=1;
}
}while(con==1);
continue;
default:
JOptionPane.showMessageDialog(null,"Invalid Choice!","ERROR",JOptionPane.ERROR_MESSAGE);
}
do{//do1
qty = Integer.parseInt(JOptionPane.showInputDialog("Quantity:"));
if (qty>0)
break;
else
{
JOptionPane.showMessageDialog(null,"Invalid Input!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(q==1);//end do1
total = price * qty;
ptotal = ptotal+total;
order = order +bibilhin+" "+qty+" "+price+"="+total+"\n";
do {//do2
con = Integer.parseInt(JOptionPane.showInputDialog("Continue?[1] yes [0] no"));
if(con==1)
{
break;
}
else if(con==0)
{
do{
pay = Integer.parseInt(JOptionPane.showInputDialog(order+"Total " +ptotal+"\nEnter Payment:"));
if(pay>=ptotal)
{
change = pay-ptotal;
JOptionPane.showMessageDialog(null," \t Mang Inasal"+
"\n\t 14 T molina st purok 6-B "+
"\n\t Alabang Muntinlupa City "+
"\nOperator: Micko Mendoza"+
"\n\t----------------------------------------------------------------"+
"\n\n\t "+order+"\nTotal "+ptotal+
"\nCash "+pay+
"\nChange "+change+
"\n----------------------------------------------------------------"+
"\n\n \t Thanks For buying!!!!!"+
"\n\t This Serve as an official reciept DTSN:41D983"+
"\n \t For Delivery Dial (519-6936)"+
"\n \t Feedbacks"+
"\n \t micko.mendoza#yahoo.com"
);
y=0;
z=0;
x=1;
gtotal = gtotal+ptotal;
trans++;
order="";
transaction = transaction+"Transaction "+ trans+ " "+ptotal+"\n";//No of transactions
ptotal = 0;
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Next Customer?\n[1] Yes\n[0] No"));
if(con==1)
break;
else if(con==0)
{
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(con==1);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Kulang pera mo");
continue;
}
}while(z==1);//do3
}
else
{
JOptionPane.showMessageDialog(null,"Intiger lang pwede","Continue",JOptionPane.ERROR_MESSAGE);
y=1;
continue;
}
} while(y==1);//do2
// break;
switch(food)
{
case 11:
price = 20;
bibilhin= " Lemonade";
break;
case 12:
price = 20;
bibilhin= "Coca Cola ";
break;
case 13:
price =820;
bibilhin = "Sprite";
break;
case 14:
price =205;
bibilhin= "Mountain dew";
break;
case 15:
price =20;
bibilhin= "Pepsi";
break;
case 16:
price =25;
bibilhin= "Cofee ";
break;
case 17:
price =25;
bibilhin= "Hot Choco";
break;
case 18:
price = 20;
bibilhin= "Nestie Ice tea";
break;
case 19:
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"You are about to exit!","Exit",JOptionPane.WARNING_MESSAGE);
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Are you sure you want to exit?\n[1] Yes\n[0] No"));
if(con==1)
System.exit(0);
else if(con==1)
{
JOptionPane.showMessageDialog(null,"THANK YOU!\nGood Bye!","Exit",JOptionPane.ERROR_MESSAGE);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
con=1;
}
}while(con==1);
continue;
default:
JOptionPane.showMessageDialog(null,"Invalid Choice!","ERROR",JOptionPane.ERROR_MESSAGE);
}
do{//do1
qty = Integer.parseInt(JOptionPane.showInputDialog("Quantity:"));
if (qty>0)
break;
else
{
JOptionPane.showMessageDialog(null,"Invalid Input!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(q==1);//end do1
total = price * qty;
ptotal = ptotal+total;
order = order +bibilhin+" "+qty+" "+price+"="+total+"\n";
do {//do2
con = Integer.parseInt(JOptionPane.showInputDialog("Continue?[1] yes [0] no"));
if(con==1)
{
break;
}
else if(con==0)
{
do{
pay = Integer.parseInt(JOptionPane.showInputDialog(order+"Total " +ptotal+"\nEnter Payment:"));
if(pay>=ptotal)
{
change = pay-ptotal;
JOptionPane.showMessageDialog(null," \t Mang Inasal"+
"\n\t 14 T molina st purok 6-B "+
"\n\t Alabang Muntinlupa City "+
"\nOperator: Micko Mendoza"+
"\n\t----------------------------------------------------------------"+
"\n\n\t "+order+"\nTotal "+ptotal+
"\nCash "+pay+
"\nChange "+change+
"\n----------------------------------------------------------------"+
"\n\n \t Thanks For buying!!!!!"+
"\n\t This Serve as an official reciept DTSN:41D983"+
"\n \t For Delivery Dial (519-6936)"+
"\n \t Feedbacks"+
"\n \t micko.mendoza#yahoo.com"
);
y=0;
z=0;
x=1;
gtotal = gtotal+ptotal;
trans++;
order="";
transaction = transaction+"Transaction "+ trans+ " "+ptotal+"\n";//No of transactions
ptotal = 0;
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Next Customer?\n[1] Yes\n[0] No"));
if(con==1)
break;
else if(con==0)
{
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(con==1);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Kulang pera mo");
continue;
}
}while(z==1);//do3
}
else
{
JOptionPane.showMessageDialog(null,"Intiger lang pwede","Continue",JOptionPane.ERROR_MESSAGE);
y=1;
continue;
}
} while(y==1);//do2
continue;
}
}
}

Related

Switch statements gets ignored after my if-else statements

//Scanner
Scanner scan = new Scanner(System.in);
//Variables
double bmi; // Body Mass Index
double weight; // Weight in kilograms
double height; // Height in meters
String[] classification = {"Underweight", "Normal", "Overweight", "Obese"};
System.out.print("Your weight in KG \n");
weight = scan.nextDouble();
System.out.print("Enter height in meters: \n");
height = scan.nextDouble();
bmi = weight / (height * height);
if (bmi < 18.5) {
System.out.print("You're " + classification[0] + "\n");
} else if (bmi < 25) {
System.out.print("You're " + classification[1] + "\n");
} else if (bmi < 30) {
System.out.print("You're " + classification[2] + "\n");
} else {
System.out.print("You're " + classification[3] + "\n");
}
switch (Arrays.toString(classification)) {
case "Underweight":
System.out.println("Underweight");
break;
case "Normal":
System.out.println("Normal");
break;
case "Overweight":
System.out.println("A bit overweighted");
break;
case "Obese":
System.out.println("A bit obese");
break;
default:
System.out.println("Ok");
break;
}
}
output,, my switch statement does not work after the if-else statements. It ignores everything and jumps directly to the default in my switch. While my intentions are to follow up with some text after the if-else. So basically if my calculations show me i'm overweighted than my switch statement must now to jump to case "overweighted" and print out that piece of code.. What am I doing wrong?
The result of Arrays.toString(classification) which is [Underweight, Normal, Overweight, Obese] does not match any switch case.
The solution could be something like this:
String result = null;
if (bmi < 18.5) {
System.out.print("You're " + classification[0] + "\n");
result = classification[0];
} else if (bmi < 25) {
System.out.print("You're " + classification[1] + "\n");
result = classification[1];
} else if (bmi < 30) {
System.out.print("You're " + classification[2] + "\n");
result = classification[2];
} else {
System.out.print("You're " + classification[3] + "\n");
result = classification[3];
}
switch (result) {
case "Underweight":
System.out.println("Underweight");
break;
case "Normal":
System.out.println("Normal");
break;
case "Overweight":
System.out.println("A bit overweighted");
break;
case "Obese":
System.out.println("A bit obese");
break;
default:
System.out.println("Ok");
break;
}

In a java project I designed to simulate a dominos home , there is error where the code takes my input for size many times

import java.io.*;
class PizzaDelivery
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String pizza[] = { "MARGHERITA", "DOUBLE CHEESE MARGHERITA" , "FARM HOUSE", "DELUXE VEGGIE" , "CHEESE N' CORN" , "PEPPERONI PIZZA" , "PERI-PERI CHICKEN" , "NON VEG SUPREME" , "CHICKEN FIESTA" , "CHICKEN SAUSAGE PIZZA" };
int smallprice[] = { 195,300,285,355,235,205,300,285,355,300};
int mediumprice[] = { 245,380,325,405,285,275,380,355,405,345};
int largeprice[] = { 305,435,405,495,305,305,435,425,495,435};
String sides[] = {"GARLIC BREADTSTICKS" , "STUFFED GARLIC BREADSTICKS" , "CHOCO LAVA CAKE" ,"COCA COLA"};
int sideprice[] = { 155, 205 , 245 , 75};
String cart[] = new String[100];
int bill[] = new int[100];
int k = 0,areacode;
String cont,street,house,area;
String name;
double total=0.0;
long phno;
int f,flag,success;
public void menu() throws IOException
{
String choice;
int ch;
System.out.println("**************** WELCOME TO DOMINOS PIZZA HOME DELIVERY ******************");
System.out.println("Veg pizzas");
System.out.println("1. MARGHERITA - A hugely popular margherita, with a deliciously tangy single cheese topping");
System.out.println("2. DOUBLE CHEESE MARGHERITA - The ever popular margherita - loaded with extra cheese.....oodies of it");
System.out.println("3. FARM HOUSE - A pizza that goes ballistic on veggies! Check out this mouth watering overload of crunchy crisp capsicum, succulent mushrooms and fresh tomatoes");
System.out.println("4. DELUXE VEGGIE - For a vegetarian looking to eat the creme de la creme pizza for veggie lovers , this is it");
System.out.println("5. CHEESE N' CORN - Cheese(tons of it) and corn - what more could someone want?");
System.out.println("Non-veg pizzas");
System.out.println("6. PEPPERONI PIZZA - for all the lovers of classic non veg pizzas out there");
System.out.println("7. PERI-PERI CHICKEN - Pepper barbeque chicken and Red paprika : the perfect combination");
System.out.println("8. NON VEG SUPREME - Bite into supreme delight of black olives, Onions , grilled mushrooms , pepper bbq chicken, peri-peri chicken, grilled chicken rashers");
System.out.println("9. CHICKEN FIESTA - A fiesta of tasty chicken for the refined taste buds");
System.out.println("10. CHICKEN SAUSAGE - A classic ");
System.out.println("Sides and beverages");
System.out.println("11. GARLIC BREADSTICKS");
System.out.println("12. STUFFED GARLIC BREAD");
System.out.println("13. CHOCO LAVA CAKE");
System.out.println("14. COCA COLA(500ml)");
System.out.println("You can enter 15 to exit");
do
{
System.out.println("Enter your choice of product");
choice = br.readLine();
ch = Integer.parseInt(0 + choice.replaceAll("\\D+",""));
if(ch<=10 && ch>0)
{
switch(ch)
{
case 1:
cart[k] = pizza[ch-1];
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 2:
cart[k] = pizza[ch-1];
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 3:
cart[k] = pizza[ch-1];
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 4:
cart[k] = pizza[ch-1];
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 5:
cart[k] = pizza[ch-1];
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 6:
cart[k] = pizza[ch-1];
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 7:
cart[k] = pizza[ch-1];
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 8:
cart[k] = pizza[ch-1];
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 9:
cart[k] = pizza[ch-1];
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 10:
cart[k] = pizza[ch-1];
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
}
}
else if(ch>10 && ch<=15)
{
switch(ch)
{
case 11:
cart[k] = sides[ch-11];
bill[k] = sideprice[ch-11];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 12:
cart[k] = sides[ch-11];
bill[k] = sideprice[ch-11];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 13:
cart[k] = sides[ch-11];
bill[k] = sideprice[ch-11];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 14:
cart[k] = sides[ch-11];
bill[k] = sideprice[ch-11];
System.out.println("the price is: " + bill[k]);
System.out.println("The item "+ cart[k] + " has been added to your cart");
k++;
break;
case 15: System.exit(0);
}
}
else
{
System.out.println("You have entered a number that doesn't correspond to any item");
}
System.out.println("Do you want to continue buying? \n If yes, please enter \"yes\" else enter \"no\" . If you want to exit , please enter yes and then press 15");
cont = br.readLine();
if(cont.equalsIgnoreCase("No")==true)
{
return;
}
if( cont.equalsIgnoreCase("yes")==true)
{
}
else
{
System.out.println("The input entered by you is not correct. Please enter yes or no");
cont = br.readLine();
}
}while(cont.equalsIgnoreCase("Yes"));
}
int size() throws IOException
{
int size ;
int f=0;
do
{
System.out.println("Enter the number for the size of the pizza you want (1.Small/2,Medium/3.Large):");
size = Integer.parseInt(br.readLine());
if(size>=1 && size<=3)
{
break;
}
if(size>3 && size<1)
{
System.out.println("That number doesnt correspond to a size");
System.out.println("Please enter a valid choice");
f=0;
}
}while(f==0);
return size;
}
void bill() throws IOException
{
for(int i = k-1;i>=0;i--)
total+= bill[i];
System.out.println ("\n\n********************************* BILL *****************************************");
System.out.println ("\n NAME: "+ name);
System.out.println ("\n-----------------------------------------------------------------------------------------");
System.out.println ("\nITEMS \t\t\t\t \t COST ");
System.out.println ("\n-----------------------------------------------------------------------------------------");
for(int i = 0;i<k;i++)
{
System.out.println(cart[i] + "\t \t \t \t \t" + bill[i]);
}
System.out.println("\n Total : \t \t \t \t \t \t" + total);
System.out.println("SGST: 2.5% \t \t \t \t \t " + (0.025 * total));
System.out.println("CGST: 2.5% \t \t \t \t \t " + (0.025 * total));
System.out.println("GRAND TOTAL: \t \t \t \t \t" + (1.05* total));
System.out.println ("\n************************************************************************************");
System.out.println ("\n ENTER YOUR AREA NAME ");
String area =br.readLine();
System.out.println("\n ENTER YOUR STREET NAME");
String street = br.readLine();
System.out.println("\n ENTER YOUR HOUSE NAME , FLAT NUMBER(IF ANY)");
String house = br.readLine();
System.out.println("\n ENTER YOUR KOLKATA AREA CODE WITHOUT A SPACE IN BETWEEN");
do
{
try
{
f=0;
areacode = Integer.parseInt(br.readLine());
if(areacode <= 700000 && areacode >= 700163)
{
f++;
System.out.println("Your area code is not valid");
System.out.println("Enter a valid area code");
}
}
catch(Exception e)
{
System.out.println("Your area code is not valid");
System.out.println("Enter a valid area code");
areacode = Integer.parseInt(br.readLine());
f++;
}
}while(f>0);
System.out.println ("\n PAYMENT CAN BE MADE IN THE FOLLOWING WAYS:");
System.out.println ();
System.out.println ("1. CREDIT CARD OR DEBIT CARD");
System.out.println ("2. CASH ON DELIVERY");
System.out.println ("\n\n ENTER YOUR CHOICE");
for(int f = -1;f==-1;)
{
int paychoice =Integer.parseInt(br.readLine());
switch (paychoice)
{
case 1:
System.out.println ("ENTER CREDIT/DEBIT CARD NO.:");
String ccn=br.readLine();
System.out.println ("ENTER CVV NO.:");
String cvv=br.readLine();
System.out.println("Enter the date till which the card is valid");
String date = br.readLine();
System.out.println("Enter the name on the card");
String name_card = br.readLine();
System.out.println ("\n\n CONGRATULATIONS!!!!!! THE PAYMENT HAS BEEN MADE");
f=1;
break;
case 2:
f=1;
break;
default:
System.out.println ("OOPS!!! WRONG CHOICE");
System.out.println("Enter your choice again");
f=-1;
}
}
do
{
flag=0;
System.out.println("Please enter your mobile number");
System.out.print("+91");
phno = Long.parseLong(br.readLine());
int c = 0;
for(long i = phno;i!=0;i/=10)
{
int d = (int)(phno%i);
c++;
}
if(c!=10)
{
System.out.println("The entered number is not valid. Please try again");
flag++;
}
}while(flag>0);
System.out.println("Please enter your email id");
String emailid = br.readLine();
System.out.println("Your product will be delivered within 30mins. If the delivery time is more than 30 mins , you will not have to pay anything. However,if prior information is given regarding any delay in delivery time, this rule will not be valid. Further details will be provided via SMS and email");
System.out.println (" \n\nFOR FUTHER DETAILS AND QUERIES, CONTACT us using the queries section of dominos website or at the number 033 2461 4505");
System.out.println ("\n\n****************************************************** THANK YOU ******************************************************");
System.out.println ("\n\n************************************************ !!!!! VISIT AGAIN !!!!! ***********************************************");
}
void main() throws IOException
{
do
{
System.out.println("Enter your name");
name = br.readLine();
success=0;
for(int i = 0;i<name.length();i++)
{
char ch = name.charAt(i);
if(ch==' ')
{
success++;
}
}
if(success==0)
{
System.out.println("Please enter your full name separated by a space");
}
}while(success==0);
PizzaDelivery obj = new PizzaDelivery();
obj.menu();
obj.bill();
}
}
ERROR:
Enter the size is being asked too many times for one pizza
In your main switch code, every case is running the size() method multiple times in the if statements:
if(size()==1)
bill[k] = smallprice[ch-1];
else if(size()==2)
bill[k] = mediumprice[ch-1];
else if(size()==3)
bill[k] = largeprice[ch-1];
You need to run size() once before the switch cases, and save the int value returned by it in a variable which is then checked in the if statements.
Instead of a "do-while" loop in the size function, just use a while loop and it should work.

getting total amount in my cashiering system

i have a cashiering system and here's my codes:
import javax.swing.*;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FinalProject {
private static final int totalsundae = 0;
public static void main(String[] args) {
int totaldrinks = 0, totalfries = 0, totalburger = 0, totalsundae = 0;
FinalProject m = new FinalProject();
m.Cashiering();
}
public void Cashiering() {
String menu[] = {"Customer's Name", "Menu", "Pay", "View Order list", "Exit"};
String choice = "";
char ch;
do {
choice = (String) JOptionPane.showInputDialog(null, "Please select your order", "menu", 1, null, menu, menu[0]);
ch = choice.toLowerCase().charAt(0);
switch (ch) {
case 'c':
customer();
break;
case 'm':
menu2();
break;
case 'p':
getTotal();
break;
case 'v':
orderList();
break;
case 'e':
System.exit(0);
}
} while (!choice.equals("Close"));
}
public void customer() {
String name = (String) JOptionPane.showInputDialog("WELCOME !!!!! \n\nPLEASE ENTER YOUR NAME: ");
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
printW.println("Customer's Name " + "\n" + name);
JOptionPane.showMessageDialog(null, "Your Name successfully stored \n\n You can Order Now ");
printW.close();
} catch (Exception e) {
}
}
public int burger() {
int burger = 0, quantity = 0, totalburger = 0;
int choice;
String burgnm = "";
String menu = "\nBURGER: \n1.Regular Burger 45 \n2.Burger with cheese---->55 \n3.Burger with egg and cheese----50 \n4.Burger with ham---->60 \n5.Burger with ham and cheese---->70 ";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu));
switch (choice) {
case 1:
burger += 45;
burgnm = "Regular Burger - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 2:
burger += 55;
burgnm = "Burger with cheese - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 3:
burger += 50;
burgnm = "Burger with egg and cheese - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 4:
burger += 60;
burgnm = "Burger with ham - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 5:
burger += 70;
burgnm = "Burger with ham and cheese - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalburger = quantity * burger;
burgnm = burgnm + totalburger + "\n";
JOptionPane.showMessageDialog(null, "Total = " + burgnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity + burgnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity + burgnm);
else if (choice == 3)
printW.println("Customer's Order " + "\n" + quantity + burgnm);
else if (choice == 4)
printW.println("Customer's Order " + "\n" + quantity + burgnm);
else if (choice == 5)
printW.println("Customer's Order " + "\n" + quantity + burgnm);
JOptionPane.showMessageDialog(null, "Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
return totalburger;
}
public int Spaghetti() {
int choice, quantity = 0, totalspag = 0, spaghetti = 0;
String spagnm = null;
String menu = "\nSPAGHETTI: \n1.Regular spaghetti---->60 \n2.Large Spaghetti---->70";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu));
switch (choice) {
case 1:
spaghetti += 60;
spagnm = "Regular Spaghetti - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 2:
spaghetti += 70;
spagnm = "Large Spaghetti - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalspag = quantity * spaghetti;
spagnm = spagnm + totalspag + "\n";
JOptionPane.showMessageDialog(null, "Total = " + spagnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity + spagnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity + spagnm);
JOptionPane.showMessageDialog(null, "Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
return totalspag;
}
public int Fries() {
int choice, fries = 0, quantity = 0, totalfries = 0;
String friesnm = "";
String menu = "\nFRIES: \n1.Regular Fries ----> 35\n2.Medium Fries ----> 45 \n3.LargeFries ----> 55";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu));
switch (choice) {
case 1:
fries += 35;
friesnm = "Regular Fries - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 2:
fries += 45;
friesnm = "Medium Fries - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 3:
fries += 55;
friesnm = "Large Fries - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalfries = quantity * fries;
friesnm = friesnm + totalfries + "\n";
JOptionPane.showMessageDialog(null, "Total = " + friesnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity + friesnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity + friesnm);
else if (choice == 3)
printW.println("Customer's Order " + "\n" + quantity + friesnm);
JOptionPane.showMessageDialog(null, "Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
return totalfries;
}
public int Sundae() {
int choice, quantity = 0, sundae = 0, totalsun = 0;
String sundaenm = "";
String menu = "\nSUNDAE: \n1.Choco sundae ----> 28 \n2.Caramel sundae ----> 28 \n3.Strawberry sundae ----> 28";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu));
switch (choice) {
case 1:
sundae += 28;
sundaenm = " Choco Sundae -";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 2:
sundae += 28;
sundaenm = " Caramel Sundae ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 3:
sundae += 28;
sundaenm = " Strawberry Sundae - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalsun = quantity * sundae;
sundaenm = sundaenm + totalsun + "\n";
JOptionPane.showMessageDialog(null, "Total = " + sundaenm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
if (choice == 1) {
printW.println("Costumer's Order " + "\n" + quantity + sundaenm);
} else if (choice == 2)
printW.println("Costumer's Order " + "\n" + quantity + sundaenm);
else if (choice == 3)
printW.println("Costumer's Order " + "\n" + quantity + sundaenm);
JOptionPane.showMessageDialog(null, "Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
return totalsun;
}
public int Drinks() {
int choice, quantity = 0, drinks = 0, totaldrinks = 0;
String drinksnm = "";
String menu = "\nDRINKS: \n1.Reg. Coke ----> 25 \n2.Large Coke ----> 35 \n3.Reg.Sprite ----> 28 \n4.Large Sprite ----> 38";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu));
switch (choice) {
case 1:
drinks += 25;
drinksnm = "Regular Coke - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 2:
drinks += 35;
drinksnm = "Large Coke - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 3:
drinks += 28;
drinksnm = "Regular Sprite - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 4:
drinksnm = "Large Sprite - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totaldrinks = quantity * drinks;
drinksnm = drinksnm + totaldrinks + "\n";
JOptionPane.showMessageDialog(null, "Total = " + drinksnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
if (choice == 1) {
printW.println("Costumer's Order " + "\n" + quantity + drinksnm);
} else if (choice == 2)
printW.println("Costumer's Order " + "\n" + quantity + drinksnm);
else if (choice == 3)
printW.println("Costumer's Order " + "\n" + quantity + drinksnm);
else if (choice == 4)
printW.println("Costumer's Order " + "\n" + quantity + drinksnm);
JOptionPane.showMessageDialog(null, "Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
return totaldrinks;
}
public void orderList() {
try {
Scanner in = new Scanner(new FileReader("cash.txt"));
String all = "\nWELCOME !!!! \nCASHIERING SYSTEM\n";
while (in.hasNext()) {
all = all + in.nextLine() + "\n";
}
JOptionPane.showMessageDialog(null, new JTextArea(all));
in.close();
} catch (Exception e) {
}
}
public void getTotal() {
}
public void menu2() {
String menu[] = {"Burger", "Spaghetti", "Fries", "Ice Sundae", "Drinks", "Exit"};
String choice = "";
char ch;
int yesorno = 0;
do {
choice = (String) JOptionPane.showInputDialog(null, "Please select your order", "menu", 1, null, menu, menu[0]);
ch = choice.toLowerCase().charAt(0);
switch (ch) {
case 'b':
burger();
yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 's':
Spaghetti();
yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'f':
Fries();
yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'i':
Sundae();
yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'd':
Drinks();
yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'e':
break;
}
} while (!choice.equals("Close"));
}
}
my problem here is the total. how can i get the total of the customer's order by using a method? i'm really a beginner in java so i hope you can give some simple codes. thank you so much for your help!
Use a totalOrder variable to store the total for that session.
import javax.swing.*;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FinalProject {
private int totalOrder;
private static final int totalsundae = 0;
public static void main(String[] args) {
int totaldrinks = 0, totalfries = 0, totalburger = 0, totalsundae = 0;
FinalProject m = new FinalProject();
m.Cashiering();
}
public void Cashiering() {
String menu[] = { "Customer's Name", "Menu", "Pay", "View Order list",
"Exit" };
String choice = "";
char ch;
do {
choice = (String) JOptionPane.showInputDialog(null,
"Please select your order", "menu", 1, null, menu, menu[0]);
ch = choice.toLowerCase().charAt(0);
switch (ch) {
case 'c':
customer();
break;
case 'm':
menu2();
break;
case 'p':
getTotal();
break;
case 'v':
orderList();
break;
case 'e':
System.exit(0);
}
} while (!choice.equals("Close"));
}
public void customer() {
String name = (String) JOptionPane
.showInputDialog("WELCOME !!!!! \n\nPLEASE ENTER YOUR NAME: ");
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
printW.println("Customer's Name " + "\n" + name);
JOptionPane.showMessageDialog(null,
"Your Name successfully stored \n\n You can Order Now ");
printW.close();
} catch (Exception e) {
}
}
public int burger() {
int burger = 0, quantity = 0, totalburger = 0;
int choice;
String burgnm = "";
String menu = "\nBURGER: \n1.Regular Burger 45 \n2.Burger with cheese---->55 \n3.Burger with egg and cheese----50 \n4.Burger with ham---->60 \n5.Burger with ham and cheese---->70 ";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: "
+ menu));
switch (choice) {
case 1:
burger += 45;
burgnm = "Regular Burger - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 2:
burger += 55;
burgnm = "Burger with cheese - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 3:
burger += 50;
burgnm = "Burger with egg and cheese - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 4:
burger += 60;
burgnm = "Burger with ham - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 5:
burger += 70;
burgnm = "Burger with ham and cheese - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalburger = quantity * burger;
burgnm = burgnm + totalburger + "\n";
JOptionPane.showMessageDialog(null, "Total = " + burgnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity
+ burgnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity
+ burgnm);
else if (choice == 3)
printW.println("Customer's Order " + "\n" + quantity
+ burgnm);
else if (choice == 4)
printW.println("Customer's Order " + "\n" + quantity
+ burgnm);
else if (choice == 5)
printW.println("Customer's Order " + "\n" + quantity
+ burgnm);
JOptionPane.showMessageDialog(null,
"Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
totalOrder = totalOrder + totalburger;
return totalburger;
}
public int Spaghetti() {
int choice, quantity = 0, totalspag = 0, spaghetti = 0;
String spagnm = null;
String menu = "\nSPAGHETTI: \n1.Regular spaghetti---->60 \n2.Large Spaghetti---->70";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: "
+ menu));
switch (choice) {
case 1:
spaghetti += 60;
spagnm = "Regular Spaghetti - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 2:
spaghetti += 70;
spagnm = "Large Spaghetti - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalspag = quantity * spaghetti;
spagnm = spagnm + totalspag + "\n";
JOptionPane.showMessageDialog(null, "Total = " + spagnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity
+ spagnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity
+ spagnm);
JOptionPane.showMessageDialog(null,
"Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
totalOrder = totalOrder + totalspag;
return totalspag;
}
public int Fries() {
int choice, fries = 0, quantity = 0, totalfries = 0;
String friesnm = "";
String menu = "\nFRIES: \n1.Regular Fries ----> 35\n2.Medium Fries ----> 45 \n3.LargeFries ----> 55";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: "
+ menu));
switch (choice) {
case 1:
fries += 35;
friesnm = "Regular Fries - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 2:
fries += 45;
friesnm = "Medium Fries - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 3:
fries += 55;
friesnm = "Large Fries - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalfries = quantity * fries;
friesnm = friesnm + totalfries + "\n";
JOptionPane.showMessageDialog(null, "Total = " + friesnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity
+ friesnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity
+ friesnm);
else if (choice == 3)
printW.println("Customer's Order " + "\n" + quantity
+ friesnm);
JOptionPane.showMessageDialog(null,
"Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
totalOrder = totalOrder + totalfries;
return totalfries;
}
public int Sundae() {
int choice, quantity = 0, sundae = 0, totalsun = 0;
String sundaenm = "";
String menu = "\nSUNDAE: \n1.Choco sundae ----> 28 \n2.Caramel sundae ----> 28 \n3.Strawberry sundae ----> 28";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: "
+ menu));
switch (choice) {
case 1:
sundae += 28;
sundaenm = " Choco Sundae -";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 2:
sundae += 28;
sundaenm = " Caramel Sundae ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 3:
sundae += 28;
sundaenm = " Strawberry Sundae - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalsun = quantity * sundae;
sundaenm = sundaenm + totalsun + "\n";
JOptionPane.showMessageDialog(null, "Total = " + sundaenm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
if (choice == 1) {
printW.println("Costumer's Order " + "\n" + quantity
+ sundaenm);
} else if (choice == 2)
printW.println("Costumer's Order " + "\n" + quantity
+ sundaenm);
else if (choice == 3)
printW.println("Costumer's Order " + "\n" + quantity
+ sundaenm);
JOptionPane.showMessageDialog(null,
"Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
totalOrder = totalOrder + totalsun;
return totalsun;
}
public int Drinks() {
int choice, quantity = 0, drinks = 0, totaldrinks = 0;
String drinksnm = "";
String menu = "\nDRINKS: \n1.Reg. Coke ----> 25 \n2.Large Coke ----> 35 \n3.Reg.Sprite ----> 28 \n4.Large Sprite ----> 38";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: "
+ menu));
switch (choice) {
case 1:
drinks += 25;
drinksnm = "Regular Coke - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 2:
drinks += 35;
drinksnm = "Large Coke - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 3:
drinks += 28;
drinksnm = "Regular Sprite - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 4:
drinksnm = "Large Sprite - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totaldrinks = quantity * drinks;
drinksnm = drinksnm + totaldrinks + "\n";
JOptionPane.showMessageDialog(null, "Total = " + drinksnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
if (choice == 1) {
printW.println("Costumer's Order " + "\n" + quantity
+ drinksnm);
} else if (choice == 2)
printW.println("Costumer's Order " + "\n" + quantity
+ drinksnm);
else if (choice == 3)
printW.println("Costumer's Order " + "\n" + quantity
+ drinksnm);
else if (choice == 4)
printW.println("Costumer's Order " + "\n" + quantity
+ drinksnm);
JOptionPane.showMessageDialog(null,
"Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
totalOrder = totalOrder + totaldrinks;
return totaldrinks;
}
public void orderList() {
try {
Scanner in = new Scanner(new FileReader("cash.txt"));
String all = "\nWELCOME !!!! \nCASHIERING SYSTEM\n";
while (in.hasNext()) {
all = all + in.nextLine() + "\n";
}
JOptionPane.showMessageDialog(null, new JTextArea(all));
in.close();
} catch (Exception e) {
}
}
public void getTotal() {
JOptionPane.showMessageDialog(null, "Order Total\n"+ totalOrder + "");
}
public void menu2() {
String menu[] = { "Burger", "Spaghetti", "Fries", "Ice Sundae",
"Drinks", "Exit" };
String choice = "";
char ch;
int yesorno = 0;
do {
choice = (String) JOptionPane.showInputDialog(null,
"Please select your order", "menu", 1, null, menu, menu[0]);
ch = choice.toLowerCase().charAt(0);
switch (ch) {
case 'b':
burger();
yesorno = Integer
.parseInt(JOptionPane
.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 's':
Spaghetti();
yesorno = Integer
.parseInt(JOptionPane
.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'f':
Fries();
yesorno = Integer
.parseInt(JOptionPane
.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'i':
Sundae();
yesorno = Integer
.parseInt(JOptionPane
.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'd':
Drinks();
yesorno = Integer
.parseInt(JOptionPane
.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'e':
break;
}
} while (!choice.equals("Close"));
}
}

Java: Number of specific character reports

I've made a program to count the number of each letters in a phrase
What I want the resulting text to return is
Enter a phrase
"Cow goes to the market"
There are 1 C('s), 3 O('s), 1 W('s), 1 G('s), 3 E('s), 1 S('s), 3 T('s).. /* you get the gist */ in your phrase.
I want the results to be concatenated into a string instead of just a multi-lined report.
import java.util.*;
public class charCount
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
//Prompt for user entry
System.out.println("Enter a phrase: ");
String letterN = keyboard.nextLine();
charCount(letterN);
}
public static int charCount(String letterN)
{ //variable initializations
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
a = b = c = d = e = f = g = h = i = j = k = l = m = n = o = p = q = r = s = t = u = v = w = x = y = z = 0;
int total = 0;
total += 1;
//for loop
for(int countN = 0; countN < letterN.length(); countN++) {
switch(letterN.charAt(countN)) {
case 'a':
a++;
break;
case 'b':
b++;
break;
case 'c':
c++;
break;
case 'd':
d++;
break;
case 'e':
e++;
break;
case 'f':
f++;
break;
case 'g':
g++;
break;
case 'h':
h++;
break;
case 'i':
i++;
break;
case 'j':
j++;
break;
case 'k':
k++;
break;
case 'l':
l++;
break;
case 'm':
m++;
break;
case 'n':
n++;
break;
case 'o':
o++;
break;
case 'p':
p++;
break;
case 'q':
q++;
break;
case 'r':
r++;
break;
case 's':
s++;
break;
case 't':
t++;
break;
case 'u':
u++;
break;
case 'v':
v++;
break;
case 'w':
w++;
break;
case 'x':
x++;
break;
case 'y':
y++;
break;
case 'z':
z++;
break;
}
}
//Console logs
System.out.println("Below are the individual character counts for your string");
System.out.println("A: " + a);
System.out.println("B: " + b);
System.out.println("C: " + c);
System.out.println("D: " + d);
System.out.println("E: " + e);
System.out.println("F: " + f);
System.out.println("G: " + g);
System.out.println("H: " + h);
System.out.println("I: " + i);
System.out.println("J: " + j);
System.out.println("K: " + k);
System.out.println("L: " + l);
System.out.println("M: " + m);
System.out.println("N: " + n);
System.out.println("O: " + o);
System.out.println("P: " + p);
System.out.println("Q: " + q);
System.out.println("R: " + r);
System.out.println("S: " + s);
System.out.println("T: " + t);
System.out.println("U: " + u);
System.out.println("V: " + v);
System.out.println("W: " + w);
System.out.println("X: " + x);
System.out.println("Y: " + y);
System.out.println("Z: " + z);
return total;
}
}
My problem is, I can of course concatenate a System.out.println(); with the int vars but it simply prints the number of letters. I don't know ahead of time what people will type so I need my program to detect if the letter = true and thus returns the character and number inside the report.
I've looked for solutions and cannot find a thing. I can't wrap my mind around this yet and frankly I have no idea how to word this inquiry.
Thank you guys ahead of time
Use a Map to store the characters you have encountered, and how many times they are seen. Using TreeMap will mean that when you iterate over the map the entries are sorted in natural order
import java.util.*;
public class CharCount {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//Prompt for user entry
System.out.println("Enter a phrase: ");
String letterN = keyboard.nextLine();
charCount(letterN);
}
public static void charCount(String letterN) {
Map<Character, Long> charMap = new TreeMap<>();
for (char aChar : letterN.toCharArray()) {
if (charMap.containsKey(aChar)) {
charMap.put(aChar, charMap.get(aChar) + 1);
}
else {
charMap.put(aChar, 1L);
}
// this if/else can be written using the ternary operator as:
// charMap.put(aChar, charMap.containsKey(aChar) ? charMap.get(aChar) + 1 : 1L);
}
for (Character character : charMap.keySet()) {
System.out.println(character + ": " + charMap.get(character));
}
}
}

Can anyone improve my application written in Java? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm least happy with case 5 as it comprises too much code and allows duplicates. I heard about Sets but can see how it would work in this example
Here's my complete source code:
public class MyApplication {
public static void main(String[] args) {
Control control = new Control();
control.run();
}
}
Driver/Control Class
import java.util.Scanner;
public class Control {
// Integers represented as module codes
private static final int UFCE1 = 0;
private static final int UFCE2 = 1;
private static final int UFCE3 = 2;
private static final int UFCE4 = 3;
private static final int UFCE5 = 4;
// Integers represented as students
private static final int STUDENT1 = 0;
private static final int STUDENT2 = 1;
private static final int STUDENT3 = 2;
private static final int STUDENT4 = 3;
private static final int STUDENT5 = 4;
public void run() {
...
while (true) {
Menu menu = new Menu();
menu.getMainMenu();
try {
Scanner scan = new Scanner(System.in);
int selection = scan.nextInt();
switch (selection) {
case 1:
for (Student student : students) {
System.out.print(student.getName() + " ");
}
break;
case 2:
for (Module module : modules) {
System.out.print(module.getName() + " ");
}
break;
case 3:
System.out.println("Search module code: ");
scan = new Scanner(System.in);
String searchModule = scan.nextLine().trim();
for (Module module : modules) {
if (searchModule.equalsIgnoreCase(module.getName())) {
for (int s = 0; s < module.getStudents().size(); s++) {
System.out.print(module.getStudents().get(s) + " ");
}
}
}
break;
case 4:
System.out.println("Search students : ");
scan = new Scanner(System.in);
String searchStudent = scan.nextLine().trim();
for (Student student : students) {
if (searchStudent.equalsIgnoreCase(student.getName())) {
for (int s = 0; s < student.getModules().size(); s++) {
System.out.print(student.getModules().get(s) + " ");
}
}
}
break;
case 5:
// Print out students
System.out.println("select a student: ");
for (int i = 0; i < students.length; i++) {
System.out.println(i + " " + students[i]);
}
selection = scan.nextInt();
switch (selection) {
case STUDENT1:
System.out.println("select a module to enroll " + students[0].getName() + " on to:");
for (int i = 0; i < modules.length; i++) {
System.out.println(i + " " + modules[i]);
}
selection = scan.nextInt();
switch (selection) {
case UFCE1:
System.out.println("You have enrolled " + students[0].getName() + " on to " + modules[0].getName());
students[0].addModule(modules[0].getName());
modules[0].addStudent(students[0].getName());
break;
case UFCE2:
System.out.println("You have enrolled " + students[0].getName() + " on to " + modules[1].getName());
students[0].addModule(modules[1].getName());
modules[1].addStudent(students[0].getName());
break;
case UFCE3:
System.out.println("You have enrolled " + students[0].getName() + " on to " + modules[2].getName());
students[0].addModule(modules[2].getName());
modules[2].addStudent(students[0].getName());
break;
case UFCE4:
System.out.println("You have enrolled " + students[0].getName() + " on to " + modules[3].getName());
students[0].addModule(modules[3].getName());
modules[3].addStudent(students[0].getName());
break;
case UFCE5:
System.out.println("You have enrolled " + students[0].getName() + " on to " + modules[4].getName());
students[0].addModule(modules[4].getName());
modules[4].addStudent(students[0].getName());
break;
default:
System.out.println("Invalid subject selected. Returning to main menu...");
}
break;
case STUDENT2:
System.out.println("select a module to enroll " + students[1].getName() + " on to:");
for (int i = 0; i < modules.length; i++) {
System.out.println(i + " " + modules[i]);
}
selection = scan.nextInt();
switch (selection) {
case UFCE1:
System.out.println("You have enrolled " + students[1].getName() + " on to " + modules[0].getName());
students[1].addModule(modules[0].getName());
modules[0].addStudent(students[1].getName());
break;
case UFCE2:
System.out.println("You have enrolled " + students[1].getName() + " on to " + modules[1].getName());
students[1].addModule(modules[1].getName());
modules[1].addStudent(students[1].getName());
break;
case UFCE3:
System.out.println("You have enrolled " + students[1].getName() + " on to " + modules[2].getName());
students[1].addModule(modules[2].getName());
modules[2].addStudent(students[1].getName());
break;
case UFCE4:
System.out.println("You have enrolled " + students[1].getName() + " on to " + modules[3].getName());
students[1].addModule(modules[3].getName());
modules[3].addStudent(students[1].getName());
break;
case UFCE5:
System.out.println("You have enrolled " + students[1].getName() + " on to " + modules[4].getName());
students[1].addModule(modules[4].getName());
modules[4].addStudent(students[1].getName());
break;
default:
System.out.println("Invalid subject selected. Returning to main menu...");
}
break;
case STUDENT3:
System.out.println("select a module to enroll " + students[2].getName() + " on to:");
for (int i = 0; i < modules.length; i++) {
System.out.println(i + " " + modules[i]);
}
selection = scan.nextInt();
switch (selection) {
case UFCE1:
System.out.println("You have enrolled " + students[2].getName() + " on to " + modules[0].getName());
students[2].addModule(modules[0].getName());
modules[0].addStudent(students[2].getName());
break;
case UFCE2:
System.out.println("You have enrolled " + students[2].getName() + " on to " + modules[1].getName());
students[2].addModule(modules[1].getName());
modules[1].addStudent(students[2].getName());
break;
case UFCE3:
System.out.println("You have enrolled " + students[2].getName() + " on to " + modules[2].getName());
students[2].addModule(modules[2].getName());
modules[2].addStudent(students[2].getName());
break;
case UFCE4:
System.out.println("You have enrolled " + students[2].getName() + " on to " + modules[3].getName());
students[2].addModule(modules[3].getName());
modules[3].addStudent(students[2].getName());
break;
case UFCE5:
System.out.println("You have enrolled " + students[2].getName() + " on to " + modules[4].getName());
students[2].addModule(modules[4].getName());
modules[4].addStudent(students[2].getName());
break;
default:
System.out.println("Invalid subject selected. Returning to main menu...");
}
break;
case STUDENT4:
System.out.println("select a module to enroll " + students[3].getName() + " on to:");
for (int i = 0; i < modules.length; i++) {
System.out.println(i + " " + modules[i]);
}
selection = scan.nextInt();
switch (selection) {
case UFCE1:
System.out.println("You have enrolled " + students[3].getName() + " on to " + modules[0].getName());
students[3].addModule(modules[0].getName());
modules[0].addStudent(students[3].getName());
break;
case UFCE2:
System.out.println("You have enrolled " + students[3].getName() + " on to " + modules[1].getName());
students[3].addModule(modules[1].getName());
modules[1].addStudent(students[3].getName());
break;
case UFCE3:
System.out.println("You have enrolled " + students[3].getName() + " on to " + modules[2].getName());
students[3].addModule(modules[2].getName());
modules[2].addStudent(students[3].getName());
break;
case UFCE4:
System.out.println("You have enrolled " + students[3].getName() + " on to " + modules[3].getName());
students[3].addModule(modules[3].getName());
modules[3].addStudent(students[3].getName());
break;
case UFCE5:
System.out.println("You have enrolled " + students[3].getName() + " on to " + modules[4].getName());
students[3].addModule(modules[4].getName());
modules[4].addStudent(students[3].getName());
break;
default:
System.out.println("Invalid subject selected. Returning to main menu...");
}
break;
case STUDENT5:
System.out.println("select a module to enroll " + students[4].getName() + " on to:");
for (int i = 0; i < modules.length; i++) {
System.out.println(i + " " + modules[i]);
}
selection = scan.nextInt();
switch (selection) {
case UFCE1:
System.out.println("You have enrolled " + students[4].getName() + " on to " + modules[0].getName());
students[4].addModule(modules[0].getName());
modules[0].addStudent(students[4].getName());
break;
case UFCE2:
System.out.println("You have enrolled " + students[4].getName() + " on to " + modules[1].getName());
students[4].addModule(modules[1].getName());
modules[1].addStudent(students[4].getName());
break;
case UFCE3:
System.out.println("You have enrolled " + students[4].getName() + " on to " + modules[2].getName());
students[4].addModule(modules[2].getName());
modules[2].addStudent(students[4].getName());
break;
case UFCE4:
System.out.println("You have enrolled " + students[4].getName() + " on to " + modules[3].getName());
students[4].addModule(modules[3].getName());
modules[3].addStudent(students[4].getName());
break;
case UFCE5:
System.out.println("You have enrolled " + students[4].getName() + " on to " + modules[4].getName());
students[4].addModule(modules[4].getName());
modules[4].addStudent(students[4].getName());
break;
default:
System.out.println("Invalid subject selected. Returning to main menu...");
}
break;
default:
System.out.println("Invalid student selected. Returning to main menu...");
}
break;
case 6:
System.out.println("Goodbye!");
System.exit(0);
break;
default:
System.out.println("Invalid option selected. You must enter a number between 1 & 6!");
} // end switch
} catch (Exception e) {
System.out.println("Invalid entry. You must enter a number between 1 & 6");
}
} // end while
}
}
Menu Class
public class Menu {
private String Menu;
public String getMainMenu() {
System.out.println("\n\nSelect an option:");
System.out.println(
"1 List all student names"
+ "\n2 List all module details"
+ "\n3 List all students on a module"
+ "\n4 List all modules attached to a student"
+ "\n5 Attach a valid student to a valid module"
+ "\n6 Exit\n");
return Menu;
}
}
Module Class
import java.util.ArrayList;
public class Module {
private String name;
private ArrayList<String> students;
public Module(String name) {
this.name = name;
students = new ArrayList<String>();
}
public String getName() {
return name;
}
public ArrayList<String> getStudents() {
return students;
}
public void addStudent(String student){
students.add(student);
}
#Override
public String toString() {
return name;
}
}
Student Class
import java.util.ArrayList;
public class Student {
private String name;
private ArrayList<String> modules;
public Student(String name) {
this.name = name;
modules = new ArrayList<String>();
}
public String getName() {
return name;
}
public ArrayList<String> getModules() {
return modules;
}
public void addModule(String module){
modules.add(module);
}
#Override
public String toString() {
return name;
}
}
replace your integers with enums
extract every switch block to a separate well named method
extract every case block into a separate method as well.
Then come back with what you have
In case 5 you set mutual references between modules and students.
You can reduce code duplication there by declaring one of the two classes as the leading one, lets say the module and inside the addStudent Method you call addModule(this)

Categories