看看这个是不是你想要的。
public class Account {
// id
private int id = 0;
// balance
private double balance = 0;
// 年利率
private double annualInterestRate = 0;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getannualInterestRate() {
return annualInterestRate;
}
public void setannualInterestRate(double annualInterest) {
this.annualInterestRate = annualInterest;
}
// 无参构造方法
public Account() {}
// id和初始余额的构造方法
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
}
// 返回月利率方法
public double getMonthlyInterestRate(){
return this.annualInterestRate / 12;
}
// 取款方法
public void withDraw(double num){
if(num > balance){
System.out.println("金额不足,请修改金额");
return;
}
this.balance = this.balance - num;
}
// 存款方法
public void deposit(double num){
if(num < 0){
System.out.println("输入金额错误,请重新输入金额");
return;
}
this.balance = this.balance + num;
}
public static void main(String[] args) {
Account account = new Account(1122, 2000);
account.setannualInterestRate(0.045);
// 取款2500
account.withDraw(2500);
// 存款3000
account.deposit(3000);
System.out.println("月利率:" + account.getMonthlyInterestRate() + ",余额:" + account.getBalance());
// 再取款25000
account.withDraw(25000);
}
}
public class Account {
public static void main(String[] args) {
Account account = new Account(1122, 20000);
account.setAnnualInterestRate(0.045);
account.withDraw(2500);
account.deposit(3000);
System.out.println("余额:"+account.getBalance());
System.out.println("月利率:"+account.getMonthlyInterestRate());
account.withDraw(25000);
}
private int id;
private double balance;
private double annualInterestRate;
public Account() {
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
}
public double getMonthlyInterestRate() {
return annualInterestRate / 12;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public void withDraw(double input) {
if (input > balance) {
System.out.println("金额不足,请修改金额!");
return;
}
balance -= input;
}
public void deposit(double input) {
balance += input;
}
}