Question: JAVA- Create an Account class with the following member data:1. Account ID (a random 4-digit number that is generated when the object is created)2. Balance
JAVA-
Create an Account class with the following member data:1. Account ID (a random 4-digit number that is generated when the object is created)2. Balance (starts at $0.00 until a deposit is made)Your program will read in commands from an input file and perform the correspondingtransactions on an instance of Account: w - withdraw from the account (e.g. w 50 means withdraw $50). Print a message statinghow much was withdrawn from the account. If the account will be overdrawn, cancel
the withdraw and print an error message. d - deposit into the account (e.g. d 10 means deposit $10). Print a message stating howmuch was deposited into the account. i - add interest to the account. Print a message stating how much interest was added tothe account. The interest rate is %0.5 (e.g. i means multiply the current balance by1.005). The interest rate should be stored as a final variable inside your class. p - prints the current balance of the account, formatted to 2 decimal places.
Notes Your program should utilize 2 separate files: Prog8.java (for your main class) and Account.java (for your Account class) You must make appropriate use of objects in this program All member data of class Account should be private. You should make appropriate use ofconstructors and getter/setter methods to interact with your data.
Record each transaction (up to 5 per instance of account), and print out the accounts history whenever the p command is read from the input file.
Print a message with the account ID every time a new instance of Account is created(this should happen at the very beginning of your program)
This is what the input/output is supposed to look like:

This is what I am getting for an output when I used the data from in1.dat:

HOW DO I EDIT MY CODE TO MATCH THE OUTPUT IN THE EXAMPLE? DO NOT SET UP TO RUN A SPECIFIC FILE. THE AMOUNTS READ IN WILL BE READ IN FROM A .DAT FILE BY USING reader.hasNext().
Here is my code:
import java.util.ArrayList; import java.util.Scanner;
public class LP2 { private static ArrayList records=new ArrayList(); public static void main(String[] args) { Scanner reader = new Scanner(System.in); Account myAccount = new Account(); System.out.println("Account " + myAccount.getAccountNumber() + " created");
char type; double amount;
while(reader.hasNext()) { type = reader.next().charAt(0);
if(type == 'd'){ amount = reader.nextDouble(); myAccount.deposit(amount); System.out.printf(" $%.2f deposited into account %d ", amount, myAccount.getAccountNumber()); records.add(amount + " deposited");
}
else if(type == 'w'){ amount = reader.nextDouble(); myAccount.withdraw(amount); records.add(amount + " withdrawn");
System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount.getAccountNumber()); }
else if(type == 'i'){ double intAdded = myAccount.addInterest(); records.add(intAdded + " earned in interest");
System.out.printf(" $%.2f interest added to account %d ", intAdded, myAccount.getAccountNumber()); }
else if(type == 'p'){ System.out.println("*******************"); System.out.println("Transaction history for Account:"+ myAccount.getAccountNumber()); for(String obj: records) { System.out.println(obj); } System.out.printf(" Current balance for account %d: $%.2f ", myAccount.getAccountNumber(), myAccount.getBalance()); } } } }
class Account{
private int accountNumber; private double balance; public Account(){
//Generating a random 4 digit Account Number accountNumber = (int)(Math.random() * 9000) + 1000; balance = 0.0; } //Getter method for Account Number public int getAccountNumber(){ return accountNumber; } //Method that adds amount to balance public void deposit(double amt){ //Updating new balance balance = balance + amt; } //Method that withdraws amount from balance public void withdraw(double amt){ //Validating amount if((balance - amt)
THIS IS A HINT WE WERE GIVEN TO ADD TO OUR ACCOUNT CLASS.
private String[] transactionHistory; // array to store transaction details private int transactionCounter; // how many transactions have been executed private final int MAX_TRANSACTION_HISTORY = 5; // the max number // other class data goes here Account() { // initialize your String array inside your constructor } // other class methods go here public void recordTransaction(String details) { // this method stores the transaction details in the array } public void printStatement() { // this method prints out the contents of the transaction history array in a statement format } File in dat w o 10 p 0 cution Account 9721 created 0.00 deposited into account 9721 $5 $10.00 ithdrawn account 9721 $0.20 interest added to account 721 Transaction history for account 9721 550.00 deposited $10.00 W. ithdrawn. 50 20 earned in interest Current balance: 40.20
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
