Text User Interface, can't get a method which adds a customer to work
Re: Text User Interface, can't get a method which adds a customer to work
your (commented-out) method call is wrong
Customer customer = new Customer(fName, lName, acctNumber, street, town, postcode);
//customerList.addCustomer = new Customer(fName, lName, acctNumber, street, town, postcode);
This is not compilable!
customerList.addCustomer(new Customer.......));
Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
and it should be working :)
Re: Text User Interface, can't get a method which adds a customer to work
works like a treat - many thanks eRaaaa
Re: Text User Interface, can't get a method which adds a customer to work
Quote:
Originally Posted by
Stefano
import Java.util
In future, don't edit a post that has a response, as that removed the context of the reply.
This is what was originally posted. I have added code tags for readability.
db
THREAD CLOSED
Quote:
-
this isssignment i'm having a bit of trouble with
i'm given the class called HW4CustomerList, i have to make a Text Based User Interface
the class (below) CustomerTUI contains a method called addCustomer which adds a new object to an arraylist - i'm not particularly sure what is wribg with it
The class specification says that I cannot take any parameters (i.e. a no-args method). we have used the BlueJ 'method box' to interact with objects and put them in array lists, I do not know if this can be used in this particular instance.
below is my CustomerTUI, HW4CustomerList and Customer classes
CustomerTUI class Code:
import java.util.Scanner;
/**
* Provides a text based user interface application to be used to manage the use of HW4CustomerList
*
*/
public class CustomerTUI {
//attributes
private HW4CustomerList customerList;
private Scanner myScanner;
/**
* Constructor for objects of class CustomerTUI
*/
public CustomerTUI() {
customerList = new HW4CustomerList();
myScanner = new Scanner(System.in);
}
public void menu() {
int command = -1;
while (command != 0) {
displayMenu();
command = getCommand();
execute(command);
}
}
/**
* add a customer to the collection
* @param details of the customer to add
*/
private void addCustomer() {
Scanner myScanner = new Scanner(System.in);
System.out.println("Enter the first name of the Customer ");
String fName = myScanner.nextLine();
System.out.println("Enter the last name of the Customer");
String lName = myScanner.nextLine();
System.out.println("Enter the account number of the Customer");
String acctNumber = myScanner.nextLine();
System.out.println("Enter street name of the Customer's address: ");
String street = myScanner.nextLine();
System.out.println("Enter the town name of the Customer's address: ");
String town = myScanner.nextLine();
System.out.println("Enter post code of the Customer's address: ");
String postcode = myScanner.nextLine();
Customer customer = new Customer(fName, lName, acctNumber, street, town, postcode);
//customerList.addCustomer = new Customer(fName, lName, acctNumber, street, town, postcode);
System.out.println("Customer successfully added to the collection");
}
/**
* Displays the Text User Interface
* @param tells the user what key to press to access which function
*/
private void displayMenu() {
System.out.println("¦------------------------------------------------------------¦");
System.out.println("¦Please enter the number of the function you would like to ¦");
System.out.println("¦carry out from the list below ¦");
System.out.println("¦------------------------------------------------------------¦");
System.out.println("¦ Function ¦Option # ¦");
System.out.println("¦Add a Customer to the collection ¦ 1 ¦");
System.out.println("¦Get the number of Customers in the collection ¦ 2 ¦");
System.out.println("¦Show all Customer details ¦ 3 ¦");
System.out.println("¦Remove a Customer from the collection ¦ 4 ¦");
System.out.println("¦Display the details of a customer (Account Number)¦ 5 ¦");
System.out.println("¦quit the application ¦ 6 ¦");
System.out.println("¦------------------------------------------------------------¦");
}
/**
*
*/
private int getCommand() {
System.out.println("\n" + "Enter command: ");
return myScanner.nextInt();
}
private void execute(int command) {
if (command == 1) {
addCustomer();
System.out.println("");
} else if (command == 2) {
getNumberOfCustomers();
System.out.println("");
} else if (command == 3) {
showAllCustomers();
System.out.println("");
} else if (command == 4) {
removeCustomer();
System.out.println("");
} else if (command == 5) {
showCustomer();
System.out.println("");
} else if (command == 6) {
quitCommand();
System.out.println("");
} else {
unknownCommand(command);
System.out.println("");
}
}
private void getNumberOfCustomers() {
if (customerList.getNumberOfCustomers() == 0) {
System.out.println("There are no customers to display");
} else if (customerList.getNumberOfCustomers() == 1) {
System.out.println("We have:" + customerList.getNumberOfCustomers() + " customer");
} else {
System.out.println("We have:" + customerList.getNumberOfCustomers() + " customers");
}
}
private void showAllCustomers() {
if (customerList.getNumberOfCustomers() == 0) {
System.out.println("There are no customers to display");
} else {
customerList.getAllCustomers();
}
}
private void removeCustomer() {
String acctNo;
System.out.print("Enter the account number of the Customer to be removed from the collection");
acctNo = myScanner.next();
if (customerList.removeCustomer(acctNo) == true) {
System.out.println("Customer with account number: " + acctNo + ", was removed successfully");
} else {
System.out.println("Removal of the Customer with account number: " + acctNo + ", was not successful, no such customer");
}
}
private void showCustomer() {
String acctNo;
System.out.println("Enter the account number of the customer to be displayed: ");
acctNo = myScanner.next();
if (customerList.getCustomer(acctNo) == false) {
System.out.println("Customer with account number: " + acctNo + "was not found");
System.out.println("Please try again");
} else {
return;
}
}
private void quitCommand() {
System.out.println("This application is now closing");
System.exit(0);
}
private void unknownCommand(int command) {
System.out.println("Command #" + command + " is not valid, Please enter an integer between 1-6");
}
} // end class