****in java
Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define three methods, Deposit which takes an amount and adds it to the balance, Withdrawal which takes an amount and subtracts it from the balance, and Compound, which multiplies the balance by the interestRate and adds that amount to the balance. Write an application which shows the user the following menu and implements all options. The program should continue to run and process requests until the user selects 8. The program should double-check that the user really wants to exit. You may use a limit of 10 possible accounts to simplify implementation. The program should assign new account numbers to assure their uniqueness. All input must be validated and appropriate feedback given for invalid input.
1 – Enter a new account2 – Delete individual account3 – Deposit to individual account4 – Withdrawal from individual account5 – Compound all accounts6 – Display all accounts7 – Set interest rate8 – Exit program
CheckingAccount.java
public class CheckingAccount {
private static double interestRate;
private String firstName, lastName;
private int ID;
private double balance;
public CheckingAccount()
{
this.ID = 0;
this.firstName = this.lastName = "";
this.balance = 0.0;
interestRate = 0.0;
}
public CheckingAccount(String firstName, String lastName, int
ID, double balance) {
setFirstName(firstName);
setLastName(lastName);
setID(ID);
setBalance(balance);
}
public static double getInterestRate() {
return interestRate;
}
public static void setInterestRate(double interestRate) {
if(interestRate < 0)
System.out.println("Interest rate cannot be negative!");
else
CheckingAccount.interestRate = interestRate;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getID() {
return ID;
}
public void setID(int ID) {
if(ID > 0)
this.ID = ID;
else
ID = 0;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
if(balance < 0)
System.out.println("Balance cannot be negative!");
else
this.balance = balance;
}
public void Deposit(double amt)
{
if(amt < 0)
System.out.println("Deposit amount cannot be negative!");
else
setBalance(getBalance() + amt);
}
public void Withdraw(double amt)
{
if(amt < 0)
System.out.println("Withdrawal amount cannot be negative!");
else if(amt > getBalance())
System.out.println("Transaction failed: Insufficient
balance!");
else
setBalance(getBalance() - amt);
}
public void Compound()
{
setBalance(getBalance() + (getInterestRate() *
getBalance()));
}
@Override
public String toString()
{
return("ID: " + getID() + ", Name: " + getFirstName() + " " +
getLastName() + ", Balance: $" + String.format("%,.2f",
getBalance()));
}
}
CheckingAccountTest.java (Main class)
import java.util.ArrayList;
import java.util.Scanner;
public class CheckingAccountTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<CheckingAccount> accounts = new
ArrayList<>();
int choice;
do
{
displayMenu();
choice = Integer.parseInt(sc.nextLine().trim());
switch(choice)
{
case 1:
{
System.out.print("\nEnter account ID: ");
int ID = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter holder first name: ");
String firstName = sc.nextLine().trim();
System.out.print("Enter holder last name: ");
String lastName = sc.nextLine().trim();
System.out.print("Enter initial balance: $");
double balance = Double.parseDouble(sc.nextLine().trim());
accounts.add(new CheckingAccount(firstName, lastName, ID,
balance));
System.out.println("Account for " + firstName + " " + lastName + "
has been successfully created.\n");
break;
}
case 2:
{
if(accounts.isEmpty())
{
System.out.println("No accounts added still now!\n");
break;
}
System.out.print("\nEnter the account ID to delete: ");
int ID = Integer.parseInt(sc.nextLine().trim());
int index = getIndex(accounts, ID);
if(index == -1)
System.out.println("No account exist with ID " + ID + "!\n");
else
{
String name = accounts.get(index).getFirstName() + " " +
accounts.get(index).getLastName();
accounts.remove(index);
System.out.println("Account for " + name + " has been deleted
successfully.\n");
}
break;
}
case 3:
{
if(accounts.isEmpty())
{
System.out.println("No accounts added still now!\n");
break;
}
System.out.print("\nEnter the account ID to deposit: ");
int ID = Integer.parseInt(sc.nextLine().trim());
int index = getIndex(accounts, ID);
if(index == -1)
System.out.println("No account exist with ID " + ID + "!\n");
else
{
System.out.print("Enter the amount to deposit: $");
double amt = Double.parseDouble(sc.nextLine().trim());
accounts.get(index).Deposit(amt);
System.out.println("Transaction successful. Current account
balance: $" + String.format("%,.2f.\n",
accounts.get(index).getBalance()));
}
break;
}
case 4:
{
if(accounts.isEmpty())
{
System.out.println("No accounts added still now!\n");
break;
}
System.out.print("\nEnter the account ID to withdraw: ");
int ID = Integer.parseInt(sc.nextLine().trim());
int index = getIndex(accounts, ID);
if(index == -1)
System.out.println("No account exist with ID " + ID + "!\n");
else
{
System.out.print("Enter the amount to withdraw: $");
double amt = Double.parseDouble(sc.nextLine().trim());
accounts.get(index).Withdraw(amt);
System.out.println("Transaction successful. Current account
balance: $" + String.format("%,.2f.\n",
accounts.get(index).getBalance()));
}
break;
}
case 5:
{
if(accounts.isEmpty())
{
System.out.println("No accounts added still now!\n");
break;
}
for(CheckingAccount ch : accounts)
ch.Compound();
System.out.println("All accounts have been compounded
successfully!\n");
break;
}
case 6:
{
if(accounts.isEmpty())
{
System.out.println("No accounts added still now!\n");
break;
}
System.out.println("\nNumber of accounts added till now = " +
accounts.size() + ".\n");
System.out.println("***** ALL ACCOUNTS *****");
for(CheckingAccount ch : accounts)
System.out.println(ch);
break;
}
case 7:
{
if(accounts.isEmpty())
{
System.out.println("No accounts added still now!\n");
break;
}
System.out.print("\nEnter the interest rate (enter 0.05 in case of
5%): ");
double interestRate =
Double.parseDouble(sc.nextLine().trim());
while(interestRate < 0)
{
System.out.println("Interest rate cannot be negative!");
System.out.print("Enter the interest rate (enter 0.05 in case of
5%): ");
interestRate = Double.parseDouble(sc.nextLine().trim());
}
double prevInt = CheckingAccount.getInterestRate();
CheckingAccount.setInterestRate(interestRate);
System.out.println("Interest rate has been revised from " +
String.format("%.2f", prevInt) + " to " + String.format("%.2f.\n",
interestRate));
break;
}
case 8:
{
System.out.println("Thanks..Goodbye!\n");
System.exit(0);
}
default:
System.out.println("\nInvalid selection!\n");
}
}while(choice != 8);
}
private static void displayMenu()
{
System.out.print("Choose from the following options:\n"
+ "1. Enter a new account\n"
+ "2. Delete individual account\n"
+ "3. Deposit to individual account\n"
+ "4. Withdrawal from individual account\n"
+ "5. Compound all accounts\n"
+ "6. Display all accounts\n"
+ "7. Set interest rate\n"
+ "8. Exit\n"
+ "Your selection >> ");
}
private static int getIndex(ArrayList<CheckingAccount> accs,
int id)
{
int index = -1;
for(int i = 0; i < accs.size(); i++)
{
if(accs.get(i).getID() == id)
{
index = i;
break;
}
}
return index;
}
}
******************************************************* SCREENSHOT *********************************************************
Get Answers For Free
Most questions answered within 1 hours.