Create a client for your BankAccount classcalled BankAccountClient.java. This a separate file from the BankAccount file. Both files must be in the same directory to compile.
Display all dollar amounts using the DecimalFormat class.
BankAccount.java
public class BankAccount {
enum AccountType
{
Checking,
Savings,
MoneyMarket,
IRA,
CertificateOfDeposit;
}
private AccountType type;
private int accountNumber;
private double balance;
private double interestRate;
public BankAccount()
{
this.type = AccountType.Savings;
this.accountNumber = 0;
this.balance = 0.0;
this.interestRate = 0.0;
}
public AccountType getType() {
return type;
}
public void setType(String type) {
if(type.equalsIgnoreCase("Checking"))
this.type = AccountType.Checking;
else if(type.equalsIgnoreCase("Savings"))
this.type = AccountType.Savings;
else if(type.equalsIgnoreCase("Money Market"))
this.type = AccountType.MoneyMarket;
if(type.equalsIgnoreCase("IRA"))
this.type = AccountType.IRA;
if(type.equalsIgnoreCase("Certificate of Deposit"))
this.type = AccountType.CertificateOfDeposit;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
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((getBalance() * getInterestRate()) / 100);
}
public void deposit(double amt)
{
if(amt < 0)
System.out.println("Amount cannot be negative!");
else
setBalance(getBalance() + amt);
}
public void withdraw(double amt)
{
if(amt > getBalance())
System.out.println("Insufficient funds!");
else
setBalance(getBalance() - amt);
}
public void applyInterest()
{
deposit(getInterest());
}
public String getAccountTypeString()
{
if(this.type == AccountType.CertificateOfDeposit)
return "Certificate of Deposit";
else if(this.type == AccountType.Checking)
return "Checking";
else if(this.type == AccountType.IRA)
return "IRA";
else if(this.type == AccountType.MoneyMarket)
return "Money Market";
else if(this.type == AccountType.Savings)
return "Savings";
else
return "Savings";
}
@Override
public String toString()
{
return("Account Number: " + getAccountNumber() + "\n"
+ "Account Type: " + getAccountTypeString() + "\n"
+ "Interest Rate: " + String.format("%.2f", getInterestRate()) +
"%\n"
+ "Balance: $" + String.format("%,.2f", getBalance()));
}
@Override
public boolean equals(Object o)
{
if(o instanceof BankAccount)
{
return(getAccountNumber() == ((BankAccount) o).getAccountNumber()
&&
getAccountTypeString().equalsIgnoreCase(((BankAccount)
o).getAccountTypeString()) &&
getInterestRate() == ((BankAccount) o).getInterestRate()
&&
getBalance() == ((BankAccount) o).getBalance());
}
else
return false;
}
}
BankAccountClient.java (Main class)
import java.util.Scanner;
public class BankAccountClient {
public static void main(String[] args) {
BankAccount account = new BankAccount();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the type of account:\n"
+ "1. Savings\n"
+ "2. Checking\n"
+ "3. Money Market\n"
+ "4. IRA\n"
+ "5. Certificate of Deposit\n"
+ "Your choice: ");
int choice = Integer.parseInt(sc.nextLine().trim());
String type = "";
while(choice < 1 || choice > 5)
{
System.out.println("Invalid selection!");
System.out.print("Enter the type of account:\n"
+ "1. Savings\n"
+ "2. Checking\n"
+ "3. Money Market\n"
+ "4. IRA\n"
+ "5. Cerrtificate of Deposit\n"
+ "Your choice: ");
choice = Integer.parseInt(sc.nextLine().trim());
}
switch(choice)
{
case 1:
type = "Savings";
break;
case 2:
type = "Checking";
break;
case 3:
type = "Money Market";
break;
case 4:
type = "IRA";
break;
case 5:
type = "Certificate of Deposit";
break;
}
System.out.print("Enter the account number: ");
int accountNumber = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter the initital deposit amount: $");
double amount = Double.parseDouble(sc.nextLine().trim());
double interestRate = 0.0;
account.setType(type);
account.setAccountNumber(accountNumber);
account.setBalance(amount);
account.setInterestRate(interestRate);
System.out.println("Initial account information..\n" +
account);
System.out.println("\nUpdating account balance to 5000..");
account.setBalance(5000);
System.out.println(account);
System.out.print("\nEnter the amount of deposit: $");
double depositAmt = Double.parseDouble(sc.nextLine().trim());
account.deposit(depositAmt);
System.out.println(account);
System.out.print("\nEnter the amount to withdraw: $");
double withdrawAmt =
Double.parseDouble(sc.nextLine().trim());
account.withdraw(withdrawAmt);
System.out.println(account);
System.out.println("\nUpdating interest rate to 7.85%..");
account.setInterestRate(7.85);
account.applyInterest();
System.out.println(account);
System.out.println("\nCreating another account with the information
of the above account and comparing them..");
BankAccount dup = new BankAccount();
dup.setAccountNumber(account.getAccountNumber());
dup.setType(type);
dup.setInterestRate(account.getInterestRate());
dup.setBalance(account.getBalance());
System.out.println("Are the two accounts same? " +
account.equals(dup));
}
}
********************************************************** SCREENSHOT ******************************************************
Get Answers For Free
Most questions answered within 1 hours.