Question: So this is my code, but how do i get it to print all the information i need for each in.dat file that i have

So this is my code, but how do i get it to print all the information i need for each in.dat file that i have all in one run of the program?

Account.java

import java.util.Random;

public class Account {

//Declaring instance variables

private int id;

private double balance;

//Zero argumented constructor

public Account() {

// Creating a random Class object

Random r = new Random();

this.id = r.nextInt(9000) + 1000;

this.balance = 0.0;

}

//This method will perform withdraw operation

public void withdraw(double amt)

{

if(amt>balance)

System.out.printf("Error: cannot withdraw $%.2f ",amt);

else

{

balance-=amt;

System.out.printf("$%.2f withdrawn from account %d ",amt,id);

}

}

//This method will perform deposit operation

public void deposit(double amt)

{

balance+=amt;

System.out.printf("$%.2f deposited into account %d ",amt,id);

}

//This method will add interest to the balance

public void addInterest()

{

double interest=balance*0.005;

balance+=interest;

System.out.printf("$%.2f interest added to account ",interest,id);

}

//This method will display account balance

public void getBalance()

{

System.out.printf("Current balance for account %d: $%.2f ",id,balance);

}

}

_______________

Prog8.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Prog8 {

public static void main(String[] args) {

//Declaring variables

char operation;

Scanner sc = null;

double amt = 0.0;

//Creating an instance of Account class

Account acc = new Account();

try {

//Opening the input file

sc = new Scanner(new File("in1.dat"));

while (sc.hasNext()) {

operation = sc.next(".").charAt(0);

if (operation == 'd') {

amt = sc.nextDouble();

acc.deposit(amt);

} else if (operation == 'w') {

amt = sc.nextDouble();

acc.withdraw(amt);

} else if (operation == 'i') {

acc.addInterest();

} else if (operation == 'p') {

acc.getBalance();

} else {

System.out.println("** Invalid Operation **");

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

Create an Account class (java) 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 corresponding transactions on an instance of Account:

? w - withdraw from the account (e.g. w 50? means withdraw $50). Print a message stating how 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 how much was deposited into the account.

? i - add interest to the account. Print a message stating how much interest was added to the account. The interest rate is %0.5 (e.g. i? means multiply the current balance by 1.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 to receive full credit (if you try to write the program without creating an Account class, you will not receive many points).

? For this program, you only need to create one instance of Account.

? All member data of class Account should be private. You should make appropriate use of constructors and getter/setter methods to interact with your data.

?Will be using import java.util.Scanner and will be using reader.hasNext and its variants

in1.dat

d 50

w 10

i

p

in1.dat example output:

$50.00 deposited into account 4978

$10.00 withdrawn from account 4978

$0.20 interest added to account 4978

Current balance for account 4978: $40.20

in2.dat

d 100

d 200

w 150

i

p

d 1000

p

w 1150.75

p

output:

$100.00 deposited into account 8890

$200.00 deposited into account 8890

$150.00 withdrawn from account 8890

$0.75 interest added to account 8890

Current balance for account 8890: $150.75

$1000.00 deposited into account 8890

Current balance for account 8890: $1150.75

$1150.75 withdrawn from account 8890

Current balance for account 8890: $0.00

in3.dat:

w 1

p

d 100

p

w 200

p

output:

Error: cannot withdraw $1.00

Current balance for account 3585: $0.00

$100.00 deposited into account 3585

Current balance for account 3585: $100.00

Error: cannot withdraw $200.00

Current balance for account 3585: $100.00

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!