Question

In: Computer Science

Java programming. *******I Need complete the following requirements in this project: 1/ Remove the applyRandomBonus method...

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

Solutions

Expert Solution

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*


Related Solutions

Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
Write any java programming to meet the following requirements. Your project must meet the following requirements:...
Write any java programming to meet the following requirements. Your project must meet the following requirements: 1. Specify input specification      Design input 2. Specify Output Specification     Design Output 3. The class must include set methods and get methods ( with or without parameters both) 4. Must include Array structure 5. Must include Input or Output Files or both 6. Demonstrate your knowledge of an Applet
Java. Part 1 of 4 - Amusement Park Programming Project Requirements: Use the Java selection constructs...
Java. Part 1 of 4 - Amusement Park Programming Project Requirements: Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Proper error handling. Class: Ticket – models admission tickets. Instance Fields: number : long – identify the unique ticket. category : String – store the category of the ticket. holder : String – store the name of the person who purchased the ticket. date...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
art A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
I need a Java application with a GUI that includes the following requirements: Three classes minimum...
I need a Java application with a GUI that includes the following requirements: Three classes minimum At least one class must use inheritance At least one class must be abstract Utilization of “get set” method. (get; set; =  is related to how variables are passed between different classes. The get method is used to obtain or retrieve a particular variable value from a class. A set value is used to store the variables. The whole point of the get and set...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
In Java I need a Flowchart and Code. Write the following method that tests whether the...
In Java I need a Flowchart and Code. Write the following method that tests whether the array has four consecutive numbers with the same value: public static boolean isConsecutiveFour(int[] values) Write a test program that prompts the user to enter a series of integers and displays it if the series contains four consecutive numbers with the same value. Your program should first prompt the user to enter the input size—i.e., the number of values in the series.
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
I need to write java program that do replace first, last, and remove nth character that...
I need to write java program that do replace first, last, and remove nth character that user wants by only length, concat, charAt, substring, and equals (or equalsIgnoreCase) methods. No replace, replaceFirst, last, remove, and indexOf methods and letter case is sensitive. if user's input was "be be be" and replace first 'b' replace first should do "e be be" replace last should do "be be e" remove nth character such as if user want to remove 2nd b it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT