Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-对结果汇总有问题_Java - Fatal编程技术网

Java-对结果汇总有问题

Java-对结果汇总有问题,java,Java,我必须编写一个java程序来打印收据,但是我在打印最终值时遇到了麻烦。我不知道我哪里出错了。。总价值应为:房间799.50美元,电话17.25美元,餐费129.50美元,小费74.87美元,税金51.97美元,总交易额1073.08美元 public class Hotel { //Class constants private static final double Room_Rate = 79.95; private static final double T

我必须编写一个java程序来打印收据,但是我在打印最终值时遇到了麻烦。我不知道我哪里出错了。。总价值应为:房间799.50美元,电话17.25美元,餐费129.50美元,小费74.87美元,税金51.97美元,总交易额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.");
    } 
}

您正在调用
customer3.calculate()
两次。删除第一个调用,您将获得预期值。

如果当前输出与所需输出不匹配,并且您不知道原因,则是时候开始调试了。如果您不确定如何执行此操作,请查看:。它不会解决你的直接问题,但它会给你提供你可以遵循的步骤,这些步骤应该可以帮助你自己解决问题,或者即使这样做不成功,那么至少可以帮助你更好地隔离你的问题,这样你的问题可以更集中,更容易回答。不要用双倍来表示货币金额。取而代之的是,使用一个长整数来记录硬币的数量。否则,您将在舍入和截断方面遇到很多麻烦。我建议您下次一定要努力,开始仔细阅读您的代码,熟悉调试,以便能够自己跟踪bug。此外,当阅读诸如<代码>酒店客户1 =新酒店(…)>代码>时,你应该考虑不同的类和变量名。使用静态字段计算总计也不是应该怎么做的。如果您刚刚开始使用Java编程,请慢慢来,学习和实践越多越好。:-)@马库斯本科谢谢你的建议。我被告知不要使用静态场进行计算,但我的教授坚持。这变得越来越容易,但由于我根本没有编程背景,我能走到这一步真是个奇迹:)啊,我没看到!非常感谢。