main에서 수행 할 작업 : 먼저 이름, 성 및 번호로 새 고객을 만듭니다. 그러면 금액, id 및 이자율로 두 개의 savingsAccount가 생성됩니다. 그런 다음 두 개의 savingsAccounts를 새 고객에 추가합니다. 마지막으로 새 고객을 은행에 추가합니다. 여기 ArrayList의 특정 요소 가져 오기
Customer newCustomer = new Customer(firstName, lastName, pnumber);
SavingsAccount savingsAccount1 = new SavingsAccount(400, "1", 4); //400$ into account no.1, with interest 4%
SavingsAccount savingsAccount2 = new SavingsAccount(300, "2", 3);
newCustomer.addAccount(savingsAccount1);
newCustomer.addAccount(savingsAccount2);
bank.addCustomer(newCustomer);
클래스 은행입니다 : 여기
public class Bank {
String bankName;
private ArrayList<Customer> customers = new ArrayList<Customer>();
Bank(String bankName) {
this.bankName = bankName;
}
public void addCustomer(Customer newCustomer) {
customers.add(newCustomer);
}
}
클래스 고객입니다 :
여기
public class Customer {
private String firstName;
private String lastName;
private String number;
private ArrayList<Account> accounts;
Customer(String firstName, String lastName, String number) {
this.firstName = firstName;
this.lastName = lastName;
this.number = number;
this.accounts = new ArrayList<Account>();
}
public void addAccount(SavingsAccount account) {
accounts.add(account);
}
public void addAccount(CreditAccount account) {
accounts.add(account);
}
public ArrayList<Account> getAccounts() {
return accounts;
}
}
클래스에서 SavingsAccount (즉, 상속 클래스 계정)입니다 :
public class SavingsAccount extends Account {
public SavingsAccount() {
super();
}
public SavingsAccount(double bal, String id, double inte) {
super(bal, id, inte);
}
@Override
public void deposit(String number, String id, double amount) {
}
@Override
public void withdraw(String number, String id, double amount) {
}
@Override
public void transfer(String number, String id, double amount) {
}
@Override
public double getBalance() {
}
@Override
public String getAccountId() {
return accountId;
}
@Override
public double getInterest(){
return interest;
}
}
내 문제는 : 특정 계좌에서 특정 고객을 위해 입금, 인출, 돈을 송금하기 위해 클래스 SavingsAccount에 코드를 작성하려면 어떻게해야합니까? 고객 번호 2에 500을 자신의 계좌 번호 1에 입금하려고한다고 가정 해 봅시다.
저축은 savingsAccount.deposit ("2", "1", 500)와 유사해야합니다.
고객 번호 2와 그의 계좌 번호 1에 액세스하는 방법을 알 수 없습니다. 누구든지 나를 도와 줄 수 있습니까? 이를 위해
public class Bank {
// Your stuff
// new method:
public boolean transfer(Account accountFrom, double amount, String nameTo, int account) {
//check if the balance can be deposit from the account
if(amount <= accountFrom.getBalance()) {
//Check if the person exists in the bank
String name = nameTo.split(" "); // name[0] is the first name, name[1] last name
boolean success = false;
for(Customer c: customers) {
if(c.getFirstName().equalsIgnoreCase(name[0]) &&
c.getLastName().equalsIgnoreCase(name[1]) {
for(Account a : c.getAccounts()) {
if(a.getAccountId() == account) {
// Add it to the account
a.deposit(amount);
success = true;
break;
}
}
break;
}
}
// Deposit it from the account (That class should only keep track of money, so it
// only takes an argument to deposit or withdraw a value, the rest is done by the bank
// Only do this if money has been dsposited at the target account
if(success){
accountFrom.withdraw(amount);
return true;
}
}
return false;
}
}
당신이 structurely 당신의 설정을 변경해야 할 일이하기 : 당신이 무엇을 할 수 있는지
왜이 방법이이 수업에 속합니까? '고객'에 대해보다 논리적 인 것으로 보입니다. 특정 고객과 관련된 모든 계정을 아는 유일한 클래스이기 때문입니다. – Dici
@Dici ok. 계정 클래스에 입력하면 어떻게하면 올바른 고객과 계정에 액세스 할 수 있습니까? 코드는 어떻게 생겼습니까? – user2939293
또한 모든 고객을 저장하는 은행 클래스입니다. – user2939293