EDITS:我有两个其他类调用的CheckingAccount和SavingsAccount。我加入我的帐户代码普遍回落。
我有一个类账户及其他类莱杰这是与客户“具有-A”的关系的工作。在分类帐,我使用HashMap <>()为不同的帐户创建的存储系统。我想我有大部分的编码正确的,希望最后两个方法。如果有人可以解释或给予在正确的方向上nodge最后两个方法,去了别的工作,这也将帮助。下面每一种方法是什么样的方法是应该做的,并返回一个注释块。谢谢。
/**
* @author Deborah
*
*/
public abstract class Account {
protected String accountID;
protected double balance;
protected String accountType;
protected String firstName;
protected String lastName;
public String getAccountID() {
return this.accountID;
}
public double getBalance() {
return this.balance ;
}
public String getAccountType() {
return this.accountType;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public void setAccountID(String accountID) {
this.accountID = accountID;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String toString() {
return "Account ID: " + accountID + "\n" +
"Account Type: " + accountType + "\n" +
"Balance: $" + balance + "\n";
}
public abstract IAccountManager getAccountManager();
}
public class Ledger {
//the single instance of Ledger
private static final Ledger instance = new Ledger();
//create accounts: Map<String, Account>
//use to add an Account, retrieve an Account, ect...
HashMap<String, Account> accounts = new HashMap<>();
//private constructor
private Ledger() {}
//method to return the single instance
public static Ledger getInstance(){
return instance;
}
public boolean store(Account account) {
/* adds a new Account to the accounts Map. The key is the account’s ID. This
* method should return a Boolean object that is "true" when the Account added
* to the ledger is being added for the first time, "false" if it is being
* updated. Check the documentation for the Map.put() method for reference.
*/
String key = account.getAccountID();
if(accounts.put(key, account) != null){
accounts.put(key, account);
return true;
}
return false;
}
public Account retrieve() {
/* returns a single Account with the specified accountID from the accounts Map.
* If none is found, return null.
*/
Account account = new Account();
String key = account.getAccountID();
if(accounts.containsKey(key)) {
return account;
}
return null;
}
public Account createAccount(Account account) {
/* this method creates and returns a new Account object with the specified
* parameters (type, firstName, lastName). Calling this method should store the
* new account within the accounts Map. Please note the first parameter passed
* into this method determines which account type (CheckingAccount vs.
* SavingsAccount) to create. The values should either be “checking” or
* “savings”.
*/
String key = account.getAccountType();
if(accounts.containsKey("Checking")) {
accounts.put(key,account);
return account;
}else{
accounts.put(key,account);
return account;
}
}
public Account getNextAccountID() {
/*this is a helper method that can be used to find out what the next
* accountID will be based on the number of Accounts within the accounts Map.
* It should return the size of the accounts Map plus one.
*/
return accounts.size() + 1;
}
public Ledger getAllAccounts() {
/*this method returns a list of all the Accounts w/in the accounts Map
*/
return null;
}
}
注:我改变了我的hashmap
键为int,如果你想使用字符串,那么你需要进行必要的修改
改变存储帐户
你的病情检查,如果账户已经存在是错误的,你不能使用put
方法是改用containsKey
public boolean store(Account account) {
/* adds a new Account to the accounts Map. The key is the account’s ID. This
* method should return a Boolean object that is "true" when the Account added
* to the ledger is being added for the first time, "false" if it is being
* updated. Check the documentation for the Map.put() method for reference.
*/
int key = account.getAccountID();
if(accounts.containsKey(key) != null){
accounts.put(key, account);
return true;
}
return false;
}
对于retrieve
方法的变化:
正在使用这个方法来获取一个帐户,以便需要在这里创建一个新的account
实例。它明确规定,如果你没有找到一个帐户,然后返回null
。
返回从账户地图指定的帐户ID单一帐户。
这意味着方法应该有accountId
作为参数,然后我们需要寻找它在我们的地图。
public Account retrieve(int accountId) {
/* returns a single Account with the specified accountID from the accounts Map.
* If none is found, return null.
*/
if(accounts.containsKey(accountId)) {
return accounts.get(accountId);
}
return null;
}
更改createAccount
:
1)传输参数(类型,名字和lastName)如在规范中指定
2)你的HashMap的关键将是现在int
,我们返回我们int
方法的getNextAccountID
。这使我更有意义。
3),因为它是需要建立一个新的帐户从这个函数中调用getNextAccountID
。
4)我假设你在SavingAccount
和CheckingAccount
类具有构造函数。如果你不这样做,那么请创建一个或默认的构造函数初始化后使用set方法。你应该构造平衡值赋值为0。
public Account createAccount(String accountType, String firstName, String lastName) {
/* this method creates and returns a new Account object with the specified
* parameters (type, firstName, lastName). Calling this method should store the
* new account within the accounts Map. Please note the first parameter passed
* into this method determines which account type (CheckingAccount vs.
* SavingsAccount) to create. The values should either be “checking” or
* “savings”.
*/
int accountId = getNextAccountID();
Account acc;
if(type == "checking"){
acc = new CheckingAccount(id, type, firstName, lastName);
} else {
acc = new SavingAccount(id, type, firstName, lastName);
}
return acc;
}
更改getNextAccountID
:
1)返回一个整数(你可以将它更改为长,如果你想)
public Integer getNextAccountID() {
/*this is a helper method that can be used to find out what the next
* accountID will be based on the number of Accounts within the accounts Map.
* It should return the size of the accounts Map plus one.
*/
return accounts.size() + 1;
}
首先这种方法GETNEXT的AccountID应该返回一个int
而对于第二个我建议
public List<Account> getAllAccounts() {
Set<Account> allaccounts = accounts.values();
LinkedList<Account> l ;
l.addAll(allacounts);
return l;
}
在我的建议,我试图使用保存在接口映射的梅索德值在一组帐户,然后添加在列表保存在集合中的所有账户,最后返回列表,我希望这将是有益的。