Results 1 to 20 of 20
  1. #1
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Calculation Issue

    Hello guys, I am having a calculation issue with my program and I don't know whats wrong. I am new to Java programming, please help! There are no "errors" but it's not calculating the numbers correctly in the Summary.

    This is my Main class
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author sithr_000
    */
    Java Code:
     public class Hotel {
    
            // Class constants 
            private static final double ROOM_RATE = 79.95;
            private static final double TAX_RATE = 6.5;
            private static final double TELEPHONE = 5.75;
            private static final double MEAL_COST = 12.95;
            private static final double TIP_RATE = 0.075;
    
            // Instance variables 
            private int noOfNights;
            private int noOfGuest;
            private double amountDue;
            private double meal;
            private double tax;
            private double subtotal;
            private double total;
            private double tip;
            private String roomNumber;
            
            private static double 	totalRoomCharges;
    	private static double	totalPhoneCharges;
    	private static double	totalMealCharges;
    	private static double	totalTip;
            private static double   totalTax;
            private static double   grossTotal;
    
        
    
        public Hotel(String room) {
            roomNumber = room;
            noOfGuest = 1;
            noOfNights = 1;
        }
    
        public Hotel(String room, int nights) {
            this(room);
            noOfNights = nights;
        }
    
        public Hotel(String room, int nights, int guest) {
            this(room, nights);
            noOfGuest = guest;
        }
    
        public void add(int nights) {
            noOfNights = noOfNights + nights;
        }
    
        public void calculate() {
            amountDue = ROOM_RATE * noOfNights * noOfGuest;
            tax = amountDue * TAX_RATE/100;
            subtotal = amountDue + tax;
            meal = MEAL_COST * noOfNights * noOfGuest;
            tip = TIP_RATE * (subtotal + meal + TELEPHONE);
            total = subtotal + TELEPHONE + meal + tip;
            
            totalRoomCharges = totalRoomCharges + amountDue;
            totalPhoneCharges = totalPhoneCharges + TELEPHONE;
            totalMealCharges = totalMealCharges + meal;
            totalTip = totalTip + tip;
            totalTax = totalTax + tax;
            grossTotal = totalTip + totalTax + totalMealCharges + totalRoomCharges + totalPhoneCharges;
        }
        
       public double getAmountDue()
    	{
    		return amountDue;
    	}
    	
    	public double getTaxDue()
    	{
    		return tax;
    	}
    	
    	public double getSubtotal()
    	{
    		return subtotal;
    	}
    	
    	public double getTotal()
    	{
    		return total;
    	}
    	
    	public double getTip()
    	{
    		return tip;
    	}
    	
    	double getMeal()
    	{
    		return meal;
    	}
    	
    	public String getRoomNumber()
    	{
    		return roomNumber;
    	}
    	public double getRoomRate()
    	{
    	 	return ROOM_RATE;
    	}
    	 
    	public int getNumberOfNights()
    	{
    	 	return noOfNights;
    	}
    	 
    	public int getNumberOfGuests()
    	{
    	 	return noOfNights;
    	}
    	public static double getPhoneCharges()
    	{
    		return TELEPHONE;
    	}
    	
    	public static double getTaxRate()
    	{
    		return TAX_RATE;
    	}
    	
    	public static double getTotalRoomCharges()
    	{
    		return totalRoomCharges;
    	}
    	
    	public static double getTotalPhoneCharges()
    	{
    		return totalPhoneCharges;
    	}
    	
    	public static double getTotalMealCharges()
    	{
    		return totalMealCharges;
    	}
    	
    	public static double getTotalTip()
    	{
    		return totalTip;
    	}
    	
    	public static double getTotalTax()
    	{
    		return totalTax;
    	}						
            public void addNights(int noOfNights)
            {
            this.noOfNights = this.noOfNights + noOfNights;
            }
            public void addGuest(int noOfGuest)
            {
                this.noOfGuest = this.noOfGuest + noOfGuest;
            }
            public static double getGrossTotal()
            {
                return  grossTotal;
            }
            
    }
    This is my TestClass

    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();
    
            //Create customer objects
            //calculate amounts, display receipts  
            Hotel customer1 = new Hotel("10 - M", 2, 2);
            customer1.calculate();
            display(customer1, f);
    
            Hotel customer2 = new Hotel("12 - B");
            customer2.addNights(1);
            customer2.calculate();
            display(customer2, f);
    
            Hotel customer3 = new Hotel("12 - C", 2);
            customer3.calculate();
            customer3.addGuest(1);
            customer3.calculate();
            display(customer3, f);
    
            display(f);
        }
    
        static void display(Hotel h, NumberFormat f) {
            //Set up and display heading and date for each receipt
            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\t" + h.getRoomNumber());
            System.out.println("Room Rate\t\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\t" + f.format(h.getAmountDue()));
            System.out.println("Tax" + h.getTaxRate() + "%\t\t\t\t" + f.format(h.getTaxDue()));
            System.out.println("\tSubtotal \t\t\t" + f.format(h.getSubtotal()));
            System.out.println("Telephone \t\t\t" + f.format(h.getPhoneCharges()));
            System.out.println("Meal charges \t\t" + f.format(h.getMeal()));
            System.out.println("Tip \t\t\t\t" + f.format(h.getTip()));
    
            System.out.println("\nTOTAL AMOUNT DUE\t.........." + f.format(h.getTotal()));
    
            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\n\tTodays Summary");
            System.out.println("\tRoom ..... " + f.format(Hotel.getTotalRoomCharges()));
            System.out.println("\tTelephone . " + f.format(Hotel.getPhoneCharges()));
            System.out.println("\tMeal ...... " + f.format(Hotel.getTotalMealCharges()));
            System.out.println("\tTip ....... " + f.format(Hotel.getTotalTip()));
            System.out.println("\tTax ....... " + f.format(Hotel.getTotalTax()));
            System.out.println("\t__________________");
            System.out.println("\tGross Transaction " + f.format(Hotel.getGrossTotal()));
        }
           //Complete this method so that it displays the summary amounts as shown in the output	
    }
    This is my output:
    run:
    The ABC Cheap Lodging, Inc
    Date: Jun 16, 2014
    Room# 10 - M
    Room Rate $79.95
    Length of stay 2 night(s)
    No. of guests 2
    Room cost $319.80
    Tax6.5% $20.79
    Subtotal $340.59
    Telephone $5.75
    Meal charges $51.80
    Tip $29.86

    TOTAL AMOUNT DUE ..........$428.00

    Thanks for staying at The ABC Cheap Lodging, Inc
    Please come again !!!


    The ABC Cheap Lodging, Inc
    Date: Jun 16, 2014
    Room# 12 - B
    Room Rate $79.95
    Length of stay 2 night(s)
    No. of guests 2
    Room cost $159.90
    Tax6.5% $10.39
    Subtotal $170.29
    Telephone $5.75
    Meal charges $25.90
    Tip $15.15

    TOTAL AMOUNT DUE ..........$217.09

    Thanks for staying at The ABC Cheap Lodging, Inc
    Please come again !!!


    The ABC Cheap Lodging, Inc
    Date: Jun 16, 2014
    Room# 12 - C
    Room Rate $79.95
    Length of stay 2 night(s)
    No. of guests 2
    Room cost $319.80
    Tax6.5% $20.79
    Subtotal $340.59
    Telephone $5.75
    Meal charges $51.80
    Tip $29.86

    TOTAL AMOUNT DUE ..........$428.00

    Thanks for staying at The ABC Cheap Lodging, Inc
    Please come again !!!


    Official Use Only

    Todays Summary
    Room ..... $959.40
    Telephone . $5.75
    Meal ...... $155.40
    Tip ....... $90.01
    Tax ....... $62.36
    __________________
    Gross Transaction $1,290.17

    BUILD SUCCESSFUL (total time: 0 seconds)

    My Summary is supposed to be:

    Room - 799.5
    telephone 17.25
    meal 129.5
    tips 74.87
    tax 51.97

    Gross: 1,073.08
    Last edited by sithclone3; 06-17-2014 at 12:24 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calculation Issue

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Which values are not being computed correctly? Carefully check the code that computes those values. Use println() statements to print out intermediate results so you can see where the code is going wrong.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Calculation Issue

    Hey, sorry, i think i fixed it the post.

    The Total values have not been computing directly. The individual customers values computed correctly, but when I try to total them all up for the Summary it calculates them wrong. I'm not sure how its calculating it wrong is what im getting at. i dont mean to sound rude, sorry...I know it has something to do with:

    Java Code:
    totalRoomCharges = totalRoomCharges + amountDue;
    totalPhoneCharges = totalPhoneCharges + TELEPHONE;
    totalMealCharges = totalMealCharges + meal;
    totalTip = totalTip + tip;
    totalTax = totalTax + tax;
    grossTotal = totalTip + totalTax + totalMealCharges + totalRoomCharges + totalPhoneCharges;
    Last edited by sithclone3; 06-17-2014 at 12:42 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calculation Issue

    Have you tried using println() statements to print out intermediate results so you can see where the code is going wrong. What was printed? Were any of the intermediate results different from the results you computed manually?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Calculation Issue

    How do I do that? Is it not like that already? Im sorry Im not sure what youre talking about...how do i run it intermediate?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calculation Issue

    Add statements like this following all the places where a value is computed:
    Java Code:
            System.out.println("tRC="+totalRoomCharges + ", aD="+amountDue); //<<<<<<<<
    This would go in the calculate() method just before where amountDue is added totalRoomCharges to show the values of those two variables.

    Its best to work on one variable's value at a time. Pick one and then add println statements every where its value is changed so you can see where the code is going wrong.

    An intermediate value is one that is used to compute the final value:
    x = a + b
    y = c + d
    valuetoShow = x + y
    x and y are intermediate values used to compute valuetoShow
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Calculation Issue

    ok i did it, i got some results but im not sure what they mean.

    this is what i got
    run:

    tRC=319.8, aD=319.8
    The ABC Cheap Lodging, Inc
    Date: Jun 16, 2014
    Room# 10 - M
    Room Rate $79.95
    Length of stay 2 night(s)
    No. of guests 2
    Room cost $319.80
    Tax6.5% $20.79
    Subtotal $340.59
    Telephone $5.75
    Meal charges $51.80
    Tip $29.86

    TOTAL AMOUNT DUE ..........$428.00

    Thanks for staying at The ABC Cheap Lodging, Inc
    Please come again !!!


    tRC=479.70000000000005, aD=159.9
    The ABC Cheap Lodging, Inc
    Date: Jun 16, 2014
    Room# 12 - B
    Room Rate $79.95
    Length of stay 2 night(s)
    No. of guests 2
    Room cost $159.90
    Tax6.5% $10.39
    Subtotal $170.29
    Telephone $5.75
    Meal charges $25.90
    Tip $15.15

    TOTAL AMOUNT DUE ..........$217.09

    Thanks for staying at The ABC Cheap Lodging, Inc
    Please come again !!!


    tRC=639.6, aD=159.9
    tRC=959.4000000000001, aD=319.8

    The ABC Cheap Lodging, Inc
    Date: Jun 16, 2014
    Room# 12 - C
    Room Rate $79.95
    Length of stay 2 night(s)
    No. of guests 2
    Room cost $319.80
    Tax6.5% $20.79
    Subtotal $340.59
    Telephone $5.75
    Meal charges $51.80
    Tip $29.86

    TOTAL AMOUNT DUE ..........$428.00

    Thanks for staying at The ABC Cheap Lodging, Inc
    Please come again !!!


    Official Use Only

    Todays Summary
    Room ..... $959.40
    Telephone . $5.75
    Meal ...... $155.40
    Tip ....... $90.01
    Tax ....... $62.36
    __________________
    Gross Transaction $1,290.17
    BUILD SUCCESSFUL (total time: 0 seconds)
    Last edited by sithclone3; 06-17-2014 at 03:45 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calculation Issue

    I dont know what the results mean
    Are the printed numbers correct? You need to work out the totals on paper and compare that result with what was printed out and posted in post #7

    You put the println in the wrong place. The two values that were printed are always the same????
    tRC=319.8, aD=319.8
    tRC=5.75, aD=5.75
    tRC=51.8aD=51.8
    tRC=29.860274999999998, aD=29.860274999999998
    tRC=20.787000000000003, aD=20.787000000000003


    Put the one println() before where amountDue is added totalRoomCharges.
    Java Code:
            System.out.println("tRC="+totalRoomCharges + ", aD="+amountDue); //<<<<<<<<
            totalRoomCharges = totalRoomCharges + amountDue;
    What are the other values that were printed? I suggested that you work on just ONE value at a time. Too many print outs just make for confusion.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Calculation Issue

    tRC=479.70000000000005, aD=159.9
    tPC=11.5, T=5.75
    tMC=77.69999999999999M=25.9
    tTT=45.0060375, T=15.1457625
    tTA=31.180500000000002, tA=10.393500000000001
    tRC=639.6, aD=319.8
    tPC=17.25, T=5.75
    tMC=103.6M=51.8
    tTT=60.151799999999994, T=29.860274999999998
    tTA=41.574000000000005, tA=20.787000000000003
    <----This is shows up above the "Summary"

    Im working on them one at a time, im just not sure what im looking for, do I add the RC and AD of the customers and see if it equals the total 959?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calculation Issue

    tRC=479.70000000000005, aD=159.9
    How did the totalRoomCharges get a value for the first line? I'd expect it to be 0 the first time it is printed.

    Im working on them one at a time
    Then I'd only expect to see the value of totalRoomCharges and amountDue to be printed.
    Trying to work on more than one variable at a time will only confuse you. Work on One at a time.
    Last edited by Norm; 06-17-2014 at 04:00 AM.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Calculation Issue

    tRC=0.0, aD=319.8
    tRC=319.8, aD=159.9
    tRC=479.70000000000005, aD=159.9


    These are the three for the customers, should I just add up 319.8 and 479.9?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calculation Issue

    Are those amounts that are printed correct?

    Is 479.70 + 159.9 the correct total?
    Last edited by Norm; 06-17-2014 at 04:04 AM.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Calculation Issue

    okay so it seems to be adding the aD=159.9 to (479.9 + 319.8).

    Its adding the Roomcost for Customer2 to the total, thats where the 159.9 is coming from. So basically instead of doing 319.8 + 159.9 + 319.8, its doing 319.8 + 159.9 + 319.8 + 159.9. How would I go about this? I understand I need to get rid of the extra value being added, but how?
    Last edited by sithclone3; 06-17-2014 at 04:08 AM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calculation Issue

    Its adding the Roomcost for Customer2 to the total, thats where the 159.9 is coming from.
    Look at the code to see what is added to what and when it is added.
    Each printed line shows the old total and what is being added to it for the current customer.
    The new total is printed on the next line.

    Are you posting all of the debug print out lines?
    Last edited by Norm; 06-17-2014 at 04:11 AM.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calculation Issue

    Are you posting all of the debug print out lines?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Calculation Issue

    Ok so at Customer 3 it adds 159.9 a second time:
    tRC=479.70000000000005, aD=159.9
    tRC=639.6, aD=319.8


    Thus I get 639.6 + 319.8 instead of what I want which is 479.7 + 159.9

    Java Code:
    totalRoomCharges = totalRoomCharges + amountDue;
    How would I add to this in order for it not to add Roomcost for Customer2 a second time?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calculation Issue

    at Customer 3 it adds 159.9 a second time:
    Why is that? Look at the code!!!
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Calculation Issue

    Java Code:
    Hotel customer3 = new Hotel("12 - C", 2);
            [B]customer3.calculate();[/B]
            customer3.addGuest(1);
            customer3.calculate();
            display(customer3, f);
    There was this extra calculate....I feel stupid now...

    Ok so the TotalRoomCharges is fixed...now I gotta spend the night fixing everything else with the TotalPhoneCharges and Im done, hahaha! uuugh! >.<

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Calculation Issue

    Glad you found it.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: Calculation Issue

    Thanks for helping out and dealing with me!

Similar Threads

  1. Calculation help =(
    By kor6666 in forum New To Java
    Replies: 1
    Last Post: 02-08-2014, 12:57 PM
  2. Getting value from pre-Mod calculation
    By jjb1989 in forum New To Java
    Replies: 1
    Last Post: 11-10-2012, 10:01 PM
  3. Tax calculation program help!!
    By sabarooni in forum New To Java
    Replies: 11
    Last Post: 11-28-2011, 10:00 PM
  4. jbutton calculation
    By nksjolinder in forum New To Java
    Replies: 1
    Last Post: 09-22-2011, 03:27 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
  •