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

• May 23rd, 2013, 04:27 AM
ProgrammerNoobE
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
• May 23rd, 2013, 04:31 AM
PhHein
Re: Could somebody help me with my homework? Calling method's from another class
What have you tried? - Matt Gemmell
• May 23rd, 2013, 04:40 AM
ProgrammerNoobE
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){

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:

Code Java:

```  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){   } }```
• May 23rd, 2013, 04:47 AM
PhHein
Re: Could somebody help me with my homework? Calling method's from another class
• May 23rd, 2013, 04:54 AM
ProgrammerNoobE
Re: Could somebody help me with my homework? Calling method's from another class
sorry for this mistake, I've changed it now
• May 23rd, 2013, 05:06 AM
PhHein
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.
• May 23rd, 2013, 05:21 AM
ProgrammerNoobE
Re: Could somebody help me with my homework? Calling method's from another class
Quote:

Originally Posted by PhHein
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?
• May 23rd, 2013, 05:27 AM
PhHein
Re: Could somebody help me with my homework? Calling method's from another class
creating an instance means:
Code :

```Bank b = new Bank(); CommandReader reader = new CommandReader(b);```
• May 23rd, 2013, 06:12 AM
ProgrammerNoobE
Re: Could somebody help me with my homework? Calling method's from another class
Quote:

Originally Posted by PhHein
creating an instance means:
Code :

```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:

Code Java:

```  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; } }```
• May 23rd, 2013, 06:20 AM
PhHein
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).
• May 23rd, 2013, 06:21 AM
ProgrammerNoobE
Re: Could somebody help me with my homework? Calling method's from another class
I also tried it with this code:

Code Java:

```  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); } }```
• May 23rd, 2013, 06:25 AM
PhHein
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.
• May 23rd, 2013, 06:39 AM
ProgrammerNoobE
Re: Could somebody help me with my homework? Calling method's from another class
Quote:

Originally Posted by PhHein
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?

Code Java:

```  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); } }```
• May 23rd, 2013, 09:39 AM
PhHein
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?
• May 23rd, 2013, 11:18 AM
ProgrammerNoobE
Re: Could somebody help me with my homework? Calling method's from another class
Thank you PhHein,

Am I in the right direction now?

Code Java:

```  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; } }```
• May 24th, 2013, 02:03 AM
PhHein
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.