Results 1 to 7 of 7
  1. #1
    ethemartian is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default Calculating Total?

    Hello, I have this assignment from school (again..) in which I am creating a receipt for 3 customers, staying different amount of nights and with different amount of guests. I've coded everything but now I have to create a summary where the program adds the totals for different attributes of the objects. Im not quite sure how to do this, this is what I have so far (test class was provided by the professor, somewhat):

    Java Code:
    public class Hotel
    {
    
        public String RoomNumber;
    
    
    
        final static double RoomRate = 79.95;
        final static double TaxRate = 6.5;
        final static double PhoneCharges = 5.75;
        final static double Meal = 12.95;
        final static double Tip = 0.075;
    
    
        private int nights;
        private int guests;
        private double AmountDue;
        private double MealCharges;
        private double TaxDue;
        private double Subtotal;
        private double Total;
        private double TipDue;
    
    
        public Hotel(String Room)
        {
            RoomNumber = Room;
            nights = 1;
            guests = 1;
        }
    
        public Hotel(String Room, int Nights)
        {
            this(Room);
            nights = Nights;
    
        }
    
        public Hotel(String Room, int Nights, int Guests)
        {
            this(Room, Nights);
            guests = Guests;
        }
    
    public String getRoomNumber()
    {
        return RoomNumber;
    }
    
    public double getRoomRate()
    {
        return RoomRate;
    }
    
    public double getNumberOfNights()
    {
        return nights;
    }
    
    public double getNumberOfGuests()
    {
        return guests;
    }
    
    public double getAmountDue()
    {
        return AmountDue;
    }
    
    public double getTaxRate()
    {
        return TaxRate;
    }
    
    public double getTaxDue()
    {
        return TaxDue;
    }
    
    public double getSubtotal()
    {
        return Subtotal;
    }
    
    public double getPhoneCharges()
    {
        return PhoneCharges;
    }
    
    public double getMeal()
    {
        return Meal;
    }
    
    public double getTipDue()
    {
        return TipDue;
    }
    
    public double getTotal()
    {
        return Total;
    }
    
    public double getMealCharges()
    {
        return MealCharges;
    }
    
    public double getSummaryTotal()
    {
        return Total;
    }
    
    public void addNights(int amount)
    {
        nights = nights + amount;
    }
    
    public void addGuest(int amount)
    {
        guests = guests + amount;
    }
    
    public void calculate()
    {
        AmountDue = RoomRate * guests * nights;
        TaxDue = AmountDue * TaxRate/100;
        Subtotal = AmountDue + TaxDue;
        MealCharges = Meal * guests * nights;
        TipDue = (Subtotal + MealCharges + PhoneCharges)* 0.075;
        Total = Subtotal + PhoneCharges + MealCharges + TipDue;
       
    }
    
    }
    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();
    
    		// 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");
    
    
    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)
    	{
    		// 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" + 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.getTipDue()));
    
    		//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");
                    System.out.println("\tToday's Summary "+ f.format(h.getSummaryTotal()));
    	}
    	static void display( NumberFormat f)
    	{
    	      // Complete this method so that it displays the summary amounts as shown in the output
                  	System.out.println("\tOfficial Use Only");
                    System.out.println("\n");
                    
    
    
    
    	}
    
    }
    The picture attached is what the professor wants it to look like.

    Thanks!
    Attached Thumbnails Attached Thumbnails Calculating Total?-output.jpg  

  2. #2
    ethemartian is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    I'm thinking can I overload the calculate() and have it do Total = Total + Total ? I'm not sure how to get it to calculate the total from 3 different objects without having to describe them (total1,total2, total3...) Totally pulling my hair on this one...

  3. #3
    Dr.Fox is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    You dont need to overload the calculate or anything like that, you need to assign the totals of the three "Hotel" objects (customer1, customer2 and customer3) to a variable and display it.

    You have a method called getTotal. You can run it for all 3. Think about how you can use this in the main of the TestHotel class.

  4. #4
    ethemartian is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    When I use Hotel.getTotal netbeans tells me that:
    non-static method getRoomRate() cannot be referenced from a static context

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ethemartian View Post
    When I use Hotel.getTotal netbeans tells me that:
    Java is telling you that, and it's correct. You must call that method on a Hotel object, not on the Hotel class as you're doing it.

  6. #6
    ethemartian is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    Aghh okay I made # of nights and guests along with room rate class variables and this is the code im trying to make work for the summary:
    Java Code:
    	static void display(NumberFormat f)
    	{
    	      // Complete this method so that it displays the summary amounts as shown in the output
                  	System.out.println("\tOfficial Use Only");
                    System.out.println("\n");
                    System.out.println("\tRoom ...." + Hotel.RoomRate * Hotel.getNumberOfGuests()*Hotel.getNumberOfNights());
    
    
    
    
    
    	}
    Its only coming up for the rate for the 3rd customer...Could you guys elaborate a little more when you say "you need to assign the totals of the three "Hotel" objects (customer1, customer2 and customer3) to a variable and display it."

    Thanks in advance...I know im very dumb but I've also only taken 3 weeks of Java and I've been fighting this all night last night and all day today...

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    No No No that was not the solution.
    Java Code:
    String s = "hello";
    int char = String.indexOf("e");
    Surely you can recognise what is wrong with that code. You were doing the same in your code.

Similar Threads

  1. Total column issue again
    By ninjalord918 in forum AWT / Swing
    Replies: 39
    Last Post: 08-06-2010, 07:50 AM
  2. How to calculate total in table?
    By ninjalord918 in forum AWT / Swing
    Replies: 8
    Last Post: 07-30-2010, 06:14 AM
  3. Total noob
    By J_Walker in forum New To Java
    Replies: 9
    Last Post: 04-24-2009, 04:10 AM
  4. total beginner needs little help
    By asambasamba in forum New To Java
    Replies: 1
    Last Post: 06-18-2008, 06:33 PM
  5. Printing total out
    By denisdoherty in forum New To Java
    Replies: 1
    Last Post: 04-25-2008, 07:40 AM

Posting Permissions

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