473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1.  import java.util.*; 
  2.  
  3. /**
  4. *
  5. */
  6.  
  7.  
  8. public class Bank
  9. {
  10. // Storage for an arbitrary number of AccountAtBank objects
  11.  
  12. private ArrayList<AccountAtBank> bankAccounts;
  13.  
  14.  
  15. // Storage for an arbitrary number of Client objects
  16.  
  17. private ArrayList<Client> clients;
  18.  
  19.  
  20.  
  21. public Scanner myScanner = new Scanner(System.in);
  22.  
  23. // Constructor
  24.  
  25. public Bank()
  26. {
  27. bankAccounts = new ArrayList<AccountAtBank>();
  28. clients = new ArrayList<Client>();
  29. }
  30.  
  31. // Mutators
  32.  
  33.  
  34. public void storeBankAccount(AccountAtBank nAccount)
  35. {
  36. /* Check that the account does not exist, before inserting it into the 
  37. * ArrayList.
  38. */
  39. Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
  40. boolean found = false;
  41. AccountAtBank checkAccount;
  42. while (iterAccounts.hasNext() && !found)
  43. {
  44. checkAccount = iterAccounts.next();
  45. if (checkAccount.getCustomer().equals(nAccount.getCustomer()) )
  46. {
  47. found = true;
  48. System.out.println("This client already has an account");
  49. }
  50. }
  51. // If the account does not exist, insert it.
  52. if (!found)
  53. bankAccounts.add(nAccount);
  54. }
  55.  
  56. public void storeClient(Client nClient)
  57. {
  58. clients.add(nClient);
  59. }
  60.  
  61. public void removeBankAccount(int bankAccountIndex)
  62. {
  63. if (bankAccountIndex < 0 || bankAccountIndex >= bankAccounts.size())
  64. {
  65. System.out.println("The account number given does not match with our records");
  66. }
  67. else
  68. {
  69. AccountAtBank accountToDelete = bankAccounts.get(bankAccountIndex);
  70. String custName = accountToDelete.getCustomer(); 
  71. int custAccount = accountToDelete.getAccountNumber();
  72. System.out.println("Is the account of " + custName +" the one you want to delete?");
  73. System.out.println("Please enter y/n.");
  74. System.out.print(">");
  75.  
  76. //Gets the next String of data from the keyboard up to a "\n" and stores it in name
  77. String answer = myScanner.nextLine();
  78. // Convert the answer to upper case to establish a canonical form for the answer
  79. answer = new String(answer.toUpperCase());
  80. if (answer.equals("Y"))
  81. {
  82. bankAccounts.remove(bankAccountIndex);
  83. Iterator<Client> iterClients = clients.iterator();
  84. boolean found = false;
  85. Client checkClient;
  86. while (iterClients.hasNext() && !found)
  87. {
  88. checkClient = iterClients.next();
  89. if(checkClient.getOwner().equals(custName) && 
  90. (checkClient.getAccountNumber() == custAccount))
  91. {
  92. found = true;
  93. clients.remove(clients.indexOf(checkClient));
  94. }
  95. }
  96.  
  97. }
  98. else
  99. System.out.println("You should verify the account you want to delete");
  100. }
  101. }
  102. }
  103.  
  104. // Accessors
  105.  
  106. // For the bank accounts
  107.  
  108. public int numberOfAccounts()
  109. {
  110. return bankAccounts.size();
  111. }
  112.  
  113. public void showAccount(int accountNumber)
  114. {
  115. Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
  116. boolean found = false;
  117. AccountAtBank checkAccount;
  118. while (iterAccounts.hasNext() && !found)
  119. {
  120. checkAccount = iterAccounts.next();
  121. if (checkAccount.getAccountNumber() == accountNumber)
  122. {
  123. found = true;
  124. System.out.println("The account belong to "+ checkAccount.getCustomer());
  125. System.out.println("The balance in the account is $" + checkAccount.getBalance());
  126. }
  127. }
  128. if (!found)
  129. {
  130. System.out.print ("The account number " + accountNumber);
  131. System.out.println(" is not a valid account number");
  132. }
  133. } // End of showAccount
  134.  
  135. public AccountAtBank getBankAccount(int bankAccountIndex)
  136. {
  137. if (bankAccountIndex < 0 || bankAccountIndex >= bankAccounts.size())
  138. {
  139. System.out.println("The account number given does not match with our records");
  140. return null;
  141. }
  142. else
  143. {
  144. AccountAtBank accountToGet = bankAccounts.get(bankAccountIndex);
  145. return accountToGet;
  146. }
  147. } // End of getBankAccount
  148.  
  149. } // End of Bank class
  150.  
Feb 20 '07 #1
1 16443
r035198x
13,262 8TB
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

