编写帐簿类AccountBook类。类中有三个属性:accountName(String类型)、income(double类型)和outcome(double类型)。定义通过收入和支出自动计算余额的方法compute。再编写测试类,在测试类中用AccoutnBook类创建一个帐簿对象,设置名称是“Zhangsan”,收入30,支出10,并将帐户名称“Zhangsan”,该帐户收入额和支出额及该账户的余额输出到控制台。
时间: 2023-05-27 20:04:12 浏览: 742
AccountBook:会计册,记账簿
```
public class AccountBook {
private String accountName;
private double income;
private double outcome;
public AccountBook(String accountName, double income, double outcome) {
this.accountName = accountName;
this.income = income;
this.outcome = outcome;
}
public void compute() {
double balance = this.income - this.outcome;
System.out.println("账户余额为:" + balance);
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
public double getOutcome() {
return outcome;
}
public void setOutcome(double outcome) {
this.outcome = outcome;
}
}
public class TestAccountBook {
public static void main(String[] args) {
AccountBook book = new AccountBook("Zhangsan", 30, 10);
System.out.println("账户名称:" + book.getAccountName());
System.out.println("收入额:" + book.getIncome());
System.out.println("支出额:" + book.getOutcome());
book.compute();
}
}
```
阅读全文