switch statement in java langauage

how to add in switch statement

Page 1 of 1

4 Replies - 2176 Views - Last Post: 11 December 2010 - 10:36 PM Rate Topic: -----

#1 Guest_brittany*


Reputation:

switch statement in java langauage

Posted 11 December 2010 - 07:41 AM

I'm a freshmen student and new to java, my instructor asked me to create a program for a spa,which enable the costumer to choose in the services offered and display the total amount charge in the services that he used.My problem is how will I compute the total amount charge, example the costumer choose 2 services in case1 and 1 service in case2 and another 2 services in case3,the total services he used is 5,but how will I add the amount charges in this 5 services to get the over all amount that the costumer should pay, if they're in different cases? hope someone can give me pieces of advice..more power


import java.io.*;
public class spa
{
 public static void main(String[] args)
 {
  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  int choice, choice1, charge=0, hour;
  char ans = 'y';
  while(ans == 'y' || ans == 'y')
  
  try
  {
   System.out.println("``THERAPY MENU``");
   System.out.println("1 BODY THERAPIES");
   System.out.println("2 SKIN CARE THERAPIES");
   System.out.println("3 INTENSIVE FACIAL THERAPIES");
   System.out.println("4 WAXING");
   System.out.println("5 HYDROTHERAPIES");
   System.out.println("6 MASSAGE THERAPIES");
   System.out.print("Enter your choice:");
   choice = Integer.parseInt(reader.readLine());
   switch (choice)
   {
		case 1: System.out.println();
				System.out.println("-=-WE HAVE 8 TYPES OF BODY THERAPY-=-");
				System.out.println("101 Pine Salt Scrub");
				System.out.println("102 Carolina Peach Nourisher");
				System.out.println("103 Holly Berry Back and Foot Treatment");
				System.out.println("104 Sculpting Seagrove Clay Wrap");
				System.out.println("105 Magnolia Mud Wrap");
				System.out.println("106 Aroma Sothys Body Treatment");
				System.out.println("107 Eau Thermale Radiance Body Wrap");
				System.out.println("108 Pinehurst Deluxe");
				System.out.print("Select specific BODY THERAPY you want:");
				choice1 = Integer.parseInt(reader.readLine());
				switch (choice1)
				{
				case 101: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 3000*hour;
							System.out.println("charge amount is:" + charge);break;
				case 102: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2800*hour;
							System.out.println("charge amount is:" + charge);break;
				case 103: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2500*hour;
							System.out.println("charge amount is:" + charge);break;
				case 104: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2500*hour;
							System.out.println("charge amount is:" + charge);break;	
				case 105: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2500*hour;
							System.out.println("charge amount is:" + charge);break;	
                case 106: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2500*hour;
							System.out.println("charge amount is:" + charge);break;	
                case 107: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2800*hour;
							System.out.println("charge amount is:" + charge);break;	
				case 108: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2800*hour;
							System.out.println("charge amount is:" + charge);break;	}break;		
				
		case 2: System.out.println();
				System.out.println("-=-WE HAVE 5 TYPES OF SKIN CARE THERAPY-=-");
				System.out.println("201 Pinehurst Signature Facial");
				System.out.println("202 Classic European Facial");
				System.out.println("203 Gentleman's Pinehurst Facial");
				System.out.println("204 Magnolia Luxury Facial");
				System.out.println("205 Eye Contour Treatment-add on to most Facials");
				System.out.print("Select specific SKIN CARE THERAPY you want:");
				choice1 = Integer.parseInt(reader.readLine());
				switch (choice1)
				{
				case 201: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 5000*hour;
							System.out.println("charge amount is:" + charge);break;
				case 202: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 3000*hour;
							System.out.println("charge amount is:" + charge);break;
				case 203: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 3000*hour;
							System.out.println("charge amount is:" + charge);break;
				case 204: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 3000*hour;
							System.out.println("charge amount is:" + charge);break;	
				case 205: 	charge= 1000;
							System.out.println("charge amount is:" + charge);break;	} break;
				
		case 3: System.out.println();
				System.out.println("-=-WE HAVE 8 TYPES OF INTENSIVE FACIAL THERAPY-=-");
				System.out.println("301 Uplifting Vitamin C Facial");
				System.out.println("302 Botanical Extract Facial");
				System.out.println("303 Ultimate Hydrating Facial");
				System.out.println("304 Eau Thermale Radiance Facial");
				System.out.println("305 Detoxifying Men's Facial");
				System.out.println("306 Collagen Hyaluronic Anti-Aging Facial");
				System.out.println("307 Pinehurst Seasonal Facial");
				System.out.println("308 Professional Peeling Treatment-add on to most Facials");
				System.out.print("Select the specific INTENSIVE FACIAL THERAPY you want:");
				choice1 = Integer.parseInt(reader.readLine());
				switch (choice1)
				{
				case 301: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 4000*hour;
							System.out.println("charge amount is:" + charge);break;
				case 302: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 5000*hour;
							System.out.println("charge amount is:" + charge);break;
				case 303: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 5000*hour;
							System.out.println("charge amount is:" + charge);break;
				case 304: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 5000*hour;
							System.out.println("charge amount is:" + charge);break;	
				case 305: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 5000*hour;
							System.out.println("charge amount is:" + charge);break;	
                case 306: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 6762*hour;
							System.out.println("charge amount is:" + charge);break;	
                case 307: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 5000*hour;
							System.out.println("charge amount is:" + charge);break;	
				case 308: 	charge= 1965;
							System.out.println("charge amount is:" + charge);break;	}break;		
		
		case 4: System.out.println();
				System.out.println("-=-WE HAVE 2 TYPES OF WAXING-=-");
				System.out.println("401 Lip, Chin, Brow or Underarm");
				System.out.println("402 Bikini (American) ");
				System.out.print("Select the specific WAXING you want:");
				choice1 = Integer.parseInt(reader.readLine());
				switch (choice1)
				{
				case 401: 	charge= 1700;
							System.out.println("charge amount is:" + charge);break;
				case 402: 	charge= 2800;
							System.out.println("charge amount is:" + charge);break;} break;
		
		case 5: System.out.println();
				System.out.println("-=-AS OF NOW, WE ONLY HAVE 1 TYPE OF HYDROTHERAPY-=-");
				System.out.println("501 Bath Escapes");
				System.out.print("Select the specific HYDROTHERAPY you want:");
				choice1 = Integer.parseInt(reader.readLine());
				switch (choice1)
				{
				case 501: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 1500*hour;
							System.out.println("charge amount is:" + charge);break;}break;
		
		case 6: System.out.println();
				System.out.println("-=-WE HAVE 12 TYPES OF MASSAGE THERAPY-=-");
				System.out.println("601 Pinehurst Signature Massage");
				System.out.println("602 Deep Tissue Massage");
				System.out.println("603 Hot Stone Massage");
				System.out.println("604 Golfer's Massage");
				System.out.println("605 Maternity Massage");
				System.out.println("606 Hot Stone Reflexology");
				System.out.println("607 Reflexology");
				System.out.println("608 Foot Refresher");
				System.out.println("609 Champion's Massage");
				System.out.println("610 Aroma Massage");
				System.out.println("611 Couples Massage-Signature");
				System.out.println("612 Couples Massage-Hot Stone or Deep Tissue");
				System.out.print("select the specific MASSAGE THERAPY you want:");
				choice1 = Integer.parseInt(reader.readLine());
				switch (choice1)
				{
				case 601: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 3000*hour;
							System.out.println("charge amount is:" + charge);break;
				case 602: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 5600*hour;
							System.out.println("charge amount is:" + charge);break;
				case 603: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 5600*hour;
							System.out.println("charge amount is:" + charge);break;
				case 604: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 5600*hour;
							System.out.println("charge amount is:" + charge);break;	
				case 605: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 3000*hour;
							System.out.println("charge amount is:" + charge);break;	
                case 606: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 5600*hour;
							System.out.println("charge amount is:" + charge);break;	
                case 607: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 3000*hour;
							System.out.println("charge amount is:" + charge);break;	
				case 608: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2000*hour;
							System.out.println("charge amount is:" + charge);break;
				case 609: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 6762*hour;
							System.out.println("charge amount is:" + charge);break;	
                case 610: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 6762*hour;
							System.out.println("charge amount is:" + charge);break;	
                case 611: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 9500*hour;
							System.out.println("charge amount is:" + charge);break;	
				case 612: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 11300*hour;
							System.out.println("charge amount is:" + charge);break;	}break;				
				
		 default : System.out.println("out of range!!!");break;
		 
		 }
		 System.out.print("Try again? [y/n]");
		 ans=reader.readLine().charAt(0);
		 System.out.println("The total amount charges is:" + charge);
    }
   catch(Exception e)
    {
     System.out.println("Error"); 
    }
  }
}		




