编写账簿类AccountBook类。类中有三个属性:accountName(String类型)、income(double类型)和outcome(double类型)。要求设置收入、支出访问器方法时收入额、支出额不能为负数。定义通过收入和支出自动计算余额的方法compute。再编写测试类,在测试类中用AccountBook类创建一个账簿对象,设置名称是“张三”,收入30,支出10,并将账户名称“张三”,该账户收入额和支出额及该账户的余额输出到控制台:
public class AccountBookTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
AccountBook account = new AccountBook("张三",30,10);
System.out.println("账户名称:" + account.accountName + "收入额:" + account.income + "支出额:" + account.outcome + "余额:" + account.compute(account.income, account.outcome));
}
}
class AccountBook{
String accountName;
double income;
double outcome;
AccountBook(String accountName,double income,double outcome)
{
this.accountName = accountName;
if(income > 0)
{
this.income = income;
}
else{
System.out.println("收入不能为负数!");
}
if(outcome > 0)
{
this.outcome = outcome;
}
else{
System.out.println("支出不能为负数!");
}
}
public double compute(double income,double outcome)
{
double balance = 0.0;
balance = income - outcome;
return balance;
}
} |
|