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 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....
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class (5 points extra credit) Create another class called CourseSection (20 points extra credit) Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of...
Create a Shape (Object) Class Create a class (Shapes) that prompts the user to select a...
Create a Shape (Object) Class Create a class (Shapes) that prompts the user to select a shape to be drawn on the screen. Objects: Circle, X, box, box with an x inside, or any other object of your choice. Depending on the user’s choice, you will then ask for the number of rows, columns or any requirements needed to draw the shape. Finally, you will display the shape selected by the user to the screen. Your class should have at...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT