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

    Default Problems getting an unique accountNumber

    I am new to object-oriented Java and have problems to get accountNumber. This is my code:

    Java Code:
    public class Bank {
    	protected String firstName;
    	protected String secondName;
    	protected String pNumber;
    		
    	// Konstruktorer
    	public Bank(String firstName, String secondName, String pNumber){
    		this.firstName = firstName;
    		this.secondName = secondName;
    		this.pNumber = pNumber;
    	}
    
    		public Bank(){
    		firstName= "unknown";
    		secondName = "unknown";
    		pNumber = "unknown";
    	}
    	
    	//----------- Set-metoder-----------------
    	 
    		public void setFirstName (String newFirstName){
    			firstName = newFirstName;
    		}
    		public void setSecondName (String newSecondName){
    			secondName = newSecondName;
    		}
    		public void setPNumber (String newPNumber){
    			pNumber = newPNumber;
    		}
    		
    	// ---------- Get-metoder ----------------
    		
    		public String getFirstName (){
    			return firstName;
    		}
    		public String getSecondtName (){
    			return secondName;
    		}
    		public String getPNumber (){
    			return pNumber;
    		}
    }
    Java Code:
    public class Customer extends Bank {
    
    	private String firstName;
            private String secondName;
    	private String pNumber;
    	
    	// constructors
    	public Customer(String theFirstName, String theSecondName, String thePNumber){
    	    firstName = theFirstName;
    	    secondName = theSecondName;
    	    pNumber = thePNumber;
    	 }
    	
    	public Customer(){
    		super();
    	}
    	
    	//----------- Set-methods-----------------
    	 
    			public void setFirstName (String newFirstName){
    				firstName = newFirstName;
    			}
    			public void setSecondName (String newSecondName){
    				secondName = newSecondName;
    			}
    			public void setPNumber (String newPNumber){
    				pNumber = newPNumber;
    			}
    			
    	// ---------- Get-methods ----------------
    			
    			public String getFirstName (){
    				return firstName;
    			}
    			public String getSecondName (){
    				return secondName;
    			}
    			public String getPNumber (){
    				return pNumber;
    			}
    }
    Java Code:
    public class SavingAccounts extends Bank{
    	
    	private int accountCounter = 1000;
    	private static int accountNumber=1000;
    	private String accountType;
    		
    	// Konstruktor
    	public SavingAccounts (String theAccountType, int theAccountNumber){
    		this.accountType = theAccountType;
    		accountNumber = accountCounter++;
    	}
    	
    		//----------- Set-metod-----------------
    	 	public void setAccountType (String newAccountType){
    			accountType = newAccountType;
    		}
    	 	
    	 	// ---------- Get-metoder ----------------
    		public String getAccountType (){
    			return accountType;
    		}
    		
    		public static int getAccountNumber(){
    			return accountNumber;
    		}
    }
    Java Code:
    public class BankMenu
    {
    	String kontoTyp1 = "Sparkonto";
    	String kontotyp2 = "Kreditkonto";
    	
    	// skapar en kundlista och en kontolista
    	List <Customer> customers = new ArrayList();
    	List<SavingAccounts> accounts = new ArrayList();
           ...........
    
    	case 3: // Create e new account
    			int aAccountNumber = SavingAccounts.getAccountNumber();	
    			System.out.println("Nya Sparkontonumret är: " + aAccountNumber);
    			SavingAccounts aAccount = new SavingAccounts(kontoTyp1, aAccountNumber);
    			accounts.add(aAccount);
    			break;	
    
            ........
    public static void main(String[] args)
    	{
    		BankMenu bankMenu = new BankMenu();
    		bankMenu.menu();
    	}
    I would very much appreciate feedback to what´s wrong with the code and tips on how I can organize the class-system more efficiently.

    / Nilla

  2. #2
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: Problems getting an unique accountNumber

    Java Code:
    public class Customer extends Bank {
     
        private String firstName;
            private String secondName;
        private String pNumber;
         
        // constructors
        public Customer(String theFirstName, String theSecondName, String thePNumber){
            firstName = theFirstName;
            secondName = theSecondName;
            pNumber = thePNumber;
         }
         
        public Customer(){
            super();
        }
         
        //----------- Set-methods-----------------
          
                public void setFirstName (String newFirstName){
                    firstName = newFirstName;
                }
                public void setSecondName (String newSecondName){
                    secondName = newSecondName;
                }
                public void setPNumber (String newPNumber){
                    pNumber = newPNumber;
                }
                 
        // ---------- Get-methods ----------------
                 
                public String getFirstName (){
                    return firstName;
                }
                public String getSecondName (){
                    return secondName;
                }
                public String getPNumber (){
                    return pNumber;
                }
    }
    The first thing I noticed was this. What you've done is told the compiler that you have a class named Customer, that has all the methods and fields of class Bank. Then you've made three fields, firstName, secondName and pNumber that already exist in class Bank. So what you've now got is two identical copies of the three fields in Bank. So the first thing you can do is delete those fields. Next is the constructor in this class. Does it look similar to you? It should, because it's the same as the constructor in the bank class. So what you can do there is invoke the superclass, using the reserved word "super", and because it's the constructor, all you have to do is this:

    Java Code:
       public Customer(String theFirstName, String theSecondName, String thePNumber){
            super(theFirstName,theSecondName,thePNumber);
         }
    What this will do is assign the values of theFirstName, theSecondName, thePNumber to the fields in the bank class.

    Next stop is your methods. They already exist in the Bank class, in the same way that the fields do.

    Now you should be looking at those two classes and seeing how scarily similar they are. In fact, they are the exact same class! Customer IS bank!

    Now I'm not doing this for you, but I've just helped you out with this one class. Try to apply the same kind of thinking to the rest of it, re-organise your code a little better and you might be able to fix your problem.

    Just to say though, because you're new, welcome to Java and I hope you have some good luck with it in the future :)

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

    Default Re: Problems getting an unique accountNumber

    Than you!
    I will follow your advices.
    /Nilla

  4. #4
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: Problems getting an unique accountNumber

    Oh and just on a side note, 'get methods' are called
    Accessor methods. 'set methods' are called 'mutator methods'.

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

    Default Re: Problems getting an unique accountNumber

    I have now followed your suggestion and it looks much better and there are no copies left - but my problem with getting the unique accoutNumber still exists.
    /Nilla

  6. #6
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: Problems getting an unique accountNumber

    Okay. What is the problem? Is there an error thrown?
    What is the name of the field and what class does it appear in?

    How are you trying to access it? And also post your new code please :)

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

    Default Re: Problems getting an unique accountNumber

    Here is my new code. Comments and texts are mostly in Swedish:

    Java Code:
    public class Customer extends Bank {
    
    	// Constructor
    	public Customer(String theFirstName, String theSecondName, String thePNumber){
    		super(theFirstName, theSecondName, thePNumber);
    	 }
    	
    	public String toString(){
    		String info = "Personnummer: " + pNumber + "\nKundens namn: " + firstName + " " + secondName;
    		return info;
    	}
    }
    Java Code:
    public class SavingAccounts extends Bank{
    	
    	private int accountCounter = 1000;
    	private static int accountNumber=1000;
    	private String accountType;
    		
    	// Constructor
    	public SavingAccounts (String theAccountType, int theAccountNumber){
    		this.accountType = theAccountType;
    		accountNumber = accountCounter++;
    	}
    	
    		//----------- Accessor-methods-----------------
    	 	public void setAccountType (String newAccountType){
    			accountType = newAccountType;
    		}
    	 	
    	 	// ---------- Mutator-methods ----------------
    		public String getAccountType (){
    			return accountType;
    		}
    		
    		public static int getAccountNumber(){
    			return accountNumber;
    		}
    	
    	public String toString(){
    		String info = "Personnummer: " + pNumber + "\nKundens namn: " + firstName + " " + secondName + 
    				"\nKontotyp " + accountType + "\nKontonummer " + accountNumber;
    		return info;
    	}
    }
    Java Code:
    public class Bank {
    	
    	protected String firstName;
    	protected String secondName;
    	protected String pNumber;
    		
    	// Constructor
    	public Bank(String firstName, String secondName, String pNumber){
    		this.firstName = firstName;
    		this.secondName = secondName;
    		this.pNumber = pNumber;
    	}
    
    	public Bank(){
    		firstName= "unknown";
    		secondName = "unknown";
    		pNumber = "unknown";
    	}
    	
    	//----------- Accessor-methods-----------------
    	 
    		public void setFirstName (String newFirstName){
    			firstName = newFirstName;
    		}
    		public void setSecondName (String newSecondName){
    			secondName = newSecondName;
    		}
    		public void setPNumber (String newPNumber){
    			pNumber = newPNumber;
    		}
    		
    	// ---------- Mutator-methods ----------------
    		
    		public String getFirstName (){
    			return firstName;
    		}
    		public String getSecondName (){
    			return secondName;
    		}
    		public String getPNumber (){
    			return pNumber;
    		}
    }
    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class BankMenu
    {
    	String kontoTyp1 = "Sparkonto";
    	String kontotyp2 = "Kreditkonto";
    	
    	// skapar en kundlista och en kontolista
    	List <Customer> customers = new ArrayList();
    	List<SavingAccounts> accounts = new ArrayList();
    		
    	// förbereder inmatning av data via konsollen
    	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("\nVälj ett av alternativen i menyn ovan: ");
    		Scanner scan = new Scanner(System.in);
    		return scan.nextInt();
    	}
    
    	public void menu()
    	{
    		String   menuTitle = "\n### VÄLKOMMEN TILL BANKEN ###\n";
    		String[] menuItems = {"1. Skriv ut kunder",
    						"2. Skapa en ny kund",
    						"3. Välj kund",
    						"0. Avsluta"};
    		
    		while(true)
    		{
    			// Presenterar menyn och får tillbaka användarens val
    			int input = showMenu(menuTitle, menuItems);
    
    			switch(input)
    			{
    				case 1: // Skriv ut en lista med befintliga kunder
    					System.out.println("\nBefintliga kunder: ");
    					for(int i=0; i<customers.size(); i++){
    						System.out.println("Kund nr " + i + ":\n" + customers.get(i) + "\n");
    					}
    					break;
    
    				case 2: // Skapa en ny kund
    					System.out.println("Skriv in kunddata");
    					System.out.println("Förnamn: ");
    					String aFirstName = scanner.nextLine();
    					System.out.println("Efternamn: ");
    					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: // Välj en kund att jobba med
    					System.out.println("Välj en kund i kundlistan nedan: \n");
    					for(int i=0; i<customers.size(); i++){
    						System.out.println("Kund nr " + i + ":\n" + customers.get(i) + "\n");
    					}
    					
    					System.out.println("Skriv in kundens personnummer: ");
    					String customerNumber = scanner.nextLine();
    					System.out.println("Du har valt kund med personnummer: " + customerNumber);
    					
    					customerMenu(customerNumber);
    					break;
    
    				case 0:
    					System.out.println("Avslutar programmet...");
    					return;
    			}
    		}
    }
    
    	public void customerMenu(String customerNumber)
    	{
    		String menuTitle = "\n### VÄLJ VAD DU VILL GÖRA ###\n";
    		String[] menuItems = {"1. Visa kund och kontoinformation",
    				      "2. Ändra namn",
    				      "3. Skapa ett nytt sparkonto",
    				      "4. Skapa ett nytt kreditkonto",
    				      "5. Välj ett konto",
    				      "6. Ta bort kunden",
    				      "0. Tillbaka till huvudmenyn"};
    
    			
    		while(true) 
    		{
    			int input = showMenu(menuTitle, menuItems);
    			switch(input)
    			{
    				case 1: // Visa information om vald kund
    															
    				System.out.println("\nDu har valt följande kundkonto: ");
    					
    				for(Customer kontoKund:customers){
    					if(customerNumber.equals(kontoKund.getPNumber())){
    					System.out.println("Personnummer: " + kontoKund.getPNumber());
    					System.out.println("Kundens namn: " + kontoKund.getFirstName() + " " + kontoKund.getSecondName());
    					System.out.println("Kundens konton: Sparkonto med kontonummer: " + SavingAccounts.getAccountNumber());
    						}
    					}
    					break;
    				
    				
    				case 2: // Ändra kundens namn
    					/* Förnamn och efternamn matas in som en String */
    					break;
    
    				case 3: // Skapa ett nytt sparkonto
    					int aAccountNumber = SavingAccounts.getAccountNumber();	
    					System.out.println("Nya Sparkontonumret är: " + aAccountNumber);
    					SavingAccounts aAccount = new SavingAccounts(kontoTyp1, aAccountNumber);
    					accounts.add(aAccount);
    					break;	
    					
    				case 4: // Skapa ett nytt kreditkonto (behövs  i inlämningsuppgift 2)
    					System.out.println("Tjänsten inte i bruk ännu.");
    					break;
    
    				case 5: // Välj ett konto att jobba med
    					
    					/* Skriv ut kontolistan så att man kan välja konto med kontonummer */
    					System.out.println("Välj en konto i kontolistan: ");
    					for(int i=0; i<accounts.size(); i++){
    						System.out.println("Konto nr " + i + ":\n" + accounts.get(i) + "\n");
    					}
    					
    					System.out.println("Skriv in valt kontonummer: ");
    					int accountNumber = scanner.nextInt();
    					System.out.println("Du har valt konto nr: " + accountNumber);
    					
    					accountMenu(customerNumber, accountNumber);
    					break;
    
    				case 6: // Ta bort kunden från banken
    					
    					// skapar en tabort-lista
    					List<Customer> taBortLista = new ArrayList<Customer>();
    					for(Customer c:customers){  	
    						// letar rätt på kund med önskat personnummer
    						if(c.getPNumber().equals(customerNumber)){
    							// lägger denna kund till tabort-listan 
    							taBortLista.add(c);
    						}
    					}
    			
    							
    					// ställer en säkerhetsfråga
    					System.out.println("Vill du ta bort konto med personnummer " + customerNumber + " från bankregistret\n" +
    					   "Bekräfta med J/N (för ja eller nej)"); 
    					String svar = scanner.nextLine();
    					System.out.println("Svar: " + svar);
    						if(svar.equals("J") || svar.equals("j")){
    							// ta bort elementet i taBortListan från customers ??*/
    							// tar bort kunden ffrån bankregistret		
    							for(Customer c: taBortLista){		
    								customers.remove(c);
    								System.out.println("Vald kund är borttagen från bankregistret.");
    							}
    						}
    						else{
    								System.out.println("Du har valt att inte ba bort kontot");
    							}
    					
    					/* ingen break här (vi VILL återgå till kundmenyn) */
    				case 0:
    					System.out.println("Tillbaka till huvudmenyn...");
    					return;
    			}
    		}
    }
    
    	public void accountMenu(String customerNumber, int accountNumber)
    	{
    		String header = "\n### HANTERA DINA KONTON ###\n";
    		String[] menuItems = {"1. Visa information om kontot",
    				      "2. Insättning",
    				      "3. Uttag",
    				      "4. Avsluta kontot",
    				      "0. Tillbaka till kundmenyn"};
    
    		while(true)
    		{
    			int input = showMenu(header, menuItems);
    			switch(input)
    			{
    				case 1: // Skriv ut data om kontot
    					
    					break;
    
    				case 2: // Gör en insättning till kontot
    
    					break;
    
    				case 3: // Gör ett uttag från kontot
    
    					break;
    
    				case 4: // Avsluta kontot
    
    					
    				case 0:
    					System.out.println("Tillbaka till kundmenyn...");
    					return;
    			}
    		}
    	}
    
    public static void main(String[] args)
    	{
    		BankMenu bankMenu = new BankMenu();
    		bankMenu.menu();
    	}
    }
    I can create customers.
    I can write a customer list
    I can delete customers

    but when I try to create two accounts for the same customer these accounts both get number 1000. The console shows:

    ### What do you want to do? ###

    1. Visa kund och kontoinformation
    2. Ändra namn
    3. Skapa ett nytt sparkonto - create a new account
    4. Skapa ett nytt kreditkonto
    5. Välj ett konto
    6. Ta bort kunden
    0. Tillbaka till huvudmenyn

    Välj (choose) ett av alternativen i menyn ovan: 3
    New account number is: 1000

    ### VÄLJ VAD DU VILL GÖRA ###

    1. Visa kund och kontoinformation
    2. Ändra namn
    3. Skapa ett nytt sparkonto
    4. Skapa ett nytt kreditkonto
    5. Välj ett konto
    6. Ta bort kunden
    0. Tillbaka till huvudmenyn

    Välj ett av alternativen i menyn ovan: 3
    New account number is: 1000

    Välj (choose) ett av alternativen i menyn ovan: 1

    You have chosen the following account:
    Person number: 12345678
    Customer name: xxxx yyyyyyyy
    Customer accounts: Saving account with accountnumber: 1000
    I have created two accounts but only one shows up.

    I don´t know why the color in the code change in case 2-6 in the Customer Menu but it is in case 3 in this menu I try to create accounts.

    /Nilla

  8. #8
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: Problems getting an unique accountNumber

    Oh God, what a mess.
    But from what I can see you're calling getAccountNumber() which will return its value, which is declared in the class.
    Java Code:
     private static int accountNumber=1000;
    Why is it static? Do you know what static means?

    Also, it makes no sense for Customer and SavingAccounts to extend Bank. Can you say Customer IS-A Bank or SavingAccounts IS-A Bank? Not really.
    There are some fundamental understanding of basic concepts of (especially) inheritance missing here.

    I'm Swedish too, and even for me (who understands your code's comments) it's still a complete mess.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Problems getting an unique accountNumber

    ^ What they said about IS A! Need to read all the posts.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Problems getting an unique accountNumber

    Java Code:
        private int accountCounter = 1000;
        private static int accountNumber=1000;
    these are back to front.
    The Counter should be static (that is there should be only one of them covering all Customers) and the Number should be an instance attribute (that is there is one per Customer).

    And the stuff about Customers and Banks from the rest of them up there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Problems getting an unique accountNumber

    Thank you! It works now!
    /Nilla

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default Re: Problems getting an unique accountNumber

    1. The class name for Bank is confusing, since the fields of Bank belong to a Person and not a Bank.
    2. Learn the difference between IS-A and HAS-A. A Customer IS-A Person and a Bank HAS Accounts and an Account HAS one or more Customers.
    Last edited by j2me64; 08-14-2012 at 12:10 PM.

Similar Threads

  1. Get unique Manufacturing Id of Usb Drive
    By arijit158 in forum New To Java
    Replies: 10
    Last Post: 02-01-2012, 04:57 PM
  2. how to maintain unique session
    By sudhakarhyd in forum New To Java
    Replies: 3
    Last Post: 08-09-2011, 06:03 PM
  3. Unique personal number
    By Lidiya in forum Advanced Java
    Replies: 9
    Last Post: 12-27-2010, 12:13 PM
  4. Encrypting unique columns
    By andy16 in forum JDBC
    Replies: 0
    Last Post: 05-26-2010, 07:57 PM
  5. Check for unique key violation
    By andy16 in forum JDBC
    Replies: 6
    Last Post: 05-24-2010, 06:23 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
  •