Create a class called CustomerLister with a main method that instantiates an arr
ID: 3627591 • Letter: C
Question
Create a class called CustomerLister with a main method that instantiates an array of String objects called customerName. The array should have room for five String objects. Use an initializer list to put the following names into the array:Cathy
Ben
Jorge
Wanda
Freddie
Print the array of names.
Add a second double array called customerBalance in the main method. Allow room for five customer balances, each stored as a double. In the loop that prints each customer name, add some code to prompt the user to enter a balance for that customer. Read the keyboard input with a Scanner object. Use the following balances for the input:
100.00
234.56
2.49
32.32
400.00
After all the balances have been entered, print out each customer and his/her balance.
====================================================
The program we constructed above created two parallel arrays that contained information about customers: name and balance.
We can simplify the work we did by creating a Customer class that encapsulates the customer name and balance.
Instead of having two parallel arrays, create an array of Customer objects.
Rewrite the CustomerLister class so that it prompts the user for a customer’s name and balance, and stores the information in a Customer object.
Include a loop that prints each Customer object.
Be sure to add a toString method to the Customer class.
Explanation / Answer
Part 1
// Create a class called CustomerLister
public class CustomerLister
{
// with a main method
public static void main(String[] args)
{
// that instantiates an array of String objects called customerName.
String[] customerName = new String[]{"Cathy", "Ben", "Jorge", "Wanda", "Freddie"};
// Add a second double array called customerBalance in the main method.
double[] customerBalance = new double[5];
// In the loop that prints each customer name, add some code to prompt the user to enter a balance for that customer.
Scanner kb = new Scanner(System.in);
for(int i = 0; i < 5; i++)
{
System.out.print("Balance for "+customerName[i]+": $");
customerBalance[i] = kb.nextDouble();
}
// After all the balances have been entered, print out each customer and his/her balance.
for(int i = 0; i < 5; i++)
System.out.println(customerName[i]+" - $"+String .format("%.2f", customerBalance[i]));
}
}
Part 2
// We can simplify the work we did by creating a Customer class that encapsulates the customer name and balance.
class Customer
{
private String name;
private double balance;
public Customer(String name, double balance)
{
this.name = name;
this.balance = balance;
}
// Be sure to add a toString method to the Customer class.
public String toString()
{
return name+" - $"+String.format("%.2f", balance);
}
}
public class CustomerLister
{
public static void main(String[] args)
{
// Instead of having two parallel arrays, create an array of Customer objects.
Customer[] customers = new Customer[5];
// Rewrite the CustomerLister class so that it prompts the user for a customer’s name and balance, and stores the information in a Customer object.
Scanner kb = new Scanner(System.in);
for(int i = 0; i < customers.length; i++)
{
System.out.print("Customer name: ");
String name = kb.next();
System.out.print("Customer balance: $");
double balance = kb.nextDouble();
customers[i] = new Customer(name, balance);
}
System.out.println();
// Include a loop that prints each Customer object.
for(Customer c : customers)
System.out.println(c);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.