2017-02-21 220 views
-3

我必須編寫一個java程序來打印收據,但是我遇到了最終值的問題。我不知道我哪裏錯了..總價值應該是:房間$ 799.50,電話$ 17.25,膳食$ 129.50,提示$ 74.87,稅務$ 51.97,而總交易$ 1073.08Java-結果總計出現問題

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."); 
    } 
} 
+4

如果您的當前輸出與您所需的輸出不匹配,並且您不知道爲什麼,那麼現在是時候開始調試了。如果你不確定如何去做這件事,那麼請看看:[如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-程式/)。它不會解決你的直接問題,但它會給你你可以遵循的步驟,應該可以幫助你自己解決它,或者即使這樣做不成功,那麼至少可以幫助你更好地隔離你的問題,以便你的問題可以更專注,更容易回答。 –

+0

請勿使用雙打來表示貨幣金額。相反,使用long int來保持跟蹤ov的數量。否則,在舍入和截斷時會遇到很多麻煩。 – FredK

+0

我建議下次再努力,仔細閱讀你的代碼,熟悉一下調試過程,以便能夠自己追蹤錯誤。此外,當閱讀「Hotel customer1 = new Hotel(...)」這樣的線路時,您應該考慮不同的類別和變量名稱。使用靜態字段計算總數也不是應該如何完成的。如果你剛剛開始用Java編程,花點時間,你學習和練習的越多,它越會變得越好。 :-) –

回答

0

要調用customer3.calculate()兩次。刪除第一個電話,你會得到預期的值。

+0

呃,我沒有看到!謝謝!!! – Mathlete