Question

In: Computer Science

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

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

Solutions

Expert Solution

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


Related Solutions

Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a method called Washing. The Washing method should return a string to the main class with the text "Now washing dishes!" In the main class create an array of DishWasher size 3. Give each DishWasher a different color and CubicFeet. Use a foreach loop to display that info, call the Washing method, and display the text returned from the Washing method call. c#
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named...
Write a class called Animal that contains a static variable called count to keep track of...
Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Using maps in Java. Make a public class called ExampleOne that provides a single class (static)...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static) method named firstOne. firstOne accepts a String array and returns a map from Strings to Integer. The map must count the number of passed Strings based on the FIRST letter. For example, with the String array {“banana”, “apples”, “blueberry”, “orange”}, the map should return {“b”:2, “a”:1, “o”:1}. Disregard empty Strings and zero counts. Retrieve first character of String as a char using charAt, or,...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT