Results 1 to 7 of 7
Thread: Calculating Total?
- 02-20-2011, 02:46 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 15
- Rep Power
- 0
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):
Test Class: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; } }
The picture attached is what the professor wants it to look like.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"); } }
Thanks!
- 02-21-2011, 01:08 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 15
- Rep Power
- 0
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, 03:18 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 6
- Rep Power
- 0
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, 03:29 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 15
- Rep Power
- 0
When I use Hotel.getTotal netbeans tells me that:
non-static method getRoomRate() cannot be referenced from a static context
-
- 02-21-2011, 03:57 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 15
- Rep Power
- 0
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:
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."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()); }
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, 04:09 AM #7
Similar Threads
-
Total column issue again
By ninjalord918 in forum AWT / SwingReplies: 39Last Post: 08-06-2010, 06:50 AM -
How to calculate total in table?
By ninjalord918 in forum AWT / SwingReplies: 8Last Post: 07-30-2010, 05:14 AM -
Total noob
By J_Walker in forum New To JavaReplies: 9Last Post: 04-24-2009, 03:10 AM -
total beginner needs little help
By asambasamba in forum New To JavaReplies: 1Last Post: 06-18-2008, 05:33 PM -
Printing total out
By denisdoherty in forum New To JavaReplies: 1Last Post: 04-25-2008, 06:40 AM


LinkBack URL
About LinkBacks


Bookmarks