Question

In: Computer Science

Requirement: To create three classes, Account, Transaction, and the main class (with the main method). Please...

Requirement: To create three classes, Account, Transaction, and the main class (with the main method). Please closely follow the requirements below. I only need to know how to create the menu and be able to use it through out the code.

Requirements for the Transaction class: A private Date data field that stores the date of the transaction  A private char data field for the type of the transaction, such as “W” for withdrawal and “D” for deposit  A private double data field for the amount of the transaction  A private double data field for the balance of the account after the transaction  A private String data field for the description of the transaction  A constructor that creates an instance of the Transaction class with specified values of transaction type, amount, balance, and description, and date of the transaction. Note: Instead of passing a Date object to the constructor, you should use the new operator inside the body of the constructor to pass a new Date instance to the date data field. Define the corresponding accessor (get) methods to access a transaction’s date, type, amount, description, and the balance, i.e., you need to define 5 get methods.  Define a toString() method which does not receive parameters but returns a String that summarizes the transaction details, including transaction type, amount, balance after transaction, description of the transaction, and transaction date. You can organize and format the returned String in your own way, but you have to include all of the required information. Note: the purpose of the Transaction class is to describe (record) a banking transaction, i.e., when a transaction happens, you can use the transaction parameters to create an instance of the Transaction class.

Requirements for the Account class:  A private int data field for the id of the account.  A private String data field that stores the name of the customer.  A private double data field for the balance of the account.  A private double data field that stores the current annual interest rate. Key assumptions: (1) all accounts created following this class construct share the same interest rate. (2) While the annual interest rate is a percentage, e. g., 4.5%, but users will instead enter the double number as 4.5. Therefore, you need to divide the annual interest rate by 100 whenever you use it in a calculation.  A private Date data field that stores the account creating date.  A private ArrayList data field that stores a list of transactions for the account. Each element of this ArrayList has to be an instance of the Transaction class defined above.  A no-arg constructor that creates a default account with default variable values.  A constructor that creates an instance of the Account class with a specified id and initial balance.  A constructor that creates an instance of the Account class with a specified id, name, and initial balance. Note: for all constructors, instead of passing a Date object to the constructor, you should use the new operator inside the body of the constructor to pass a new Date instance to the date data field.  Define corresponding accessor (get) and mutator (set) methods to access and reset the account id, balance, and interest rate, i.e., you need to define 3 get methods and 3 set methods. Define corresponding accessor (get) methods to access the account name, transactions, and date created information, i.e., you need to define 3 get methods  Define a method named getMonthlyInterest() that returns the monthly interest earned.  Define a method named withdraw that withdraws a specified amount for a specified purpose from the account and then add this transaction including its description to the ArrayList of transactions.  Define a method named deposit that deposits a specified amount from a specified source to the account and then then add this transaction including its description to the ArrayList of transactions.

Notes: (1) The method getMonthlyInterest() is to return monthly interest earned, not the interest rate. Monthly interest = balance * monthlyInterestRate, where monthlyInterestRate = annualInterestRate/12. (2) The constructors for the Account class will initiate the date data field. (3) You should create the Account and Transaction classes as independent files in the same package of your main class. Requirements for the main class: To test the two classes above, in the main method of your main class you need to do the following first:  Create an instance of the Account class with an annual interest rate of 1.5%, a balance of 1000, an id of 1122, and a name as George.  Deposit $3000 as salary to the account and then withdraw $2500 as rent from the account.

 Print an account summary that shows the account holder’s name, annual interest rate, balance, monthly interest, the date when this account was created, and all transactions. (Check the results of this print statement to verify that your Account and Transaction classes work properly.)  After you pass the above test, continue on to do the rest. Simulate an ATM machine experience: The ATM system will ask users to enter a valid ID first (corresponding to user login). If the entered ID matches to one of the Accounts’ ID, the system will present the user an ATM main menu.

Description of the ATM machine main menu: This ATM machine main menu offers users four choices: choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money, and 4 for exiting the main menu (corresponding to user logoff).  The ATM main menu prompts the user to enter a choice, and depending on the user’s choice it may ask for additional input (i.e., withdraw or deposit amount) before acting on the user’s choice.  Your code should behave properly for each user choice as shown in the sample-run figure at the end.  Description of the sample-run figure (shown at the end): In the sample-run figure below, the user first entered a choice 1 for viewing the current balance, and after hitting the Enter key a statement of balance was shown. Then the user entered 2 for withdrawing money, and after hitting the Enter key the system asked the user to enter the withdraw amount. The user next entered 1 to verify the balance amount. Afterward, the user entered 3 for depositing money, and after hitting the Enter key the system asked the user to enter the deposit amount. Again, The user next entered 1 to verify the balance amount. Lastly, the user entered 4 for exiting the main menu and the system responses by prompting for an id again. In the main method, create an Array of ten accounts with account IDs as 1, 2,. . . , 10, and an initial balance of $100 for each account. You will use this Array to simulate an ATM machine experience in the main method. To do that, you need a Scanner to capture the user’s input and define a few variables. You should also define a few methods in the main class, to be used inside the main method, to make the main method looks lean. There are many options to choose from, and the objective is to model an ATM machine experience described above. Additional simulation requirements: After creating the 10-element array (and a few variables), the system should prompt the user to enter an ID and it will verify if the ID is a valid one. An ID is valid if it matches to one of the accounts of the 10-element Array. If the ID is valid, the ATM main menu (described above) is displayed; otherwise the system will ask the user to enter a correct ID. That is, the system will not present the ATM main menu until the user enters a correct ID. You may have noticed that based on the above ATM simulation description, once the system starts it will not stop because after a user chooses option 4 to exit the ATM system will ask user to enter an ID again. To give users an option exiting the system after a few rounds of ATM simulation, inform users that when entering an ID, enter 0 to exit the ATM run (since valid ids are from 1 to 10). That is, if users enter 0 as ID, instead of showing the main menu, the system will terminate the project, but before that it will print out all ATM transactions for each account (in the Array) that actually incurred transactions.

