Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Good day everyone, My plan is I'll checked multiple checkbox which is the pizza choices and a one radiobutton the pizza size and the checkbox values/prices are displayed in a textfield. I need to compute it and display it on the TOTAL COST textfield, I'll show you my work right now, it works but it only computes one checkbox.

Java
/*public double GreekPizzaPrice= 9.3, CaliforniaClubPrice=10.6,SpicyMexicanPrice=11.5,
	UltimateCheesePrice=14.0,BrooklynSpecialPrice=14.7,HSPizzaPrice=15.1,TexasFinestPrice=13.5,
	small=1.0,medium=2.0,large=3.0,total;
*/
//-----------------------------------------------------------------------------------------------------	
	if(e.getSource()==compute){
		if(GreekPizza.isSelected()&&s.isSelected()){		 	total=GreekPizzaPrice+small*Double.parseDouble(MnuINum.getText());
		 	MnuTotal.setText(String.valueOf("$"+total));
		}
		else if(m.isSelected()){
			total=GreekPizzaPrice+medium*Double.parseDouble(MnuINum.getText());
	 		MnuTotal.setText(String.valueOf("$"+total));
		}
		else {
		 	total=GreekPizzaPrice+large*Double.parseDouble(MnuINum.getText());
		 	MnuTotal.setText(String.valueOf("$"+total));
		}
		if(CaliforniaClub.isSelected()&&s.isSelected()){
			 total=CaliforniaClubPrice+small*Double.parseDouble(MnuINum.getText());
			 MnuTotal.setText(String.valueOf("$"+total));
		}
		else if(m.isSelected()){
			total=CaliforniaClubPrice+medium*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		else 
			 total=CaliforniaClubPrice+large*Double.parseDouble(MnuINum.getText());
			 MnuTotal.setText(String.valueOf("$"+total));
		
		if(SpicyMexican.isSelected()&&s.isSelected()){
			total=SpicyMexicanPrice+small*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		else if(m.isSelected()){
			total=SpicyMexicanPrice+medium*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
			}
		else {
			total=SpicyMexicanPrice+large*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		if(UltimateCheese.isSelected()&&s.isSelected()){
			total=UltimateCheesePrice+small*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		else if(m.isSelected()){
			total=UltimateCheesePrice+medium*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		else 
			total=UltimateCheesePrice+large*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));

		if(BrooklynSpecial.isSelected()&&s.isSelected()){
			total=BrooklynSpecialPrice+small*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		else if(m.isSelected()){
			total=BrooklynSpecialPrice+medium*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		else 
			total=BrooklynSpecialPrice+large*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
				
		if(HomeStylePizza.isSelected()&&s.isSelected()){
			total=HSPizzaPrice+small*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		else if(m.isSelected()){
			total=HSPizzaPrice+medium*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		else 
			total=HSPizzaPrice+large*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
						
		if(TexasFinest.isSelected()&&s.isSelected()){
			total=TexasFinestPrice+small*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		else if(m.isSelected()){
			total=TexasFinestPrice+medium*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
		else 
			total=TexasFinestPrice+large*Double.parseDouble(MnuINum.getText());
			MnuTotal.setText(String.valueOf("$"+total));
		}
Posted
Updated 29-Sep-14 4:13am
v2
Comments
Sergey Alexandrovich Kryukov 29-Sep-14 15:35pm    
What is the problem. Note, the code you show is impossibly bad. Programming should not be done by repeating similar fragments of code. Do you know functions, function parameters, and other abstraction devices?
—SA

1 solution

You are setting a text somewhere. Why?

You want to go through all of these cases - so there must only be if and all if must be on same level.
Also you want to create a sum of all cases that have been true - so do that:

Java
 double dSum = 0.0;

if(something){
  dSum += 1;
}
if(somethingelse){
 dSum +=2:
}

// to be continued to infinity and back


 System.out.println("Sum is " + dSum);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900