使用HashMap添加/检索等

时间:2019-02-10 23:25:28

标签: java hashmap

编辑:我还有另外两个类,称为CheckingAccount和SavingsAccount。我还要添加我的帐户代码。

我有一个Account类,正在研究另一个Ledger类,它与Account具有“ 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;
    }
}

2 个答案:

答案 0 :(得分:1)

注意:我将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

  

从帐户映射中返回具有指定帐户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)传递规范中指定的参数(类型,名字和姓氏)

2)当我们从int方法返回一个int时,您的哈希映射键现在为getNextAccountID。这对我来说更有意义。

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

答案 1 :(得分:0)

首先,此方法getNextAccountID应该返回一个整数

第二点,我建议

 public List<Account> getAllAccounts() {
        Set<Account> allaccounts = accounts.values(); 
        LinkedList<Account> l ; 
        l.addAll(allacounts);
        return l; 
  }

在我的建议中,我尝试使用界面映射中的methode值将帐户保存在一组中,然后将保存在该组中的所有帐户添加到列表中,最后返回该列表,我希望它将是有用。