# Calculating Total?

• 02-20-2011, 03:46 AM
ethemartian
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):

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:

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!
• 02-21-2011, 02:08 AM
ethemartian
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...
• 02-21-2011, 04:18 AM
Dr.Fox
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.
• 02-21-2011, 04:29 AM
ethemartian
When I use Hotel.getTotal netbeans tells me that:
Quote:

non-static method getRoomRate() cannot be referenced from a static context
• 02-21-2011, 04:34 AM
Fubarable
Quote:

Originally Posted by ethemartian
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.
• 02-21-2011, 04:57 AM
ethemartian
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:
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...
• 02-21-2011, 05:09 AM
Junky
No No No that was not the solution.
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.