Results 1 to 15 of 15
- 08-20-2012, 12:10 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Create customers with unique personNumber
I have customers in an arrayList.
List <Customer> customers = new ArrayList();
In a Bank class I have code to create a customer with a unique personNumber
In the Customer class I have:Java Code:case 2: System.out.println("Personnummer: "); String aPNumber = scanner.nextLine(); for(Customer kund: customers){ if(aPNumber.equals(kund.getPNumber())){ System.out.println("There is already a customer with this p-number”); break; } else{ Customer aCustomer = new Customer(aFirstName, aSecondName, aPNumber); customers.add(aCustomer); } } break;
I can create two or more customers with the same personNumber with the following code. But in the code above I always get the message "There is already a customer with this p-number” even for the first customer. I can´t find what is wrong. I must be blind. Please give me a hint about what to do to solve the problem.Java Code:public void setPNumber(String personNumber){ pNumber= personNumber; } public String getPNumber(){ return pNumber; }
Java Code:System.out.println("Personnummer: "); String aPNumber = scanner.nextLine(); Customer aCustomer = new Customer(aFirstName, aSecondName, aPNumber); customers.add(aCustomer);
- 08-20-2012, 01:00 PM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
- 08-21-2012, 09:49 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: Create customers with unique personNumber
I have now solved this problem but when I choose a customer with the following code:
and want to delete this customer with the following code:Java Code:case 3: System.out.println("Choose a customer in the customerlist: \n"); for(int i=0; i<customers.size(); i++){ System.out.println("Kund nr " + (i+1) + ":\n" + customers.get(i) + "\n"); } System.out.println("Write the personNumber of the choosen customer): "); String kundNummer = scanner.nextLine(); // control that the chosen number exist in the register for(Customer kund: customers){ if(kundNummer.equals(kund.getPNumber())){ System.out.println("You have chosen a customer with personNumber: " + kundNummer); customerMenu(kundNummer); } else { System.out.println("The chosen number doesn´t exist in the register."); } } break;
I get the following error:Java Code:Customer removeCustomer = null; for(Customer kund1: customers){ if(kundNummer.equals(kund1.pNumber)){ removeCustomer = kund1; } } customers.remove(removeCustomer); System.out.println("Customer with personNummer " + kundNummer + " is deleted from the register. ");
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unk nown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
Without the control code above:
it works fine but when I add this code there is some conflict. How do I solve this problem?Java Code:for(Customer kund: customers){ if(kundNummer.equals(kund.getPNumber())){ System.out.println("You have chosen a customer with personNumber: " + kundNummer); customerMenu(kundNummer); } else { System.out.println("The chosen number doesn´t exist in the register."); } }
- 08-21-2012, 11:06 AM #4
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Create customers with unique personNumber
I believe that ConcurrentModificationException is thrown because you are using the iterator (kund) in your for-loop at the same time that you are modifying the list. You cannot do both at the same time.
Since we don't have that much code to go on, I'm not sure I can help you further.
- 08-21-2012, 12:00 PM #5
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: Create customers with unique personNumber
Of course I can send all my code but when I did it in another thread it wasn´t popular. I have four classes and one of them is quite long - and all the comments are in Swedish. Maybe I can send you extract from the long back class, but the problem seems to be that different code snippets interact with each other. If I take the control code in create a customer away the remove-code works fine.
Instead of the above described remove-code I have tried the following way to do delete customers but the same problem occurs.
Java Code:// create a delete-list List<Customer> deleteList = new ArrayList<Customer>(); for(Customer c:customers){ if(c.getPNumber().equals(kundNummer)){ deleteList.add(c); } } for(Customer c: deleteList){ customers.remove(c); System.out.println("Chosen customer is deleted.");
- 08-21-2012, 12:13 PM #6
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Create customers with unique personNumber
You are iterating through a list with a for loop that at the same time you are modifying. That doesn't go hand in hand.
- 08-21-2012, 12:48 PM #7
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: Create customers with unique personNumber
But there is no problem to remove customers with this code if I don´t have the following code in anopther code-snippet:
Is there any other way to remove a customer from an ArrayList?Java Code:for(Customer kund: customers){ if(kundNummer.equals(kund.getPNumber())){ System.out.println("You have chosen a customer with personNumber: " + kundNummer); customerMenu(kundNummer); } else { System.out.println("The chosen number doesn´t exist in the register."); } }
- 08-21-2012, 01:20 PM #8
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Create customers with unique personNumber
I'm not sure I'm helping right now but, perhaps split the code up into different methods, get the index of the customer to be removed, remove it, end method.
Then do what else you want to do. I don't know how you've structured your design, so I can't say what I would've done. All I know is that if you get ConcurrentModificationException, there is work going on on the same object at several places at the same time.
- 08-21-2012, 02:28 PM #9
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: Create customers with unique personNumber
Thank you for your patience with my problem. I have now made a quick translation of my code to English and here is the Customer class and Bank class
Customer class
Bank classJava Code:public class Customer { private String firstName; private String secondName; protected String pNumber; public Customer(){ firstName= "unknown"; secondName = "unknown"; pNumber = "unknown"; } public Customer(String firstName, String secondName, String pNumber){ this.firstName = firstName; this.secondName = secondName; this.pNumber = pNumber; } public void changeName(String fname, String ename){ firstName = fname; secondName = ename; } public void setPNumber(String personNummer){ pNumber= personNummer; } public String getFirstName (){ return firstName; } public String getSecondName (){ return secondName; } public String getPNumber (){ return pNumber; } public String toString(){ String info = "Personnumber: " + pNumber + "\nCustomer name: " + firstName + " " + secondName; return info; } }
Please, tell me if you also need the Account class.Java Code:import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Bank { SavingAccounts konto = new SavingAccounts(); List <Customer> customers = new ArrayList(); List<SavingAccounts> accounts = new ArrayList(customers); Scanner scanner = new Scanner(System.in); private int showMenu(String title, String[] items) { System.out.println(title); for(int i = 0; i < items.length; i++) { System.out.println(items[i]); } System.out.print("\nChoose in the menu: "); Scanner scan = new Scanner(System.in); return scan.nextInt(); } public void menu() { String menuTitle = "\n### Wellcome to the bank ###\n"; String[] menuItems = {"1. Show customers", "2. Create a customer", "3. Choose customer", "0. Exit"}; while(true) { int input = showMenu(menuTitle, menuItems); switch(input) { case 1: System.out.println("\nCustomers: "); if(customers.size()>0){ for(int i=0; i<customers.size(); i++){ System.out.println("Kund nr " + (i+1) + ":\n" + customers.get(i) + "\n"); } } else{ System.out.println("Det finns inga kunder i bankregistret."); } break; case 2: // Create a customer System.out.println("Who are you?"); System.out.println("First name: "); String aFirstName = scanner.nextLine(); System.out.println("Second name: "); String aSecondName = scanner.nextLine(); System.out.println("Personnummer: "); String aPNumber = scanner.nextLine(); Customer aCustomer = new Customer(aFirstName, aSecondName, aPNumber); customers.add(aCustomer); break; case 3: // Choose a customer System.out.println("Choose a customer from the list: \n"); for(int i=0; i<customers.size(); i++){ System.out.println("Kund nr " + (i+1) + ":\n" + customers.get(i) + "\n"); } System.out.println("Write the person number : "); String kundNummer = scanner.nextLine(); for(Customer kund: customers){ if(kundNummer.equals(kund.getPNumber())){ System.out.println("You have chosen a customer with personnumber: " + kundNummer); customerMenu(kundNummer); } else { System.out.println("Your chosen person umber doesn´t exist in the register."); } } break; case 0: System.out.println("Close the program..."); return; } } } public void customerMenu(String kundNummer) { String menuTitle = "\n### What do you want to do? ###\n"; String[] menuItems = {"1. Show information about customer and accounts", "2. Change name", "3. Create a new account", "4. Not in use yet", "5. Choose an account", "6. Remove customer", "0. Back to startmenu"}; while(true) { int input = showMenu(menuTitle, menuItems); switch(input) { case 1:// Show information about the chosen customer for(Customer kund: customers){ if(kundNummer.equals(kund.pNumber)){ System.out.println("\nYpu have chosen to ..: \n" + kund.toString()); for(SavingAccounts konto: accounts){ if(kundNummer.equals(konto.getPNumber())){ System.out.println("\nAccount info: \n" + konto.toString()); } else { System.out.println("Chosen customer has no account."); // snurrar igenom hela kontolistan } } } } break; case 2: // Change the name of the chosen customer for(Customer kund: customers){ if(kundNummer.equals(kund.pNumber)){ System.out.println("Write new first name: "); String fname = scanner.nextLine(); System.out.println("write nes second name: "); String ename = scanner.nextLine(); kund.changeName(fname,ename); System.out.println("The name is now changed to: " + fname + " " + ename); } } break; case 3: // Create a new account for(Customer kund: customers){ if(kundNummer.equals(kund.pNumber)){ SavingAccounts aAccount = new SavingAccounts(); aAccount.setPNumber(kundNummer); System.out.println("New Account number is: " + aAccount.getAccountNumber()); System.out.println("New Accounts´ person number is: " + aAccount.getPNumber()); accounts.add(aAccount); } } break; case 4: // Not in use yet System.out.println("Not yet in use."); break; case 5: // Choose an account System.out.println("Choose an account from the list: "); for(int i=0; i<accounts.size(); i++){ System.out.println("Account nr " + i + ":\n" + accounts.get(i) + "\n"); } System.out.println("Write account number: "); int kontoNummer = scanner.nextInt(); System.out.println("You have chosen account number: " + kontoNummer); for(SavingAccounts konto:accounts){ if(!(kontoNummer==konto.accountNumber) || accounts.isEmpty()){ System.out.println("You have chosen an account which doesn´t exist."); } } accountMenu(kundNummer, kontoNummer); break; case 6: // Remove the customer List<Customer> taBortLista = new ArrayList<Customer>(); for(Customer c:customers){ if(c.getPNumber().equals(kundNummer)){ taBortLista.add(c); } } // security question System.out.println("Do you want to remove customer with person number " + kundNummer + " from the register\n" + "confirm J/N "); String svar = scanner.nextLine(); System.out.println("Svar: " + svar); if(svar.equals("J") || svar.equals("j")){ for(Customer c: taBortLista){ customers.remove(c); System.out.println("Chosen customer is deleted."); } } else{ System.out.println("You have chosen not to remove the customer"); } case 0: System.out.println("Back to main menu..."); return; } } } public void accountMenu(String kundNummer, int kontoNummer) { String header = "\n### What to do with the account? ###\n"; String[] menuItems = {"1. Show information about the account", "2. Deposit", "3. Withdraw", "4. Remove account", "0. Back to customer menu"}; while(true) { int input = showMenu(header, menuItems); switch(input) { case 1: // Print accountinfo for(SavingAccounts konto: accounts){ if(kontoNummer==konto.accountNumber){ System.out.println("\nAccount info:\n" + konto.toString()); } } break; case 2: // Deposit money System.out.println("How much do you want to deposit: "); double amountIn = scanner.nextInt(); for(SavingAccounts konto: accounts){ if(kontoNummer==konto.accountNumber){ System.out.println("Balance: " + konto.getBalance()); konto.depositAmount(kontoNummer, amountIn); System.out.println("The deposit " + amountIn + " om account " + kontoNummer + ". \nBalance is now: " + konto.getBalance()); } else{ System.out.println("The chosen account doesn´t exist."); } } break; case 3: // Withdraw money System.out.println("How much dp you want to withdraw från the account: "); double amountOut = scanner.nextInt(); for(SavingAccounts konto: accounts){ if(kontoNummer==konto.accountNumber){ System.out.println("Balance: " + konto.getBalance()); konto.withdrawAmount(kontoNummer, amountOut); if(amountOut<=konto.getBalance()){ System.out.println("The withdrawel " + amountOut + " on account " + kontoNummer); } System.out.println("Balance is now: " + konto.getBalance()); } } break; case 4:// Remove account SavingAccounts removeAccount = null; for(SavingAccounts konto: accounts){ if(kontoNummer==konto.accountNumber){ konto.countInterest(kontoNummer,konto.getBalance(), konto.getInterestRate()); System.out.println("\nBalance: \n" + konto.toString()); removeAccount = konto; } } accounts.remove(removeAccount); System.out.println("Account wioth account number " + kontoNummer + " is removed."); case 0: System.out.println("\nBack to customer menu..."); return; } } } public static void main(String[] args) { Bank bankMenu = new Bank(); bankMenu.menu(); } }
- 08-21-2012, 07:03 PM #10
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Create customers with unique personNumber
Why do you have the program to print out balance before you delete the account? I'm not a big fan of using switch cases but...Java Code:case 4:// Remove account SavingAccounts removeAccount = null; for(SavingAccounts konto: accounts){ if(kontoNummer==konto.accountNumber){ konto.countInterest(kontoNummer,konto.getBalance(), konto.getInterestRate()); System.out.println("\nBalance: \n" + konto.toString()); removeAccount = konto; } } accounts.remove(removeAccount); System.out.println("Account wioth account number " + kontoNummer + " is removed.");
What does this do?
Can't you just get the account number and then useJava Code:konto.countInterest(kontoNummer,konto.getBalance(), konto.getInterestRate());
to delete the account?Java Code:accounts.remove(removeAccount);
If you had built methods you probably would have had a removeAccount(accountNumber)-method, that solely did this task. You can still keep the switch cases and just add some proper methods.
I don't know if I'm just ranting, since I'm still a newbie at programming myself, but I get the feeling that the code is very unorganized right now, and hard to follow.
- 08-22-2012, 05:54 AM #11
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: Create customers with unique personNumber
Remove accounts works fine, it´s remove customer which doesn´t work.
- 08-22-2012, 09:25 AM #12
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: Create customers with unique personNumber
I have now solved this problem but have still problem with creating customers when I control if the personNumber already exist.
This code works okay:
but this code doesn´t work - no customers are createdJava Code:System.out.println("Who are you?"); System.out.println("First name: "); String aFirstName = scanner.nextLine(); System.out.println("Second name: "); String aSecondName = scanner.nextLine(); System.out.println("Personnummer: "); String aPNumber = scanner.nextLine(); Customer aCustomer = new Customer(aFirstName, aSecondName, aPNumber); customers.add(aCustomer);
What´s wrong with this check for pNumber?Java Code:System.out.println("Who are you?"); System.out.println("First name: "); String aFirstName = scanner.nextLine(); System.out.println("Second name: "); String aSecondName = scanner.nextLine(); System.out.println("Personnummer: "); String aPNumber = scanner.nextLine(); for(Customer kund: customers){ if(!(aPNumber.equals(kund.getPNumber()))){ Customer aCustomer = new Customer(aFirstName, aSecondName, aPNumber); customers.add(aCustomer); } else{ System.out.println("There is already a customber with this personNumber."); } }
- 08-22-2012, 09:56 PM #13
Re: Create customers with unique personNumber
Can you insert a println between lined 10 and 11 and see if that code is happening?
- 08-23-2012, 10:33 AM #14
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: Create customers with unique personNumber
Good idea. Nothing happens. It seems that there must be som customer in the arrayList before I can use
Later in the code when there is one or more customers it works.Java Code:for(Customer kund: customers){ if(!(aPNumber.equals(kund.getPNumber()))){
I must find another way to test if there already is some customer with the scanned PNumber.
How??
- 08-23-2012, 03:12 PM #15
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Similar Threads
-
Credit() crediting customers account
By JavaB in forum New To JavaReplies: 1Last Post: 04-08-2012, 12:33 AM -
How to get unique rows from a table
By Jdevelop in forum New To JavaReplies: 1Last Post: 06-15-2011, 12:08 PM -
Unique personal number
By Lidiya in forum Advanced JavaReplies: 9Last Post: 12-27-2010, 12:13 PM -
limit disk space for my customers accounts
By mtz1406 in forum Java ServletReplies: 6Last Post: 04-04-2010, 04:38 PM -
Generate unique letters
By bl00dr3d in forum New To JavaReplies: 22Last Post: 04-10-2009, 02:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks