Results 1 to 5 of 5
  1. #1
    Loui3 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Something Easy yet I can't figure it out

    Having one simple problem here didn't include the whole code but can, what I am having is the totals are not adding up for each total at the summary. It keeps adding the last input, which is customer 3 in the test class, three times. Maybe I'm missing something I don't know any help would be appreciated. Here is the part i cut out to show how I am trying to add the totals for the summary.

    Java Code:
     public void calculate()
       {
           amountDue = ROOM * noOfNights * noOfGuest;
           tax = amountDue * TAX/100;
           subtotal = amountDue + tax;
           meal = MEAL * noOfNights * noOfGuest;
           tip = TIP * (subtotal + meal + PHONE);
           total = subtotal + PHONE + meal + tip;
           
           grossRoom = amountDue + grossRoom;
           grossTelephone = grossTelephone + PHONE ;
           grossMeal = grossMeal + meal;
           grossTips = grossTips + tip;
           grossTax = grossTax + tax;
          
       }
       
       public static double getGrossRoom()
       {
           return grossRoom;
       }
    When it it prints the total its not adding each different room price and its the same when trying to add all of the totals from the test class it basically uses customer3 information and adds that three times. I don't know what I am doing wrong

    here is the test class
    Java Code:
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.NumberFormat;
    
    class TestHotel
    {
        public static void main(String[] arg)
    	{
                NumberFormat f = NumberFormat.getCurrencyInstance();
    		
    		
                Hotel customer1 = new Hotel("10 - M", 2, 2);
                customer1.calculate();
                display(customer1, f);	
    
                Hotel customer2 = new Hotel("12 - B");
    		
    
                Hotel customer3 = new Hotel("12 - C", 2);
                customer3.calculate();
    
                customer2.addNights(1);
                customer2.calculate();
                display(customer2, f);
    
                customer3.addGuest(1);
                customer3.calculate();
                display(customer3, f);
    		
                display(f);
    	}
    	
    	static void display(Hotel  h, NumberFormat f)
    	{
    		
    		System.out.println("\tThe ABC Cheap Lodging, Inc");
    		Date d = new Date();
    		DateFormat df = DateFormat.getDateInstance();
    		System.out.println("\tDate: \t" + df.format(d));
    			
    		// Display expenses line by line including subtotal
    		System.out.println("Room# \t\t\t" + h.getRoomNumber());
    		System.out.println("Room Rate\t\t" + f.format(h.getRoomRate()));
    		System.out.println("Length of stay\t\t" + h.getNumberOfNights() + " night(s)");
    		System.out.println("No. of guests\t\t" + h.getNumberOfGuests());
    		System.out.println("Room cost\t\t" + f.format(h.getAmountDue()));
    		System.out.println("Tax " + h.getTaxRate() + "%\t\t"  + f.format(h.getTaxDue()));
    		System.out.println("\tSubtotal \t\t" + f.format(h.getSubtotal()));
    		System.out.println("Telephone \t\t" + f.format(h.getPhoneCharges()));
    		System.out.println("Meal charges \t\t" + f.format(h.getMeal()));
    		System.out.println("Tip \t\t\t" + f.format(h.getTip()));
    
    		//Display to total
    		System.out.println("\nTOTAL AMOUNT DUE\t........" + f.format(h.getTotal()));
    		
    		// Display thank you message
    		System.out.println("\nThanks for staying at The ABC Cheap Lodging, Inc" );
    		System.out.println("\tPlease come again !!!");
    		System.out.println("\n");
    	}
    	static void display(NumberFormat f)
                {
    	      System.out.println("\tOfficial Use Only\n");
                  System.out.println("\tToday's Summary");
                  System.out.println("Room\t\t" + f.format(Hotel.getGrossRoom()));
                  System.out.println("Telephone\t" + f.format(Hotel.getGrossTelephone()));
                  System.out.println("Meal\t\t" + f.format(Hotel.getGrossMeal()));
                  System.out.println("Tips\t\t" + f.format(Hotel.getGrossTips()));
                  System.out.println("Tax\t\t" + f.format(Hotel.getGrossTax()));
                  System.out.println("__________________________________");
                  System.out.println("Gross Transaction\t       " + f.format(Hotel.getTotalGross()));
                  
                }
           
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: Something Easy yet I can't figure it out

    Where is your Hotel class?

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Loui3 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Something Easy yet I can't figure it out

    Here it is:
    Java Code:
    public class Hotel
    {
        private static final double ROOM = 79.95;
        private static final double TAX = 6.5;
        private static final double PHONE = 5.75;
        private static final double MEAL = 12.95;
        private static final double TIP = 0.075;
        
        private int noOfNights;
        private int noOfGuest;
        private static double meal, tax, subtotal, total, tip, amountDue;
        private String roomNumber;
        
        //summary variables
        private static double grossRoom, grossTelephone, grossMeal, grossTips, 
                grossTax;
        
       public Hotel(String room)
       {
           roomNumber = room;
           noOfNights = 1;
           noOfGuest = 1;
           
       }
       
       public Hotel(String room, int nights)
       {
           this(room);
           noOfNights = nights;
           
       }
       
       public Hotel(String room, int nights, int guests)
       {
           this(room, nights);
           noOfGuest = guests;
    
           
       }
       
       public void addNights(int nights)
       {
           noOfNights = noOfNights + nights;
       }
       public void addGuest(int guest)
       {
           noOfGuest = noOfGuest + guest;
       }
       
         
       public void calculate()
       {
           amountDue = ROOM * noOfNights * noOfGuest;
           tax = amountDue * TAX/100;
           subtotal = amountDue + tax;
           meal = MEAL * noOfNights * noOfGuest;
           tip = TIP * (subtotal + meal + PHONE);
           total = subtotal + PHONE + meal + tip;
          
           
           grossRoom = amountDue + grossRoom;
           grossTelephone = PHONE * 3;
           grossMeal = grossMeal + meal;
           grossTips = grossTips + tip;
           grossTax = grossTax + tax;
          
       }
       
       public static double getGrossRoom()
       {
           return grossRoom;
       }
       
       public static double getGrossTelephone()
       {
           return grossTelephone;
       }
       
       public static double getGrossMeal()
       {
           return grossMeal;
       }
       public static double getGrossTips()
       {
           return grossTips;
       }
       
       public static double getGrossTax()
       {
           return grossTax;
       }
       
       public static double getTotalGross()
       {
           return (grossRoom + grossTelephone + grossMeal + grossTips + 
                   grossTax);
               
       }
       
       
       public String getRoomNumber()
       {
           return roomNumber;
       }
       
       public double getRoomRate()
       {
           return ROOM;
           
       }
       
       public int getNumberOfNights()
       {
           return noOfNights;
           
       }
       public int getNumberOfGuests()
       {
           return noOfGuest;
       }
       
       public double getAmountDue()
       {
           return amountDue;
       }
       
       public double getTaxRate()
       {
           return TAX;
       }
       
       public double getTaxDue()
       {
           return tax;
           
       }
       
       public double getSubtotal()
       {
           return subtotal;
       }
       
       public double getPhoneCharges()
       {
           return PHONE;
           
       }
       public double getMeal()
       {
           return meal;
       }
       public double getTip()
       {
           return tip;
       }
       public double getTotal()
       {
           return total;
       }
    }

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: Something Easy yet I can't figure it out

    Ok, first you need to change your customer variables from static to instance. This can be seen by commenting all your displays out and putting them at the bottom before the call to display(f). You can see what happens then as the amountDue will always be from the last customer operated on.

    Second, you call calculate four times. Notice that you update the grossRoom in that calculation which means that which ever customer instance you use you are adding the room charge back into the grossRoom total. To see this call calculate() n times on a customer and the gross amounts will increase n times that customer's room charge.

    Hope this helps!
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: Something Easy yet I can't figure it out

    One more suggestion. It seems that the display routine is actually integral to the Hotel class and not the TestHotel class. So if you move the customer display method into the Hotel class and change it to an instance method, then you could call it like this:
    Java Code:
    customer1.display(f);
    customer2.display(f);
    You would then not have to qualify your get routines with a customer instance.

    You could also move the display(f) method there too. But since it works only on static values it should not be an instance method. You would also have to rename it to something like displaySummary(f) since it would now otherwise conflict with the other display(f) method. These aren't required of course, just suggestions.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Please me figure this out
    By atrodeyo in forum New To Java
    Replies: 7
    Last Post: 04-30-2012, 02:14 PM
  2. can't figure it out myself
    By Doyle Raymond in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-18-2011, 03:34 PM
  3. can someone help me to figure this out >.<
    By kissmeeq in forum New To Java
    Replies: 2
    Last Post: 08-01-2011, 03:19 PM
  4. can someone please help me to figure this out >.<
    By lyzeofkiel in forum New To Java
    Replies: 11
    Last Post: 07-24-2011, 11:34 AM
  5. Need help - I can't figure it out.
    By Joshsmith in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 10:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •