admin管理员组文章数量:1648644
账户类Account
编写账户类Account,要求如下:
属性:账户类customer,账号id,余额balance,年利率annualInterestRate。
方法: 各属性的set和get方法,取款方式withdraw(),存款方式deposit()。
编写测试类AccountText,创建一个账户名为Jane Smith,账号为1000,余额为2000,年利率为1.23%。对该账户进行操作:存入100,再取出960,再取出2000。
class Account {
//创建全局变量
private String customer;
private String id;
private double balance;
private String annualInterestRate;
// customer, id, balance,annualInterestRate的set和get方法
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(String annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//取款方法
public void withdraw(double b) {
if (balance > b) {
System.out.println("成功取出:" + b);
balance = balance - b;
} else {
System.out.println("余额不足,取款失败!");
}
}
//存款方法
public void deposit(double a) {
System.out.println("成功存入:" + a);
balance = balance + a;
}
//打印账户基本信息
public void print() {
System.out.println("Customer[Smith, Jane] has a account : id is " + getId() + ",annualInterestRate is" + getAnnualInterestRate() + ",balance is " + getBalance());
}
}
public class AccountText {
public static void main(String[] args) {
Account a = new Account();
a.setCustomer("Jane Smith");
a.setId("1000");
a.setBalance(2000);
a.setAnnualInterestRate("1.23%");
a.deposit(100);
a.withdraw(960);
a.withdraw(2000);
a.print();
}
}
版权声明:本文标题:账户类Account 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1729495410a1202848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论