Is This A Good Question/Topic? 0

Replies To: switch statement in java langauage

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: switch statement in java langauage

Posted 11 December 2010 - 07:53 AM

I suggest a subtotal for each of the main cases, 1 - 6, then a final total that is the sum of each subtotal. I would even show a final accounting:

Subtotal 1 =
Subtotal 2 =
Subtotal 3 =

...

Grand total = (sum of subtotals 1 - 6)

But I don't have the requirements for your assignment.
Was This Post Helpful? 0
  • +
  • -

#3 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 202
  • Joined: 11-December 10

Re: switch statement in java langauage

Posted 11 December 2010 - 08:23 AM

Why do you use:
charge=x*hour;
instead of:
charge=charge+(x*hour);

Was This Post Helpful? 0
  • +
  • -

#4 Guest_brittany*


Reputation:

Re: switch statement in java langauage

Posted 11 December 2010 - 09:37 PM

View PostGregBrannon, on 11 December 2010 - 06:53 AM, said:

I suggest a subtotal for each of the main cases, 1 - 6, then a final total that is the sum of each subtotal. I would even show a final accounting:

Subtotal 1 =
Subtotal 2 =
Subtotal 3 =

...

Grand total = (sum of subtotals 1 - 6)

But I don't have the requirements for your assignment.

