In: Computer Science
Java programming.
*******I Need complete the following requirements in this
project:
1/ Remove the applyRandomBonus method from the test class
2/ Create a File, Printwriter for an output file
yourlastnameErrorLog.txt
3/ Set your maximum array size for accounts to 10
4/ Catch InputMismatch and ArrayIndexOutOfBounds exceptions when
reading data from the file:
a. Skip any lines that cause an exception
b. Write information about the exception to the log file,
yourlastnameError.txt
c. Include exception type and line number in exception message to
your log file.
Example:
Data Input Mismatch
line:3 line skipped
d. Continue processing the file
5/ Add Account Processing for the user to deposit or withdraw from
an account:
a. Ask the user if they would like to process account(s)
b. Ask the user for account number
c. Search for account (using a separate method for your linear
search), print and
error if account not found to the screen and to the log file,
yourlastnameError.txt.
d. Include account number and a message account not found to the
log file print,
example:
Account number searched for not found:
555555
e. Once account is found, send it to a separate method that will
handle
withdrawals and deposits. Ask the user if they would like to
Deposit, Withdraw
or Exit. Print an error message to the screen and to the log file
if the deposit or
withdrawal did not go through (see requirements from Phase 1).
Include
account number of unsuccessful action, and the action (withdrawal,
deposit) to
your error message, example:
Insufficient funds for
withdrawal: 5023742 balance: 540.0
f. Once the user is done with deposits/withdrawals for an account,
loop around
and ask again if they’d like to process another account.
6/ All requirements from Final Phase 1 are required in this
project, except for the
appyRandomBonus method and method call.
public class Account {
private long accountNumber;
private String firstName;
private String lastName;
private double balance;
// constructor
public Account(long accountNumber, String firstName, String
lastName, double balance) {
this.accountNumber = accountNumber;
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;
}
// setters and getters
public long getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(long accountNumber) {
this.accountNumber = accountNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
// deposit
public boolean deposit(double amount) {
boolean isDeposit = false;
if(amount>10 && amount <200) {
this.balance +=amount;
isDeposit = true;
}
return isDeposit;
}
public boolean withdraw(double amount) {
boolean isWithdraw = false;
if(amount>=10 && this.balance>=10) {
this.balance-=amount;
isWithdraw = true;
}
return isWithdraw;
}
// toString method is used to return current object data in the
form of string
@Override
public String toString() {
return "Account{" +
"accountNumber=" + accountNumber +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", balance=" + balance +
'}';
}
}
*************
Test class:::
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class AccountTest {
static int index=0;
public static void main(String[] args) throws FileNotFoundException
{
Account [] accounts = new Account[5];
Scanner scanner = new Scanner(new File("E:\\Data.txt"));
/* This method will get each line from the text file and split with
the space and then saved each attribute
* into corresponding element.*/
while (scanner.hasNextLine()) {
String eachLine = scanner.nextLine();
String splitLine[] = eachLine.split(" ");
for (int i=0; i<splitLine.length;i++) {
String firstName = splitLine[i];
String lastName = splitLine[++i];
long accountNumber = Long.parseLong(splitLine[++i]);
double balance = Double.parseDouble(splitLine[++i]);
accounts[index] = new
Account(accountNumber,firstName,lastName,balance);
index++;
}
}
// static methods
displayAccounts(accounts);
applyRandomBonus(accounts);
displayAccounts(accounts);
System.out.println("Check deposit and withdraw:");
System.out.println(accounts[0]);
System.out.println(accounts[0].deposit(300));
System.out.println(accounts[0]);
System.out.println(accounts[0].withdraw(215));
System.out.println(accounts[0]);
}
private static void applyRandomBonus(Account[] accounts) {
for(Account account: accounts) {
if(account!=null) {
// Here random number can be generated between 100 and 300
int randomNumber = (int) (100+Math.random()*(300-100));
System.out.println("Bonus: " +randomNumber);
// adding each bonus into account balance
account.setBalance(account.getBalance()+randomNumber);
}
}
}
private static void displayAccounts(Account[] accounts) {
// By using for each loop displays the details of account
for(Account account: accounts) {
if(account!=null) {
System.out.println(account); d
}
}
}
}
** data.txt include:
1/ Johnson Nell 98845255 28.00
2/ Kendrick Farwell 67854321 100.00
3/ Peter Jackson 55325678 400.00
4/ Layla Kerns 56789332 350.00
5/ London Parker 23412378 250.00
Account class
public class Account {
private long accountNumber;
private String firstName;
private String lastName;
private double balance;
// constructor
public Account(long accountNumber, String firstName, String
lastName, double balance) {
this.accountNumber = accountNumber;
this.firstName = firstName;
this.lastName = lastName;
this.balance = balance;
}
// setters and getters
public long getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(long accountNumber) {
this.accountNumber = accountNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
// deposit
public boolean deposit(double amount) {
boolean isDeposit = false;
if(amount>10 && amount <200) {
this.balance +=amount;
isDeposit = true;
}
return isDeposit;
}
public boolean withdraw(double amount) {
boolean isWithdraw = false;
if(amount>=10 && this.balance>=10) {
this.balance-=amount;
isWithdraw = true;
}
return isWithdraw;
}
// toString method is used to return current object data in the
form of string
@Override
public String toString() {
return "Account{" +
"accountNumber=" + accountNumber +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", balance=" + balance +
'}';
}
}
Account test class
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Scanner;
public class AccountTest {
static int index=0;
static Scanner sc = new Scanner(System.in);
static PrintWriter printWriter;
public static void main(String[] args) throws FileNotFoundException
{
Account [] accounts = new Account[10];
Scanner scanner = new Scanner(new File("E:\\Data.txt"));
File file = new File("E:\\yourlastnameErrorLog.txt");
printWriter = new PrintWriter(file);
int lineNumber = 0;
/* This method will get each line from the text file and split with
the space and then saved each attribute
* into corresponding element.*/
while (scanner.hasNextLine()) {
lineNumber++;
try{
String firstName = scanner.next();
String lastName = scanner.next();
long accountNumber = scanner.nextLong();
double balance = scanner.nextDouble();
accounts[index] = new
Account(accountNumber,firstName,lastName,balance);
index++;
}catch(InputMismatchException e){
printWriter.write("Data input Mismatch line:" + lineNumber+ " line
skipped\n");
scanner.nextLine();
}catch(ArrayIndexOutOfBoundsException e1){
printWriter.write("Array index out of bound line:" + lineNumber+ "
line skipped\n");
}catch(NumberFormatException e3){
}
}
while(true){
System.out.println("Would you like to process
accounts?(y/n)");
String choice = sc.nextLine();
if(choice.equalsIgnoreCase("y")){
System.out.println("Enter account number");
long accountNumber = sc.nextLong();
choice = sc.nextLine();
Account account = searchAccount(accounts,accountNumber);
if(account == null){
printWriter.write("\nAccount number searched for not
found:"+accountNumber+"\n");
System.out.println("Account number searched for not
found:"+accountNumber);
}else{
handleOperation(account);
}
}else if(choice.equalsIgnoreCase("n")){
break;
}else{
System.out.println("Invalid choice");
}
}
// static methods
System.out.println("Displaying Account details");
displayAccounts(accounts);
printWriter.close();
scanner.close();
}
private static void displayAccounts(Account[] accounts) {
// By using for each loop displays the details of account
for(Account account: accounts) {
if(account!=null) {
System.out.println(account);
}
}
}
private static Account searchAccount(Account[] accounts,long
accountNumber){
for(Account account : accounts){
if(account!=null){
if(accountNumber == account.getAccountNumber()){
return account;
}
}
}
return null;
}
private static void handleOperation(Account account){
while(true){
System.out.println("1.Deposit\n2.Withdraw\n3.Exit");
System.out.println("Enter option\n");
String choice = sc.nextLine();
if(choice.equalsIgnoreCase("1")){
System.out.println("Enter amount");
double amount = sc.nextDouble();
choice = sc.nextLine();
boolean result = account.deposit(amount);
if(result == true){
System.out.println("Amount deposited");
}else{
printWriter.write("\nAmount didn't fall in deposit range for
deposit: "+account.getAccountNumber()+" balance:
"+account.getBalance()+"\n");
System.out.println("Amount didn't fall in deposit range for
deposit: "+account.getAccountNumber()+" balance:
"+account.getBalance());
}
}else if(choice.equalsIgnoreCase("2")){
System.out.println("Enter amount");
double amount = sc.nextDouble();
choice = sc.nextLine();
boolean result = account.withdraw(amount);
if(result == true){
System.out.println("Amount withdrawn");
}else{
if(account.getBalance()<10){
printWriter.write("\nInsufficient funds for withdrawal:
"+account.getAccountNumber()+" balance:"+
account.getBalance()+"\n");
System.out.println("Insufficient funds for withdrawal:
"+account.getAccountNumber()+" balance:"+
account.getBalance());
}else if(amount<10){
printWriter.write("\nInsufficient amount for withdrawal:
"+account.getAccountNumber()+" balance:"+
account.getBalance()+"\n");
System.out.println("Insufficient amount for withdrawal:
"+account.getAccountNumber()+" balance:"+
account.getBalance());
}
}
}else if(choice.equalsIgnoreCase("3")){
return;
}else{
System.out.println("Invalid choice");
}
}
}
}
Error log
*Change error log filename and path as your requirement*
*Leave a thumbs up*