我不明白为什么我的计算不起作用

时间:2018-09-26 12:00:23

标签: java

我对编程非常陌生。 这是汽车租赁类。二选一。我需要开发一个基于GUI的汽车租赁应用程序。因此,当我输入内容时,一切似乎都正常,但是我的租金仍然是0。我无法弄清楚。

公共类CarHire {

    private String customerName;
    private String licenseNumber;
    private int daysHired;

        CarHire(){
            customerName=null;
            licenseNumber=null;
            daysHired=0;
            }
        CarHire(String customerName, String licenseNumber, int daysHired){
            this.customerName = customerName;
            this.licenseNumber = licenseNumber;
            this.daysHired = daysHired;
            }
            public void setCustomerName(String customerName){
                this.customerName = customerName;
            }
            public void setLicenseNumber(String licenseNumber){
                this.licenseNumber = licenseNumber;
            }
            public void setDaysHired(int daysHired){
                this.daysHired = daysHired;
            }

            public String getCustomerName()
            {
                return customerName;
            }
            public String getLicenseNumber()
            {
                return licenseNumber;
            }
            public int getDaysHired()
            {
                return daysHired;
            }

    public double calculateHireRental(){
        final double BASE_RATE = 34.5;
    final double NEXT_TIER_RATE = 30.5;
    final double LAST_TIER_RATE = 22.5;

    final int NEXT_TIER_START_DAY=4;
    final int LAST_TIER_START_DAY=7;

    double rental= 0.0;
        int days = 0;

    if(days<NEXT_TIER_START_DAY){   
            rental=days*BASE_RATE;
     } 
        else if(days<=LAST_TIER_START_DAY){
            rental=3*BASE_RATE+(days-3)*NEXT_TIER_RATE;
        }
        else{
    rental=3*BASE_RATE+4*NEXT_TIER_RATE+(days-7)*LAST_TIER_RATE;
        }
        return rental;
    }
}`

以下是GUI类。

//处理输入数据

            `public void enterData()
            {
          if (nameField.getText().compareTo("") == 0)
                {
                    JOptionPane.showMessageDialog(null,"You must enter a customer name","XYZ Car Hire App",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (licenseField.getText().compareTo("") == 0)
                {
                    JOptionPane.showMessageDialog(null,"You must enter a license number","XYZ Car Hire App",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (daysField.getText().compareTo("") == 0)
                {
                    JOptionPane.showMessageDialog(null,"You must enter days hired","XYZ Car Hire App",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                //if (currentCustomer == MAX_NUM)


                                String customerName = nameField.getText();
                                String licenseNumber = licenseField.getText();
                                int daysHired = Integer.parseInt(daysField.getText());

                                displayHeading();
                                CarHire g = new CarHire(customerName,licenseNumber,daysHired);

                    carHireArray[currentCustomer] = g;

                    textArea.append(String.format("%-25s%-28s%-32s$%3.2f\n",customerName, licenseNumber, daysHired, g.calculateHireRental()));






            if (enterButton.isEnabled())//todo-- clear textfields and return focus
                {
                                nameField.setText("");
                nameField.requestFocus();

                licenseField.setText("");
                licenseField.requestFocus();

                daysField.setText("");
                daysField.requestFocus();

        }
                currentCustomer++;//todo-- incremental current cusomer number
        }     
        // Display all bookings
    public void displayAll()
    {
                textArea.setText("");

                displayHeading();
           //todo-- call displayHeading() method
                for(int i=0;i<MAX_NUM;i++)
        {
            CarHire listCustomer = carHireArray[i];

                        textArea.append(String.format("%-25s%-22s%-28s$%3.2f\n",listCustomer.getCustomerName(), listCustomer.getLicenseNumber(), listCustomer.getDaysHired(), listCustomer.calculateHireRental()));
        }//todo-- display all entries entered so far (need using a loop)
                            //todo-- display number of entries, average days hired, total rental
                    if (nameField.getText().compareTo("") == 0)
                    {
            JOptionPane.showMessageDialog(null,"No customer entered","XYZ Car Hire App",JOptionPane.ERROR_MESSAGE);
                    }//todo-- complete error message

    }`

4 个答案:

答案 0 :(得分:1)

calculateHireRental()中,您有int days = 0;,因此第一个if为true,然后返回days * BASE_RATE,即0

删除days变量,而使用类成员daysHired

答案 1 :(得分:0)

在您提供的代码中: 在您的GUI中,将结果分配给“ daysHired”变量

        int daysHired = Integer.parseInt(daysField.getText());

但是在您的CarHire类中,可变天数已预定义为0:

int days = 0;  

因此,无论您乘以“天”(乘以0),您总会得到0:

 rental=days*BASE_RATE;

答案 2 :(得分:0)

看起来您总是在设置天数= 0 ...查找

答案 3 :(得分:0)

public double calculateHireRental(){
    final double BASE_RATE = 34.5;
final double NEXT_TIER_RATE = 30.5;
final double LAST_TIER_RATE = 22.5;

final int NEXT_TIER_START_DAY=4;
final int LAST_TIER_START_DAY=7;

double rental= 0.0;

/*Delete the "int days = 0;" variable and use the class member "daysHired" instead*/

if(daysHired<NEXT_TIER_START_DAY){   
        rental=daysHired*BASE_RATE;
 } 
    else if(daysHired<=LAST_TIER_START_DAY){
        rental=3*BASE_RATE+(daysHired-3)*NEXT_TIER_RATE;
    }
    else{
rental=3*BASE_RATE+4*NEXT_TIER_RATE+(daysHired-7)*LAST_TIER_RATE;
    }
    return rental;
}

尝试一下...