-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.");
}
}
현재 출력이 원하는 출력과 일치하지 않는데 그 이유를 모르면 디버깅을 시작할 시간입니다. 이 작업을 수행하는 방법에 대해 잘 모르는 경우 다음을 살펴보십시오. [작은 프로그램을 디버깅하는 방법] (http://ericlippert.com/2014/03/05/how-to-debug-small- 프로그램 /). 직접적인 문제는 해결되지 않지만 직접 해결할 수있는 단계를 제공합니다. 문제가 해결되지 않을 경우에도 문제를 격리하여 문제를 해결할 수 있도록 도와줍니다. 더 집중하고 대답하기 쉽다. –
금액을 나타 내기 위해 double을 사용하지 마십시오. 대신 long int를 사용하여 페니 수를 추적하십시오. 그렇지 않으면 반올림 및 잘림으로 인해 많은 문제가 발생할 것입니다. – FredK
나는 다음 번에 힘든 길을 가고 코드를주의 깊게 읽고 디버깅에 익숙해 져서 스스로 버그를 추적 할 수 있습니다. 또한 'Hotel customer1 = new Hotel (...)'과 같은 행을 읽을 때 다른 클래스와 변수 이름을 고려해야합니다. 합계를 계산하기 위해 정적 필드를 사용하는 것은 그것이 수행되어야하는 방법이 아닙니다. Java로 프로그래밍을 시작한 후 시간이 오래 걸리면 배우고 실습하는 것이 더 좋아질 것입니다. :-) –