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:

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(). The first number in the input file is the number of random accounts that need to be created. I am having trouble getting my code to create more than one random number. I am also not able to get my code to read in the correct numbers.
Here is my code:
import java.util.Scanner;
public class LP2 { public static void main(String[] args) { Scanner reader = new Scanner(System.in); Account myAccount = new Account(); Account[] myAccount; char type; double amount; int numAccnts = reader.nextInt(); myAccount = new Account[numAccnts];
while(reader.hasNext()) { type = reader.next().charAt(0); if(type == 'd'){ int accNum = reader.nextInt(); if (accNum >= numAccnts) System.out.println("Sorry you cannot create this account!!!"); else{ amount = reader.nextDouble(); myAccount[accNum] = new Account(); myAccount[accNum].deposit(amount); System.out.printf(" $%.2f deposited into account %d ", amount, myAccount[accNum].getAccountNumber());
} } else if(type == 'w'){ int accNum = reader.nextInt(); if (accNum >= myAccount.length ) System.out.println("Sorry this account does not exist!!"); else{ amount = reader.nextDouble(); myAccount[accNum].withdraw(amount); System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount[accNum].getAccountNumber()); } } else if (type == 'p') { System.out.println(" *******************"); System.out.println("* Transaction history for Account:" + myAccount.getAccountNumber()); myAccount.printStatement(); System.out.printf("* Current balance for account %d: $%.2f ", myAccount.getAccountNumber(), myAccount.getBalance()); System.out.println("*******************"); } } } }
import java.util.ArrayList;
public class Account {
private int accountNumber;
private double balance;
private final double interest = 1.005;
private ArrayList
private int transactionCounter; // how many transactions have been executed
private final int MAX_TRANSACTION_HISTORY = 5; // the max number
public Account() {
// Generating a random 4 digit Account Number
accountNumber = (int) (Math.random() * 9000) + 1000;
records = new ArrayList(5);
balance = 0.0;
transactionCounter = 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)
System.out.println(" Error: Can not withdraw $" + amt);
else
// Updating new balance
balance = balance - amt;
}
// Method that adds interest to balance amount
public double addInterest() {
double actualBalance = balance;
// Adding interest to balance
balance = balance * interest;
// Returning amount of interest added
return (balance - actualBalance);
}
// Method that returns balance amount
public double getBalance() {
return balance;
}
public void recordTransaction(String details) {
// this method stores the transaction details in the array
if (transactionCounter >= MAX_TRANSACTION_HISTORY) {
records.remove(0);
}
records.add(details);
transactionCounter++;
}
public void printStatement() {
// this method prints out the contents of the transaction history array in a statement
// format
for (String obj : records) {
System.out.println("* " + obj);
}
}
}
File in2 dat d o 100 d 1 100 w 1 101 d 2 5 w 0 150 i 1 i 2 d 0 1000 0 1100.50 p 0 p 1 p 2 Execution Account 882 l created Account 6842 created Account 7720 created $100.00 deposited into account 8821 $100.00 deposited into account 6842 cannot withdraw $101 Error 0 from account 6B42 $5 0 deposited into account 7720 Error: cannot withdraw $150.00 from account 8821 $0.50 interest added to account 8821 $0.50 interest added to account 6842 $0.0 2 interest added to account 7720 $1000.00 deposited into account B821. $1100.5 withdrawn from account 8821 Transactio n history for account 8821 $100.0 0 deposited $0.50 earned in interest $1000.00 deposited $1.100.50 withdrawn Current balance 0.000 Transactio n history for account 6842 $100.0 deposited $0.50 earned in interest Current balance $100.50 Transaction history for account 7720 5.00 deposited 0.02 earned in interest Current balance 5.02
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
