This is my program if anything is off lmk. public class Customer { private Strin
ID: 3606746 • Letter: T
Question
This is my program if anything is off lmk.
public class Customer {
private String firstName;
private String lastName;
private String accountNumber;
private String accountBalance;
//Constructors
public Customer(String fname, String lname)
{
firstName = fname;
lastName = lname;
}
public Customer(String fname, String lname, String accNum, double bal)
{
firstName = fname;
lastName = lname;
accountNumber = accNum;
accountBalance = bal;
}
// instance methods
public void setfirstName(String fname)
{ firstName = fname; }
public String getfirstName()
{ return firstName; }
public void setCustomer(String cust)
{ return customer; }
public void deposit(double amount)
{ accountBalance += amount; }
public boolean equals(String acctnum)
{ return accountNumber.equals(acctnum); }
public int CompareTo(Customer other)
{ int x = this.lastName.compareTo(other.lastName);
if(x!=0) return x;
x= this.lastName.compareTo(other.lastName);
if(x!=0) return x;
return(this.accountNumber).compareTo(other.accountNumber);
}
I need help writing a tester for the program above.
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.
Explanation / Answer
/**
* The java program that test the Customer class.
* The program will test the methods of the Customer
* class and print appropriate test results to console.
* */
//CustomerTester.java
import java.util.ArrayList;
public class CustomerTester
{
public static void main(String[] args)
{
//Create an Arraylist of type Customer
ArrayList<Customer>cusList=new ArrayList<Customer>();
//insert items of Customers into the customer list
cusList.add(new Customer("John", "Kim", "AB101", 5000));
cusList.add(new Customer("Mark", "Twain", "AB102", 4000));
cusList.add(new Customer("Stephen", "Kom", "AB103", 3000));
cusList.add(new Customer("Micheal", "John", "AB104", 7000));
//calling searchByID function
Customer temp=searchByID(cusList,"AB104");
System.out.println("Searching Account id :AB104");
System.out.println(temp);
//calling searchByFirstName function
temp=searchByFirstName(cusList,"John");
System.out.println("Searching Account first Name : John");
System.out.println(temp);
//3.Deposit into account and show new balance
temp=searchByID(cusList,"AB104");
System.out.println(" "+temp);
System.out.println("Before deposit : "+temp.getBalance());
double amt=2000;
//calling deposit method
temp.deposit(amt);
System.out.println("After deposit : "+temp.getBalance());
//4.Get selected customer info
temp=cusList.get(3);
System.out.println("Customer at index=3");
System.out.println(temp);
//calling printList
printList(cusList);
}
//4.Printing all the customers in the list
public static void printList(ArrayList<Customer> cusList)
{
System.out.println(" Printing all customers");
for (Customer customer : cusList)
{
//print customer to console
System.out.println(customer.toString());
}
}
/*1. Method to find customer by acnumber**/
private static Customer searchByID(ArrayList<Customer> cusList,
String acno) {
Customer temp=null;
boolean found=false;
for (int i = 0; i < cusList.size() &&!found; i++) {
if(cusList.get(i).getAccountCustomer().equals(acno))
{
temp=cusList.get(i);
found=true;
}
}
return temp;
}
/*2. Method to find customer by first name**/
private static Customer searchByFirstName(ArrayList<Customer> cusList,
String fname) {
Customer temp=null;
boolean found=false;
for (int i = 0; i < cusList.size() &&!found; i++) {
if(cusList.get(i).getfirstName().equals(fname))
{
temp=cusList.get(i);
found=true;
}
}
return temp;
}
}//end of the class
------------------------------------------------------------------------------------
///Customer.java
public class Customer
{
private String firstName;
private String lastName;
private String accountNumber;
private double accountBalance;
//Constructors
public Customer(String fname, String lname)
{
firstName = fname;
lastName = lname;
}
/*Constructor to set first name, last name, account number
and balance */
public Customer(String fname, String lname, String accNum, double bal)
{
firstName = fname;
lastName = lname;
accountNumber = accNum;
accountBalance = bal;
}
// instance methods
public void setfirstName(String fname)
{ firstName = fname; }
public String getfirstName()
{ return firstName; }
public void setAccountCustomer(String accountNumber)
{
this.accountNumber= accountNumber;
}
public String getAccountCustomer()
{
return accountNumber;
}
public void deposit(double amount)
{
accountBalance += amount;
}
public boolean equals(String acctnum)
{
return accountNumber.equals(acctnum);
}
//Returns the balance
public double getBalance()
{
return accountBalance;
}
public int CompareTo(Customer other)
{
int x = this.lastName.compareTo(other.lastName);
if(x!=0) return x;
x= this.lastName.compareTo(other.lastName);
if(x!=0) return x;
return(this.accountNumber).compareTo(other.accountNumber);
}
//String represenation of the Customer class
public String toString()
{
return String.format("%-10s%-10s%-10s%-10.2f",
firstName,lastName,accountNumber,
accountBalance);
}
}
------------------------------------------------------------------------------------
Sample Output:
Searching Account id :AB104
Micheal John AB104 7000.00
Searching Account first Name : John
John Kim AB101 5000.00
Micheal John AB104 7000.00
Before deposit : 7000.0
After deposit : 9000.0
Customer at index=3
Micheal John AB104 9000.00
Printing all customers
John Kim AB101 5000.00
Mark Twain AB102 4000.00
Stephen Kom AB103 3000.00
Micheal John AB104 9000.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.