Question

Instructions Using the BCException.java and the BankCustomer.java from the first part of the assignment you will...

Instructions

Using the BCException.java and the BankCustomer.java from the first part of the assignment you will implement a driver class called Bank.java to manipulate an ArrayList of BankCustomer objects.

Your code should read bank customers’ information from the user. It will then create a bank customer objects and add them to an arraylist in a sorted fashion (sorted by account number). The arraylist should be sorted in ascending order at all times. The arraylist should not contain null objects at any point. You may NOT add a BankCustomer and then sort the arraylist, you should insert the new element in the appropriate position. The arraylist should not contain duplicate bank customers. For the purpose of this assignments duplicate bank customers are those that have the same account number.

When removing a bank customer you should find the customer using the account number. You will search for the account number and if found, delete the customer from the arraylist. If not found you should display a message saying so and go back to the main menu.

Printing all the bank customers should be done in ascending order, which is the same way that the arraylist is sorted.

Bank.java should not end execution if an exception happens. You should handle exceptions gracefully. Execution should end only when the user decides to exit by selecting 0 from the menu. (50 points)

Your code should display the following menu:

1. Add a bank customer  (50 points)

2. Remove a bank customer (50 points)

3. Print all bank customers (50 points)

0. Exit

The program should end only when the user selects 0 to exit, otherwise the menu should continue to loop executing options as selected by the user. Feel free to use any extra helper/static methods to make your code more efficient and legible. Not required, but you may want to do it for extra credit, as extra credit is given for well-written code.

You should submit the driver class as well as your BankCustomer.java and BCException.java (again) (40 points). If previously, your code allowed for the creation of invalid objects you must review and fix your code or you will get deductions in this assignment as well.

Below is my BankCustomer.java and BCException.java

BankCustomer.java

public class BankCustomer {

private int acctNumber;
private double balance;
private String name;

public BankCustomer(int acctNumber, double balance, String name) throws Exception{
this.setAcctNumber(acctNumber);
this.setBalance(balance);
this.setName(name);
}
  
public BankCustomer(){
this.acctNumber = 10001;
this.balance = 120.0;
this.name = "sample";
}
  
public void setAcctNumber(int acctNumber) throws Exception {
if (acctNumber >= 10001 && acctNumber <= 99999){
this.acctNumber = acctNumber;
}
else{
BCException me = new BCException();
me.setMessage("The account number passed " + acctNumber + " is not valid");
throw (me);
}

}
public void setBalance(double balance) throws Exception {
if (balance >= 100){
this.balance = balance;
}
else {
BCException me = new BCException();
me.setMessage("The amount added " + balance + " should be more than 100");
throw (me);
}

}
public void setName(String name) throws Exception {
if (name.trim().length() >= 5){
this.name = name.trim();
}
else {
BCException me = new BCException();
me.setMessage("The name " + name + " should be atleast 5 non-blank characters");
throw (me);
}

}
public int getAcctNumber() {
return acctNumber;
}

public double getBalance() {
return balance;
}

public String getName() {
return name;
}


}

BCException.java

public class BCException extends Exception{
//one instance variable
private String message;

public void setMessage(String message){
this.message = message;
}

public String getMessage(){
return this.message;
}
}

Please help me create a driver class plus the array requirement that is needed based on the instruction given plus " You should submit the driver class as well as your BankCustomer.java and BCException.java (again) (40 points). If previously, your code allowed for the creation of invalid objects you must review and fix your code or you will get deductions in this assignment as well. " If there is something wrong with my BankCustomer.java or BCException.java please point it out and help me fix it.

Thanks again for helping.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. No modifications has been made to BankCustomer class as it looks OK. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Bank.java

import java.util.ArrayList;

import java.util.Scanner;

public class Bank {

// method to display the menu

static void menu() {

System.out.println("\n1. Add a bank customer");

System.out.println("2. Remove a bank customer");

System.out.println("3. Print all bank customers");

System.out.println("4. Quit");

}

// method to add a customer in sorted order of account number into an array

// list. returns true if addition is successful, else false

static boolean addCustomer(ArrayList<BankCustomer> customers,

BankCustomer newCustomer) {

// looping and finding the proper position to add

for (int i = 0; i < customers.size(); i++) {

// checking if new customer can be added to index i

if (newCustomer.getAcctNumber() < customers.get(i).getAcctNumber()) {

// adding to index i

customers.add(i, newCustomer);

return true; // success

} else if (newCustomer.getAcctNumber() == customers.get(i)

.getAcctNumber()) {

// duplicate found, returning false

return false;

}

}

// adding to the end, if still not added

customers.add(newCustomer);

return true; // success

}

// method to remove a customer by account number, if exists

static boolean removeCustomer(ArrayList<BankCustomer> customers,

int accountNum) {

for (int i = 0; i < customers.size(); i++) {

if (customers.get(i).getAcctNumber() == accountNum) {

// found, removing current customer

customers.remove(i);

return true; // found and removed

}

}

return false; // not found

}

// method to print customers

static void printCustomers(ArrayList<BankCustomer> customers) {

// displaying heading

System.out.printf("%-15s %-15s %-15s\n", "Customer Name",

"Account Number", "Account Balance");

// looping and printing each customer's name, acc num and balance

for (BankCustomer c : customers) {

System.out.printf("%-15s %-15d $%-14.2f\n", c.getName(),

c.getAcctNumber(), c.getBalance());

}

System.out.println("-----------------------------------------------\n");

}

// main method

public static void main(String[] args) {

// creating an array list of BankCustomer

ArrayList<BankCustomer> accounts = new ArrayList<BankCustomer>();

// scanner to read user input

Scanner scanner = new Scanner(System.in);

int ch = 0;

// looping until ch is 4

do {

// displaying menu

menu();

// inside try with multiple catches block, getting and processing

// user input

try {

System.out.print("Enter your choice: ");

//reading choice

ch = Integer.parseInt(scanner.nextLine());

System.out.println();

if (ch == 1) {

//reading details for bank customer

System.out.print("Enter name: ");

String name = scanner.nextLine();

System.out.print("Enter account number: ");

int accNum = Integer.parseInt(scanner.nextLine());

System.out.print("Enter balance: ");

double balance = Double.parseDouble(scanner.nextLine());

  

//creating a customer, will throw BCException if any detail is invalid

BankCustomer cust = new BankCustomer(accNum, balance, name);

//if no exception occurred, adding to the list and displaying the result

if (addCustomer(accounts, cust)) {

System.out.println("Success!");

} else {

System.out

.println("Customer with same account number already exists!");

}

} else if (ch == 2) {

//fetching an account number, removing customer

System.out.print("Enter account number: ");

int accNum = Integer.parseInt(scanner.nextLine());

if (removeCustomer(accounts, accNum)) {

System.out.println("Success!");

} else {

System.out

.println("Customer with given account number does not exist!");

}

} else if (ch == 3) {

//printing customers

printCustomers(accounts);

}

} catch (BCException e) {

//BCException is occurred

System.out.println(e.getMessage());

} catch (Exception e) {

//exception due to non numeric input

System.out.println("Invalid input!");

}

} while (ch != 4);

}

}

