Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By ostkanten

Thread: Create customers with unique personNumber

  1. #1
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default 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

    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;
    In the Customer class I have:

    Java Code:
    public void setPNumber(String personNumber){
    	pNumber= personNumber;
    }
    	
    public String getPNumber(){
    	return pNumber;
    }
    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:
    System.out.println("Personnummer: ");
    String aPNumber = scanner.nextLine();
    Customer aCustomer = new Customer(aFirstName, aSecondName, aPNumber);
    customers.add(aCustomer);

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Create customers with unique personNumber

    Quote Originally Posted by ostkanten View Post
    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

    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;
    In the Customer class I have:

    Java Code:
    public void setPNumber(String personNumber){
    	pNumber= personNumber;
    }
    	
    public String getPNumber(){
    	return pNumber;
    }
    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:
    System.out.println("Personnummer: ");
    String aPNumber = scanner.nextLine();
    Customer aCustomer = new Customer(aFirstName, aSecondName, aPNumber);
    customers.add(aCustomer);
    Please post compilable and runnable code.

    Wouldn't it be easier to use an integer (int) for the personal numbers (since they are numbers only in Sweden, which I think you want..?)?

  3. #3
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default Re: Create customers with unique personNumber

    I have now solved this problem but when I choose a 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;
    and want to delete this customer with the following code:

    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. ");
    I get the following error:

    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:

    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.");
    	    }
    	}
    it works fine but when I add this code there is some conflict. How do I solve this problem?
    sukeerthi likes this.

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default 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.

  5. #5
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default 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.");

  6. #6
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default 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.

  7. #7
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default 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:

    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.");       
     }    
    }
    Is there any other way to remove a customer from an ArrayList?

  8. #8
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Create customers with unique personNumber

    Quote Originally Posted by ostkanten View Post
    But there is no problem to remove customers with this code if I don´t have the following code in anopther code-snippet:

    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.");       
     }    
    }
    Is there any other way to remove a customer from an ArrayList?
    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.

  9. #9
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default 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

    Java 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;
    		}
    }
    Bank 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();
    	}
    }
    Please, tell me if you also need the Account class.

  10. #10
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Create customers with unique personNumber

    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.");
    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...

    What does this do?
    Java Code:
    konto.countInterest(kontoNummer,konto.getBalance(), konto.getInterestRate());
    Can't you just get the account number and then use

    Java Code:
    accounts.remove(removeAccount);
    to delete the account?
    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.

  11. #11
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default Re: Create customers with unique personNumber

    Remove accounts works fine, it´s remove customer which doesn´t work.

  12. #12
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default 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:

    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();
    Customer aCustomer = new Customer(aFirstName, aSecondName, aPNumber);
     customers.add(aCustomer);
    but this code doesn´t work - no customers are created

    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.");
      }
    }
    What´s wrong with this check for pNumber?

  13. #13
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Create customers with unique personNumber

    Can you insert a println between lined 10 and 11 and see if that code is happening?

  14. #14
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default 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
    Java Code:
    for(Customer kund: customers){   
    if(!(aPNumber.equals(kund.getPNumber()))){
    Later in the code when there is one or more customers it works.
    I must find another way to test if there already is some customer with the scanned PNumber.
    How??

  15. #15
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default Re: Create customers with unique personNumber

    I have solved this last problem too.
    Thank you for feedback.

Similar Threads

  1. Credit() crediting customers account
    By JavaB in forum New To Java
    Replies: 1
    Last Post: 04-08-2012, 12:33 AM
  2. How to get unique rows from a table
    By Jdevelop in forum New To Java
    Replies: 1
    Last Post: 06-15-2011, 12:08 PM
  3. Unique personal number
    By Lidiya in forum Advanced Java
    Replies: 9
    Last Post: 12-27-2010, 12:13 PM
  4. limit disk space for my customers accounts
    By mtz1406 in forum Java Servlet
    Replies: 6
    Last Post: 04-04-2010, 04:38 PM
  5. Generate unique letters
    By bl00dr3d in forum New To Java
    Replies: 22
    Last Post: 04-10-2009, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •