admin管理员组文章数量:1648352
主方法
public class TestAccount {
public static void main(String[] args) {
int id=307;
double balance=10000;
double annualInterestRate=0.12;
Account account=new Account(id,balance);
account.setAnnualInterestRate(annualInterestRate);
System.out.println("The Account ID is "+account.getID()+", The Balance is "+account.getBalance()
+", The AnnualInterestRate is "+account.getAnnualInterestRate()+"%");
double wa=200;
account.withdraw(wa);
System.out.println("Account Balance is "+account.getBalance());
double da=300;
account.deposit(da);
System.out.println("Account Balance is "+account.getBalance());
System.out.printf("The monthly interest is %2.2f\n",account.getMonthlyInterestRate());
System.out.println("The date when this account was created on "+account.getdateCreated().toString());
}
}
Account 类
import java.util.Date;
public class Account {
private int ID;
private double Balance;
private double AnnualInterestRate;
private Date dateCreated;
/* A no-arg constructor that creates a default account.*/
Account(){
}
/* A constructor that creates an account with the specified id and initial balance.*/
public Account(int ID,double Balance) {
this.ID=ID;
this.Balance=Balance;
dateCreated=new Date();
}
/* The accessor and mutator methods for id, balance, and annualInterestRate.*/
public int getID() {
return ID;
}
public double getBalance() {
return Balance;
}
public double getAnnualInterestRate() {
return AnnualInterestRate;
}
public void setID(int ID) {
this.ID=ID;
}
public void setBalance(double Balance) {
this.Balance=Balance;
}
public void setAnnualInterestRate(double AnnualInterestRate) {
this.AnnualInterestRate=AnnualInterestRate;
}
/* The accessor method for dateCreated*/
public Date getdateCreated() {
return dateCreated;
}
/* A method named getMonthlyInterestRate() that returns the monthly interest rate.*/
double getMonthlyInterestRate(){
return AnnualInterestRate/12;
}
/* A method named withdraw that withdraws a specified amount from the account.*/
public void withdraw(double money) {
this.Balance=Balance-money;
}
/* A method named deposit that deposits a specified amount to the account.*/
public void deposit(double money) {
this.Balance=Balance+money;
}
}
版权声明:本文标题:java类 Account类 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1729497935a1203131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论