我正在尝试为客户自动生成一个 id,每次我得到的都是 0。
很确定问题出在public void regCustomer()
.
public class User {
private String firstName, gender, age;
String surename;
private int customerID;
private static int idCounter = 1000;
User next;
public User(String fN, String sn, String g, String a) {
firstName = fN;
surename = sn;
gender = g;
age = a;
//customerID = cID;
next = null;
}
public void setCustomerID() {
customerID = idCounter++;
}
public int getCustomerID() {
return customerID;
}
public String toString() {
return customerID + "\t" + surename + "\t" + firstName + "\t" + age
+ "\t" + gender;
}
}
在Window
课堂上
public void regCustomer() {
//int customerID = 0;//= Integer.parseInt(customerIDField.getText());
String firstName = firstNameField.getText();
String surename = surenameField.getText();
String gender = genderField.getText();
String age = ageField.getText();
if (!firstName.equals("") && !surename.equals("") && !gender.equals("")&& !age.equals("")) {
userA.regCustomer(new User(firstName, surename, gender,age));
User u = new User(firstName, surename, gender,age);
u.getCustomerID();
customerIDField.setText("");
firstNameField.setText("");
surenameField.setText("");
ageField.setText("");
genderField.setText("");
firstNameField.requestFocus();
} else
JOptionPane.showMessageDialog(this, "Alle felt må fylles inn");
}