设计Account1类,包含: ■ 一个名为id 的int 类型的私有数据域(默认值为0),长度为6位。 ■ 一个名为balance的double类型的私有数据域(默认值为0)。 ■ 一个名为annualInterestRate 的double类型的私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利率。 ■ 一个名为dateCreated的Date类型的私有数据域存储账户的开户日期。 ■ 一个能创建默认账户的无参构造方法。 ■ 一个能创建带特定id和初始余额的构造方法,初始余额不能为负数。 ■ id、balance和annualInterestRate 的访问器和修改器。 ■ dateCreated的访问器。 ■ 一个名为getMonthlyInterestRate的方法返回月利率。 ■ 一个名为withDraw的方法从账户提取特定金额。 ■ 一个名为deposit的方法向账户存人特定金额。 ■ double类型的数据域保留2位小数。 ■ 成员方法和数据域应进行基本的合理性检查。 设计测试类ATMMachine1: ■ 创建一个有100个账户的数组,其id 为0,1,2,...99, 并初始化收支为1000美元。 ■ 主菜单如下: Main menu 1: check balance 2: withdraw 3: deposit 4: exit

时间: 2024-04-21 09:27:49 浏览: 26
下面是Account1类和ATMMachine1类的代码实现: ``` import java.util.Date; public class Account1 { private int id; private double balance; private double annualInterestRate; private Date dateCreated; public Account1() { id = 0; balance = 0; annualInterestRate = 0; dateCreated = new Date(); } public Account1(int id, double balance) { this.id = id; if (balance >= 0) { this.balance = balance; } else { System.out.println("Initial balance cannot be negative."); this.balance = 0; } annualInterestRate = 0; dateCreated = new Date(); } 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 Date getDateCreated() { return dateCreated; } public double getMonthlyInterestRate() { return annualInterestRate / 12; } public void withdraw(double amount) { if (balance >= amount) { balance -= amount; } else { System.out.println("Insufficient balance."); } } public void deposit(double amount) { if (amount >= 0) { balance += amount; } else { System.out.println("Deposit amount cannot be negative."); } } public String toString() { return "Account ID: " + id + "\nBalance: $" + String.format("%.2f", balance) + "\nAnnual Interest Rate: " + annualInterestRate + "%\nDate Created: " + dateCreated; } } import java.util.Scanner; public class ATMMachine1 { public static void main(String[] args) { Account1[] accounts = new Account1[100]; for (int i = 0; i < accounts.length; i++) { accounts[i] = new Account1(i, 1000); } Scanner input = new Scanner(System.in); int choice = 0; while (choice != 4) { System.out.println("Main menu"); System.out.println("1: check balance"); System.out.println("2: withdraw"); System.out.println("3: deposit"); System.out.println("4: exit"); System.out.print("Enter a choice: "); choice = input.nextInt(); switch (choice) { case 1: System.out.print("Enter an account ID (0-99): "); int id = input.nextInt(); if (id >= 0 && id <= 99) { System.out.println(accounts[id]); } else { System.out.println("Invalid account ID."); } break; case 2: System.out.print("Enter an account ID (0-99): "); id = input.nextInt(); if (id >= 0 && id <= 99) { System.out.print("Enter an amount to withdraw: "); double amount = input.nextDouble(); accounts[id].withdraw(amount); } else { System.out.println("Invalid account ID."); } break; case 3: System.out.print("Enter an account ID (0-99): "); id = input.nextInt(); if (id >= 0 && id <= 99) { System.out.print("Enter an amount to deposit: "); double amount = input.nextDouble(); accounts[id].deposit(amount); } else { System.out.println("Invalid account ID."); } break; case 4: System.out.println("Goodbye!"); break; default: System.out.println("Invalid choice."); } System.out.println(); } } } ``` 这个程序创建了一个有100个账户的数组,初始收支为1000美元。主菜单提供了4个选项:检查余额、提取金额、存款和退出。用户可以根据需要选择相应的选项,对账户进行操作。每个账户都有一个6位的ID、余额和利率,以及一个开户日期。可以使用访问器和修改器来获取或设置这些值。程序还提供了getMonthlyInterestRate、withdraw和deposit等方法来计算月利率、提取特定金额和存入特定金额。这个程序还对输入进行了错误检查,确保提取金额不超过余额,存款金额不为负数,账户ID在0到99之间等。

相关推荐

最新推荐

recommend-type

设计一个学生类(CStudent)

设计一个学生类(CStudent),它具有的私有数据成员是注册号、姓名、数学、外语、计算机课程的成绩。具有的公有成员函数是:求三门课总成绩的函数sum,求三门课平均成绩的函数average,显示学生数据信息的函数display...
recommend-type

基于Java实现的明日知道系统.zip

基于Java实现的明日知道系统
recommend-type

NX二次开发uc1653 函数介绍

NX二次开发uc1653 函数介绍,Ufun提供了一系列丰富的 API 函数,可以帮助用户实现自动化、定制化和扩展 NX 软件的功能。无论您是从事机械设计、制造、模具设计、逆向工程、CAE 分析等领域的专业人士,还是希望提高工作效率的普通用户,NX 二次开发 Ufun 都可以帮助您实现更高效的工作流程。函数覆盖了 NX 软件的各个方面,包括但不限于建模、装配、制图、编程、仿真等。这些 API 函数可以帮助用户轻松地实现自动化、定制化和扩展 NX 软件的功能。例如,用户可以通过 Ufun 编写脚本,自动化完成重复性的设计任务,提高设计效率;或者开发定制化的功能,满足特定的业务需求。语法简单易懂,易于学习和使用。用户可以快速上手并开发出符合自己需求的 NX 功能。本资源内容 提供了丰富的中英文帮助文档,可以帮助用户快速了解和使用 Ufun 的功能。用户可以通过资源中的提示,学习如何使用 Ufun 的 API 函数,以及如何实现特定的功能。
recommend-type

别墅图纸编号D020-三层-10.00&12.00米- 效果图.dwg

别墅图纸编号D020-三层-10.00&12.00米- 效果图.dwg
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依