描述:
设计一个名称为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.idInt();//账户ida.balanceDouble();//初始余额a.annuallnterestRateDouble();//当前利率double withdrawDouble();//提取金额double depositDouble();//存储金额double newbalance;//新余额//存、取金额有误if(a.withDraw(withdraw)==false||a.deposit(deposit)==false){newbalance=a.balance;//提取金额if(a.withDraw(withdraw)==true)newbalance=newbalance-withdraw;elseSystem.out.println("WithDraw Amount Wrong");//存储金额if(a.deposit(deposit)==true)newbalance=newbalance+deposit;elseSystem.out.println("Deposit Amount Wrong");//输出System.out.printf("The Account'balance:"+"%.2fn",newbalance);System.out.printf("The Monthly interest:"+"%.2fn",(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:"+"%.2fn",newbalance);System.out.printf("The Monthly interest:"+"%.2fn",(a.getMonthlylnteresRate(newbalance,a.annuallnterestRate)));a.getDateCreated();}}
}class Account{int id;//账户iddouble 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;}
}
本文发布于:2024-01-31 13:20:39,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170667843828825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |