简体   繁体   中英

bank account client in java with multiple classes

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);
  }
}
  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 eg 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.

I'm not going to answer this question for you. Instead, I'm going to recommend that you read a bit more about Java programming concepts that will explain it to you, by default.

If you don't want to read any of these links, @YogendraSingh answered this question really well, use that answer.

Attributes to an object shouldn't be static, for example your "name, actype, bal and amt". Also I think your money class should exist and those methods could be in a bank account (you deposit/withdraw from a bank account).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM