创建账户类Account
描述:
设计一个名称为Account的类,具体包括:
id:账号,整型,默认值为0;
balance:余额,实型,默认值为0;
annualInterestRate:当前利率,实型,默认值为0,假设所有帐户均有相同的利率;
dateCreated:账户开户时间,LocalDate类型,默认为2020年7月31日;
一个能创建默认账户的无参构造方法;
一个能创建带特定id和初始余额的账户的构造方法;
id、balance、annualInterstRate的getter及setter方法;
dateCreated的getter方法;
一个名为getMonthlyInterestRate()的方法返回月利率(月利率计算公式:余额*(年利率/1200));
一个名为withDraw的方法从账户提取特定数额,当提取数额大于余额或为负数系统返回WithDraw Amount Wrong提示;
一个名为deposit的方法向账户存储特定数额,当存储数额大于20000元或为负数系统返回Deposit Amount Wrong提示。
编写一个测试程序:
创建一个账户,其账户id、余额及利率分别有键盘输入,账户开户时间取系统当前时间;
输入取钱金额,系统进行取钱操作,如果取钱金额有误,则输出提示信息后系统继续运行;
输入存钱金额,系统进行存钱操作,如果存钱金额有误,则输出提示信息后系统继续运行;
系统输出,以如下格式分别输出该账户余额、月利息以及开户日期(输出实型数均保留两位小数)
输入格式:
在一行内分别输入账户id、初始余额、当前利率、提取金额、存储金额,数据间采用一个或多个空格分隔。
输出格式:
共分三行输出,分别为约、计算的月利息以及开户日期,格式如下:
The Account'balance:余额
The Monthly interest:月利息The Account'dateCreated:年-月-日
输入样例1:
1122 20000 0.045 800 600
输出样例1:
The Account’balance:19800.00
The Monthly interest:0.74
The Account’dateCreated:2020-07-31
输入样例2:
1122 20000 0.045 8000 30000
输出样例2:
Deposit Amount Wrong
The Account’balance:12000.00
The Monthly interest:0.45
The Account’dateCreated:2020-07-31
//创建用户Account类
import java.time.LocalDate;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Account a=new Account();
//输入
a.id=in.nextInt();//账户id
a.balance=in.nextDouble();//初始余额
a.annuallnterestRate=in.nextDouble();//当前利率
double withdraw=in.nextDouble();//提取金额
double deposit=in.nextDouble();//存储金额
double newbalance;//新余额
//存、取金额有误
if(a.withDraw(withdraw)==false||a.deposit(deposit)==false)
{
newbalance=a.balance;
//提取金额
if(a.withDraw(withdraw)==true)
newbalance=newbalance-withdraw;
else
System.out.println("WithDraw Amount Wrong");
//存储金额
if(a.deposit(deposit)==true)
newbalance=newbalance+deposit;
else
System.out.println("Deposit Amount Wrong");
//输出
System.out.printf("The Account'balance:"+"%.2f\n",newbalance);
System.out.printf("The Monthly interest:"+"%.2f\n",(a.getMonthlylnteresRate(newbalance,a.annuallnterestRate)));
a.getDateCreated();
}
//存、取金额无误
if(a.withDraw(withdraw)==true&&a.deposit(deposit)==true)
{
//输出
newbalance=a.balance-withdraw+deposit;
System.out.printf("The Account'balance:"+"%.2f\n",newbalance);
System.out.printf("The Monthly interest:"+"%.2f\n",(a.getMonthlylnteresRate(newbalance,a.annuallnterestRate)));
a.getDateCreated();
}
}
}
class Account{
int id;//账户id
double balance;//余额
double annuallnterestRate;//初始利率
LocalDate dateCreated;//2020 7 31
)
public int getId(int id){
return id;
}
public void setId(){
this.id=id;
}
public double getBalance(double balance){
return balance;
}
public void setBalance(){
this.balance=balance;
}
public double getAnnuallnterestRate(double annuallnterestRate){
return annuallnterestRate;
}
public void setAnnuallnterestRate(){
this.annuallnterestRate=annuallnterestRate;
}
//开户日期
public void getDateCreated(){
System.out.println("The Account'dateCreated:2020-07-31");
}
//月利率
public double getMonthlylnteresRate(double balance,double annuallnterestRate){
double monthlyInterestRate;
return monthlyInterestRate=balance*(annuallnterestRate/1200.0);
}
//提取金额
public boolean withDraw(double withdraw){
boolean result=true;
if(withdraw>balance||withdraw<0)
result=false;
return result;
}
//存储金额
public boolean deposit(double deposit){
boolean result=true;
if(deposit>20000||deposit<0)
result=false;
return result;
}
}