Results 1 to 12 of 12
- 08-13-2012, 04:44 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
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; } }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.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(); }
/ Nilla
- 08-13-2012, 04:57 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
Re: Problems getting an unique accountNumber
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 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; } }
What this will do is assign the values of theFirstName, theSecondName, thePNumber to the fields in the bank class.Java Code:public Customer(String theFirstName, String theSecondName, String thePNumber){ super(theFirstName,theSecondName,thePNumber); }
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 :)
- 08-13-2012, 06:19 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: Problems getting an unique accountNumber
Than you!
I will follow your advices.
/Nilla
- 08-13-2012, 06:28 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
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'.
- 08-13-2012, 06:39 PM #5
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
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
- 08-13-2012, 06:43 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
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 :)
- 08-13-2012, 08:39 PM #7
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
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; } }I can create customers.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 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
- 08-14-2012, 03:16 AM #8
Member
- Join Date
- May 2012
- Posts
- 18
- Rep Power
- 0
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.
Why is it static? Do you know what static means?Java Code:private static int accountNumber=1000;
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.
- 08-14-2012, 03:57 AM #9
Re: Problems getting an unique accountNumber
^ What they said about IS A! Need to read all the posts.
- 08-14-2012, 09:41 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Problems getting an unique accountNumber
these are back to front.Java Code:private int accountCounter = 1000; private static int accountNumber=1000;
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.
- 08-14-2012, 11:49 AM #11
Member
- Join Date
- Jul 2012
- Posts
- 37
- Rep Power
- 0
Re: Problems getting an unique accountNumber
Thank you! It works now!
/Nilla
- 08-14-2012, 12:08 PM #12
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
-
Get unique Manufacturing Id of Usb Drive
By arijit158 in forum New To JavaReplies: 10Last Post: 02-01-2012, 04:57 PM -
how to maintain unique session
By sudhakarhyd in forum New To JavaReplies: 3Last Post: 08-09-2011, 06:03 PM -
Unique personal number
By Lidiya in forum Advanced JavaReplies: 9Last Post: 12-27-2010, 12:13 PM -
Encrypting unique columns
By andy16 in forum JDBCReplies: 0Last Post: 05-26-2010, 07:57 PM -
Check for unique key violation
By andy16 in forum JDBCReplies: 6Last Post: 05-24-2010, 06:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks