Java 具有多态性的银行应用程序,getter 变为空
Java banking app with polymorphism, getter getting null
我完全迷失了,我无法恢复或克服这个问题。
我想要做的是拥有一个银行应用程序,我应该在其中拥有应该从帐户 class 继承的 SavingsAccount 和 CreditAccount。当我的 subclasses 尝试 getBalance/getIntereset 时,我没有得到正确的值,而是有时得到 Null。
你能帮我指出问题并看看需要解决什么问题才能使我的 withdraw() 和 deleteCustomer() 方法正常工作吗?
非常感谢!
逻辑class
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class BankLogic {
//Deklarera global lista med kunder
private ArrayList<Customer> customers = new ArrayList<>();
private ArrayList<Transaction> transactions = new ArrayList<>();
Date date;
public ArrayList<String> getAllCustomers() {
ArrayList<String> customersStr = new ArrayList<>();
if (customers == null) {
customersStr.add("");
return customersStr;
} else
for (Customer customer : customers) {
customersStr.add(customer.getpNo() + " " + customer.getName() + " " + customer.getSurname());
}
return customersStr;
}
public boolean createCustomer(String name, String surname, String pNo) {
try {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
return false;
}
}
if (!name.equals("") || !surname.equals("")) {
Customer customer = new Customer(name, surname, pNo);
customers.add(customer);
return true;
}
} catch (Exception e) {
System.out.println(e);
}
return false;
}
public ArrayList<String> getCustomer(String pNo) {
ArrayList<String> presentCustomerArr = new ArrayList<>();
try {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
presentCustomerArr.add(customers.get(i).getpNo() + " " + customers.get(i).getName() + " " + customers.get(i).getSurname());
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
if (customers.get(i).getCustomerAccounts() != null) {
for (int j = 0; j < accountList.size(); j++) {
presentCustomerArr.add(accountList.get(j).getAccountNumber() + " " + stringFormatted(accountList.get(j).getBalance()) + " " + accountList.get(j).getAccountType() + " " + accountList.get(j).getInterestRate() + " %");
}
}
return presentCustomerArr;
}
}
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public boolean changeCustomerName(String name, String surname, String pNo) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
if (!name.equals("") && !surname.equals("")) {
customers.get(i).setName(name);
customers.get(i).setSurname(surname);
return true;
}
if (name.equals("") && !surname.equals("")) {
customers.get(i).setName(customers.get(i).getName());
customers.get(i).setSurname(surname);
return true;
}
if (!name.equals("") && surname.equals("")) {
customers.get(i).setName(name);
customers.get(i).setSurname(customers.get(i).getSurname());
return true;
}
}
}
return false;
}
public int createSavingsAccount(String pNo) {
try {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
ArrayList<Account> accountList = new ArrayList<>();
if (customers.get(i).getCustomerAccounts() != null) {
accountList = customers.get(i).getCustomerAccounts();
}
SavingsAccount savingsAccount = new SavingsAccount();
accountList.add(savingsAccount);
customers.get(i).setCustomerAccounts(accountList);
int accNr = -1;
for (int j = 0; j < accountList.size(); j++) {
accNr = savingsAccount.getAccountNumber();
}
return accNr;
}
}
} catch (Exception e) {
}
return -1;
}
public String getAccount(String pNo, int accountId) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
if (customers.get(i).getCustomerAccounts() != null) {
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountNumber() == accountId) {
String accPresentation = accountList.get(j).getAccountNumber() + " " + stringFormatted(accountList.get(j).getBalance()) + " " + accountList.get(j).getAccountType() + " " + accountList.get(j).getInterestRate() + " %";
return accPresentation;
}
}
}
}
}
return null;
}
public boolean deposit(String pNo, int accountId, int amount) {
if (amount > 0) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
if (customers.get(i).getCustomerAccounts() != null || customers.get(i).getCustomerAccounts() != null) {
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountNumber() == accountId) {
accountList.get(j).setBalance(Double.valueOf(accountList.get(j).getBalance() + amount));
transactions = addTransaction(new Date(), amount, accountList.get(j).getBalance(), accountList.get(j).getAccountNumber(), false);
customers.get(j).setAccountTransactions(transactions);
return true;
}
}
}
}
}
}
return false;
}
private ArrayList<Transaction> addTransaction(Date date, int amount, Double balance, int accountnumber, boolean isAWithdrawal) {
Transaction transaction = new Transaction(new Date(), (double) amount, balance, accountnumber, isAWithdrawal);
transactions.add(transaction);
return transactions;
}
public boolean withdraw(String pNo, int accountId, int amount) {
Boolean isUpdated = false;
if (amount > 0) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
if (customers.get(i).getCustomerAccounts() != null) {
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountNumber() == accountId) {
isUpdated = accountList.get(j).withdraw(accountList, amount, customers, transactions);
if (isUpdated){
return false;
}
//transactions = accountList.get(j).addTransaction(transactions, date, amount, Double.valueOf(accountList.get(j).getBalance()), accountList.get(j).getAccountNumber(), true);
customers.get(j).setAccountTransactions(transactions);
return true;
}
}
}
}
}
}
return false;
}
public void updateTransactions(ArrayList<Transaction> transactions) {
this.transactions = transactions;
}
private boolean balanceAfterWithdrawIsOk(int amount, Double balance) {
if (balance - amount < -5000) {
return false;
} else return true;
}
public String closeAccount(String pNr, int accountId) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNr)) {
if (customers.get(i).getCustomerAccounts() != null) {
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountNumber() == accountId) {
String closedAcc = accountList.get(j).calculateClosedAccountBalance(customers.get(i).getCustomerAccounts().get(j));
customers.get(i).getCustomerAccounts().remove(accountList.get(j));
return closedAcc;
}
}
}
}
}
return null;
}
public ArrayList<String> deleteCustomer(String pNo) {
ArrayList<String> deletedCustomerArr = new ArrayList<>();
try {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
int accNr = -1;
deletedCustomerArr.add(customers.get(i).getpNo() + " " + customers.get(i).getName() + " " + customers.get(i).getSurname());
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
if (customers.get(i).getCustomerAccounts() != null) {
for (int j = 0; j < accountList.size(); j++) {
//String hej = stringFormatted(accountList.get(j).getInterest());
try {
String addString = accountList.get(j).getAccountNumber() + " " + stringFormatted(accountList.get(j).getBalance()) + " " + accountList.get(j).getAccountType() + " " + stringFormatted(accountList.get(j).getInterest());
deletedCustomerArr.add(addString);
}
catch (Exception e) {
System.out.println(e);
}
}
accountList.clear();
}
customers.remove(i);
return deletedCustomerArr;
}
}
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public int createCreditAccount(String pNo) {
try {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
ArrayList<Account> accountList = new ArrayList<>();
if (customers.get(i).getCustomerAccounts() != null) {
accountList = customers.get(i).getCustomerAccounts();
}
for (int j = 0; j < accountList.size(); j++) {
}
CreditAccount creditAccount = new CreditAccount((double) 0, 0.5, "Kreditkonto");
accountList.add(creditAccount);
customers.get(i).setCustomerAccounts(accountList);
int accNr = -1;
for (int j = 0; j < accountList.size(); j++) {
accNr = creditAccount.getAccountNumber();
}
return accNr;
}
}
} catch (Exception e) {
}
return -1;
}
public ArrayList<String> getTransactions(String pNo, int accountId) {
ArrayList<String> arrList = new ArrayList<>();
try {
for (Customer customer : customers) {
if (customer.getpNo() == pNo) {
for (Transaction transaction : customer.getAccountTransactions()) {
if (accountId == transaction.getAccountNumber()) {
transactions.get(this.transactions.indexOf(transaction));
arrList.add(simpleDateFormatter(transactions.get(this.transactions.indexOf(transaction)).getDateAndTime()) + " " + (transactions.get(this.transactions.indexOf(transaction)).isAWithdrawal() == true ? "−" : "").concat(stringFormatted(transactions.get(this.transactions.indexOf(transaction)).getAmount())) + " Saldo: " + stringFormatted(transactions.get(this.transactions.indexOf(transaction)).getBalanceAfterTransaction()));
}
}
}
}
} catch (NullPointerException e) {
for (Customer customer : customers) {
for (int i = 0; i < customer.getCustomerAccounts().size(); i++) {
if (customer.getCustomerAccounts().get(i).getAccountNumber() == accountId && customer.getAccountTransactions() == null) {
return arrList;
}
}
if (customer.getAccountTransactions() == null) {
return null;
}
}
return null;
}
return arrList;
}
// Custom methods
private String stringFormatted(Double balance) {
balance = Double.valueOf(balance);
String balanceStr = NumberFormat.getCurrencyInstance().format(balance);
return balanceStr;
}
private String simpleDateFormatter(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(date);
return strDate;
}
}
帐号class
import java.util.ArrayList;
import java.util.Date;
public abstract class Account {
private Double balance;
private Double interestRate;
private Double interest;
private int accountNumber;
private String accountType;
private int numberOfWithdraws = 0;
private static int lastAssignedNumber = 1000;
public Account() {
lastAssignedNumber++;
this.balance = Double.valueOf(0);
this.accountNumber = lastAssignedNumber;
}
public Account(Double balance, Double interestRate, String accountType, int lastAssignedNumber) {
}
public Account(Double balance, Double interestRate, String accountType) {
lastAssignedNumber++;
this.accountNumber = lastAssignedNumber;
this.balance = balance;
this.interestRate = interestRate;
this.accountType = accountType;
}
public Account(Double balance, Double interestRate, Double interest, int accountNumber, String accountType, int numberOfWithdraws) {
this.balance = balance;
this.interestRate = interestRate;
this.interest = interest;
this.accountNumber = accountNumber;
this.accountType = accountType;
this.numberOfWithdraws = numberOfWithdraws;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
public Double getInterestRate() {
return interestRate;
}
public void setInterestRate(Double interestRate) {
this.interestRate = interestRate;
}
public Double getInterest() {
return interest;
}
public int getAccountNumber() {
return accountNumber;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public int getNumberOfWithdraws() {
return numberOfWithdraws;
}
public void setNumberOfWithdraws(int numberOfWithdraws) {
this.numberOfWithdraws = numberOfWithdraws;
}
/
public abstract String calculateClosedAccountBalance(Account customerAccounts);
public ArrayList<Transaction> addTransaction(ArrayList<Transaction> transactions, Date date, int amount, Double balance, int accountnumber, boolean isAWithdrawal) {
Transaction transaction = new Transaction(new Date(), (double) amount, balance, accountnumber, isAWithdrawal);
transactions.add(transaction);
return transactions;
}
public abstract boolean withdraw(ArrayList<Account> customerAccounts, int amount, ArrayList<Customer> customers, ArrayList<Transaction> transactions);
public abstract String stringFormatted(Double balance);
}
信用账户
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
public class CreditAccount extends Account {
private String accountType = "Kreditkonto";
private Double interestRate;
private Double creditLimit = Double.valueOf(5000);
private Double interest;
private Double balance;
public CreditAccount(String accountType, Double interestRate, Double creditLimit) {
this.accountType = accountType;
this.interestRate = interestRate;
this.creditLimit = creditLimit;
}
public CreditAccount(Double balance, Double interestRate, String accountType, int lastAssignedNumber, String accountType1, Double interestRate1, Double creditLimit) {
super(balance, interestRate, accountType, lastAssignedNumber);
this.accountType = accountType1;
this.interestRate = interestRate1;
this.creditLimit = creditLimit;
}
public CreditAccount(Double balance, Double interestRate, String accountType, String accountType1, Double interestRate1, Double creditLimit) {
super(balance, interestRate, accountType);
this.accountType = accountType1;
this.interestRate = interestRate1;
this.creditLimit = creditLimit;
}
public CreditAccount(Double balance, Double interestRate, String accountType) {
super(balance, interestRate, accountType);
}
public CreditAccount(Double balance, Double interestRate, Double interest, int accountNumber, String accountType, int numberOfWithdraws) {
super(balance, interestRate, interest, accountNumber, accountType, numberOfWithdraws);
}
public CreditAccount() {
}
public CreditAccount(Double balance, Double interestRate, String accountType, int lastAssignedNumber) {
super(balance, interestRate, accountType, lastAssignedNumber);
}
public Double getCreditLimit() {
return creditLimit;
}
@Override
public String calculateClosedAccountBalance(Account customerAccounts) {
String closedAccStr = "";
closedAccStr = customerAccounts.getAccountNumber() + " " + stringFormatted(customerAccounts.getBalance()) + " " + customerAccounts.getAccountType() + " " + stringFormatted(customerAccounts.getInterest());
return closedAccStr;
}
@Override
public String stringFormatted(Double balance) {
String balanceStr = NumberFormat.getCurrencyInstance().format(balance);
return balanceStr;
}
@Override
public String getAccountType() {
return accountType;
}
@Override
public Double getInterestRate() {
if (this.getBalance() < 0) {
this.interestRate = Double.valueOf(7);
} else this.interestRate = Double.valueOf(0.5);
return interestRate;
}
@Override
public Double getInterest() {
if (getBalance() < 0) {
interest = getBalance() * -7 / 100;
}
return interest;
}
@Override
public boolean withdraw(ArrayList<Account> accountList, int amount, ArrayList<Customer> customers, ArrayList<Transaction> transactions) {
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountType().equals("Kreditkonto")) {
if (balanceAfterWithdrawIsOk(amount, accountList.get(j).getBalance())) {
accountList.get(j).setBalance(Double.valueOf(accountList.get(j).getBalance() + (-amount)));
if (accountList.get(j).getBalance() < 0) {
accountList.get(j).setInterestRate(Double.valueOf(7));
}
transactions = addTransaction(transactions, new Date(), amount, Double.valueOf(accountList.get(j).getBalance()), accountList.get(j).getAccountNumber(), true);
BankLogic bankLogic = new BankLogic();
bankLogic.updateTransactions(transactions);
customers.get(j).setAccountTransactions(transactions);
return true;
}
}
}
return false;
}
private boolean balanceAfterWithdrawIsOk(int amount, Double balance) {
if (balance - amount < -5000) {
return false;
} else return true;
}
}
储蓄账户
import java.text.NumberFormat;
import java.util.ArrayList;
public class SavingsAccount extends Account {
private String accountType = "Sparkonto";
private Double interestRate = 1.2;
private Double withdrawalFeePercentage = Double.valueOf(2);
private Double interest;
public SavingsAccount(String accountType, Double interestRate, Double withdrawalFeePercentage) {
this.accountType = accountType;
this.interestRate = interestRate;
this.withdrawalFeePercentage = withdrawalFeePercentage;
}
public SavingsAccount(Double balance, Double interestRate, String accountType, int lastAssignedNumber, String accountType1, Double interestRate1, Double withdrawalFeePercentage) {
super(balance, interestRate, accountType, lastAssignedNumber);
this.accountType = accountType1;
this.interestRate = interestRate1;
this.withdrawalFeePercentage = withdrawalFeePercentage;
}
public SavingsAccount(Double balance, Double interestRate, String accountType, String accountType1, Double interestRate1, Double withdrawalFeePercentage) {
super(balance, interestRate, accountType);
this.accountType = accountType1;
this.interestRate = interestRate1;
this.withdrawalFeePercentage = withdrawalFeePercentage;
}
public SavingsAccount(Double balance, Double interestRate, String accountType, int accountNumber, int numberOfWithdraws, double withdrawalFeePercentage) {
super(balance, interestRate, accountType, accountNumber);
this.withdrawalFeePercentage = Double.valueOf(2);
}
public SavingsAccount() {
}
@Override
public String calculateClosedAccountBalance(Account customerAccounts) {
String closedAccStr = "";
closedAccStr = customerAccounts.getAccountNumber() + " " + stringFormatted(customerAccounts.getBalance()) + " " + customerAccounts.getAccountType() + " " + stringFormatted(customerAccounts.getInterest());
return closedAccStr;
}
@Override
public String stringFormatted(Double balance) {
String balanceStr = NumberFormat.getCurrencyInstance().format(balance);
return balanceStr;
}
public Double getWithdrawalFeePercentage() {
return withdrawalFeePercentage;
}
@Override
public String getAccountType() {
return accountType;
}
@Override
public Double getInterestRate() {
return interestRate;
}
@Override
public Double getInterest() {
interest = getBalance() * 1.2 / 100;
return interest;
}
@Override
public boolean withdraw(ArrayList<Account> accountList, int amount, ArrayList<Customer> customers, ArrayList<Transaction> transactions) {
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountType().equals("Sparkonto")) {
//accountList.get(j).withdrawMoney();
if (accountList.get(j).getNumberOfWithdraws() > 0) {
amount = (int) (amount * 1.02);
}
if (accountList.get(j).getBalance() >= amount) {
accountList.get(j).setBalance(Double.valueOf(accountList.get(j).getBalance() - amount));
accountList.get(j).setNumberOfWithdraws(+1);
BankLogic bankLogic = new BankLogic();
bankLogic.updateTransactions(transactions);
return true;
}
}
}
return false;
}
}
如果您在 getBalance/getInterest 时得到空值,您一定还没有初始化它们。我建议跟踪您的代码并使用 debugger/print 语句来查找您何时 assign/update 这些值,因为您的继承结构似乎是正确的。
我完全迷失了,我无法恢复或克服这个问题。
我想要做的是拥有一个银行应用程序,我应该在其中拥有应该从帐户 class 继承的 SavingsAccount 和 CreditAccount。当我的 subclasses 尝试 getBalance/getIntereset 时,我没有得到正确的值,而是有时得到 Null。
你能帮我指出问题并看看需要解决什么问题才能使我的 withdraw() 和 deleteCustomer() 方法正常工作吗?
非常感谢!
逻辑class
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class BankLogic {
//Deklarera global lista med kunder
private ArrayList<Customer> customers = new ArrayList<>();
private ArrayList<Transaction> transactions = new ArrayList<>();
Date date;
public ArrayList<String> getAllCustomers() {
ArrayList<String> customersStr = new ArrayList<>();
if (customers == null) {
customersStr.add("");
return customersStr;
} else
for (Customer customer : customers) {
customersStr.add(customer.getpNo() + " " + customer.getName() + " " + customer.getSurname());
}
return customersStr;
}
public boolean createCustomer(String name, String surname, String pNo) {
try {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
return false;
}
}
if (!name.equals("") || !surname.equals("")) {
Customer customer = new Customer(name, surname, pNo);
customers.add(customer);
return true;
}
} catch (Exception e) {
System.out.println(e);
}
return false;
}
public ArrayList<String> getCustomer(String pNo) {
ArrayList<String> presentCustomerArr = new ArrayList<>();
try {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
presentCustomerArr.add(customers.get(i).getpNo() + " " + customers.get(i).getName() + " " + customers.get(i).getSurname());
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
if (customers.get(i).getCustomerAccounts() != null) {
for (int j = 0; j < accountList.size(); j++) {
presentCustomerArr.add(accountList.get(j).getAccountNumber() + " " + stringFormatted(accountList.get(j).getBalance()) + " " + accountList.get(j).getAccountType() + " " + accountList.get(j).getInterestRate() + " %");
}
}
return presentCustomerArr;
}
}
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public boolean changeCustomerName(String name, String surname, String pNo) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
if (!name.equals("") && !surname.equals("")) {
customers.get(i).setName(name);
customers.get(i).setSurname(surname);
return true;
}
if (name.equals("") && !surname.equals("")) {
customers.get(i).setName(customers.get(i).getName());
customers.get(i).setSurname(surname);
return true;
}
if (!name.equals("") && surname.equals("")) {
customers.get(i).setName(name);
customers.get(i).setSurname(customers.get(i).getSurname());
return true;
}
}
}
return false;
}
public int createSavingsAccount(String pNo) {
try {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
ArrayList<Account> accountList = new ArrayList<>();
if (customers.get(i).getCustomerAccounts() != null) {
accountList = customers.get(i).getCustomerAccounts();
}
SavingsAccount savingsAccount = new SavingsAccount();
accountList.add(savingsAccount);
customers.get(i).setCustomerAccounts(accountList);
int accNr = -1;
for (int j = 0; j < accountList.size(); j++) {
accNr = savingsAccount.getAccountNumber();
}
return accNr;
}
}
} catch (Exception e) {
}
return -1;
}
public String getAccount(String pNo, int accountId) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
if (customers.get(i).getCustomerAccounts() != null) {
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountNumber() == accountId) {
String accPresentation = accountList.get(j).getAccountNumber() + " " + stringFormatted(accountList.get(j).getBalance()) + " " + accountList.get(j).getAccountType() + " " + accountList.get(j).getInterestRate() + " %";
return accPresentation;
}
}
}
}
}
return null;
}
public boolean deposit(String pNo, int accountId, int amount) {
if (amount > 0) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
if (customers.get(i).getCustomerAccounts() != null || customers.get(i).getCustomerAccounts() != null) {
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountNumber() == accountId) {
accountList.get(j).setBalance(Double.valueOf(accountList.get(j).getBalance() + amount));
transactions = addTransaction(new Date(), amount, accountList.get(j).getBalance(), accountList.get(j).getAccountNumber(), false);
customers.get(j).setAccountTransactions(transactions);
return true;
}
}
}
}
}
}
return false;
}
private ArrayList<Transaction> addTransaction(Date date, int amount, Double balance, int accountnumber, boolean isAWithdrawal) {
Transaction transaction = new Transaction(new Date(), (double) amount, balance, accountnumber, isAWithdrawal);
transactions.add(transaction);
return transactions;
}
public boolean withdraw(String pNo, int accountId, int amount) {
Boolean isUpdated = false;
if (amount > 0) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
if (customers.get(i).getCustomerAccounts() != null) {
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountNumber() == accountId) {
isUpdated = accountList.get(j).withdraw(accountList, amount, customers, transactions);
if (isUpdated){
return false;
}
//transactions = accountList.get(j).addTransaction(transactions, date, amount, Double.valueOf(accountList.get(j).getBalance()), accountList.get(j).getAccountNumber(), true);
customers.get(j).setAccountTransactions(transactions);
return true;
}
}
}
}
}
}
return false;
}
public void updateTransactions(ArrayList<Transaction> transactions) {
this.transactions = transactions;
}
private boolean balanceAfterWithdrawIsOk(int amount, Double balance) {
if (balance - amount < -5000) {
return false;
} else return true;
}
public String closeAccount(String pNr, int accountId) {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNr)) {
if (customers.get(i).getCustomerAccounts() != null) {
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountNumber() == accountId) {
String closedAcc = accountList.get(j).calculateClosedAccountBalance(customers.get(i).getCustomerAccounts().get(j));
customers.get(i).getCustomerAccounts().remove(accountList.get(j));
return closedAcc;
}
}
}
}
}
return null;
}
public ArrayList<String> deleteCustomer(String pNo) {
ArrayList<String> deletedCustomerArr = new ArrayList<>();
try {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
int accNr = -1;
deletedCustomerArr.add(customers.get(i).getpNo() + " " + customers.get(i).getName() + " " + customers.get(i).getSurname());
ArrayList<Account> accountList = customers.get(i).getCustomerAccounts();
if (customers.get(i).getCustomerAccounts() != null) {
for (int j = 0; j < accountList.size(); j++) {
//String hej = stringFormatted(accountList.get(j).getInterest());
try {
String addString = accountList.get(j).getAccountNumber() + " " + stringFormatted(accountList.get(j).getBalance()) + " " + accountList.get(j).getAccountType() + " " + stringFormatted(accountList.get(j).getInterest());
deletedCustomerArr.add(addString);
}
catch (Exception e) {
System.out.println(e);
}
}
accountList.clear();
}
customers.remove(i);
return deletedCustomerArr;
}
}
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public int createCreditAccount(String pNo) {
try {
for (int i = 0; i < customers.size(); i++) {
if (customers.get(i).getpNo().equals(pNo)) {
ArrayList<Account> accountList = new ArrayList<>();
if (customers.get(i).getCustomerAccounts() != null) {
accountList = customers.get(i).getCustomerAccounts();
}
for (int j = 0; j < accountList.size(); j++) {
}
CreditAccount creditAccount = new CreditAccount((double) 0, 0.5, "Kreditkonto");
accountList.add(creditAccount);
customers.get(i).setCustomerAccounts(accountList);
int accNr = -1;
for (int j = 0; j < accountList.size(); j++) {
accNr = creditAccount.getAccountNumber();
}
return accNr;
}
}
} catch (Exception e) {
}
return -1;
}
public ArrayList<String> getTransactions(String pNo, int accountId) {
ArrayList<String> arrList = new ArrayList<>();
try {
for (Customer customer : customers) {
if (customer.getpNo() == pNo) {
for (Transaction transaction : customer.getAccountTransactions()) {
if (accountId == transaction.getAccountNumber()) {
transactions.get(this.transactions.indexOf(transaction));
arrList.add(simpleDateFormatter(transactions.get(this.transactions.indexOf(transaction)).getDateAndTime()) + " " + (transactions.get(this.transactions.indexOf(transaction)).isAWithdrawal() == true ? "−" : "").concat(stringFormatted(transactions.get(this.transactions.indexOf(transaction)).getAmount())) + " Saldo: " + stringFormatted(transactions.get(this.transactions.indexOf(transaction)).getBalanceAfterTransaction()));
}
}
}
}
} catch (NullPointerException e) {
for (Customer customer : customers) {
for (int i = 0; i < customer.getCustomerAccounts().size(); i++) {
if (customer.getCustomerAccounts().get(i).getAccountNumber() == accountId && customer.getAccountTransactions() == null) {
return arrList;
}
}
if (customer.getAccountTransactions() == null) {
return null;
}
}
return null;
}
return arrList;
}
// Custom methods
private String stringFormatted(Double balance) {
balance = Double.valueOf(balance);
String balanceStr = NumberFormat.getCurrencyInstance().format(balance);
return balanceStr;
}
private String simpleDateFormatter(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(date);
return strDate;
}
}
帐号class
import java.util.ArrayList;
import java.util.Date;
public abstract class Account {
private Double balance;
private Double interestRate;
private Double interest;
private int accountNumber;
private String accountType;
private int numberOfWithdraws = 0;
private static int lastAssignedNumber = 1000;
public Account() {
lastAssignedNumber++;
this.balance = Double.valueOf(0);
this.accountNumber = lastAssignedNumber;
}
public Account(Double balance, Double interestRate, String accountType, int lastAssignedNumber) {
}
public Account(Double balance, Double interestRate, String accountType) {
lastAssignedNumber++;
this.accountNumber = lastAssignedNumber;
this.balance = balance;
this.interestRate = interestRate;
this.accountType = accountType;
}
public Account(Double balance, Double interestRate, Double interest, int accountNumber, String accountType, int numberOfWithdraws) {
this.balance = balance;
this.interestRate = interestRate;
this.interest = interest;
this.accountNumber = accountNumber;
this.accountType = accountType;
this.numberOfWithdraws = numberOfWithdraws;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
public Double getInterestRate() {
return interestRate;
}
public void setInterestRate(Double interestRate) {
this.interestRate = interestRate;
}
public Double getInterest() {
return interest;
}
public int getAccountNumber() {
return accountNumber;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public int getNumberOfWithdraws() {
return numberOfWithdraws;
}
public void setNumberOfWithdraws(int numberOfWithdraws) {
this.numberOfWithdraws = numberOfWithdraws;
}
/
public abstract String calculateClosedAccountBalance(Account customerAccounts);
public ArrayList<Transaction> addTransaction(ArrayList<Transaction> transactions, Date date, int amount, Double balance, int accountnumber, boolean isAWithdrawal) {
Transaction transaction = new Transaction(new Date(), (double) amount, balance, accountnumber, isAWithdrawal);
transactions.add(transaction);
return transactions;
}
public abstract boolean withdraw(ArrayList<Account> customerAccounts, int amount, ArrayList<Customer> customers, ArrayList<Transaction> transactions);
public abstract String stringFormatted(Double balance);
}
信用账户
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
public class CreditAccount extends Account {
private String accountType = "Kreditkonto";
private Double interestRate;
private Double creditLimit = Double.valueOf(5000);
private Double interest;
private Double balance;
public CreditAccount(String accountType, Double interestRate, Double creditLimit) {
this.accountType = accountType;
this.interestRate = interestRate;
this.creditLimit = creditLimit;
}
public CreditAccount(Double balance, Double interestRate, String accountType, int lastAssignedNumber, String accountType1, Double interestRate1, Double creditLimit) {
super(balance, interestRate, accountType, lastAssignedNumber);
this.accountType = accountType1;
this.interestRate = interestRate1;
this.creditLimit = creditLimit;
}
public CreditAccount(Double balance, Double interestRate, String accountType, String accountType1, Double interestRate1, Double creditLimit) {
super(balance, interestRate, accountType);
this.accountType = accountType1;
this.interestRate = interestRate1;
this.creditLimit = creditLimit;
}
public CreditAccount(Double balance, Double interestRate, String accountType) {
super(balance, interestRate, accountType);
}
public CreditAccount(Double balance, Double interestRate, Double interest, int accountNumber, String accountType, int numberOfWithdraws) {
super(balance, interestRate, interest, accountNumber, accountType, numberOfWithdraws);
}
public CreditAccount() {
}
public CreditAccount(Double balance, Double interestRate, String accountType, int lastAssignedNumber) {
super(balance, interestRate, accountType, lastAssignedNumber);
}
public Double getCreditLimit() {
return creditLimit;
}
@Override
public String calculateClosedAccountBalance(Account customerAccounts) {
String closedAccStr = "";
closedAccStr = customerAccounts.getAccountNumber() + " " + stringFormatted(customerAccounts.getBalance()) + " " + customerAccounts.getAccountType() + " " + stringFormatted(customerAccounts.getInterest());
return closedAccStr;
}
@Override
public String stringFormatted(Double balance) {
String balanceStr = NumberFormat.getCurrencyInstance().format(balance);
return balanceStr;
}
@Override
public String getAccountType() {
return accountType;
}
@Override
public Double getInterestRate() {
if (this.getBalance() < 0) {
this.interestRate = Double.valueOf(7);
} else this.interestRate = Double.valueOf(0.5);
return interestRate;
}
@Override
public Double getInterest() {
if (getBalance() < 0) {
interest = getBalance() * -7 / 100;
}
return interest;
}
@Override
public boolean withdraw(ArrayList<Account> accountList, int amount, ArrayList<Customer> customers, ArrayList<Transaction> transactions) {
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountType().equals("Kreditkonto")) {
if (balanceAfterWithdrawIsOk(amount, accountList.get(j).getBalance())) {
accountList.get(j).setBalance(Double.valueOf(accountList.get(j).getBalance() + (-amount)));
if (accountList.get(j).getBalance() < 0) {
accountList.get(j).setInterestRate(Double.valueOf(7));
}
transactions = addTransaction(transactions, new Date(), amount, Double.valueOf(accountList.get(j).getBalance()), accountList.get(j).getAccountNumber(), true);
BankLogic bankLogic = new BankLogic();
bankLogic.updateTransactions(transactions);
customers.get(j).setAccountTransactions(transactions);
return true;
}
}
}
return false;
}
private boolean balanceAfterWithdrawIsOk(int amount, Double balance) {
if (balance - amount < -5000) {
return false;
} else return true;
}
}
储蓄账户
import java.text.NumberFormat;
import java.util.ArrayList;
public class SavingsAccount extends Account {
private String accountType = "Sparkonto";
private Double interestRate = 1.2;
private Double withdrawalFeePercentage = Double.valueOf(2);
private Double interest;
public SavingsAccount(String accountType, Double interestRate, Double withdrawalFeePercentage) {
this.accountType = accountType;
this.interestRate = interestRate;
this.withdrawalFeePercentage = withdrawalFeePercentage;
}
public SavingsAccount(Double balance, Double interestRate, String accountType, int lastAssignedNumber, String accountType1, Double interestRate1, Double withdrawalFeePercentage) {
super(balance, interestRate, accountType, lastAssignedNumber);
this.accountType = accountType1;
this.interestRate = interestRate1;
this.withdrawalFeePercentage = withdrawalFeePercentage;
}
public SavingsAccount(Double balance, Double interestRate, String accountType, String accountType1, Double interestRate1, Double withdrawalFeePercentage) {
super(balance, interestRate, accountType);
this.accountType = accountType1;
this.interestRate = interestRate1;
this.withdrawalFeePercentage = withdrawalFeePercentage;
}
public SavingsAccount(Double balance, Double interestRate, String accountType, int accountNumber, int numberOfWithdraws, double withdrawalFeePercentage) {
super(balance, interestRate, accountType, accountNumber);
this.withdrawalFeePercentage = Double.valueOf(2);
}
public SavingsAccount() {
}
@Override
public String calculateClosedAccountBalance(Account customerAccounts) {
String closedAccStr = "";
closedAccStr = customerAccounts.getAccountNumber() + " " + stringFormatted(customerAccounts.getBalance()) + " " + customerAccounts.getAccountType() + " " + stringFormatted(customerAccounts.getInterest());
return closedAccStr;
}
@Override
public String stringFormatted(Double balance) {
String balanceStr = NumberFormat.getCurrencyInstance().format(balance);
return balanceStr;
}
public Double getWithdrawalFeePercentage() {
return withdrawalFeePercentage;
}
@Override
public String getAccountType() {
return accountType;
}
@Override
public Double getInterestRate() {
return interestRate;
}
@Override
public Double getInterest() {
interest = getBalance() * 1.2 / 100;
return interest;
}
@Override
public boolean withdraw(ArrayList<Account> accountList, int amount, ArrayList<Customer> customers, ArrayList<Transaction> transactions) {
for (int j = 0; j < accountList.size(); j++) {
if (accountList.get(j).getAccountType().equals("Sparkonto")) {
//accountList.get(j).withdrawMoney();
if (accountList.get(j).getNumberOfWithdraws() > 0) {
amount = (int) (amount * 1.02);
}
if (accountList.get(j).getBalance() >= amount) {
accountList.get(j).setBalance(Double.valueOf(accountList.get(j).getBalance() - amount));
accountList.get(j).setNumberOfWithdraws(+1);
BankLogic bankLogic = new BankLogic();
bankLogic.updateTransactions(transactions);
return true;
}
}
}
return false;
}
}
如果您在 getBalance/getInterest 时得到空值,您一定还没有初始化它们。我建议跟踪您的代码并使用 debugger/print 语句来查找您何时 assign/update 这些值,因为您的继承结构似乎是正确的。