我正在尝试创建一个银行系统(面向对象),该系统可以保存/存储用户必须输入的帐户信息。创建帐户后,可以使用该帐户的编号来查找/找到该帐户,以进行提款,存款,查看交易和删除。
我遇到的问题是交易部分。对于每个帐户,您必须只能按升序存储最后6个事务,并且必须将其打印出来。您能指导我寻找什么问题吗?
class BankAccount{
private int accountNumber;
private String holderName;
private String holderAddress;
private String openDate;
private double balance;
private double[] transactions;
private String[] transactionsSummary;
private int numOfTransactions;
private static int noOfAccounts=0;
public String getAccountInfo(){
return "Account number: " + accountNumber + "\nCustomer Name: " + holderName + "\nHolder's Address: " + holderAddress + "\nOpen Date: " + openDate + "\nBalance:" + balance +"\n";
}
public String getTransactionInfo(int n)
{
String transaction = transactionsSummary[n];
if (transaction == null) {
return "No transaction exists with that number.";
}
else {
return transaction;
}
}
public BankAccount(String abc, double xyz, String address, String open){
holderName = abc;
balance = xyz;
holderAddress = address;
openDate = open;
noOfAccounts ++;
accountNumber = noOfAccounts;
transactions = new double[100];
transactionsSummary = new String[100];
transactions[0] = balance;
transactionsSummary[0] = "A balance of : $" + Double.toString(balance) + " was deposited.";
numOfTransactions = 1;
}
public int getAccountNum(){
return accountNumber;
}
public int getNumberOfTransactions() {
return numOfTransactions;
}
public void deposit(double amount){
if (amount<=0) {
System.out.println("Amount to be deposited should be positive");
} else {
balance = balance + amount;
transactions[numOfTransactions] = amount;
transactionsSummary[numOfTransactions] = "$" + Double.toString(amount) + " was deposited.";
numOfTransactions++;
}
}
public void withdraw(double amount)
{
if (amount<=0){
System.out.println("Amount to be withdrawn should be positive");
}
else
{
if (balance < amount) {
System.out.println("Insufficient balance");
} else {
balance = balance - amount;
transactions[numOfTransactions] = amount;
transactionsSummary[numOfTransactions] = "$" + Double.toString(amount) + " was withdrawn.";
numOfTransactions++;
}
}
}
}//end of class
class BankAccount{
private int accountNumber;
private String holderName;
private String holderAddress;
private String openDate;
private double balance;
private double[] transactions;
private String[] transactionsSummary;
private int numOfTransactions;
private static int noOfAccounts=0;
public String getAccountInfo(){
return "Account number: " + accountNumber + "\nCustomer Name: " + holderName + "\nHolder's Address: " + holderAddress + "\nOpen Date: " + openDate + "\nBalance:" + balance +"\n";
}
public String getTransactionInfo(int n)
{
String transaction = transactionsSummary[n];
if (transaction == null) {
return "No transaction exists with that number.";
}
else {
return transaction;
}
}
public BankAccount(String abc, double xyz, String address, String open){
holderName = abc;
balance = xyz;
holderAddress = address;
openDate = open;
noOfAccounts ++;
accountNumber = noOfAccounts;
transactions = new double[100];
transactionsSummary = new String[100];
transactions[0] = balance;
transactionsSummary[0] = "A balance of : $" + Double.toString(balance) + " was deposited.";
numOfTransactions = 1;
}
public int getAccountNum(){
return accountNumber;
}
public int getNumberOfTransactions() {
return numOfTransactions;
}
public void deposit(double amount){
if (amount<=0) {
System.out.println("Amount to be deposited should be positive");
} else {
balance = balance + amount;
transactions[numOfTransactions] = amount;
transactionsSummary[numOfTransactions] = "$" + Double.toString(amount) + " was deposited.";
numOfTransactions++;
}
}
public void withdraw(double amount)
{
if (amount<=0){
System.out.println("Amount to be withdrawn should be positive");
}
else
{
if (balance < amount) {
System.out.println("Insufficient balance");
} else {
balance = balance - amount;
transactions[numOfTransactions] = amount;
transactionsSummary[numOfTransactions] = "$" + Double.toString(amount) + " was withdrawn.";
numOfTransactions++;
}
}
}
}//end of class
最佳答案
我只会给你一些提示。
首先,Java是一种OO语言,因此您应该使用对象。不应有两个数组来保存交易金额和交易摘要,而应该有一个数组来保存类型为Transaction
的对象。 Transaction
类应具有一个字段amount
和一个字段summary
。
第二:您可以使用List<Transaction>
代替数组。每次有新交易进入时,您都将其添加到列表的末尾。如果列表大小大于6,则删除列表的第一个元素(最旧的元素)。 LinkedList是此功能的不错选择。由于列表具有大小,因此您无需自己维护numOfTransactions
。列表为您做到了。
您正在使用字符串保存日期。那是不合适的类型。您应该使用日期。
您正在使用静态字段来保存已创建帐户的数量。那是糟糕的设计。相反,您应该有一个Bank对象,其中包含银行的所有帐户(例如,作为Map<Integer, BankAccount>
,这将允许根据其编号查找一个帐户)。
关于java - Java-银行系统+交易功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27122176/