user input going to wrong ArrayList
I am having trouble figuring out how to get my inputs to go to the right array list. Here is some of my code:
Code java:
public class Week01 {
String givenName = null;
String surname = null;
String customerID = null;
String employeeID = null;
String phoneNumber = null;
String streetAddress = null;
String city = null;
String state = null;
String zip = null;
String zipPlus4 = null;
ArrayList<Customer> mycustomer;
ArrayList<Employee> myemployee;
private JFrame frame;
private JTextField tfgiven;
private JTextField tflast;
private JTextField tfpersonID;
private JTextField tfphone;
private JTextField tfaddress;
private JTextField tfcity;
private JTextField tfstate;
private JTextField tfzip;
private JTextField tfzip4;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Week01 window = new Week01();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Week01() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() { JButton btnAddCust = new JButton("Add Customer");
btnAddCust.setFont(new Font("Tahoma", Font.BOLD, 11));
btnAddCust.setBounds(25, 239, 123, 40);
frame.getContentPane().add(btnAddCust);
mycustomer = new ArrayList<Customer>();
btnAddCust.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
mycustomer.add(new Customer(givenName, surname, 0));
givenName = tfgiven.getText();
tfgiven.setText("");
surname = tflast.getText();
tflast.setText("");
customerID = tfpersonID.getText();
tfpersonID.setText("");
if(customerID.trim().equals("")) {
JOptionPane.showMessageDialog(null, "Customer ID required.");
} else {
Integer.parseInt(customerID);
phoneNumber = tfphone.getText();
tfphone.setText("");
streetAddress = tfaddress.getText();
tfaddress.setText("");
city = tfcity.getText();
tfcity.setText("");
state = tfstate.getText();
tfstate.setText("");
zip = tfzip.getText();
tfzip.setText("");
if(zip.trim().equals("")) {
return;
} else {
Integer.parseInt(zip);
}
zipPlus4 = tfzip4.getText();
tfzip4.setText("");
if(zipPlus4.trim().equals("")) {
return;
} else {
Integer.parseInt(zipPlus4);
}
}
}
});
JButton btnAddEmpl = new JButton("Add Employee");
btnAddEmpl.setFont(new Font("Tahoma", Font.BOLD, 11));
btnAddEmpl.setBounds(158, 239, 117, 40);
frame.getContentPane().add(btnAddEmpl);
myemployee = new ArrayList<Employee>();
btnAddEmpl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
myemployee.add(new Employee(givenName, surname, false, 0));
givenName = tfgiven.getText();
tfgiven.setText("");
surname = tflast.getText();
tflast.setText("");
employeeID = tfpersonID.getText();
tfpersonID.setText("");
if(employeeID.trim().equals("")) {
JOptionPane.showMessageDialog(null, "Employee ID required.");
} else {
Integer.parseInt(employeeID);
phoneNumber = tfphone.getText();
tfphone.setText("");
streetAddress = tfaddress.getText();
tfaddress.setText("");
city = tfcity.getText();
tfcity.setText("");
state = tfstate.getText();
tfstate.setText("");
zip = tfzip.getText();
tfzip.setText("");
if(zip.trim().equals("")) {
return;
} else {
Integer.parseInt(zip);
}
zipPlus4 = tfzip4.getText();
tfzip4.setText("");
if(zipPlus4.trim().equals("")) {
return;
} else {
Integer.parseInt(zipPlus4);
}
}
}
});
JButton btnDisplayAll = new JButton("Display All");
btnDisplayAll.setForeground(new Color(255, 192, 203));
btnDisplayAll.setBackground(new Color(0, 139, 139));
btnDisplayAll.setFont(new Font("Tahoma", Font.BOLD, 11));
btnDisplayAll.setBounds(320, 239, 105, 40);
frame.getContentPane().add(btnDisplayAll);
btnDisplayAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < mycustomer.size(); i++) {
System.out.println("Customer ID: " + mycustomer.get(i).getCustomerID() +
"\n" + mycustomer.get(i).toString());
}
for(int j = 0; j < myemployee.size(); j++) {
System.out.println("Employee ID: " + myemployee.get(j).getEmployeeID() +
"\n" + myemployee.get(j).toString());
}
}
});
}
}
The problem I am having right now is that when I enter in information in the text fields and hit the Add Customer button, that data is being sent to the ArrayList<Employee> myemployee instead of mycustomer arrayList and the data that I input and hit the Add Employee button is going off into never never land. Here is an example of what my results look like:
Code java:
Customer ID: 0
null null
unknown
unknown
unknown, unknown, 99999-9999
Employee ID: 0
some person
unknown
unknown
unknown, unknown, 99999-9999
So my other issue is getting my inputs to read in the output. The "some person" was an entry I did and hit the Add Customer button, but it showed up in the employee array. This is where I am not sure what direction I should be going.
Re: user input going to wrong ArrayList
Your logic is in the wrong order. You create the new Employee / Customer object before you assign the Strings (such as 'givenName'). Try to limit the scope of your variables by declaring them closer to where they're being used. The reason you're seeing data from one in the other is that you're doing
Code :
create a new employee from the (null) temporary variables
assign all the temporary variables
create a new customer from the (assigned for employee) temporary variables
assign all the temporary variables