When I successfully register for a customer, this customer can only order the food, how can I save the order of this customer in array? Is it using super class but it cannot work. And also I have to display all the past order of the customer.
Customer class
public class Customer {
protected String name;
protected String id;
protected String home;
protected String email;
protected String orderDate;
protected double amountPaid;
protected int restaurantName;
public Customer(String a, String b, String c, String d){
name = a;
id = b;
home = c;
email = d;
}
public String getName(){
return name;
}
public String getID(){
return id;
}
public String getHome(){
return home;
}
public String getEmail(){
return email;
}
public String getOrderDate(){
return orderDate;
}
public double getAmountPaid(){
return amountPaid;
}
public int getRestaurantName(){
return restaurantName;
}
}
final int MAX = 100;
int customerCount = 0;
Customer[] customerList = new Customer[MAX];
This is how I define the array of object Customer
Order class
public class Order extends Customer{
int restaurant;
String order;
double amount;
String orderDate;
double amountPaid;
int restaurantName;
public Order(String a, String b, String c, String d, int e, String f, double g){
super(a,b,c,d);
e = restaurant;
f = order;
g = amount;
}
public int getRestaurant(){
return restaurant;
}
public String getOrder(){
return order;
}
public double getAmount(){
return amount;
}
}
Register button
private void registerBTNActionPerformed(java.awt.event.ActionEvent evt) {
String name = "", id1 = "", home = "", email = "";
try {
name = nameTF.getText();
if (name.equals("")) {
throw new IllegalArgumentException("You must specify Customer name!");
}
id1 = idTF1.getText();
if (id1.equals("")) {
throw new IllegalArgumentException("You must specify Customer ID!");
}
home = homeTF.getText();
if (home.equals("")) {
throw new IllegalArgumentException("You must specify Customer home address!");
}
email = emailTF.getText();
if (email.equals("")) {
throw new IllegalArgumentException("You must specify Customer email address!");
}
for (int i = 0; i < customerCount; i++) {
if (id1.equals(customerList[i].getID())) {
outputTA.setText("The customer ID is registered before!");
idTF1.setText("");
return;
}
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Illegal Customer Name or Customer ID!");
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
if (id1.length() != 6) {
outputTA.setText("Register unsuccessfully, please key in customer ID in 6 digits!");
idTF1.setText("");
} else if ((name.length() == 0) || (home.length() == 0) || (email.length() == 0)) {
outputTA.setText("Register unsuccessfully, please fill in all the information!");
} else {
customerList[customerCount] = new Customer(name, id1, home, email);
customerCount++;
try {
File fl1 = new File("Customer Registration.txt");
FileWriter fw1 = new FileWriter(fl1);
PrintWriter pw1 = new PrintWriter(fw1);
pw1.println(name);
pw1.println(id1);
pw1.println(home);
pw1.println(email);
pw1.close();
fw1.close();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "Error! File not found!");
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Write operation fails!");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Illegal Customer ID!");
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
outputTA.setText("1 new customer has been succesfully added!");
}
}
Save Order button
private void saveBTNActionPerformed(java.awt.event.ActionEvent evt) {
outputTA.setText("");
String id = "", amountPaid = "", orderDate = "";
int unitIndex = 0;
try {
id = idTF.getText();
String home = homeTF.getText();
String email = emailTF.getText();
String name = nameTF.getText();
unitIndex = restaurantNameCB.getSelectedIndex();
orderDate = orderDateTF.getText();
if (orderDate.equals("")) {
throw new IllegalArgumentException("You must specify order date!");
}
amountPaid = amountPaidTF.getText();
if (amountPaid.equals("")) {
throw new IllegalArgumentException("You must specify amount paid!");
}
double amountP = Double.parseDouble(amountPaid);
for (int i = 0; i < customerCount; i++) {
if (id.equals(customerList[i].getID())) {
id = customerList[i].getID();
unitIndex = customerList[i].getRestaurantName();
orderDate.equals(customerList[i].getOrderDate());
amountP = customerList[i].getAmountPaid();
customerList[customerCount] = new Order(name, id, home, email, unitIndex, orderDate, amountP);
}
}
outputTA.setText("Add order successfully!");
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
if (id.length() != 6) {
outputTA.setText("Register unsuccessfully, please key in customer ID in 6 digits!");
idTF1.setText("");
} else if ((orderDate.length() == 0) || (amountPaid.length() == 0)) {
outputTA.setText("Register unsuccessfully, please fill in all the information!");
} else {
File fl = new File("Customer Order.txt");
try {
FileWriter fw = new FileWriter(fl);
PrintWriter pw = new PrintWriter(fw);
pw.println(id);
pw.println(unitIndex);
pw.println(orderDate);
pw.println(amountPaid);
pw.close();
fw.close();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "Error! File not found!");
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error! ");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Fill in the blanks!");
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
}
}