首页 > 解决方案 > 使用 HashMap 添加/检索等

问题描述

编辑:我还有另外两个名为 CheckingAccount 和 SavingsAccount 的类。我也在添加我的帐户代码。

我有一个班级帐户,正在处理另一个与帐户有“HAS-A”关系的分类帐。在 Ledger 中,我使用 HashMap<>() 为不同的帐户创建存储系统。我想我的大部分编码都是正确的,期待最后两种方法。如果有人可以为最后两种方法解释或向正确的方向点头,并复习我的其他工作,那也会有所帮助。每个方法下面是一个注释块,说明该方法应该做什么和返回什么。谢谢。

/**
 * @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;
    }
}

标签: javahashmap

解决方案


注意:我将hashmap密钥更改为 int,如果要使用 String,则需要进行必要的更改

更改商店帐户

您检查帐户是否已存在的条件是错误的,您不能使用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

从 accounts 映射返回具有指定 accountID 的单个 Account。

这意味着方法应该有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) 传递规范中指定的参数(类型、名字和姓氏)

2)当我们从我们的方法int返回一个时,您的 hashmap 键现在将是。这对我来说更有意义。intgetNextAccountID

3)getNextAccountID从这个函数调用,因为它需要创建一个新帐户。

4)我假设你SavingAccountCheckingAccount类中有构造函数。如果您不这样做,请在使用默认构造函数初始化后创建一个或使用 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) 返回一个整数(如果需要,可以将其更改为 long)

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;
}

推荐阅读