473,795 Members | 3,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList to HashMap

1 New Member
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 16465
r035198x
13,262 MVP
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
11404
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 HashMap represented?
6
2934
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 greatly appreciated. /** A CarDealer object represents a business that buys and resells used cars */ import java.util.ArrayList; import java.util.HashMap; public class CarDealer { private int dayNumber; // Day number from start of operation of...
0
1158
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 states the destinations from that string along with the travel time. But how to find connections between two places? t.ex, a to b takes 15mins by buss , b to c takes 10mins by train, so if i want to find connection between a to c, it should...
1
1834
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 = allWeights.entrySet().iterator(); log.info("Number of users in the dataset: {}", allWeights.size()); Collection<Callable<Void>> evaluatorCallables = new ArrayList<Callable<Void>>(); while (itr.hasNext()) { Map.Entry entry =...
1
7524
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 the results I indeed see. The two pairs of code I am going to show are meant to find all the prime factors of a number (it is not my original code, I took it from Project Euler, and I'm further using it solve a later, harder problem in the project,...
0
983
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 (String words : a2) { if (words.contains(bigram1)){ if(test.containsKey(bigram1)) { test.get(bigram1).add(words);
0
1686
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 I have to do is get the distinct characters from the text that's been passed in and then assign values to them using a HashMap. I've done that. Also in front of the file there should be the number of distinct characters and the distinct characters...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10437
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.