Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class called Customer. The Customer class should contain the following

ID: 3605155 • Letter: D

Question

Design a class called Customer.

The Customer class should contain the following data:
firstName
lastName
accountNumber
accountBalance

Your class should have 2 constructors: one that just accepts the customer’s name, and one that also accepts the new account number and initial balance.

Your class should implement the “sets” and “gets” for the class, and contain a method to deposit, and a method to withdraw.

We will also add methods to “compare”, test for “equals”, and prepare information for display.

compile the Customer class, including the methods: equals, compareTo, toString.
Use a driver class to:
1. Input information for a new customer from the keyboard.
2. Create the customer.
3. Deposit into the customer’s account.
4. Withdraw from the customer’s account.
5. Edit the customer’s name.
6. Display the customer’s information using the toString method.

You are writing a program to maintain customers’ information and transactions for a (small) bank.

Your program will use the Customer class designed in class, with each individual customer stored in a sorted array of Customer. You may use the compareTo method to “sort” the customers by name.

Your program should be able to:

1. Find a customer’s account by account number for deposits and withdrawals.
2. Find a customer’s account by searching for the customer’s name.
3. Deposit into an account, and show the new balance.
4. Withdraw from an account and show the new balance, if there is enough money!
5. Display a selected customer’s information.
6. Display all of the customers. (This will become a text file soon!)

Explanation / Answer

Customer.java

import java.util.Comparator;

public class Customer implements Comparable {

private String firstName;

private String lastName;

private String accountNumber;

private double accountBalance;

public Customer(String firstName, String lastName) {

super();

this.firstName = firstName;

this.lastName = lastName;

}

public Customer(String firstName, String lastName, String accountNumber, double accountBalance) {

super();

this.firstName = firstName;

this.lastName = lastName;

this.accountNumber = accountNumber;

this.accountBalance = accountBalance;

}

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 String getAccountNumber() {

return accountNumber;

}

public void setAccountNumber(String accountNumber) {

this.accountNumber = accountNumber;

}

public double getAccountBalance() {

return accountBalance;

}

public void setAccountBalance(double accountBalance) {

this.accountBalance = accountBalance;

}

public void display(){

System.out.println("First Name:"+firstName);

System.out.println("Last Name:"+lastName);

System.out.println("Account Number:"+accountNumber);

System.out.println("Account Balance:"+accountBalance);

}

public double withDraw(double amount){

accountBalance-=amount;

return accountBalance;

}

public double deposit(double amount){

accountBalance+=amount;

return accountBalance;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

long temp;

temp = Double.doubleToLongBits(accountBalance);

result = prime * result + (int) (temp ^ (temp >>> 32));

result = prime * result + ((accountNumber == null) ? 0 : accountNumber.hashCode());

result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());

result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Customer other = (Customer) obj;

if (Double.doubleToLongBits(accountBalance) != Double.doubleToLongBits(other.accountBalance))

return false;

if (accountNumber == null) {

if (other.accountNumber != null)

return false;

} else if (!accountNumber.equals(other.accountNumber))

return false;

if (firstName == null) {

if (other.firstName != null)

return false;

} else if (!firstName.equals(other.firstName))

return false;

if (lastName == null) {

if (other.lastName != null)

return false;

} else if (!lastName.equals(other.lastName))

return false;

return true;

}

@Override

public int compareTo(Object o) {

// TODO Auto-generated method stub

Customer c=(Customer)o;

return firstName.compareTo(c.getFirstName());

}

}

TestApp.java

import java.util.Iterator;

import java.util.Random;

import java.util.Scanner;

import java.util.Set;

import java.util.TreeSet;

