我正在尝试制作一个银行帐户计划,但我无法弄清楚如何让我的所有变量都可见,或者如何使我的代码的提款和存款方法可见。任何人都可以看看我的代码并告诉我有什么问题吗?我只想在客户端类中输入和输出。
由于
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);
}
}
答案 0 :(得分:2)
将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;
}
.....
}
使用bankaccount.getMoney()
调用deposit
和withdraw
作为:
b1.getMoney().deposit();
b1.getMoney().withdraw();
此外,我建议制作Money
类属性,例如amount,accntType ...非静态并通过构造函数设置。静态变量与类定义相关联,因此您不会按每个银行帐户维护它们。
答案 1 :(得分:1)
我不打算为你回答这个问题。相反,我建议您阅读更多关于Java编程概念的内容,默认情况下会向您解释。
如果您不想阅读这些链接,@ YogendraSingh非常好地回答了这个问题,请使用该答案。
答案 2 :(得分:0)
对象的属性不应该是静态的,例如你的“name,actype,bal和amt”。此外,我认为您的资金等级应该存在,这些方法可以存入银行账户(您存入/取出银行账户)。