So I have a Store that I created and I wanted to give employees, prodcuts and customers IDs. I created a class called ObjectIDs
that I call in my other classes. Problem is, it gives out null when I actually go and create another class object.
I have a parent class called Person
that has 2 classes which inherit from him: Employee
and Customer
.
So this is Person
:
class Person implements Output {
private String firstName;
private String lastName;
private static final String VALID_PERSON_NAME = "^[a-zA-Z]{2,50}$";
Person(String firstName, String lastName) throws Exception {
this.setNames(firstName, lastName);
}
private void setNames(String firstName, String lastName) throws Exception {
if ( !firstName.matches(VALID_PERSON_NAME) || !lastName.matches(VALID_PERSON_NAME)) {
throw new IllegalArgumentException ("Please only use English");
}
this.firstName = firstName;
this.lastName = lastName;
}
String getNames() {
return this.firstName+" "+lastName;
}
public String createOutput() {
return this.firstName+","+this.lastName;
}
}
Now that we got that out of the way, we'll take Customer
as an example for the null ID I'm getting:
class Customer extends Person{
private String customerID;
Customer(String firstName, String lastName, String customerID) throws Exception {
super(firstName, lastName);
this.customerID = ObjectIDs.generateID(customerID);
}
public String getCustID() {
return customerID;
}
}
And here is the ObjectIDs
class:
import java.util.Random;
public class ObjectIDs {
String id;
public static String generateID(String id) throws Exception {
Random rand = new Random();
int randInt = rand.nextInt(1000) + 100;
String randStr = Integer.toString(randInt);
return randStr;
}
}
我有一个菜单,可以在其中调用相关功能来创建新客户,例如:
Customer newCustomer(String id, String firstName, String lastName) throws Exception {
for ( Customer c : customers) {
if ( id.equals(c.getCustID())) {
throw new IllegalArgumentException("Customer ID already exist.");
}
}
Customer c = new Customer(id, firstName, lastName);
this.customers.add(c);
System.out.println("Customer "+c.createOutput()+" was added to the list");
return c;
}
Now, like I said, when I create a new customer for example, I get null
instead of a number and I'm not sure what I'm missing. I didn't want to overwhem here with huge walls of code so I believe the issue lies within the pieces of code I shared here.
谢谢 :)