gpt4 book ai didi

java - java中具有多个类的银行帐户客户端

转载 作者:行者123 更新时间:2023-12-02 00:06:59 24 4
gpt4 key购买 nike

我正在尝试制作一个银行帐户程序,但我不知道如何使我的所有变量对我拥有的每个类都可见,或者如何使我的代码的取款和存款方法可见。任何人都可以查看我的代码并告诉我出了什么问题吗?我只想在客户端类中输入和输出。

谢谢

客户端类

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

最佳答案

  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 类属性设置为,例如amount、accntType...非静态并通过构造函数设置。静态变量与类定义相关联,因此您无法按银行帐户维护它们。

关于java - java中具有多个类的银行帐户客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13630579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com