sir how will I the charges after the costumer tries again?
example:
amount charge=5000
try again? yes
charges=3000
try again? yes
amount charge=1000
try again? no
the total charge is:9000 //how will I get this sir?
I can't get the right formula:(
Was This Post Helpful? 0

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: switch statement in java langauage

Posted 11 December 2010 - 10:36 PM

Sorry, but... this is the most ridiculous code I have ever seen about using a switch statement:)

				switch (choice1)
				{
				case 101: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 3000*hour;
							System.out.println("charge amount is:" + charge);break;
				case 102: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2800*hour;
							System.out.println("charge amount is:" + charge);break;
				case 103: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2500*hour;
							System.out.println("charge amount is:" + charge);break;
				case 104: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2500*hour;
							System.out.println("charge amount is:" + charge);break;	
				case 105: 	System.out.print("Enter how many hour:");
							hour=Integer.parseInt(reader.readLine());
							charge= 2500*hour;
							System.out.println("charge amount is:" + charge);break;	



switch are to select different based on a criteria
your code can easily be replaced by
   System.out.print("Enter how many hour:");
   hour = Integer.parseInt(reader.readLine();
   switch(choice1)  {
      case 101:
        charge = 3000 * hour;
        break;
      case 102:
        charge = 2800 * hour;
        break;
      case 103:
      case 104:
      case 105:
        charge = 2500 * hour;
        break;
   }
   System.out.println(("charge amount is:" + charge);



The same thing applies to all your other swicth statment
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1