JAVA Create an Account class with the following member data:1. Account ID (a ran
ID: 3823242 • Letter: J
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 (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 account’s 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)
SAMPLE INPUT/OUTPUT:
I have most of it except for the last two bullet points. This is my code so far:
import java.util.Scanner;
public class Prog8 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
Account myAccount = new Account();
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());
}
else if(type == 'w'){
amount = reader.nextDouble();
myAccount.withdraw(amount);
System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount.getAccountNumber());
}
else if(type == 'i'){
double intAdded = myAccount.addInterest();
System.out.printf(" $%.2f interest added to account %d ", intAdded, myAccount.getAccountNumber());
}
else if(type == 'p'){
System.out.printf(" Current balance for account %d: $%.2f",
myAccount.getAccountNumber(), myAccount.getBalance());
}
}
System.out.println();
}
}
******************
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) < 0)
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 * 1.005;
//Returning amount of interest added
return (balance - actualBalance);
}
//Method that returns balance amount
public double getBalance(){
return balance;
}
}
Explanation / Answer
Please Find the updated Account.java file:
which creates a arrayList of Strings to hold the transaction made in past and when p command typed which prints the stored msgs.
import java.util.ArrayList;
import java.util.Scanner;
public class Prog8 {
private static ArrayList<String> records=new ArrayList<String>();
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
Account myAccount = new Account();
System.out.println(myAccount.getAccountNumber());
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("deposited "+ amount);
}
else if(type == 'w'){
amount = reader.nextDouble();
myAccount.withdraw(amount);
records.add("withdraw "+ amount);
System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount.getAccountNumber());
}
else if(type == 'i'){
double intAdded = myAccount.addInterest();
records.add("added intrest "+ intAdded);
System.out.printf(" $%.2f interest added to account %d ", intAdded, myAccount.getAccountNumber());
}
else if(type == 'p'){
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());
}
}
}
}
Thanks,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.