Q:

Java- Having trouble with the resulting totals

I have to write a java program to print up the receipts, but I'm having trouble with the resulting final values. I don't know where I went wrong.. The total values should be: Room $799.50, Telephone $17.25, Meal $129.50, Tips $74.87, Tax $51.97, and Gross Transaction $1073.08
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 noOfGuests;
    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 TotalTelephoneCharges;
    private static double TotalMealCharges;
    private static double TotalTips;
    private static double TotalTax;
    private static double GrossTransaction;

    public Hotel (String room)
    {
        roomNumber = room;
        noOfGuests = 1;
        noOfNights = 1;
    }

    public Hotel (String room, int nights)
    {
        this (room);
        noOfNights = nights;
    }

    public Hotel (String room, int nights, int guest)
    {
        this (room, nights);
        noOfGuests = guest;
    }

    public void addNights (int nights)
    {
        noOfNights = noOfNights + nights;
    }

    public void addGuest (int guests)
    {
        noOfGuests = noOfGuests + guests;
    }

    public void calculate ()
    {
        amountDue = Room_Rate * noOfNights * noOfGuests;
        tax = amountDue * Tax_Rate / 100;
        subtotal = amountDue + tax;
        meal = Meal_Cost * noOfNights *noOfGuests;
        tip = Tip_Rate * (subtotal + meal + Telephone);
        total = subtotal + Telephone + meal + tip;

        TotalRoomCharges = TotalRoomCharges + amountDue;
        TotalTelephoneCharges = TotalTelephoneCharges + Telephone;
        TotalMealCharges = TotalMealCharges + meal;
        TotalTips = TotalTips + tip;
        TotalTax = TotalTax + tax;
        GrossTransaction = GrossTransaction + total;

    }

    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 noOfGuests;
    }

    public static double getPhoneCharges()
    {
        return Telephone;
    }

    public static double getTaxRate()
    {
        return Tax_Rate;
    }

    public static double getTotalRoomCharges()
    {
        return TotalRoomCharges;
    }

    public static double getTotalTelephoneCharges()
    {
        return TotalTelephoneCharges;
    }

    public static double getTotalMealCharges()
    {
        return TotalMealCharges;
    }

    public static double getTotalTips()
    {
        return TotalTips;
    }

    public static double getTotalTax()
    {
        return TotalTax;
    }

    public static double getGrossTransaction()
    {
        return GrossTransaction;
    }
}

public class TestHotel 
{
    public static void main(String[] args) 
    {
        Date d = new Date();
        DateFormat df = DateFormat.getDateInstance();
        NumberFormat f = NumberFormat.getCurrencyInstance();

        //Define customers
        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" + h.getRoomNumber());
        System.out.println("Room Rate: \t" + f.format(h.getRoomRate()));
        System.out.println("Length of Stay:\t" + h.getNumberOfNights() + " Night(s)");
        System.out.println("No. of Guests: \t" + h.getNumberOfGuests());
        System.out.println("Room Cost: \t" + f.format(h.getAmountDue()));
        System.out.println("Tax:" + h.getTaxRate() + "%\t" + f.format(h.getTaxDue()));
        System.out.println("\tSubtotal \t" + f.format(h.getSubtotal()));
        System.out.println("Telephone \t" + f.format(h.getPhoneCharges()));
        System.out.println("Meal Charges \t" + f.format(h.getMeal()));
        System.out.println("Tip \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("\t\t Official Use Only");
        System.out.println("\t\t Today's Summary");
        System.out.println("\tRoom      ....." + f.format(Hotel.getTotalRoomCharges()));
        System.out.println("\tTelephone ....." + f.format (Hotel.getTotalTelephoneCharges()));
        System.out.println("\tMeal      ....." + f.format (Hotel.getTotalMealCharges()));
        System.out.println("\tTips      ....." + f.format (Hotel.getTotalTips()));
        System.out.println("\tTax       ....." + f.format (Hotel.getTotalTax()));
        System.out.println("\t------------------------------\n");
        System.out.println("\tGross Transaction .." + f.format (Hotel.getGrossTransaction()));
        System.out.println("Process completed.");
    } 
}

A:

You are calling customer3.calculate() twice. Remove the first call and you get the expected values.

