ArrayList to HashMap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrodri1080
    New Member
    • Feb 2007
    • 1

    #1

    ArrayList to HashMap

    I have a script that i need help with from school, i have an arraylist and i need to switch that for a HashMap. I would appreciate if anyone can help. THANKS

    Code:
     import java.util.*; 
     
    /**
    *
    */
     
     
    public class Bank
    {
    // Storage for an arbitrary number of AccountAtBank objects
     
    private ArrayList<AccountAtBank> bankAccounts;
     
     
    // Storage for an arbitrary number of Client objects
     
    private ArrayList<Client> clients;
     
     
     
    public Scanner myScanner = new Scanner(System.in);
     
    // Constructor
     
    public Bank()
    {
    bankAccounts = new ArrayList<AccountAtBank>();
    clients = new ArrayList<Client>();
    }
     
    // Mutators
     
     
    public void storeBankAccount(AccountAtBank nAccount)
    {
    /* Check that the account does not exist, before inserting it into the 
    * ArrayList.
    */
    Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
    boolean found = false;
    AccountAtBank checkAccount;
    while (iterAccounts.hasNext() && !found)
    {
    checkAccount = iterAccounts.next();
    if (checkAccount.getCustomer().equals(nAccount.getCustomer()) )
    {
    found = true;
    System.out.println("This client already has an account");
    }
    }
    // If the account does not exist, insert it.
    if (!found)
    bankAccounts.add(nAccount);
    }
     
    public void storeClient(Client nClient)
    {
    clients.add(nClient);
    }
     
    public void removeBankAccount(int bankAccountIndex)
    {
    if (bankAccountIndex < 0 || bankAccountIndex >= bankAccounts.size())
    {
    System.out.println("The account number given does not match with our records");
    }
    else
    {
    AccountAtBank accountToDelete = bankAccounts.get(bankAccountIndex);
    String custName = accountToDelete.getCustomer(); 
    int custAccount = accountToDelete.getAccountNumber();
    System.out.println("Is the account of " + custName +" the one you want to delete?");
    System.out.println("Please enter y/n.");
    System.out.print(">");
     
    //Gets the next String of data from the keyboard up to a "\n" and stores it in name
    String answer = myScanner.nextLine();
    // Convert the answer to upper case to establish a canonical form for the answer
    answer = new String(answer.toUpperCase());
    if (answer.equals("Y"))
    {
    bankAccounts.remove(bankAccountIndex);
    Iterator<Client> iterClients = clients.iterator();
    boolean found = false;
    Client checkClient;
    while (iterClients.hasNext() && !found)
    {
    checkClient = iterClients.next();
    if(checkClient.getOwner().equals(custName) && 
    (checkClient.getAccountNumber() == custAccount))
    {
    found = true;
    clients.remove(clients.indexOf(checkClient));
    }
    }
     
    }
    else
    { 
    System.out.println("You should verify the account you want to delete");
    }
    }
    }
     
    // Accessors
     
    // For the bank accounts
     
    public int numberOfAccounts()
    {
    return bankAccounts.size();
    }
     
    public void showAccount(int accountNumber)
    {
    Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
    boolean found = false;
    AccountAtBank checkAccount;
    while (iterAccounts.hasNext() && !found)
    {
    checkAccount = iterAccounts.next();
    if (checkAccount.getAccountNumber() == accountNumber)
    {
    found = true;
    System.out.println("The account belong to "+ checkAccount.getCustomer());
    System.out.println("The balance in the account is $" + checkAccount.getBalance());
    }
    }
    if (!found)
    {
    System.out.print ("The account number " + accountNumber);
    System.out.println(" is not a valid account number");
    }
    } // End of showAccount
     
    public AccountAtBank getBankAccount(int bankAccountIndex)
    {
    if (bankAccountIndex < 0 || bankAccountIndex >= bankAccounts.size())
    {
    System.out.println("The account number given does not match with our records");
    return null;
    }
    else
    {
    AccountAtBank accountToGet = bankAccounts.get(bankAccountIndex);
    return accountToGet;
    }
    } // End of getBankAccount
     
    } // End of Bank class
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by jrodri1080
    I have a script that i need help with from school, i have an arraylist and i need to switch that for a HashMap. I would appreciate if anyone can help. THANKS

    Code:
     import java.util.*; 
     
    /**
    *
    */
     
     
    public class Bank
    {
    // Storage for an arbitrary number of AccountAtBank objects
     
    private ArrayList<AccountAtBank> bankAccounts;
     
     
    // Storage for an arbitrary number of Client objects
     
    private ArrayList<Client> clients;
     
     
     
    public Scanner myScanner = new Scanner(System.in);
     
    // Constructor
     
    public Bank()
    {
    bankAccounts = new ArrayList<AccountAtBank>();
    clients = new ArrayList<Client>();
    }
     
    // Mutators
     
     
    public void storeBankAccount(AccountAtBank nAccount)
    {
    /* Check that the account does not exist, before inserting it into the 
    * ArrayList.
    */
    Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
    boolean found = false;
    AccountAtBank checkAccount;
    while (iterAccounts.hasNext() && !found)
    {
    checkAccount = iterAccounts.next();
    if (checkAccount.getCustomer().equals(nAccount.getCustomer()) )
    {
    found = true;
    System.out.println("This client already has an account");
    }
    }
    // If the account does not exist, insert it.
    if (!found)
    bankAccounts.add(nAccount);
    }
     
    public void storeClient(Client nClient)
    {
    clients.add(nClient);
    }
     
    public void removeBankAccount(int bankAccountIndex)
    {
    if (bankAccountIndex < 0 || bankAccountIndex >= bankAccounts.size())
    {
    System.out.println("The account number given does not match with our records");
    }
    else
    {
    AccountAtBank accountToDelete = bankAccounts.get(bankAccountIndex);
    String custName = accountToDelete.getCustomer(); 
    int custAccount = accountToDelete.getAccountNumber();
    System.out.println("Is the account of " + custName +" the one you want to delete?");
    System.out.println("Please enter y/n.");
    System.out.print(">");
     
    //Gets the next String of data from the keyboard up to a "\n" and stores it in name
    String answer = myScanner.nextLine();
    // Convert the answer to upper case to establish a canonical form for the answer
    answer = new String(answer.toUpperCase());
    if (answer.equals("Y"))
    {
    bankAccounts.remove(bankAccountIndex);
    Iterator<Client> iterClients = clients.iterator();
    boolean found = false;
    Client checkClient;
    while (iterClients.hasNext() && !found)
    {
    checkClient = iterClients.next();
    if(checkClient.getOwner().equals(custName) && 
    (checkClient.getAccountNumber() == custAccount))
    {
    found = true;
    clients.remove(clients.indexOf(checkClient));
    }
    }
     
    }
    else
    { 
    System.out.println("You should verify the account you want to delete");
    }
    }
    }
     
    // Accessors
     
    // For the bank accounts
     
    public int numberOfAccounts()
    {
    return bankAccounts.size();
    }
     
    public void showAccount(int accountNumber)
    {
    Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
    boolean found = false;
    AccountAtBank checkAccount;
    while (iterAccounts.hasNext() && !found)
    {
    checkAccount = iterAccounts.next();
    if (checkAccount.getAccountNumber() == accountNumber)
    {
    found = true;
    System.out.println("The account belong to "+ checkAccount.getCustomer());
    System.out.println("The balance in the account is $" + checkAccount.getBalance());
    }
    }
    if (!found)
    {
    System.out.print ("The account number " + accountNumber);
    System.out.println(" is not a valid account number");
    }
    } // End of showAccount
     
    public AccountAtBank getBankAccount(int bankAccountIndex)
    {
    if (bankAccountIndex < 0 || bankAccountIndex >= bankAccounts.size())
    {
    System.out.println("The account number given does not match with our records");
    return null;
    }
    else
    {
    AccountAtBank accountToGet = bankAccounts.get(bankAccountIndex);
    return accountToGet;
    }
    } // End of getBankAccount
     
    } // End of Bank class
    You can't just switch ArrayList with HashMap. There are two different structures. A map maps two objects. A key and a value. You have to decide what you want as the key and what you want as the value.

    Comment

    Working...