Question

****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...

****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

Homework Answers

Answer #1

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 *********************************************************

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to...
Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to get 4 integers from the user and store them in variables. The program should calculate the average of the 6 numbers as a floating point. Output all of the original input and the calculated average in the Command window. The code is expected to be commented and user-friendly.
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and...
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and make a java program for your bank. However, there is another type of customer that may not have money in the bank and may not in fact have a back account with this bank at all, but may have a loan here. Make another class in the application you are making called CarLoan. Carloan's should have a name, amount owed, a rate, and a...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
For Java For this assignment you will develop two classes called ATM and Customer that simulate...
For Java For this assignment you will develop two classes called ATM and Customer that simulate an imaginary automated teller machine (ATM). In the assignment, you should also develop a UML class diagram for the ATM class and a jUnit test case. In the program, we assume that an ATM initially keeps $100.00 cash for customer transactions. Additionally, we assume that there are a total of ten customers combined for the Union Bank and BOA banks. This is a list...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT