Results 1 to 16 of 16
Like Tree2Likes
  • 2 Post By Tolls

Thread: How to structure my code more obejctoriented?

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

    Default How to structure my code more obejctoriented?

    I have five classes. In this first step I have almost all the metods in the Bank class. It works fine but I now want the program to be more objectoriented by using interface and move all the methods from the Bank class to the classes Account, SavingAccounts and CreditAccounts. I donīt know how to do with the arrayLists which now are in the BankClass. Nor do I know exactly how to do with the methods. Do I have to describe all the method code in the superclass Account. In the subclases (SavingsAccounts, CreditAccounts) the code will differ somewhat, but I donīt know what to put in the Account class. Have you any suggestions of how I can go further to get my code objectoriented?

    class Bank{
    List <Customer> customer = new ArrayList();
    List <SavingAccounts> savingAccounts = new ArrayList();
    List <CreditAccounts> creditAccounts = new ArrayList();Menu

    AccountMethods
    createAccount
    chooseAccount
    …
    }
    class Customer{}

    class Account extends Customer{}

    class SavingAccounts extends Account{}

    class CreditAccounts extends Account{}


    interface AccountMethods{
    public abstract void createAccount();
    public abstract void choosAccount();
    …..
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: How to structure my code more obejctoriented?

    I don't know about you, but my various Accounts are not me.
    DarrylBurke and Fubarable like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to structure my code more obejctoriented?

    To amplify what Tolls is stating, when figuring out what class should be a child class of a parent class, you need to ask yourself, does the class fulfill the "is-a" requirement or is it better represented by a "has-a" relationship. For instance, a dog fulfills the "is-a" relationship to animal as does cat to animal since both are more specific types of animals, but dog does not fulfill the "is-a" relationship to pet owner. Rather a pet owner may "have-a" dog.

    Similarly a bank account does not fulfill the "is-a" relationship to customer since a bank account is not a more specific type of customer. Rather a customer may have one or more bank accounts, and so this fulfills the "has-a" relationship. Note that both savings account and checking account fulfill the "is-a" relationship to bank account, and so it is perfectly reasonable to create a parent class, BankAccount, and then have CheckingAccount and SavingsAccount be children of this parent. But a Customer will have BankAccount fields. i.e.,

    Java Code:
    Class Customer {
       private SavingsAccount savingsAccount;
       private CheckingAccount checkingAccount;
    
       // ....
    }
    But CheckingAccount or BankAccount should never extend Customer.

    Clear as mudd?

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

    Default Re: How to structure my code more obejctoriented?

    I see! The reason why I let Customer "has-an" Account was that they are connected with each other with a personNumber, and I didnīt figure out how to do it otherwise. I still really donīt know whatīs the best way to do it. Maybe your code snippet is the solution?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to structure my code more obejctoriented?

    Quote Originally Posted by ostkanten View Post
    I see! The reason why I let Customer "has-an" Account was that they are connected with each other with a personNumber, and I didnīt figure out how to do it otherwise.
    You don't use inheritance to share variables as it won't work.

    I still really donīt know whatīs the best way to do it. Maybe your code snippet is the solution?
    No two ways about it, a Customer should hold variables that represents his accounts. They could be individual variables as I've shown or an ArrayList of accounts, or other such collection. It all depends on your need. Your best bet is to experiment with this idea, try out code, and see what you can come up with.

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

    Default Re: How to structure my code more obejctoriented?

    Thank you very much. I will try different solutions tomorrow.

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

    Default Re: How to structure my code more obejctoriented?

    I have worked all the day to rebuild my structure and I have now some constructorproblems among the account classes. This is my code:

    Java Code:
    public class Account implements BankAccountHandling {
    	protected static double balance; 
    	protected int accountCounter = 1001; 
    	protected static int accountNumber=1000;
    	protected static String pNumber;
    	
    	List<Customer> customers = new ArrayList();
    	List<Account> accounts = new ArrayList(customers);
    	List<SavingAccounts> savingAccounts = new ArrayList(customers);
    	List<CreditAccounts> creditAccounts = new ArrayList(customers);
    	
    	Customer kund = new Customer();
    	Bank bank = new Bank();
    	
    	/**
    	 * Deafult constructor
    	 */
    	public Account(){
    		accountNumber = accountCounter++;
    	}
    
    	/**
    	 * Overloded constructor
    	 */
    	public Account(double startingBalance){
    		accountNumber = accountCounter++;
    		this.balance= startingBalance;
    	}
    Java Code:
    public class SavingAccounts extends Account implements BankAccountHandling{
    	private double savingInterest;
    	private final double interestRate=0.005;
    	private double outputFee;
    	private double outputFeeRate=0.01;
    		
    	List <Customer> customers = new ArrayList();
    	
    	Bank bank = new Bank();
    	Customer kund = new Customer();
    	Account aKonto = new Account();
    	
    	/**
    	 * Default constructor
    	 */
    	public SavingAccounts (){
    		super(accountNumber);
    	}
    	
    	/**
    	 * Overloaded constructor
    	 */
    	public SavingAccounts (int accountNumber){
    		super(accountNumber);
    	}

    Java Code:
    public class Customer  {
    	private String firstName;
    	private String secondName;
    	protected String pNumber;
    	
    	/**
    	 * Default constructor
    	 */
    	public Customer(){
    		firstName= "unknown";
    		secondName = "unknown";
    		pNumber = "unknown";
    	}
    	
    	/**
    	 * Overloaded constructor1
    	 */
    	public Customer(String firstName, String secondName, String pNumber){
    		this.firstName = firstName;
    		this.secondName = secondName;
    		this.pNumber = pNumber;
    	}
    	
    	/**
    	 * Overloaded" constructor2
    	 */
    	public Customer(String pNumber){
    		this.pNumber = pNumber;
    	}

    Java Code:
    public class Bank
    {
    	SavingAccounts sparKonto = new SavingAccounts();
    	CreditAccounts kreditKonto = new CreditAccounts();
    	Customer kund =new Customer();
    	Account aKonto = new Account();
    			
    	List <Customer> customers = new ArrayList();
    	List<SavingAccounts> savingAccounts = new ArrayList(customers);
    	List<CreditAccounts> creditAccounts = new ArrayList(customers);

    These classes result in the following exception:

    Exception in thread "main" java.lang.StackOverflowError
    at java.util.ArrayList.<init>(Unknown Source)
    at java.util.ArrayList.<init>(Unknown Source)
    at Account.<init>(Account.java:30)
    at SavingAccounts.<init>(SavingAccounts.java:42)
    at Bank.<init>(Bank.java:22)
    at Customer.<init>(Customer.java:35)
    at Account.<init>(Account.java:40)
    at SavingAccounts.<init>(SavingAccounts.java:42)
    at Bank.<init>(Bank.java:22)
    at Customer.<init>(Customer.java:35)
    etc.

    I donīt know exactly how to communicate between the classes and how to use super() properly. I would appreciate a hint och how to solve this problem.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to structure my code more obejctoriented?

    Things are very confused here.

    Your current exception is coming because your Account class creates a Bank object and the Bank class creates an Account object and so you'll get infinite recursion of Bank creating Account which creates Bank which creates Account, ....

    But once you get that figured out, you've got to re-think all that you're doing here as you're asking all your classes to do too much. For instance Account shouldn't be creating banks, shouldn't have lists of customers or other accounts. Instead it should have information and methods that deal with being an account and that's it.

    I recommend that you scrap most of this code, it's not salvageable, and instead try to isolate just what information an account needs. It needs a Customer, the person that owns the account and a single Bank, the bank the account is held in. It should create neither of those objects but instead should be given references to those objects in its constructor since an account in real life will never create a bank or a customer but rather a bank creates the account and assigns it to a customer. Think through your program structure almost as a mirror of the duties and responsibililites of banks, customers and accounts in real life. Keep your Customer class as it looks to be well defined. Note its simplicity and its limited responsibilities. A change that you'll have to make to it is that you'll need to give it some Account fields, but likely the Customer won't create the Account objects for these fields as that will be the Bank's job.

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

    Default Re: How to structure my code more obejctoriented?

    I think I have solved even this problem now.

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

    Default Re: How to structure my code more obejctoriented?

    Sorry, but I still have problems to communicate between the classes. Can you see whatīs wrong in the followuing summary of my program. I am not sure what to write in the menu case 1 and 2 to get it work properly. This code is accepted by Eclipse but there is no account created although the createAccount method works in another setting.

    Java Code:
    public class Bank{
    
    List<Customer> customers = new ArrayList();
    List<SavingAccounts> savingAccounts = new ArrayList(customers);
    List<CreditAccounts> creditAccounts = new ArrayList(customers);
    
    public void menu(){
    case 1:
    Account sKonto = new SavingAccount();
    sKonto.createAccount(pNumber);
    break;
    
    case 2:
    Account cKonto = new CreditAccount();
    cKonto.createAccount(pNumber);
    break;
    }
    public static void main(String[] args)
    {
    	Bank bankMenu = new Bank();
    	bankMenu.menu();
    }
    }
    
    public class Customer{
    }
    
    public class Account{
    public void createAccount(String pNumber){}
    }
    
    public class SavingAccounts extends Account{
    @Override;
    public void createAccount(String pNumber){ ….. }
    }
    
    public class CreditAccounts extends Account{
    @Override;
    public void createAccount(String pNumber){ ……}
    }
    
    public interface BankAccountHandling{
    public abstract void createAccount(String pNumber);
    }

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

    Default Re: How to structure my code more obejctoriented?

    Quote Originally Posted by ostkanten View Post
    but there is no account created although the createAccount method works in another setting.
    This is ok. When you use a collection the size() of the collection is 0. In order to fill the collection use add() for lists or put() for maps.

    Think about it: An account could be owned by more than one person, example if the owner is a couple. How would you handle this with the classes posted here?
    Last edited by j2me64; 09-01-2012 at 11:34 AM.

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

    Default Re: How to structure my code more obejctoriented?

    My createAccount method code for SavingAccount is as follows:

    Java Code:
    public void createAccount(String kundNummer){
    		for(Customer kund: customers){
    			// ser till att kontot registreras på rätt kund
    			if(kundNummer.equals(kund.pNumber)){
    				SavingAccounts aAccount = new SavingAccounts();
    				aAccount.setPNumber(kundNummer);
    				System.out.println("Nytt kontonummer är: " + aAccount.getAccountNumber());
    				System.out.println("Nya kontonumrets personnummer är: " + aAccount.getPNumber());
    				savingAccounts.add(aAccount);
    				System.out.println("Nytt kontonummer är: " + konto.getAccountNumber());
    				System.out.println("Nya kontonumrets personnummer är: " + konto.getPNumber());
    				Date accountStartDate = new Date();
    				SimpleDateFormat ft= new SimpleDateFormat("E yyyy.MM.dd 'kl.' hh:mm:ss");
    				System.out.println("Sparkonto med kontonummer " + konto.getAccountNumber() + " skapades: " + ft.format(accountStartDate));
    			}
    		}
    	}
    I think I have a problem to registrate two persons on the same account. However, before I solve that problem I must be able to registrate one account/person and just now it is impossible. The createCustomer code works when I put it in the BankClass, but not when I put it in the Customer class. Why??

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to structure my code more obejctoriented?

    Do your application requirements state that an account must be shareable by more than one customer? If not, in the sake of simplicity, I would not implement this. Have each Customer able to have several Accounts, but each Account should be associated with only one Customer.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to structure my code more obejctoriented?

    By the way is this for a class? If so, can you post the actual assignment requirements for us to review?

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

    Default Re: How to structure my code more obejctoriented?

    I try to learn Java just by myself but I use assigments from old courses. A customer should be able to have several acccounts, but each account should be associated with only one customer. Just now I try to understand why I seem to add a customer with this code, but when I display a list of customers no customer seems to be added. When I had the code placed in the Bank class it did work well, but since I moved the code to the Customer class. There seems to be same problem when I use the code to the Customer class itself. Perhaps I have to synchronize the arrayList between the classes?? But I donīt know how.

    Java Code:
    List <Customer>customers = new ArrayList<Customer>();
    
    public void createCustomer(){
    		System.out.println("Skriv in kunduppgifter.");
    		System.out.println("First name: ");
    		String aFirstName = scanner.nextLine();
    		System.out.println("Second Name: ");
    		String aSecondName = scanner.nextLine();
    		System.out.println("Person Number: ");
    		String aPNumber = scanner.nextLine();
    		
    		customers.add(new Customer(aFirstName, aSecondName, aPNumber));
    		System.out.println("Customer is registered"); 
    }
    but when I use the show customer method no customer seems to be registered


    Java Code:
    List <Customer>customers = new ArrayList<Customer>();
    
    public void showCustomers(){
    		if(customers.size()>0){
    			for(int i=0; i<customers.size(); i++){
    			System.out.println("Customer nr " + (i) + ":\n" + customers.get(i) + "\n");
    			}
    		}
    		else{
    			System.out.println("There are no customers in the register .");
    		}
    	}
    Last edited by ostkanten; 09-02-2012 at 10:27 AM.

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

    Default Re: How to structure my code more obejctoriented?

    The show Customer problem is now solved with

    Java Code:
    static List <Customer>customers = Collections.synchronizedList(new ArrayList());
    I donīt know if it is the right way to do, but it works.

Similar Threads

  1. Help with Table and code structure.
    By stuckonjava in forum New To Java
    Replies: 4
    Last Post: 03-21-2012, 08:28 PM
  2. UML, robust code, structure questions
    By mac in forum New To Java
    Replies: 2
    Last Post: 12-22-2009, 11:06 PM
  3. data structure code
    By vgvt in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 02:49 PM
  4. Use if then else structure, help
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:00 AM
  5. Help with Structure
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 03:27 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
  •