In JAVA, Given CODE need to be optmized Optimization and Exception Handling Refi
ID: 3734687 • Letter: I
Question
In JAVA, Given CODE need to be optmized
Optimization and Exception Handling
Refine Source Code for singular bank account program.
Look for code optimization conventions and syntax.
Catch all Exceptions generated during program run-time. The program should never exit with error message.
Bank Account Program
Create an Account Class
We are forming the skeleton of a more complex bank account program.
Create instance variables for balance….You may need more depending on how you implement your bank account program. (We will continue to update the program as the semester continues.)
Create an 2 arrays as a members of the class.
Deposit_Array
Withdraw_Array
We will need methods:
Method for Depositing values into the account.
What type of method will it be?
Method for Withdrawing values from the account.
What type of method will it be?
Method to output the balance of the account.
What type of method will it be?
Method that will output all deposits made to the account.
Method that will output all withdraws made from the account.
Add a new method that will output the contents of the bank account program to a text file.
Program will output the balance of the account.
Program will output the account withdrawals.
Program will output the account deposits.
Use “class constructor” to initialize values in programmer class for program execution.
Balance Value
Withdrawal History
Deposit History
Output should be like
**************************************
* Bank Account Program: *
* Enter # to run program or Quit *
* 1) Make a Deposit *
* 2) Make a Withdrawal *
* 3) Output balance *
* 4) Output all deposits *
* 5) Output all withdrawals *
* 6) Quit *
**************************************
This is the code that need to be optimized.
import java.io.*;
import java.util.*;
class Account{
private ArrayList<String> Deposit;
private ArrayList<String> Withdrawl;
private double balance;
public Account(){
balance = 0;
Deposit = new ArrayList<String>();
Withdrawl = new ArrayList<String>();
}
public void withdrawl(double a){
if (balance >= a){
balance = balance - a;
String str = "Withdrawl amount :" + Double.toString(a);
Withdrawl.add(str);
}
else {
System.out.println("Insufficient fund");
}
}
public void deposit(double a){
balance = balance + a;
String str = "Deposit amount :" + Double.toString(a);
Deposit.add(str);
}
public double getBalance(){
return balance;
}
public ArrayList<String> getAllDeposits(){
return Deposit;
}
public ArrayList<String> getAllWitdrawls(){
return Withdrawl;
}
public void outputToFile(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter filename:");
String name = sc.nextLine();
try {
FileOutputStream fout = new FileOutputStream(name);
PrintStream pw = new PrintStream(fout);
pw.println("Balance:" + balance);
for (int i = 0; i<Withdrawl.size(); i++){
pw.println(Withdrawl.get(i));
}
for (int i = 0; i<Deposit.size(); i++){
pw.println(Deposit.get(i));
}
pw.close();
fout.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
public class Demo100{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Account acc = new Account();
while (true){
System.out.println("1.Make a deposit");
System.out.println("2.Make a withdrawl");
System.out.println("3.Output balance");
System.out.println("4.Output all deposits");
System.out.println("5.Output all withdrawls");
System.out.println("6.Quit");
System.out.println("Enter option: ");
int ch = Integer.parseInt(sc.nextLine());
if (ch == 6)
break;
if (ch == 1){
System.out.println("Enter deposit amount :");
double amt = Double.parseDouble(sc.nextLine());
acc.deposit(amt);
}
if (ch == 2){
System.out.println("Enter withdrwal amount :");
double amt = Double.parseDouble(sc.nextLine());
acc.withdrawl(amt);
}
if (ch == 3){
System.out.printf("Account Balance : %.2f", acc.getBalance());
System.out.println();
}
if (ch == 4){
ArrayList<String> list = acc.getAllDeposits();
for (int i = 0; i<list.size(); i++){
System.out.println(list.get(i));
}
}
if (ch == 5){
ArrayList<String> list = acc.getAllWitdrawls();
for (int i = 0; i<list.size(); i++){
System.out.println(list.get(i));
}
}
}
acc.outputToFile();
}
}
Explanation / Answer
Please find the below optimized Account and Demo100 classes. In both the classes’ validation and exception handling has been introduced wherever possible.
Please check and revert if anything else required.
***** Account ******
import java.io.*;
import java.util.*;
class Account {
private ArrayList<String> Deposit;
private ArrayList<String> Withdrawl;
private double balance;
public Account() {
balance = 0;
Deposit = new ArrayList<String>();
Withdrawl = new ArrayList<String>();
}
public void withdrawl(double a) {
if (balance >= a) {
balance = balance - a;
String str = "Withdrawl amount :" + Double.toString(a);
Withdrawl.add(str);
} else {
System.out.println("Insufficient fund");
}
}
public void deposit(double a) {
balance = balance + a;
String str = "Deposit amount :" + Double.toString(a);
Deposit.add(str);
}
public double getBalance() {
return balance;
}
public ArrayList<String> getAllDeposits() {
return Deposit;
}
public ArrayList<String> getAllWitdrawls() {
return Withdrawl;
}
public void outputToFile() {
Scanner sc = null;
FileOutputStream fout = null;
PrintStream pw = null;
try {
sc = new Scanner(System.in);
System.out.println("Enter file Name:");
String name = sc.nextLine();
fout = new FileOutputStream(name);
pw = new PrintStream(fout);
pw.println("Balance:" + balance);
for (int i = 0; i < Withdrawl.size(); i++) {
pw.println(Withdrawl.get(i));
}
for (int i = 0; i < Deposit.size(); i++) {
pw.println(Deposit.get(i));
}
pw.flush();
fout.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pw != null) {
pw.close();
}
if (fout != null) {
fout.close();
}
if (sc != null) {
sc.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
****** Demo100 ******
import java.util.ArrayList;
import java.util.Scanner;
public class Demo100 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Account acc = new Account();
while (true) {
System.out.println("1.Make a deposit");
System.out.println("2.Make a withdrawl");
System.out.println("3.Output balance");
System.out.println("4.Output all deposits");
System.out.println("5.Output all withdrawls");
System.out.println("6.Quit");
System.out.println("Enter option: ");
String choice = sc.nextLine();
if (isValidChoice(choice)) {
int ch = Integer.parseInt(choice);
if (ch == 6)
break;
if (ch == 1) {
while (true) {
double amt = 0;
System.out.println("Enter deposit amount :");
try {
amt = Double.parseDouble(sc.nextLine());
acc.deposit(amt);
break;
} catch (NumberFormatException ignore) {
System.out.println("Invalid input");
}
}
}
if (ch == 2) {
while (true) {
double amt = 0;
System.out.println("Enter withdrwal amount :");
try {
amt = Double.parseDouble(sc.nextLine());
acc.withdrawl(amt);
break;
} catch (NumberFormatException ignore) {
System.out.println("Invalid input");
}
}
}
if (ch == 3) {
System.out.printf("Account Balance : %.2f", acc.getBalance());
System.out.println();
}
if (ch == 4) {
ArrayList<String> list = acc.getAllDeposits();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
if (ch == 5) {
ArrayList<String> list = acc.getAllWitdrawls();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
} else {
System.out.println("Invalid input!");
}
}
acc.outputToFile();
sc.close();
}
public static boolean isValidChoice(String choice) {
if (choice.length() != 1) {
return false;
}
if (!Character.isDigit(choice.charAt(0))) {
return false;
}
if (Integer.parseInt(choice) > 6 || Integer.parseInt(choice) < 1) {
return false;
}
return true;
}
}
****** Output ******
1.Make a deposit
2.Make a withdrawl
3.Output balance
4.Output all deposits
5.Output all withdrawls
6.Quit
Enter option:
sdadadad
Invalid input!
1.Make a deposit
2.Make a withdrawl
3.Output balance
4.Output all deposits
5.Output all withdrawls
6.Quit
Enter option:
1
Enter deposit amount :
acsaccac
Invalid input
Enter deposit amount :
23332
1.Make a deposit
2.Make a withdrawl
3.Output balance
4.Output all deposits
5.Output all withdrawls
6.Quit
Enter option:
2
Enter withdrwal amount :
sdadadad
Invalid input
Enter withdrwal amount :
211
1.Make a deposit
2.Make a withdrawl
3.Output balance
4.Output all deposits
5.Output all withdrawls
6.Quit
Enter option:
9
Invalid input!
1.Make a deposit
2.Make a withdrawl
3.Output balance
4.Output all deposits
5.Output all withdrawls
6.Quit
Enter option:
3
Account Balance : 23121.00
1.Make a deposit
2.Make a withdrawl
3.Output balance
4.Output all deposits
5.Output all withdrawls
6.Quit
Enter option:
6
Enter file Name:
balance.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.