/*OUTPUT*/

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 1

Enter name: Oliver Queen

Enter account number: 11223

Enter balance: 270

Success!

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 3

Customer Name Account Number Account Balance

Oliver Queen 11223 $270.00

--------------------------------------------------

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 1

Enter name: John diggle

Enter account number: 10020

Enter balance: 677

Success!

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 3

Customer Name Account Number Account Balance

John diggle 10020 $677.00

Oliver Queen 11223 $270.00

--------------------------------------------------

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 1

Enter name: Barry Allen

Enter account number: 23456

Enter balance: 100

Success!

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 3

Customer Name Account Number Account Balance

John diggle 10020 $677.00

Oliver Queen 11223 $270.00

Barry Allen 23456 $100.00

--------------------------------------------------

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 1

Enter name: kevin

Enter account number: 1230

Enter balance: 300

The account number passed 1230 is not valid

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 2

Enter account number: 12345

Customer with given account number does not exist!

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 2

Enter account number: 11223

Success!

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 3

Customer Name Account Number Account Balance

John diggle 10020 $677.00

Barry Allen 23456 $100.00

--------------------------------------------------

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: x

Invalid input!

1. Add a bank customer

2. Remove a bank customer

3. Print all bank customers

4. Quit

Enter your choice: 4

Add a comment
Know the answer?
Add Answer to:
Instructions Using the BCException.java and the BankCustomer.java from the first part of the assignment you will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • 1- Write a code for a banking program. a) In this question, first, you need to...

    1- Write a code for a banking program. a) In this question, first, you need to create a Customer class, this class should have: • 2 private attributes: name (String) and balance (double) • Parametrized constructor to initialize the attributes • Methods: i. public String toString() that gives back the name and balance ii. public void addPercentage; this method will take a percentage value and add it to the balance b) Second, you will create a driver class and ask...

  • WHERE in this code should you put the main method so that the code will compile...

    WHERE in this code should you put the main method so that the code will compile WITHOUT changing anything else in the code? Simply adding the main method only? public class Account { private String name; private double balance;    public Account(String name, double balance) { this.name = name; if (balance > 0.0) this.balance = balance; } public void deposit(double depositAmount) { if (depositAmount > 0.0) balance += depositAmount; }    public double getBalance() { return balance; } public String...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

  • For this assignment, you will write a program to work with Huffman encoding. Huffman code is...

    For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...

  • 02. Second Assignment – The FacebookUser Class We’re going to make a small change to the...

    02. Second Assignment – The FacebookUser Class We’re going to make a small change to the UserAccount class from the last assignment by adding a new method: public abstract void getPasswordHelp(); This method is abstract because different types of user accounts may provide different types of help, such as providing a password hint that was entered by the user when the account was created or initiating a process to reset the password. Next we will create a subclass of UserAccount...

  • What is wrong with this code? Please bold the changes that were made so this code...

    What is wrong with this code? Please bold the changes that were made so this code runs properly. The Problem is: 11.16 (Catching Exceptions with SuperClasses) Use inheritance to create an exception superclass (called Exception) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherites from ExceptionA and ExceptionC inherits from ExeptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exceptions of types ExceptionB and ExceptionC. I got this far but I'm still getting a lot...

  • Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list...

    Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list using bubble sort Do not modify the test code in each function. You can look at that code for some ideas for implementing the methods. import java.lang.Comparable; import java.util.*; public class IteratorExercise {       public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {        // first line to start you off        ListIterator<E> iit = c.listIterator(), jit;...

  • import java.util.Scanner; import class17.HeapPriorityQueue; import class17.PriorityQueue; /*************** * Homework D * * * Remove any initial...

    import java.util.Scanner; import class17.HeapPriorityQueue; import class17.PriorityQueue; /*************** * Homework D * * * Remove any initial package declaration that might be added to your file in * case you edit it in eclipse. * * The goal of the homework is to create two ArrayList based implementations of * a Priority Queue as explained in Section 9.2 (in 9.2.4 and 9.2.5) of the * textbook. * * These are to be made by completing the classes PQunsorted and PQsorted as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT