我正在尝试创建一个银行系统(面向对象),可以保存/存储用户必须输入的帐户信息。创建帐户后,可以使用帐号来找到/找到帐户,以便提取,存款,查看交易和删除。
我遇到的问题是交易部分。对于每个帐户,您必须能够按升序存储最后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
答案 0 :(得分:3)
我只会给你一些提示。
首先,Java是一种OO语言,因此您应该使用对象。您应该拥有一个包含Transaction
类型对象的数组,而不是拥有两个数组来保存事务数量和事务摘要。 Transaction
类应包含字段amount
和字段summary
。
第二:您可以使用List<Transaction>
而不是使用数组。每次有新的交易进入时,您都会在列表的末尾添加它。如果列表大小大于6,则删除列表的第一个元素(最旧的元素)。 LinkedList是此功能的不错选择。由于列表具有大小,因此您无需自己维护numOfTransactions
。这个清单适合你。
您正在使用String来保存日期。这不是合适的类型。你应该使用日期。
您正在使用静态字段来保存已创建帐户的数量。那是糟糕的设计。您应该有一个Bank对象,其中包含银行的所有帐户(例如Map<Integer, BankAccount>
,这样可以根据其编号查找帐户)。
答案 1 :(得分:1)
如果您想使用数组执行此操作,则必须编写其他方法,并且应将事务和摘要放在一个类中(如@JB Nizet所说)。最好的方法是使用List
。否则你必须自己编写所有方法。方法应如下所示:
import java.util.*;
private List<Transaction> transactions = new ArrayList<Transaction>();
public void addTransaction (Transaction t) {
if (transactions.size() + 1 > 6)
transactions.remove(0); // deletes the list element on the first place
transactions.add(t);
}
如果你真的想要使用数组,打电话给我,我可以帮助你,但我建议你使用List。
我希望能帮到你!