gpt4 book ai didi

Java 银行程序。如何让客户拥有多个账户?

转载 作者:行者123 更新时间:2023-12-03 20:26:43 25 4
gpt4 key购买 nike

我正在用 Java 编写银行程序并有 5 个类:Account、SavingsAccount(继承 Account)、CreditAccount(继承 Account)、Bank、Customer。

该程序按原样运行,但我不知道如何让一个客户拥有两个或更多帐户。假设一位客户同时想要一个信用账户和一个储蓄账户,或者两个储蓄账户。

谁能给我一些建议?谢谢

银行类:

public class Bank {
String bankName;
private Customer[] customers = new Customer[100];

Bank(String bankName) {
this.bankName = bankName;
}

public Customer[] getCustomer() {
return customers;
}

public String getBankname() {
return bankName;
}
}

账户类别:

public abstract class Account {
protected double balance = 0;
protected String accountId;

public Account() {} //Defaultkonstruktor

public Account(double bal, String id) { //Konstruktor
if (balance >= 0) {
balance = bal;
}
else {
balance = 0;
}
accountId = id;
}

public abstract void deposit(double amount);

public abstract void withdraw(double amount);

public abstract double getBalance();

public abstract String getAccountId();

public abstract void transfer(double amount, Account account);
}

SavingsAccount 类:(CreditAccount 类类似)

public class SavingsAccount extends Account{ 
private double interest = 2.9;

public SavingsAccount() { //Konstruktor
super();
}

public SavingsAccount(double balance, String id) { //Konstruktor
super(bal,id);
}

public void setInterest(Customer customer) {
//code
}

public void setBalance(double balance) {
//code
}

@Override
public void deposit(double amount) {
//code
}

@Override
public void withdraw(double amount) {
//code
}

@Override
public double getBalance(){
//code
}

@Override
public String getAccountId(){
//code
}

@Override
public void transfer(double amount, Account account) {
//code
}

public void setInterest(double interest){
//code
}

public double getInterest(){
//code
}
}

客户类:

public class Customer {
private String firstName;
private String lastName;
private String number;

private SavingsAccount account = new SavingsAccount();
private CreditAccount cAccount = new CreditAccount();

Customer(String firstName, String lastName, String number, SavingsAccount account) {
this.firstName = firstName;
this.lastName = lastName;
this.number = number;
this.account = account;
}

Customer(String firstName, String lastName, String number, CreditAccount cAccount) {
this.firstName = firstName;
this.lastName = lastName;
this.number = number;
this.cAccount = cAccount;
}

public SavingsAccount getAccount() {
return account;
}

public CreditAccount getCreditAccount() {
return cAccount;
}
}

主要内容:

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
int numberOfCustomers = 0;
boolean endProgram = false;
String bankName;
System.out.print("Name of bank: ");
bankName = input.next();
Bank bank = new Bank(bankName);
String accountId;

SavingsAccount acc = new SavingsAccount();
Customer[] customer = bank.getCustomer();

do {
System.out.println(" " + bank.getBankname() + "\n");
System.out.println(" 1. See balance ");
System.out.println(" 2. Withdraw ");
System.out.println(" 3. Deposit ");
System.out.println(" 4. Transfer ");
System.out.println(" 5. Add interest ");
System.out.println(" 6. Add new customer ");
System.out.println(" 7. Show customers ");
System.out.println(" 8. Change interest ");
System.out.println(" 0. Exit ");

choice = input.nextInt();

switch(choice) {

case 1:
//code
break;


case 2:
//code
break;


case 3:
//code
break;


case 4:
//code
break;


case 5:
//code
break;


case 6: //Add customer
System.out.println("Choose account: ");
System.out.println("1. Savings account");
System.out.println("2. Credit account");
choice = input.nextInt();
switch(choice) {

case 1: //Create savings account
System.out.print("Enter amount to deposit: ");
double amount = input.nextDouble();
System.out.println("Account number is: " + numberOfCustomers);
SavingsAccount savingsAccount = new SavingsAccount(amount, String.valueOf(numberOfCustomers));
System.out.print("First name: ");
String firstName = input.next();
System.out.print("Last name: ");
String lastName = input.next();
System.out.print("Customer number: ");
String pnumber = input.next();

Customer newCustomer = new Customer(firstName, lastName, pnumber, savingsAccount);
customer[numberOfCustomers] = newCustomer;
numberOfCustomers++;

break;

case 2: //Create credit account
System.out.print("Enter amount to deposit: ");
double amount = input.nextDouble();
System.out.println("Account number is: " + numberOfCustomers);
CreditAccount creditAccount = new CreditAccount(amount, String.valueOf(numberOfCustomers));
System.out.print("First name: ");
String firstName = input.next();
System.out.print("Last name: ");
String lastName = input.next();
System.out.print("Customer number: ");
String pnumber = input.next();

Customer newCustomer = new Customer(firstName, lastName, pnumber, creditAccount);
customer[numberOfCustomers] = newCustomer;
numberOfCustomers++;

break;
}
break;


case 7:
//code
break;


case 8:
//code
break;


case 0:
//code
break;
}
} while (!endProgram);
}

最佳答案

首先将 Account 重命名为 AbstractAccount 并创建一个 Account 接口(interface),您的 Credit 和 Saving 帐户是具体的实现。然后在您的 Customer 类中声明一个变量 accounts,它是一个 List accounts。有点像

public class Customer {
private String firstName;
private String lastName;
private String number;

private List<Account> accounts;

您的帐户界面可能看起来像

interface Account {

public void deposit(double amount);

public void withdraw(double amount);

public double getBalance();

public String getAccountId();
}

你现有的类被重构

 public abstract AbstractAccount implements Account {
....
}

您的 SavingAccount 变为

 public SavingAccount extends AbstractAccount {
....
}

我有点担心此方法在 Account 类中被声明为抽象方法。为什么转移会在两个不同的类(class)上以不同的方式实现?

 public abstract void transfer(double amount, Account account);

我建议此方法更多地属于 AccountManager 类,以确保从两个帐户中贷记/借记正确的金额。

 public void transfer(double amount, Account fromAccount, Account toAccount);

然后,此类可以在实际转账发生之前检查“fromAccount”是否具有可用于转账的所需资金,作为验证步骤。

关于Java 银行程序。如何让客户拥有多个账户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25952086/

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