public class TestApp {

public static String getAccountNumber(){

String accNumber ="SB"+ (100000 + new Random().nextInt(900000));

return accNumber;

}

public static void main(String[] args) {

Set<Customer> custSet=new TreeSet<Customer>();

String custFname;

String custLname;

String accNumber;

Customer c;

Scanner sc=new Scanner(System.in);

int choice;

boolean isAccFound=false;

while(true){

System.out.println("Please choose your option");

System.out.println("1. Create Customer Account");

System.out.println("2. Deposit into Customer Account");

System.out.println("3. Withdraw from Customer Account");

System.out.println("4. Edit Customer's first and last names");

System.out.println("5. Search Customer's account by name");

System.out.println("6. Display Customer's info by account number");

System.out.println("7. Display all customers info");

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

choice=sc.nextInt();

switch (choice) {

case 1:

System.out.println("Please enter custome first name");

custFname=sc.next();

System.out.println("Please enter custome last name");

custLname=sc.next();

c=new Customer(custFname, custLname);

c.setAccountNumber(getAccountNumber());

c.setAccountBalance(0.00);

custSet.add(c);

System.out.println("Account is created successfully. Please note account number for future reference");

System.out.println(c.getAccountNumber());

c.display();

break;

case 2:

System.out.println("Please enter account number");

String accNum=sc.next();

for (Iterator iterator = custSet.iterator(); iterator.hasNext();) {

Customer customer = (Customer) iterator.next();

if(customer.getAccountNumber().equals(accNum)){

isAccFound=true;

System.out.println("Please enter amount to deposit");

System.out.println("account is deposited successfully."

+ "Available balance:"+customer.deposit(sc.nextDouble()));

break;

}

}

if(!isAccFound)

System.out.println("could not find the account number");

break;

case 3:

System.out.println("Please enter account number");

accNum=sc.next();

for (Iterator iterator = custSet.iterator(); iterator.hasNext();) {

Customer customer = (Customer) iterator.next();

if(customer.getAccountNumber().equals(accNum)){

isAccFound=true;

System.out.println("Please enter amount to withdraw");

double bal=sc.nextDouble();

if(customer.getAccountBalance()-bal>0){

System.out.println("account is withdrawn successfully."

+ "Available balance:"+customer.withDraw(bal));

}else{

System.out.println("There is no sufficient balance to withdraw");

}

break;

}

}

if(!isAccFound)

System.out.println("could not find the account number");

break;

case 4:

System.out.println("Please enter account number");

accNum=sc.next();

for (Iterator iterator = custSet.iterator(); iterator.hasNext();) {

Customer customer = (Customer) iterator.next();

if(customer.getAccountNumber().equals(accNum)){

isAccFound=true;

System.out.println("Please enter the first name");

customer.setFirstName(sc.next());

System.out.println("Please enter the last name");

customer.setLastName(sc.next());

System.out.println("customer details are updates successfully");

break;

}

}

if(!isAccFound)

System.out.println("could not find the account number");

break;

case 5:

System.out.println("Please enter customer's first name");

custFname=sc.next();

for (Iterator iterator = custSet.iterator(); iterator.hasNext();) {

Customer customer = (Customer) iterator.next();

if(customer.getFirstName().equals(custFname)){

isAccFound=true;

customer.display();

break;

}

}

if(!isAccFound)

System.out.println("could not find the account");

break;

case 6:

System.out.println("Please enter account number");

accNum=sc.next();

for (Iterator iterator = custSet.iterator(); iterator.hasNext();) {

Customer customer = (Customer) iterator.next();

if(customer.getAccountNumber().equals(accNum)){

isAccFound=true;

customer.display();

break;

}

}

if(!isAccFound)

System.out.println("could not find the account number");

break;

case 7:

for (Iterator iterator = custSet.iterator(); iterator.hasNext();) {

Customer customer = (Customer) iterator.next();

customer.display();

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

}

break;

case 8:

System.exit(0);

break;

default:

break;

}

}

}

}

Sample Output

Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit
1
Please enter custome first name
ABC
Please enter custome last name
DEF
Account is created successfully. Please note account number for future reference
SB151854
First Name:ABC
Last Name:DEF
Account Number:SB151854
Account Balance:0.0
Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit
2
Please enter account number
SB151854
Please enter amount to deposit
100
account is deposited successfully.Available balance:100.0
Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit
3
Please enter account number
SB151854
Please enter amount to withdraw
500
There is no sufficient balance to withdraw
Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit
3
Please enter account number
SB151854
Please enter amount to withdraw
10
account is withdrawn successfully.Available balance:90.0
Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit
4
Please enter account number
SB151854
Please enter the first name
abc
Please enter the last name
defg
customer details are updates successfully
Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit
5
Please enter customer's first name
abc
First Name:abc
Last Name:defg
Account Number:SB151854
Account Balance:90.0
Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit
6
Please enter account number
SB151854
First Name:abc
Last Name:defg
Account Number:SB151854
Account Balance:90.0
Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit
7
First Name:abc
Last Name:defg
Account Number:SB151854
Account Balance:90.0
********************
Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit
1
Please enter custome first name
zzz
Please enter custome last name
aaa
Account is created successfully. Please note account number for future reference
SB806051
First Name:zzz
Last Name:aaa
Account Number:SB806051
Account Balance:0.0
Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit
7
First Name:abc
Last Name:defg
Account Number:SB151854
Account Balance:90.0
********************
First Name:zzz
Last Name:aaa
Account Number:SB806051
Account Balance:0.0
********************
Please choose your option
1. Create Customer Account
2. Deposit into Customer Account
3. Withdraw from Customer Account
4. Edit Customer's first and last names
5. Search Customer's account by name
6. Display Customer's info by account number
7. Display all customers info
8. Quit

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote