In: Computer Science
****in java please***
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
outline below:
import java.util.Scanner;
public class Lab6Shell
{
public static void main(String[] args)
{
// data
Scanner input = new
Scanner(System.in);
CheckingAccount [] accounts = new
CheckingAccount [10];
boolean exit = false;
int selection = 0;
int nextIDNumber = 100000;
int accountNumber = 0;
String firstName;
String lastName;
double balance;
double amount;
boolean found = false;
// ALGORITHM
// loop until user exits
do
{
// display
options
// get selection (validate)
// switch on
selection
switch(selection)
{
case 1:
// get name from user
// get balance from user (validate)
// set found to false
// loop through array looking for empty
spot
for (int i = 0; i < accounts.length;
i++)
{
if (accounts[i] ==
null)
{
// create new CheckingAccount
and assign to current array element
// set found to true
// break
out of loop
}
}
// if not found, give error message
// break out of switch statement
case 2:
// get id number to delete
// set found to false
// loop through array looking for this
account
for (int i = 0; i < accounts.length;
i++)
{
if (accounts[i] != null
&& accounts[i].GetAccountNumber() == accountNumber)
{
// delete
object
// set found to true
// break
out of loop
}
}
// if not found, give error message
// break out of switch statement
case 3:
// get id number to deposit to
// get amount to deposit (validate)
// set found to false
// loop through array looking for this
account
// call
deposit method
// set
found to true
// break
out of loop
// if not found, give error message
// break out of switch statement
case 4:
// get id number to withdraw from
// get amount to withdraw (validate)
// set found to false
// loop through array looking for this
account
// call
withdraw method if sufficient funds
// set
found to true
// if not found, give error message
// break out of switch statement
case 5:
// loop through array looking for valid
objects
// call
compoundMonthly method
// break out of switch statement
case 6:
// loop through array looking for valid objects
and display all info about each object
// break out of switch statement
case 7:
// get new interest rate
(validate)
// set interest rate
// break out of switch statement
case 8:
// confirm user wants to exit
// set variable to break out
of loop
// break out of switch statement
} // end stitch
} while (!exit);// End
loop
}
}
class CheckingAccount
{
// declare static variable
// declare instance variables
// one constructor
// get/set methods
If you have any problem with the code feel free to comment.
Lab6Shell
import java.util.Scanner;
public class Lab6Shell{
public static void main(String[] args) {
// data
Scanner input = new Scanner(System.in);
CheckingAccount[] accounts = new CheckingAccount[10];
boolean exit = false;
int selection = 0;
int nextIDNumber = 100000;
int accountNumber = 0;
String firstName;
String lastName;
double balance;
double amount;
boolean found = false;
// ALGORITHM
// loop until user exits
do {
// display options
System.out.println(
"1 – Enter a new account"
+ "\n2 – Delete individual account"
+ "\n3 – Deposit to individual account"
+ "\n4 – Withdrawal from individual account"
+ "\n5 – Compound all accounts"
+ "\n6 – Display all accounts"
+ "\n7 – Set interest rate"
+ "\n8 – Exit");
// get selection (validate)
selection = input.nextInt(); input.nextLine();
// switch on selection
switch (selection) {
case 1:
// get name from user
System.out.print("First name: ");
firstName = input.nextLine();
System.out.print("Last name: ");
lastName = input.nextLine();
// get balance from user (validate)
System.out.print("Balance: $");
balance = input.nextDouble(); input.nextLine();
if(balance<0) {
System.out.println("Invalid balance");
balance =0;
}
// set found to false
found = false;
// loop through array looking for empty spot
for (int i = 0; i < accounts.length; i++) {
if (accounts[i] == null) {
// create new CheckingAccount and assign to current array element
accounts[i] = new CheckingAccount(firstName, lastName, nextIDNumber++, balance);
// set found to true
found = true;
// break out of loop
break;
}
}
// if not found, give error message
if(!found)
System.out.println("No space for new account");
// break out of switch statement
break;
case 2:
// get id number to delete
System.out.print("Account Number: ");
accountNumber = input.nextInt(); input.nextLine();
// set found to false
found = false;
// loop through array looking for this account
for (int i = 0; i < accounts.length; i++) {
if (accounts[i] != null && accounts[i].getId() == accountNumber) {
// delete object
accounts[i] = null;
// set found to true
found = true;
// break out of loop
break;
}
}
// if not found, give error message
if(!found)
System.out.println("Account not found!");
// break out of switch statement
break;
case 3:
// get id number to deposit to
System.out.print("Account Number: ");
accountNumber = input.nextInt(); input.nextLine();
// get amount to deposit (validate)
System.out.print("Amount: $");
amount = input.nextDouble(); input.nextLine();
if(amount<0) {
System.out.println("Invalid amount!");
amount=0;
}
// set found to false
found = false;
// loop through array looking for this account
for (int i = 0; i < accounts.length; i++) {
if (accounts[i] != null && accounts[i].getId() == accountNumber) {
// call deposit method
accounts[i].deposit(amount);
// set found to true
found = true;
// break out of loop
break;
}
}
// if not found, give error message
if(!found)
System.out.println("Account not found!");
// break out of switch statement
break;
case 4:
// get id number to withdraw from
System.out.print("Account Number: ");
accountNumber = input.nextInt(); input.nextLine();
// get amount to withdraw (validate)
System.out.print("Amount: $");
amount = input.nextDouble(); input.nextLine();
if(amount<0) {
System.out.println("Invalid amount!");
amount=0;
}
// set found to false
found = false;
// loop through array looking for this account
for (int i = 0; i < accounts.length; i++) {
if (accounts[i] != null && accounts[i].getId() == accountNumber) {
if(amount> accounts[i].getBalance()) {
System.out.println("Insufficient balance!");
found = true;
break;
}
// call withdraw method if sufficient funds
accounts[i].withdraw(amount);
// set found to true
found = true;
}
}
// if not found, give error message
if(!found)
System.out.println("Account not found!");
// break out of switch statement
break;
case 5:
// loop through array looking for valid objects
for (int i = 0; i < accounts.length; i++) {
if(accounts[i] != null) {
// call compoundMonthly method
accounts[i].compound();
}
}
// break out of switch statement
break;
case 6:
// loop through array looking for valid objects and display all info about each
for (int i = 0; i < accounts.length; i++) {
if(accounts[i] != null) {
// object
System.out.println("Name: "+accounts[i].getFirstName()+" "+accounts[i].getLastName());
System.out.println("Account ID: "+accounts[i].getId());
System.out.println("Balance: $"+accounts[i].getBalance());
System.out.println();
}
}
// break out of switch statement
break;
case 7:
// get new interest rate (validate)
System.out.print("Interest Rate(in decimal): ");
double rate = input.nextDouble(); input.nextLine();
if(rate<0) {
System.out.println("Invalid Rate!");
rate = 0;
}
// set interest rate
CheckingAccount.setInterestRate(rate);
// break out of switch statement
break;
case 8:
// confirm user wants to exit
System.out.print("Do you want to exit(y/n)? ");
char ch = input.nextLine().charAt(0);
if(ch == 'y' || ch == 'Y') {
// set variable to break out of loop
exit = true;
}
// break out of switch statement
break;
} // end stitch
} while (!exit);// End loop
}
}
CheckingAccount
class CheckingAccount {
private static double interestRate;
private String firstName;
private String lastName;
private int id;
private double balance;
public CheckingAccount(String firstName, String lastName, int id, double balance) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
setBalance(balance);
}
public static double getInterestRate() {
return interestRate;
}
public static void setInterestRate(double interestRate) {
if (interestRate < 0)
CheckingAccount.interestRate = 0;
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) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
if (balance < 0)
this.balance = 0;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
public void compound() {
double amount = balance * interestRate;
balance += amount;
}
}
Output