Expand|Select|Wrap|Line Numbers
  1.  import java.util.*; 
  2.  
  3. /**
  4. *
  5. */
  6.  
  7.  
  8. public class Bank
  9. {
  10. // Storage for an arbitrary number of AccountAtBank objects
  11.  
  12. private ArrayList<AccountAtBank> bankAccounts;
  13.  
  14.  
  15. // Storage for an arbitrary number of Client objects
  16.  
  17. private ArrayList<Client> clients;
  18.  
  19.  
  20.  
  21. public Scanner myScanner = new Scanner(System.in);
  22.  
  23. // Constructor
  24.  
  25. public Bank()
  26. {
  27. bankAccounts = new ArrayList<AccountAtBank>();
  28. clients = new ArrayList<Client>();
  29. }
  30.  
  31. // Mutators
  32.  
  33.  
  34. public void storeBankAccount(AccountAtBank nAccount)
  35. {
  36. /* Check that the account does not exist, before inserting it into the 
  37. * ArrayList.
  38. */
  39. Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
  40. boolean found = false;
  41. AccountAtBank checkAccount;
  42. while (iterAccounts.hasNext() && !found)
  43. {
  44. checkAccount = iterAccounts.next();
  45. if (checkAccount.getCustomer().equals(nAccount.getCustomer()) )
  46. {
  47. found = true;
  48. System.out.println("This client already has an account");
  49. }
  50. }
  51. // If the account does not exist, insert it.
  52. if (!found)
  53. bankAccounts.add(nAccount);
  54. }
  55.  
  56. public void storeClient(Client nClient)
  57. {
  58. clients.add(nClient);
  59. }
  60.  
  61. public void removeBankAccount(int bankAccountIndex)
  62. {
  63. if (bankAccountIndex < 0 || bankAccountIndex >= bankAccounts.size())
  64. {
  65. System.out.println("The account number given does not match with our records");
  66. }
  67. else
  68. {
  69. AccountAtBank accountToDelete = bankAccounts.get(bankAccountIndex);
  70. String custName = accountToDelete.getCustomer(); 
  71. int custAccount = accountToDelete.getAccountNumber();
  72. System.out.println("Is the account of " + custName +" the one you want to delete?");
  73. System.out.println("Please enter y/n.");
  74. System.out.print(">");
  75.  
  76. //Gets the next String of data from the keyboard up to a "\n" and stores it in name
  77. String answer = myScanner.nextLine();
  78. // Convert the answer to upper case to establish a canonical form for the answer
  79. answer = new String(answer.toUpperCase());
  80. if (answer.equals("Y"))
  81. {
  82. bankAccounts.remove(bankAccountIndex);
  83. Iterator<Client> iterClients = clients.iterator();
  84. boolean found = false;
  85. Client checkClient;
  86. while (iterClients.hasNext() && !found)
  87. {
  88. checkClient = iterClients.next();
  89. if(checkClient.getOwner().equals(custName) && 
  90. (checkClient.getAccountNumber() == custAccount))
  91. {
  92. found = true;
  93. clients.remove(clients.indexOf(checkClient));
  94. }
  95. }
  96.  
  97. }
  98. else
  99. System.out.println("You should verify the account you want to delete");
  100. }
  101. }
  102. }
  103.  
  104. // Accessors
  105.  
  106. // For the bank accounts
  107.  
  108. public int numberOfAccounts()
  109. {
  110. return bankAccounts.size();
  111. }
  112.  
  113. public void showAccount(int accountNumber)
  114. {
  115. Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
  116. boolean found = false;
  117. AccountAtBank checkAccount;
  118. while (iterAccounts.hasNext() && !found)
  119. {
  120. checkAccount = iterAccounts.next();
  121. if (checkAccount.getAccountNumber() == accountNumber)
  122. {
  123. found = true;
  124. System.out.println("The account belong to "+ checkAccount.getCustomer());
  125. System.out.println("The balance in the account is $" + checkAccount.getBalance());
  126. }
  127. }
  128. if (!found)
  129. {
  130. System.out.print ("The account number " + accountNumber);
  131. System.out.println(" is not a valid account number");
  132. }
  133. } // End of showAccount
  134.  
  135. public AccountAtBank getBankAccount(int bankAccountIndex)
  136. {
  137. if (bankAccountIndex < 0 || bankAccountIndex >= bankAccounts.size())
  138. {
  139. System.out.println("The account number given does not match with our records");
  140. return null;
  141. }
  142. else
  143. {
  144. AccountAtBank accountToGet = bankAccounts.get(bankAccountIndex);
  145. return accountToGet;
  146. }
  147. } // End of getBankAccount
  148.  
  149. } // End of Bank class
  150.  
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.
Feb 20 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Welson Sun | last post by:
Hi, I am new to UML, I would like to know how can represent JAVA's ArrayList and HashMap with UML diagram? How can I represent the element's class in the ArrayList/HashMap? How is the key in the...
6
by: bumrag | last post by:
This is the car dealership object relating to the coursework, there is also a separate object named car that i think i need to link to. The problem is on the addcar() method. Any help would be...
0
by: trask | last post by:
hi, I am troubled by a small problem. I am now using a graph metod which uses edge to connect various places in a map. So i created a hashmap which takes in String and an arraylist (edge) which...
1
by: jamborta | last post by:
hi guys, I'm sure it's something silly but I can't figure out what's wrong with this code: HashMap allWeights = weights.getAllWeights(); Iterator itr =...
1
blazedaces
by: blazedaces | last post by:
Before we begin let me say that I already found the solution to the problem, but I am still unable to explain why it solved the problem, so I am here to ask you experts why this java code produces...
0
by: Anthony Kwang | last post by:
Hi, Basically i have 2 arraylist and 1 hashmap and i want to store the value inside the hashmap. but my codes seem never do any comparison. for (String bigram1 : bigram) { for...
0
yoda
by: yoda | last post by:
So has the title says I need to find a away to loop over a Character ArrayList and replace the characters with a values from a HasMap. This is a simple encryption but I can't figure it out. What...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.