【发布时间】:2015-02-07 12:43:49
【问题描述】:
我对 OOP 和 Java 还是很陌生,有一个问题可能很简单,但我在网上找不到答案。
我正在用 Java 编写标准银行账户程序 - 一个有客户的程序,每个客户都有银行账户(一个客户可能有多个银行账户!),我可以存款或取款。每个银行账户都有一个唯一的号码(即使某人有多个银行账户,每个银行账户都有唯一的号码)
我的代码编译成功,存款和取款操作正常。
问题如下 - 我不能将多个银行帐户归于一个客户,在我的程序中,客户只能拥有一个银行,但不能超过一个银行帐户。
我有 3 个类 - Account、Client、BankMain。你可以在下面看到它们
public class Account {
private int balance;
private String NumAccount; // each bank account has a unique number
public Account(int money, String num) {
balance = money;
NumAccount = num;
}
public String printAccountNumber() {
return NumAccount;
}
// below are the methods for withdraw,deposit- they are working properly
}
类客户端
public class Client {
private String name;// name of the client
private Account account;
// the account associated to the client. Probably I should change sth
// here
// but I don't know what
public Client(String nom, Compte c) {
name = nom;
account = c;
}
public void printName() {
System.out.println(
"Name: " + name
+ "\nAccount number: " + account.printAccountNumber()
+ "\nSolde: " + account.printBalance() + "\n"
);
}
}
和BankMain
public class BankMain {
public static void main(String[] args) {
Account account1 = new Account(1000, "11223A");
Account account2 = new Account(10000, "11111A");
Client client1 = new Client("George", account1);
Client client2 = new Client("Oliver", account2);
// now this is working correctly
client1.printName();
client2.ptintName();
/*
* The problem is that I don't know what to do if I want account1
* and account2 to belong both to client1. I tried Client client1 =
* new Client("George",account1); Client client1 = new
* Client("Oliver", account2); but got compilation error
*/
}
}
你知道我该如何解决这个问题吗?我应该怎么做才能让多个银行账户关联到同一个客户?
提前致谢 乔治
【问题讨论】: