【发布时间】:2020-12-20 11:42:38
【问题描述】:
我是一年级学生,我在这里真的很挣扎。这是我必须为我的作业做的问题之一(我可以使用 StackOverflow 作为指导)。
创建一个名为Customer 的类,该类将确定客户对赊购产品的每月还款金额。该类有五个字段:customer name、contact number、product price、number of months 和 the monthly repayment amount。
为每个字段编写get和set方法,每月还款金额字段除外。设置方法必须提示用户输入以下字段的值:customer name、contact number、product price 和 number of months。
这个类还需要一个计算每月还款额(产品价格除以月数)的方法。
添加一个名为Finance_Period 的子类,它将确定客户是否会支付利息。
如果支付产品的月数大于三个月,客户将支付 25% 的利息,否则不计利息。
为产品付款的最长月数为 12 个月。
通过确定客户是否支付利息并计算每月还款额来覆盖calculate_repayment() 方法。
创建一个名为 Customer_Finance 的类,其中包含测试这两个类的逻辑。
提示用户输入第一个不感兴趣的对象的数据并显示结果;然后提示用户提供感兴趣的数据并显示结果。
在不使用getters 和setters 的情况下,我很难将amtRepay 调用到我的主目录中。
我什至不确定我是否正确理解了这个问题,任何指导或建议将不胜感激。
另外,我还有一个名为 Finace_Period 的课程,还没有任何内容,我还不能 100% 确定我在做什么。
这是我的主要课程,我想在其中显示amtRepay。
package main;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
//Variables
String name;
int cNumber, months;
double price;
//Input
name = JOptionPane.showInputDialog(null, "Please enter the customer's name:");
cNumber = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the customer's contact number:"));
price = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the price of the product:"));
months = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the number of repayment months:"));
Customer c = new Customer(name, cNumber, months, price);
JOptionPane.showMessageDialog(null, c.calcaAmtRepay());
}
}
这是我的中学课程,计算amtRepay。
package Main;
public class Customer extends Finance_Period {
//Atributes
private String name;
private int cNumber, months;
private double price, amtRepay;
//Constructors
public Customer (String name, int cNumber, int months, double price) {
this.name = name;
this.cNumber = cNumber;
this.months = months;
this.price = price;
}
//Getters
public String getName() {
return name;
}
public int getcNumber() {
return cNumber;
}
public int getMonths() {
return months;
}
public double getPrice() {
return price;
}
//Setter
public void setName(String name) {
this.name = name;
}
public void setcNumber(int cNumber) {
this.cNumber = cNumber;
}
public void setMonths(int months) {
this.months = months;
}
public void setPrice(double price) {
this.price = price;
}
//Calculation of monthly repayments
public double calcAmtRepay () {
amtRepay = price / months;
return price / months;
}
}
谢谢。
【问题讨论】:
-
主课的输出是什么?
-
线程“main”中的异常 java.lang.RuntimeException:无法编译的源代码 - 错误的树类型:main.Main.main(Main.java:25)处的 main.Customer 它只是给了我这个错误当谈到输出 amtRepay @jaSnom
-
首先,我认为您需要定义另一个构造函数,因为您的默认构造函数需要 AmtRepay,而您没有传递。因此您可以定义一个新的构造函数帽子,只包含您传递的参数:
public Customer (String name, int cNumber, int months, double price) { this.name = name; this.cNumber = cNumber; this.months = months; this.price = price; } -
也不能直接访问私有变量。您需要将它们公开或定义一个吸气剂。在这种情况下,getter 就足够了,因为您不想从外部更改它,如果我是正确的。
-
好的,但是我不能使用 getter 和 setter?我已从构造函数中删除了 amtRepay。
标签: java