Results 1 to 5 of 5
  1. #1
    runlos is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default school assignment: creating a simple bank, need help

    I'm very new to java and we have just been doing very simple basic stuff at school so far. But no we got this heavy assignment and I am really stuck!

    I have translated the assignment to english myself, so hope it's understandable:

    Create a bank using three classes. The bank consist of the these classes: Bank-class (main class), Account-class, Person-class.

    The classes should contain get and set methods, a toString method and a constructor which gives values to the variables in the class.

    The Bank-class will not have a contructor, nor get and set methods.

    The Person-class should be able to store full name of the person aswell as soscial security number and phonenumber.

    The Account-class should be able to store account number, who it belongs to and the accounts current balance.

    No one should be able to change the balance of the account, the balance should only be calculated.

    Through the Bank-class you should be able to deposit and withdraw money.

    The Bank-class should have these functionalities:
    - print out customer register which shows name and account number.
    - a register with all accounts and the balance

    Hard code the main-method to check functionality

    Good luck!


    I have written some code for all the different classes, but they are not complete at all since i'm strugling to find good tutorials and/or examples that looks like my assignment (without being to confusing). Hope that someone out there can give me some pointers or some sample-code to help me out.

    I'm aware that there is missing alot of code, but I'm doing this little bit out of desperation.

    Bank-class:
    Java Code:
    public class Bank
    {
      private Person[] personarray;
      private Account[] accountarray;
      
      public static void main(String[] args){
        Bank bank1 = new Bank();
        bank1.run();
        //bank1.writeReg(Person personarray[]);
        System.out.println(bank1);
        
    }
      
      public void run()
      {
        // Array-objekt som alle kundene lagres i
        personarray = new Person[5];
        
        // Personer/kunder
        personarray[0] = new Person("Ola Normann","1000","800 10 100");
        personarray[1] = new Person("Kari Normann","2000","800 20 100");
        personarray[2] = new Person("Hans Normann","3000","800 30 100");
        personarray[3] = new Person("Per Normann","4000","800 40 100");
        personarray[4] = new Person("Grete Normann","5000","800 50 100");
        
        // Array-objekt som all kontoinformasjon lagres i
        accountarray = new Account[5];
        
        // Kontoer
        accountarray[0] = new Account("11111", 10000.50, personarray[0]);
        accountarray[1] = new Account("22222", 20000.50, personarray[1]);
        accountarray[2] = new Account("33333", 30000.50, personarray[2]);
        accountarray[3] = new Account("44444", 40000.50, personarray[3]);
        accountarray[4] = new Account("55555", 50000.50, personarray[4]);
      }
      /*
      public void writeReg(Person[] personarray)
      {
        for(int i = 0; i < personarray.length; i++)
        {
          System.out.println(personarray[i].toString);
        }
      }*/
    }
    Person-class:
    Java Code:
    public class Person
    {
        private String name;
        private String ssn;
        private String phoneNumber;
    
      
      // Constructor
      public Person(String name, String ssn, String phoneNumber)
      {
        this.name = name;
        this.ssn = ssn;
        this.phoneNumber = phoneNumber;
      }
      
      // Getters
      public String getName() { return this.name; }
      public String getSsn() { return this.ssn; }
      public String getPhoneNumber() { return this.phoneNumber; }
      
      // Setters
      public void setName(String name) { this.name = name; }
      public void setSsn(String ssn) { this.ssn = ssn; }
      public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }  
    
      public String toString()
      {
        return "Kontoinnehaver:" + name;
      }
    
    }
    Account-class:
    Java Code:
    public class Account
    {
      private String accountNumber;
      private double balance;
      private Person name;
      
      // FINN UT HVORDAN DET SKAL GJØRES NÅR DU SKAL HENTE FRA ANNEN KLASSE !!!
      
      // CONSTRUCTOR
      public Account (String accountNumber, double balance, Person name)
      {
        this.accountNumber = accountNumber;
        this.balance = balance;
        this.name = name;
      }
      
      // GETTERS
      public String getAccountNumber() { return this.accountNumber; }
      public Person getName() { return this.name; }
      public double getBalance() { return this.balance; }
      
      // SETTERS
      public void setAccountNumber(String accountNumber) { this.accountNumber = accountNumber; }
      public void setName(Person name) { this.name = name; }
      public void setBalance(double balance) { this.balance = balance; }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    For starters: you didn't create your personarray nor your accountarray so they are still equal to null. User the new operator to create them.

    kind regards,

    Jos

  3. #3
    runlos is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    I have now done some work on the assignment. It looks like it's working to some extent...
    Is there anything I can rewrite to improve the code, make it better?

    Here are the classes so far:

    Bank-class with hard code:
    Java Code:
    public class Bank
    {
      private Person[] personarray;
      private Account[] accountarray;
      
      public static void main(String[] args){
        Bank bank1 = new Bank();
        Account[] accountarray = new Account[5];
        Person[] personarray = new Person[5];
        
        // Personer
        personarray[0] = new Person("Ola Normann","1000","800 10 100");
        personarray[1] = new Person("Kari Normann","2000","800 20 100");
        personarray[2] = new Person("Hans Normann","3000","800 30 100");
        personarray[3] = new Person("Per Normann","4000","800 40 100");
        personarray[4] = new Person("Grete Normann","5000","800 50 100");
        
        // Kontoer
        accountarray[0] = new Account("1000.10.1000",0.0, personarray[0]); 
        accountarray[1] = new Account("2000.10.1000",0.0, personarray[1]);
        accountarray[2] = new Account("3000.10.1000",0.0, personarray[2]);
        accountarray[3] = new Account("4000.10.1000",0.0, personarray[3]);
        accountarray[4] = new Account("5000.10.1000",0.0, personarray[4]);
        
        
        accountarray[3].deposit(1200);
        accountarray[0].withdraw(650);
        
        bank1.writeReg(accountarray);
        bank1.writeReg2(accountarray);
    }
      
      public void writeReg(Account[] accountarray)
      {
        System.out.println("Personregister med kontonummer:");
          System.out.println("------------------------------------");
        for(int i = 0; i < accountarray.length; i++)
        {
          System.out.println("Person: " +accountarray[i].getName());
          System.out.println("Kontonr: " +accountarray[i].getAccountNumber()+"\n");
        }
      }
      
        public void writeReg2(Account[] accountarray)
      {
          System.out.println("Kontoregister med kontoens saldo:");
          System.out.println("------------------------------------");
        for(int i = 0; i < accountarray.length; i++)
        {
          System.out.println("Kontonr: " +accountarray[i].getAccountNumber());
          System.out.println("Saldo: " +accountarray[i].getBalance()+"\n");
        }
      }
    }
    Account-class
    Java Code:
    public class Account
    {
      private String accountNumber;
      private double balance;
      private Person name;
      
      // CONSTRUCTOR
      public Account (String accountNumber, double balance, Person name)
      {
        this.accountNumber = accountNumber;
        this.balance = balance;
        this.name = name;
      }
      
      // GETTERS
      public String getAccountNumber() { return this.accountNumber; }
      public Person getName() { return this.name; }
      public double getBalance() { return this.balance; }
      
      // SETTERS
      public void setAccountNumber(String accountNumber) { this.accountNumber = accountNumber; }
      public void setName(Person name) { this.name = name; }
    
      public String toString()
      {
        String out = (accountNumber+balance);
        return out;
      }
      
      public void deposit(double innskudd)
      {
        balance = balance + innskudd;
      }
      
      public void withdraw(double uttak)
      {
        balance = balance - uttak;
      }
      
    }
    Person-class
    Java Code:
    public class Person
    {
        private String name;
        private String ssn;
        private String phoneNumber;
    
      
      // Constructor
      public Person(String name, String ssn, String phoneNumber)
      {
        this.name = name;
        this.ssn = ssn;
        this.phoneNumber = phoneNumber;
      }
      
      // Getters
      public String getName() { return this.name; }
      public String getSsn() { return this.ssn; }
      public String getPhoneNumber() { return this.phoneNumber; }
      
      // Setters
      public void setName(String name) { this.name = name; }
      public void setSsn(String ssn) { this.ssn = ssn; }
      public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }  
    
      public String toString()
      {
        return name;
      }
    
    }

  4. #4
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    Looks good. In Account's toString method you can avoid creation of a new variable by simply returning accountNumber + balance:

    return (accountNumber + balance);

    Otherwise there's not really much more of an optimization you can do that would be of any significant benefit.

  5. #5
    runlos is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Ok, thanks for fast reply. I think it's a wrap then. Amazing how some "study hard" improves understanding. Seems much simpler now, then when I first looked at the assignment. will mark as SOLVED.

Similar Threads

  1. Need help with school assignment
    By sljkbn in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 01:07 PM
  2. Help with noob school assignment
    By debo3381 in forum New To Java
    Replies: 3
    Last Post: 09-23-2009, 04:02 AM
  3. Question about school assignment
    By wata in forum New To Java
    Replies: 7
    Last Post: 08-18-2009, 02:00 PM
  4. Please help... assignment for school
    By confused2000 in forum New To Java
    Replies: 3
    Last Post: 11-12-2007, 08:12 AM

Posting Permissions

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