0

我正在尝试制作一个银行帐户程序,但我无法弄清楚如何让我的所有变量对我拥有的每个类都可见,或者如何使我的代码的提款和存款方法可见。谁能看看我的代码并告诉我出了什么问题?我只想要客户端类中的输入和输出。

谢谢

客户端类

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

金钱类

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;
  }

}

银行账户类

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);
  }
}
4

3 回答 3

2
  1. 添加Money到您的BankAccount并创建一个 getter 方法:

    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. 用于bankaccount.getMoney()调用depositwithdraw作为:

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

此外,我建议使Money类属性(例如数量、帐户类型...)非静态并通过构造函数进行设置。静态变量与类定义相关联,因此您无法为每个银行帐户维护它们。

于 2012-11-29T16:55:57.563 回答
1

我不会为你回答这个问题。相反,我将建议您阅读更多有关 Java 编程概念的内容,默认情况下会向您解释。

如果您不想阅读这些链接中的任何一个,@YogendraSingh 很好地回答了这个问题,请使用该答案。

于 2012-11-29T17:00:45.933 回答
0

对象的属性不应是静态的,例如您的“名称、actype、bal 和 amt”。另外我认为您的货币类应该存在,并且这些方法可以在银行账户中(您从银行账户存款/取款)。

于 2012-11-29T16:51:53.410 回答