JAVA Implement the object model shown in Figure 1-25 to implement a small bankin
ID: 3847428 • Letter: J
Question
JAVA
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. The deposit method simply increments the balance. The withdraw method decrements the balance, but cannot always do so. A customer can withdraw from a SavingsAccount only an amount less than or equal to the current balance. For a CheckingAccount 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.
Add a main method to the Bank class to test. Create one customer and give that customer a savings account. Try a deposit and withdrawal. Give the customer a checking account and try another deposit. Try withdrawals that succeed without a transfer, that transfer founds from savings, and for which there are insufficient funds.
BankAccount Bank account No account List has 0..n Owner customer List balance addCustomer deposit abstract withdraw add Account has Savings Account CheckingAccount withdraw withdraw 0..n Customer 0..1 0..1 savings Acct has checking Acct has add Savings Account addCheckingAccount Figure 1-25 Object model for a banking applicationExplanation / 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");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.