bank account client in java with multiple classes

Ricky McQuesten picture Ricky McQuesten · Nov 29, 2012 · Viewed 23k times · Source

I am trying to make a bank account program, but I cannot figure out how to get all my variables visible to every class that I have, or how to make the withdrawal and deposit methods of my code visible. Can anyone look at my code and tell me what is wrong? I only want input and output in the client class.

Thanks

Client Class

public class Client {
  public static void main(String args[]) {
    Scanner input = new Scanner(System.in);       
    System.out.println("Enter your Name: ");
    String cusName = input.nextLine();
    System.out.println("Enter Account Type: ");
    String type = input.next();
    System.out.println("Enter Initial Balance: ");
    int bal = input.nextInt();
    BankAccount b1 = new BankAccount(cusName, num, type, bal);
    int menu;
    System.out.println("Menu");
    System.out.println("1. Deposit Amount");
    System.out.println("2. Withdraw Amount");
    System.out.println("3. Display Information");
    System.out.println("4. Exit");
    boolean quit = false;
    do {
      System.out.print("Please enter your choice: ");
      menu = input.nextInt();
      switch (menu) {
      case 1:
        b1.deposit();
        break;

      case 2:
        b1.withdraw();
        System.out.println("Current Account Balance=" + Balance);
        System.out.print("Enter withdrawal amount:");
        amount = input.nextInt();
        break;

      case 3:
        b1.display();
        break;

      case 4:
        quit = true;
        break;
      }
    } while (!quit);
  }
}

Money Class

public class Money
{

  static int accountNumber, Balance, amount;
  Scanner input = new Scanner(System.in);
  static String name, actype;
  public int deposit() {
    System.out.print("Enter depost amount:");
    amount = input.nextInt();
    if (amount < 0) {
      System.out.println("Invalid");
      return 1;
    }
    Balance = Balance + amount;
    return 0;
  }

  int withdraw()  {

    if (Balance < amount) {
      System.out.println("Not enough funds.");
      return 1;
    }
    if (amount < 0) {
      System.out.println("Invalid");
      return 1;
    }
    Balance = Balance - amount;
    return 0;
  }

}

BankAccount Class

class BankAccount {
  Scanner input = new Scanner(System.in);
  static String name, actype;
  static int bal, amt;
  Random randomGenerator = new Random();
  int accNo = randomGenerator.nextInt(100);

  BankAccount(String name, int accNo, String actype, int bal) {
    this.name = name;
    this.accNo = accNo;
    this.actype = actype;
    this.bal = bal;
  }
  void display() {
    System.out.println("Name:" + name);
    System.out.println("Account No:" + accNo);
    System.out.println("Balance:" + bal);

  }

  void dbal() {
    System.out.println("Balance:" + bal);
  }
}

Answer

Yogendra Singh picture Yogendra Singh · Nov 29, 2012
  1. Add Money to your BankAccount and create a getter method as:

    class BankAccount {
       Scanner input = new Scanner(System.in);
       static String name, actype;
       static int bal, amt;
       Random randomGenerator = new Random();
       int accNo = randomGenerator.nextInt(100);
       Money money;
    
       BankAccount(String name, int accNo, String actype, int bal) {
        this.name = name;
        this.accNo = accNo;
        this.actype = actype;
        this.bal = bal;
        this.money = new Money();
      }
    
      public Money getMoney(){
         return this.money;
      }
      .....
    }
    
  2. Use bankaccount.getMoney() to invoke deposit and withdraw as :

     b1.getMoney().deposit();
     b1.getMoney().withdraw();
    

In addition, I would advice to make the Money class attributes e.g. amount, accntType... non-static and set through through a constructor. Static variables are associated with class definition and hence you won't be abl to maintain them per Bank Account.