java - Java Bank Account 类,用于跟踪特定时间范围内发生的交易
问题描述
对于学校的 Java 编程作业(11 年级 - 现在只学习了大约 3 个月的 Java),我必须创建一个代表银行帐户的类。该程序应包括帐号、账户余额以及取款和存款背后的所有逻辑。到目前为止,除了我无法弄清楚如何使用数组列表(由我的老师提供)来返回某个 DateTime 范围内的所有事务以及将这些事务从最旧到最新排序时,我已经完成了作业所需的一切。
到目前为止,这是我的代码:
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.text.DecimalFormat;
public class BankAccount {
//Instance Variables
String accountNumber;
double balance;
double withdrawalFee;
double annualInterestRate;
String description;
LocalDateTime transactionTime;
LocalDateTime startTime;
LocalDateTime endTime;
//Accessors and Mutators
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public double getWithdrawalFee() {
return withdrawalFee;
}
public void setWithdrawalFee(double withdrawalFee) {
this.withdrawalFee = withdrawalFee;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//Constructors
public BankAccount(String accountNumber) {
this.accountNumber = accountNumber;
}
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
balance = initialBalance;
}
public BankAccount(String accountNumber, double balance, double withdrawalFee, double annualInterestRate) {
this.accountNumber = accountNumber;
this.balance = balance;
this.withdrawalFee = withdrawalFee;
this.annualInterestRate = annualInterestRate;
}
//Public Methods
public ArrayList<Transaction> getTransactions = new ArrayList<Transaction>();
public void deposit(double amount) {
this.balance = balance + amount;
getTransactions.add(new Transaction(amount));
}
public void deposit(LocalDateTime transactionTime, double amount, String description) {
this.balance = balance + amount;
this.description = description;
this.transactionTime = transactionTime;
getTransactions.add(new Transaction(transactionTime, amount, description));
}
public void deposit(double amount, String description) {
this.balance = balance + amount;
this.description = description;
getTransactions.add(new Transaction(amount, description));
}
public void withdraw(double amount) {
this.balance = balance - amount;
getTransactions.add(new Transaction(amount));
}
public void withdraw(LocalDateTime transactionTime, double amount, String description) {
this.balance = balance - amount - withdrawalFee;
this.description = description;
this.transactionTime = transactionTime;
getTransactions.add(new Transaction(transactionTime, amount, description));
}
public void withdraw(double amount, String description) {
this.balance = balance - amount - withdrawalFee;
this.description = description;
getTransactions.add(new Transaction(amount, description));
}
//Array list that should return all transactions that occur within the DateTime range
//Transactions will be sorted from oldest to newest
//If startTime is null, then assume that there is no lower bound
//If endTime is null, then assume that there is no upper bound
public ArrayList<Transaction> getTransactions(LocalDateTime startTime, LocalDateTime endTime){
for(int i = 0 ; i < getTransactions.size() ; i++) {
}
}
boolean isOverDrawn() {
if(this.balance < 0) {
return true;
} else {
return false;
}
}
public String toString() {
DecimalFormat df = new DecimalFormat("0.00");
if(balance >= 0) {
return "BankAccount " + accountNumber + ": " + "$"+df.format(balance);
} else {
return "BankAccount " + accountNumber + ": " + "("+"$"+df.format(balance*-1)+")";
}
}
}
这是事务类:
import java.time.LocalDateTime;
public class Transaction {
LocalDateTime transactionTime;
double amount;
String description;
public Transaction(LocalDateTime transactionTime, double amount, String description){
this.transactionTime = transactionTime;
this.amount = amount;
this.description = description;
}
public Transaction(double amount) {
this.transactionTime = LocalDateTime.now();
this.amount = amount;
}
public Transaction(double amount, String description){
this.transactionTime = LocalDateTime.now();
this.amount = amount;
this.description = description;
}
public String getDescription(){
return description;
}
public LocalDateTime getTransactionTime(){
return transactionTime;
}
public double getAmount(){
return amount;
}
public String toString() {
return this.getClass().toString() + ":" + this.transactionTime.toString();
}
}
最后是我的代码需要通过的 JUnit 测试:
import java.time.LocalDate;
public class BankAccountImprovedTester extends junit.framework.TestCase
{
BankAccount chequing1;
BankAccount chequing2;
BankAccount chequing3;
Transaction t1;
protected void setUp()
{
chequing1 = new BankAccount("chequing1");
chequing2 = new BankAccount("chequing2", 100);
chequing3 = new BankAccount("chequing3", 100, 2.00, 5.0);
}
public void testAccessors()
{
assertEquals("chequing2", chequing2.getAccountNumber());
assertEquals(100.0, chequing2.getBalance(), 0);
assertEquals(5.0, chequing3.getAnnualInterestRate(), 0);
assertEquals(2.0, chequing3.getWithdrawalFee(), 0);
}
public void testMutators()
{
//test for setters
chequing1.setWithdrawalFee(3.0);
chequing1.setAnnualInterestRate(4.0);
assertEquals(3.0, chequing1.getWithdrawalFee(), 0);
assertEquals(4.0, chequing1.getAnnualInterestRate(), 0);
}
public void testDeposits()
{
//test various deposits and withdrawals
chequing1.setWithdrawalFee(3.0);
chequing1.deposit(500, "deposit 1");
assertEquals(500.0, chequing1.getBalance(), 0);
chequing1.withdraw(200, "withdrawal 1");
assertEquals(297.0, chequing1.getBalance(), 0);
assertEquals(false, chequing1.isOverDrawn());
chequing1.withdraw(300,"withdrawal 2");
assertEquals(-6.0, chequing1.getBalance(), 0);
assertEquals(true, chequing1.isOverDrawn());
chequing2.deposit(0.42, "deposit 2");
assertEquals(100.42, chequing2.getBalance(), 0.00);
chequing2.deposit(0.001, "deposit 3");
assertEquals(100.421, chequing2.getBalance(), 0.001);
}
public void testToString() {
assertEquals("BankAccount chequing1: $0.00", chequing1.toString());
assertEquals("BankAccount chequing2: $100.00", chequing2.toString());
chequing2.deposit(0.42, "deposit 1");
assertEquals("BankAccount chequing2: $100.42", chequing2.toString());
chequing2.deposit(0.001, "deposit 2");
assertEquals("BankAccount chequing2: $100.42", chequing2.toString());
BankAccount chequing4 = new BankAccount("chequing4", -100);
assertEquals("BankAccount chequing4: ($100.00)", chequing4.toString());
}
public void testGetTransactions()
{
chequing1.setWithdrawalFee(3.0);
LocalDate d1 = LocalDate.of(2000, 12, 31);
LocalTime t1 = LocalTime.of(14, 15, 30);
LocalDateTime dt1 = LocalDateTime.of(d1, t1);
chequing1.deposit(dt1, 600, "deposit 1");
LocalDate d2 = LocalDate.of(2001, 01, 02);
LocalTime t2 = LocalTime.of(14, 15, 30);
LocalDateTime dt2 = LocalDateTime.of(d2, t2);
chequing1.withdraw(dt2, 200, "withdrawal 1");
LocalDate d3 = LocalDate.of(2001, 01, 01);
LocalTime t3 = LocalTime.of(14, 15, 30);
LocalDateTime dt3 = LocalDateTime.of(d3, t3);
chequing1.withdraw(dt3, 300, "withdrawal 2");
LocalDate d4 = LocalDate.of(2001, 01, 03);
LocalTime t4 = LocalTime.of(14, 15, 30);
LocalDateTime dt4 = LocalDateTime.of(d4, t4);
chequing1.deposit(dt4, 100, "deposit 2");
//order should be: ["deposit 1"], ["withdrawal 2"], ["withdrawal 1"], ["deposit 2"]
ArrayList<Transaction> trans1 = chequing1.getTransactions(null, null);
assertEquals(4, trans1.size());
assertEquals("withdrawal 1", trans1.get(2).getDescription());
ArrayList<Transaction> trans2 = chequing1.getTransactions(dt2, null);
assertEquals(2, trans2.size());
assertEquals("withdrawal 1", trans2.get(0).getDescription());
ArrayList<Transaction> trans3 = chequing1.getTransactions(null, dt3);
assertEquals(2, trans3.size());
assertEquals("withdrawal 2", trans3.get(1).getDescription());
ArrayList<Transaction> trans4 = chequing1.getTransactions(dt3, dt2);
assertEquals(2, trans4.size());
//test if transactions without dateTime will be added to end of list
chequing1.deposit(50);
chequing1.withdraw(50);
ArrayList<Transaction> trans5 = chequing1.getTransactions(null, null);
assertEquals(6, trans5.size());
//test if transactions without dateTime have a dateTime equivalent to 'now'
assertEquals(false, LocalDateTime.now().isBefore(trans5.get(5).getTransactionTime()));
assertEquals(false, trans5.get(5).getTransactionTime() == null);
//test if transactions are returned in correct order
for (int i = 0; i < trans5.size() - 1; i++) {
Transaction earlierTranscation = trans5.get(i);
Transaction laterTranscation = trans5.get(i+1);
assertEquals(false, (laterTranscation.getTransactionTime().isBefore(earlierTranscation.getTransactionTime())));
}
}
}
我知道这有很多问题要问,但是我已经在这部分作业上卡了几天了,所以任何帮助都将不胜感激!
解决方案
- 第一个问题 - 过滤列表:
您希望根据满足约束的事务中的现有实例变量列表 ( )返回一个新的 ArrayList
getTransactions
BankAccount
ArrayList<Transaction> returnList = new ArrayList<>();
for(int i = 0 ; i < getTransactions.size() ; i++) {
Transaction currentTransaction = getTransactions.get(i);
//cater for if no lower bound or if the current transaction is after the lower bound
boolean pastStart = (startTime == null || currentTransaction.isAfter(startTime));
//cater for if no upper bound or if the current transaction is before the upper bound
boolean beforeEnd = (endTime == null || currentTransaction.isBefore(endTime));
//If both are true add to the return list
if (pastStart && beforeEnd) {
returnList.add(transaction);
}
}
Collections.sort(returnList);
return returnList;
- 第二个问题 - 排序列表
对于Collections.sort(returnList)
您需要Transaction
类来实现Comparable
接口的行 - 这在您的工作中是否涵盖?
- 增强
此外,可以简化 for 循环以在 Java 8 中使用 lambda,或者至少使用增强的“for each”循环,但我将其保留为带有索引的 for 循环,根据您的原始问题
推荐阅读
- javascript - 如何在 d3 v5 中为两个不同的列绘制圆圈?
- django - 无法显示媒体文件 Django
- python - 每个 n 元素划分一个 Python 列表
- ios - 在设备上使用播客应用程序时,MPMusicPlayerController 未启动(在前台)
- django - 迭代时首先显示某些类别
- amazon-web-services - 将 SSL 添加到使用 aws eb cli 构建的 Web 应用程序
- html - 如果不将它们包围在 div 中,如何将兄弟元素固定到粘性元素的底部,以便它们都充当单个粘性元素?
- php - 未定义类型“Dotenv\Dotenv”。intelephense(1009)
- python - 不能使用美丽的汤只打印文本
- assembly - 查找 MIPS 系统调用的操作码和功能码