Solutions

Expert Solution

Account.java

/********************************************************
*                     Account                           *
*-------------------------------------------------------*
* -id: int                                              *
* -balance: double                                      *
* -annualInterestRate: double                           *
* ---------------------------                           *
* -dateCreated: String                                  *
* +Account()                                            *
* +Account(newid: int, newBalance: double)              *
* +setId(newId: int)                                    *
* +setBalance(newBalance: double)                       *
* +setAnnualInterestRate(newAnnualInterestRate: double) *
* +getId(): int                                         *
* +getBalance(): double                                 *
* +getAnnualInterestRate(): double                      *
* +getDateCreated(): String                             *
* +getMonthlyInterestRate(): double                     *
* +getMonthlyInterest(): double                         *
* +withdraw(amount: double)                             *
* +deposit(amount: double)                              *
********************************************************/

// Implement the Account class
import java.util.Date;

public class Account {
   // Data fields
   private int id;
   private double balance;
   private static double annualInterestRate;
   private Date dateCreated;

   // Constructors
   /** Creates a default account */
   Account() {
       id = 0;
       balance = 0;
       annualInterestRate = 0;
       dateCreated = new Date();
   }

   /** Creates an account with the specified id and initial balance */
   Account(int newId, double newBalance) {
       id = newId;
       balance = newBalance;
       dateCreated = new Date();
   }

   // Mutator methods
   /** Set id */
   public void setId(int newId) {
       id = newId;
   }

   /** Set balance */
   public void setBalance(double newBalance) {
       balance = newBalance;
   }

   /** Set annualInterestRate */
   public void setAnnualInterestRate(double newAnnualInterestRate) {
       annualInterestRate = newAnnualInterestRate;
   }

   // Accessor methods
   /** Return id */
   public int getId() {
       return id;
   }

   /** Return balance */
   public double getBalance() {
       return balance;
   }

   /** Return annualInterestRate */
   public double getAnnualInterestRate() {
       return annualInterestRate;
   }

   /** Return dateCreated */
   public String getDateCreated() {
       return dateCreated.toString();
   }

   /** Return monthly interest rate */
   public double getMonthlyInterestRate() {
       return annualInterestRate / 12;
   }

   // Methods
   /** Return monthly interest */
   public double getMonthlyInterest() {
       return balance * (getMonthlyInterestRate() / 100);
   }

   /** Decrease balance by amount */
   public void withdraw(double amount) {
       balance -= amount;
   }

   /** Increase balance by amount */
   public void deposit(double amount) {
       balance += amount;
   }
}


Exercise.java


import java.util.Scanner;

public class Exercise {
   /** Main method */
   public static void main(String[] args) {
       // Create a Scanner
       Scanner input = new Scanner(System.in);

       // Create ten accounts in an array
       Account[] accounts = new Account[10];

       // Initialize accounts with balance
       initialBalance(accounts);
      
       // Once the system starts, it will not stop
       do {
           // Prompt user to enter an id
           System.out.print("Enter an id: ");
           int id = input.nextInt();

           if (isValidID(id, accounts)) {
               int choice;
               do {  
                   // Get user choice
                   choice = displayMainMenu(input);
                   if (isTransaction(choice)) {
                           executeTransaction(choice, accounts, id, input);
                   }
               } while (choice != 4); // If 4 exit main menu
           }
           // Once you exit, the system will prompt for an id again
       } while (true);
   }

   /** Initialize accounts with balance of 100 */
   public static void initialBalance(Account[] a) {
       int initialBalance = 100;
       for (int i = 0; i < a.length; i++) {
           a[i] = new Account(i, initialBalance);
       }
   }

   /** Return true if choice is a transaction */
   public static boolean isTransaction(int choice) {
       return choice > 0 && choice < 4;
   }

   /** Return true if ID is valid */
   public static boolean isValidID(int id, Account[] a) {
       for (int i = 0; i < a.length; i++) {
           if (id == a[i].getId())
               return true;
       }
       return false;
   }

   /** Display main menu */
   public static int displayMainMenu(Scanner input) {
       System.out.print(
           "\nMain menu\n1: check balance\n2: withdraw" +
           "\n3: deposit\n4: exit\nEnter a choice: ");
       return input.nextInt();
   }

   /** Execute a Transaction */
   public static void executeTransaction(
       int c, Account[] a, int id, Scanner input) {
       switch (c) {
           case 1: // Viewing the current balance
                      System.out.println("The balance is " + a[id].getBalance());
                      break;
           case 2: // Withdraw money
                      System.out.print("Enter an amount to withdraw: ");
                      a[id].withdraw(input.nextDouble());
                      break;
           case 3: // Deposit money
                      System.out.print("Enter an amount to deposit: ");
                      a[id].deposit(input.nextDouble());
       }
   }
}


Related Solutions

Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and  numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have constructor that initializes all its data. Classes Car, Bus, and Truck will have constructors which will...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method...
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have constructor that initializes all its data. Classes Car, Bus, and Truck will have constructors which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT