Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Could somebody help me with my homework? Calling method's from another class

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Could somebody help me with my homework? Calling method's from another class

    4 Banking

    You are going to build a simple system of bank accounts. The program reads commands from input and executes them on the accounts (such as withdrawals, deposits, enrolling of new accounts, printing, etc.).

    Your program should be able to perform the following commands. All amounts are integer numbers.
    Interest calculations are rounded to the nearest integer.

    print “name”: prints the data (name and balance) of the account with name “name” ;

    printAll: prints all accounts;

    enroll “name”: enrolls a new account under the name “name”;

    the initial balance is 0;

    deposit “name” “amount”: adds “amount” to the account of “name”;
    you may assume that “amount” is positive;

    withdraw “name” “amount”: subtracts “amount” from the account of “name”;
    you may assume that “amount” is positive;
    if the operation would result in a balance of less than -1000, the withdrawal is not performed;

    stop: stops the program.

    The following extensions should be provided:

    Add a command "printRed" that prints the data of all accounts with a negative balance.

    Add a command "interest rate" that adds interest to all positive accounts;
    Rate is a floating point number (double) representing the percentage of the current balance that should be added;
    for negative accounts, a fixed penalty of 10% is subtracted, even if the resulting balance would be less than -1000.

    Make the program a little bit more robust and have it perform elegantly when the commands "print", "withdraw", and "deposit" are given with a "name" that does not exist in the accounts. No operation should be performed and the message "no such name ..." should be printed, where the given name should appear instead of the dots. Furthermore, the command "enroll" with a "name" that is already in the accounts should result in no operation and the message "... already enrolled".

    Use the following design.
    The class "Account" has objects that represent the accounts. Each object has a name and a balance (Dutch: saldo). Design methods for the class Account, including those that enable to comply with the requirement that instance variables of a class should never be accessed directly (mentioned in the code) outside the class.

    Input is read and processed by an object of the class "CommandReader". You do not have to write this class. It is provided on the course’s web page. The central class is "Bank" that contains the accounts (an ArrayList). It has at least the following methods, each corresponding to a command:

    void print(String name)
    void printAll()
    void enroll(String name)
    void deposit(String name, int amount)
    void withdraw(String name, int amount)
    void printRed()
    void interest(double rate)

    4.1 Input
    A list of commands in the format as specified above. You may assume that every command is complete, so, e.g., "deposit" is always followed by a string (the name of an account) and an integer number (the amount to be withdrawn), etc. Two features are added in "CommandReader" for your convenience. When a word(string) is input that is not a command, the rest of the input line will be ignored and the message "unknown command" will be output. Furthermore, command words are not case sensitive.

    4.2 Output
    See the description above.

    The data of an account with name “piet” and balance 200 should be printed as follows.
    Account of piet
    balance: 200
    The word balance should be preceded by 2 spaces.

    When printAll is issued is and there are no accounts, one line is to be printed with the text: no accounts

    When printRed is issued is and there are no accounts with a negative balance, one line is to be printed with the text: no negative balances

    4.3 Example
    Text in bold face is input.

    enroll piet
    print piet

    Account of piet
    balance: 0
    deposit piet 100
    enroll jan
    withdraw jan 50
    printall

    Account of piet
    balance: 100
    Account of jan
    balance: -50
    withdraw piet 2000
    printAll

    Account of piet
    balance: 100
    Account of jan
    balance: -50
    stop


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    578
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    What have you tried? - Matt Gemmell
    Post your code and ask specific questions. Nobody will do your assignment for you.

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    This is the code I have so far, but it does not work I think I choose the wrong constructor, because in the assignment stands that the class Bank should be the main class, so that should be the class Java should execute firts. But because the class CommandReader calls the methods in the class Bank, I thought I had to execute that class first thats why I have

    public static void main(String[] args){
    new CommandReader().run();

    in the class CommandReader. But I think its wrong and I think I should maken the public static void in the class Bank, but how do I call the method in CommandReader which that calls the method in Bank?

    This is the code:

     
    import java.util.Scanner;
     
    class CommandReader{
        Bank bank;
        Account account;
     
        /*CommandReader(Bank bank) {
            this.bank = bank;
        }*/
     
        // @pre: input complies to specified format
        void run(){
            Scanner scanner;
            String command;
            String name;
            int amount;
            scanner = new Scanner(System.in);
            do{
                command = scanner.next().toLowerCase();
                if (command.equals("enroll")) {
                    name = scanner.next();
                    bank.enroll(name);
                } else if (command.equals("deposit")) {
                    name = scanner.next();
                    amount = scanner.nextInt();
                    bank.deposit(name, amount);
                } else if (command.equals("withdraw")) {
                    name = scanner.next();
                    amount = scanner.nextInt();
                    bank.withdraw(name, amount);
                } else if (command.equals("print")) {
                    name = scanner.next();
                    bank.print(name);
                } else if (command.equals("printall")) {
                    bank.printAll();
                } else if (command.equals("printred")) {
                    bank.printRed();
                } else if (command.equals("interest")) {
                    double rate = scanner.nextDouble();
                    bank.interest(rate);
                } else if (command.equals("stop")) { 
                    // do nothing
                } else {
                    System.out.println("unknown command");
                    // skip rest of line
                    scanner.nextLine();
                }
            } while (! command.equals("stop"));
        }
     
        public static void main(String[] args){
          new CommandReader().run();
        }
    }
     
    class Account{
      double balance;
     
      Account(String name){
        balance = 0;
      }
    }
     
    class Bank{
      double balance;
      Account account;
     
      void enroll(String name){
        account = new Account(name);
      }
     
      void deposit(String name, int amount){
        balance = balance + amount;
      }
     
      void withdraw(String name, int amount){
        balance = balance - amount;
      }
     
      void print(String name){
        System.out.println("Account of "+name);
        System.out.println("  balance: "+balance);
      }
     
      void printAll(){
     
      }
     
      void printRed(){
     
      }
     
      void interest (double rate){
     
      }
    }

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    578
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class


  5. #5
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    sorry for this mistake, I've changed it now

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    578
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    Put the main method into Bank, create an instance of Bank. Create an instance of CommandReader using the constructor, that you've commented. Call Command reader's run() method.

  7. #7
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    Quote Originally Posted by PhHein View Post
    Put the main method into Bank, create an instance of Bank. Create an instance of CommandReader using the constructor, that you've commented. Call Command reader's run() method.
    Hello PhHein,
    Thank you for your fast replies!

    Could you explain it a little bit more, I'm a dutch student and I'm not really good with English terms so maybe you could explain it with some examples.

    I don't understand create een instance of Bank and CommandReader?

  8. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    578
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    creating an instance means:
    Bank b = new Bank();
    CommandReader reader = new CommandReader(b);

  9. #9
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    Quote Originally Posted by PhHein View Post
    creating an instance means:
    Bank b = new Bank();
    CommandReader reader = new CommandReader(b);
    Thank you very much, it is working better now!

    Now I'm stuck on the array part. I don't really understand arrays and now I have to make a print of multiple accounts of people. How do I have to do that?

    What I have right now:

     
    import java.util.Scanner;
    import java.util.ArrayList;
     
    class Bank{
      double balance;
      Account account[];
      CommandReader commandReader;
      String name;
     
      Bank(){
        commandReader = new CommandReader();
      }
     
      void run(){
        balance = 0;
        commandReader.run();
        account = new Account[];
      }
     
      void enroll(String name){
        account = new Account[];
      }
     
      void deposit(String name, int amount){
        balance = balance + amount;
      }
     
      void withdraw(String name, int amount){
        balance = balance - amount;
      }
     
      void print(String name){
        System.out.println("Account of "+name);
        System.out.println("  balance: "+balance);
      }
     
      void printAll(){
        System.out.println(account[]);
      }
    }
     
    class Account{
      double balance;
     
      Account(String name){
        balance = 0;
      }
    }

  10. #10
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    578
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    You didn't read the assignment properly! It says: The central class is "Bank" that contains the accounts (an ArrayList).

  11. #11
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    I also tried it with this code:

     
    import java.util.Scanner;
    import java.util.ArrayList;
     
    class Bank{
      double balance;
      //Account account;
      ArrayList<String>account;
      CommandReader commandReader;
      String name;
     
      Bank(){
        commandReader = new CommandReader();
      }
     
      void run(){
        balance = 0;
        commandReader.run();
      }
     
      void enroll(String name){
        account = new ArrayList<String>();
      }
     
      void deposit(String name, int amount){
        balance = balance + amount;
      }
     
      void withdraw(String name, int amount){
        balance = balance - amount;
      }
     
      void print(String name){
        System.out.println("Account of "+name);
        System.out.println("  balance: "+balance);
      }
     
      void printAll(){
        System.out.println(account);
      }
    }

  12. #12
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    578
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    Ok. You're confused. Your ArrayList will contain Accounts not Strings. The ArrayList will be created once. In enroll you create a new Account and add it to the ArrayList.

  13. #13
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    Quote Originally Posted by PhHein View Post
    Ok. You're confused. Your ArrayList will contain Accounts not Strings. The ArrayList will be created once. In enroll you create a new Account and add it to the ArrayList.
    Thank you very much, I searched in the coursereader for information about arraylists, but what I don't understand is how I have to print the accounts?

     
    import java.util.Scanner;
    import java.util.ArrayList;
     
    class Bank{
      double balance;
      //Account account;
      ArrayList<Account>account;
      CommandReader commandReader;
      String name;
     
      Bank(){
        commandReader = new CommandReader();
      }
     
      void run(){
        balance = 0;
        commandReader.run();
      }
     
      void enroll(String name){
        account = new ArrayList<Account>();
      }
     
      void printAll(){
        System.out.println(account);
      }
    }

  14. #14
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    578
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    You've still got it wrong. The bank(class) is your room, in your room there is a folder(Arraylist) and with every 'enroll' call you put a new sheet with a name and balance(Account) into the folder.
    Right now, with every enroll call you throw away the existing folder and grab a new empty one, without ever putting a sheet into it. Got it now?

  15. #15
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    Thank you PhHein,

    Am I in the right direction now?

     
    import java.util.Scanner;
    import java.util.ArrayList;
     
    class Bank{
      double balance;
      //Account account;
      ArrayList<Account>account = new ArrayList<Account>();
      CommandReader commandReader;
      boolean name;
     
      Bank(){
        commandReader = new CommandReader();
      }
     
      void run(){
        commandReader.run();
      }
     
      void enroll(String name){
        if(account.contains(name)){
          System.out.println("already enrolled");
        } else {
          account.add(new Account());
        }
      }
     
      void deposit(String name, int amount){
        if(account.contains(name)){
          balance = balance + amount;
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void withdraw(String name, int amount){
        if(account.contains(name)){
          balance = balance - amount;
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void print(String name){
        System.out.println("Account of "+name);
        System.out.println("  balance: "+balance);
      }
     
      void printAll(){
        System.out.println(account);
      }
     
      void printRed(){
        if (balance < 0){
          System.out.println(account);
        } else {
          System.out.print("no negative balances");
        }
      }
     
      void interest (double rate){
     
      }
     
      public static void main(String[] args){
          new Bank().run();
      }
    }
     
    class CommandReader{
        Bank bank;
        Account account;
     
        /*
        CommandReader(Bank bank) {
            this.bank = bank;
        }*/
     
        //CommandReader() {
        //    bank = new Bank();
        //}
     
        // @pre: input complies to specified format
        void run(){
            Scanner scanner;
            String command;
            String name;
            int amount;
            scanner = new Scanner(System.in);
            bank = new Bank();
            do{
                command = scanner.next().toLowerCase();
                if (command.equals("enroll")) {
                    name = scanner.next();
                    bank.enroll(name);
                } else if (command.equals("deposit")) {
                    name = scanner.next();
                    amount = scanner.nextInt();
                    bank.deposit(name, amount);
                } else if (command.equals("withdraw")) {
                    name = scanner.next();
                    amount = scanner.nextInt();
                    bank.withdraw(name, amount);
                } else if (command.equals("print")) {
                    name = scanner.next();
                    bank.print(name);
                } else if (command.equals("printall")) {
                    bank.printAll();
                } else if (command.equals("printred")) {
                    bank.printRed();
                } else if (command.equals("interest")) {
                    double rate = scanner.nextDouble();
                    bank.interest(rate);
                } else if (command.equals("stop")) { 
                    // do nothing
                } else {
                    System.out.println("unknown command");
                    // skip rest of line
                    scanner.nextLine();
                }
            } while (! command.equals("stop"));
        }
    }
     
    class Account{
      double balance;
      String name;
     
      Account(){
        balance = 0;
        this.name = name;
      }
     
      String getName(){
        return this.name;
      }
     
      void setName(String name){
       this.name = name; 
      }
    }

  16. #16
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    578
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Could somebody help me with my homework? Calling method's from another class

    Better, but it still needs a lot of work. Your check account.contains(name) won't work as name is a String, but the ArrayList contains Accounts. Bank class doesn't need an atrribute balance, Account takes care of it. In Enroll you never set the Account's name. That's what I see just glancing at it.

Similar Threads

  1. calling a method in class
    By MrBean in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2012, 12:31 AM
  2. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  3. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 12:32 PM
  4. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 09:24 AM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 04:24 PM

Tags for this Thread