I have this Java Project and it is only done, but I have a few problems. First h
ID: 665556 • Letter: I
Question
I have this Java Project and it is only done, but I have a few problems. First here is the assignment:
Implement the object model shown in Figure 1-25 to implement a small banking application. Create five classes: Bank, BankAccount, SavingsAccount,CheckingAccount, and Customer.Bank Should be a singleton class. For simplicity, the bank stores bank accounts in one array and customers in another. All bank accounts are savings accounts or checking accounts, and any customer may have zero or one account of each type.
The difference between the two accounts affects only the withdraw method. Thedeposit method simply increments the balance. The withdraw method decrements the balance, but cannot always do so. A customer can withdraw from aSavingsAccount only an amount less than or equal to the current balance. For aCheckingAccount the logic is:
Withdraw the amount if less than or equal to the balance.
If the customer has a SavingsAccount, and the SavingsAccount has enough money to cover the shortfall in the CheckingAccount, transfer enough money from saving to checking to cover the withdrawal. Then withdraw the money.
If the customer has no savings account, or the total of both accounts is less than the withdrawal amount, issue an InsufficientFundsException exception(just print message if Exception has not been covered).
Here is my code:
Bank.java
public class Bank
{
private ArrayList<BankAccount> accountList=new ArrayList<BankAccount>();
private ArrayList<Customer> customerList=new ArrayList<Customer>();
public void addCustomer(Customer cus)
{
customerList.add(cus);
}
public void addAccount(BankAccount acc)
{
accountList.add(acc);
}
public static void main(String[] args)
{
SavingsAccount savings = new SavingsAccount(1, "Bob", 2000);
CheckingAccount checking = new CheckingAccount(1, "Bob", 2000);
Customer customer1 = new Customer(1, savings);
Customer customer2 = new Customer(1, checking);
Bank newcust = new Bank();
newcust.addCustomer(customer1);
newcust.addCustomer(customer2);
customer1.addSavingsAccount().withdraw(1000);
customer1.addSavingsAccount().displayAmount();
customer1.addSavingsAccount().deposit(500);
customer1.addSavingsAccount().displayAmount();
customer1.addSavingsAccount().withdraw(1000);
customer1.addSavingsAccount().displayAmount();
customer2.addCheckingAccount().withdraw(1000);
customer2.addCheckingAccount().displayAmount();
customer2.addCheckingAccount().deposit(1000);
customer2.addCheckingAccount().displayAmount();
customer2.addCheckingAccount().withdraw(1000);
BankAccount.java
public abstract class BankAccount
{
private int accountNo;
private String owner;
double balance;
public BankAccount(int accountnum, String own, double bal)
{
accountNo = accountnum;
owner = own;
balance = bal;
}
public void deposit(int a)
{
balance = a + balance;
}
public abstract void withdraw(int a);
}
}
}
CheckingAccount.java
public class CheckingAccount extends BankAccount
{
public CheckingAccount(int accountNo, String owner, double balance)
{
super(accountNo, owner, balance);
}
public void withdraw(int a, SavingsAccount f)
{
if(a <= balance)
{
balance = balance - a;
}
else if(a >= balance && f.equals(true) && f.balance >= balance - a )
{
balance = 0;
f.balance = f.balance + (balance - a);
}
else
System.out.println("InsufficientFundsException" );
}
public void displayAmount()
{
System.out.println("The checking balance is: " + balance);
}
@Override
public void withdraw(int a) {
}
}
SavingsAccount.java
public class SavingsAccount extends BankAccount
{
public SavingsAccount(int accountNo, String owner, double balance)
{
super(accountNo, owner, balance);
}
public void withdraw(int a)
{
if(a<=balance)
{
balance=balance-a;
}
else
System.out.println("InsufficientFundException");
}
public void displayAmount()
{
System.out.println("The savings balance is: "+balance);
}
}
Customer.java
public class Customer
{
private int id;
private SavingsAccount savingsAcct;
private CheckingAccount checkingAcct;
public Customer(int i, SavingsAccount savingsAcc)
{
id = i;
savingsAcct = savingsAcc;
}
public Customer(int i, CheckingAccount checkingAcc)
{
id = i;
checkingAcct = checkingAcc;
}
public void setSavingAccount(SavingsAccount saving)
{
savingsAcct = saving;
}
public void setCheckingAccount(CheckingAccount checking)
{
checkingAcct = checking;
}
public SavingsAccount addSavingsAccount()
{
return savingsAcct;
}
public CheckingAccount addCheckingAccount()
{
return checkingAcct;
}
}
My problem, is that my Bank class is not singleton and I can't seem to make a customer have both a checking and savings account. I just need to fix my program to fit the criteria of the instructions. Please do not just correct small mistakes in my code, what I need help with is how to make my Bank class a singleton class, and fit the requirement of the assignment.
Explanation / Answer
First Method :
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;
}
}
Account class:
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 class: (CreditAccount class is similar)
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;
}
}
Main:
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() + " ");
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);
}
public Account() {
}
public void setID(int i) {
id = i;
}
public int getID() {
return id;
}
public void withdraw(double amount)
{
if (balance >= amount)
{
balance -= amount;
}
else
{
System.out.println("Insufficient funds");
}
}
/ Method that adds deposit amount to balance.
public void deposit(double amount)
{
balance += amount;
}
public double getBalance()
{
return balance;
}
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
Second method :
public class BankAccount {
String firstName;
String lastName;
String ssn;
protected float balance;
float withdraw;
float deposit;
long accountNumber;
BankAccount (){
}
BankAccount(String firstName, String lastName, String ssn, float balance){
this.firstName = firstName;
this.lastName = lastName;
this.ssn = ssn;
this.balance = balance;
}
long accountNumber() {
long accountNumber = (long) Math.floor(Math.random() * 9000000000L) + 1000000000L;
return accountNumber;
}
public void deposit(float amount) {
balance = balance + amount;
System.out.println(firstName + " " + lastName + " deposited $" + deposit + ". Current Balance $" + balance);
}
public void withdraw(float amount) {
if (balance >= withdraw) {
balance = balance - amount;
System.out.println(firstName + " " + lastName + " withdrew $" + withdraw + ". Current Balance $" + balance);
}
if (balance < withdraw) {
System.out.println("Unable to withdraw " + amount + " for " + firstName + " " + lastName + " due to insufficient funds.");
}
}
}
//Checking Account Subclass:
public class CheckingAccount extends BankAccount {
float amtInterest;
float applyInterest;
String displayBalance;
public CheckingAccount() {
}
public CheckingAccount(String firstName, String lastName, String ssn, float balance) {
super(firstName, lastName, ssn, balance);
System.out.println("Successfully created account for " + firstName + " " + lastName + " " + accountNumber);
System.out.println(firstName + " " + lastName + ", Balance $" + balance);
}
float applyInterest () {
if (balance <= 10000) {
balance = balance * 0.1f;
}
if (balance > 10000) {
balance = 1000 + (balance * 0.02f);
}
return balance;
}
float displayBalance() {
return balance;
}
}
public class SavingsAccount extends BankAccount
{
public SavingsAccount(String string, double rate)
{
interestRate = rate;
}
public SavingsAccount(SavingsAccount yourAccount, int rate) {
}
public void addInterest()
{
double interest = getBalance() * interestRate / 100;
deposit(interest);
}
private double interestRate;
public void deposit(double amount) {}
public boolean withdraw(double amount) {
return false;}
public void deductFees() {}
private int transactionCount;
public void postInterest() {
double balance = getBalance();
balance += (balance * interestRate);
deposit(balance);
}
}
public class BankApp {
public static void main(String[] args) {
CheckingAccount acct1 = new CheckingAccount("Alin", "Parker", "123-45-6789", 1000.0f);
CheckingAccount acct2 = new CheckingAccount("Mary", "Jones", "987-65-4321", 500.0f);
SavingsAccount acct3 = new SavingsAccount("John", "Smith", "1233-45-6789", 200.0f);
acct1.deposit(22000.00f);
acct2.deposit(12000.00f);
acct1.withdraw(2000.00f);
acct2.withdraw(1000.00f);
acct1.applyInterest(); //seems to skip
acct2.applyInterest(); //seems to skip
acct1.displayBalance(); //seems to skip
acct2.displayBalance(); //seems to skip
acct1.withdraw(30000.00f);
}
}
public void withdraw(double amount)
{
if (balance >= amount)
{
balance -= amount;
}
else
{
System.out.println("Insufficient funds");
}