从其他课程调用其他方法和工厂

时间:2016-05-06 05:00:39

标签: java class oop methods maps

您好我试图在测试类中打印出来自CarReg的字符串s以及来自DriversLicence的字符串n以及诸如dateofbirth和序列号之类的变量,但是我得到的错误是构造函数是未定义的,我该如何修复它?欢呼声

CarReg类

import java.util.HashMap;
import java.util.Map;

public final class CarReg {

private int number = (int) (Math.random() * 9000) + 1000;
private String stringrep = String.valueOf(number);
// creates random 4 digit number and converts to string
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private int c = (int) (Math.random() * 26);
private String letter = alphabet.substring(c, c + 1);
// creates string of alphabet then takes random character.

public static final Map<String, CarReg> REG = new HashMap<String, CarReg>();
// initialises map REG

private CarReg(String letter, String stringrep) {
    // checks letter and stringrep are valid
    this.letter = letter;
    this.stringrep = stringrep;
}

public CarReg newInstance(String letter, String stringrep) {
    final String s = letter + stringrep;
    if (!REG.containsKey(s))
        REG.put(s, new CarReg(letter, stringrep));
    return REG.get(s);
    // combines strings letter + stringrep to make new string which is
    // entered into map REG, and guarantees its uniqueness
}

public String getLetter() {
    return letter;
}

public int getNumber() {
    return number;
}

public String toString() {
    return stringrep;
  }
  }

许可证类

public final class DriversLicence {
private String driverName;
private Calendar dateOfBirth;
private Calendar dateOfIssue;
private boolean isFull;
private int serialno = (int) (Math.random() * 90) + 10;
private String serial = String.valueOf(serialno);

public static final Map<String, DriversLicence> LICENCE = new        HashMap<String, DriversLicence>();

public DriversLicence newInstance(String driverName, Date dateofIssue,  String serial) {

    final String n = driverName.substring(0) + driverName.substring(5) + "-"    + dateOfIssue.get(Calendar.YEAR) + "-"
            + serial;

    if (!LICENCE.containsKey(n))
        LICENCE.put(n, new DriversLicence(driverName, dateofIssue, serial));
    return LICENCE.get(n);
}

public DriversLicence(String driverName, int dobDay, int dobMonth, int dobYear, int doiDay, int doiMonth,
        int doiYear, boolean isFull) {
    driverName = "John Smith";

    dobDay = 26;
    dobMonth = Calendar.MAY;
    dobYear = 1996;
    Calendar dateOfBirth = Calendar.getInstance();
    dateOfBirth.set(dobDay, dobMonth, dobYear);

    doiDay = 14;
    doiMonth = Calendar.JANUARY;
    doiYear = 2015;
    Calendar dateOfIssue = Calendar.getInstance();
    dateOfIssue.set(doiDay, doiMonth, doiYear);

    isFull = true;

}

public DriversLicence(String driverName, Date dateofIssue, String serial) {
    this.driverName = driverName;
    this.serial = serial;
}

public String getDriverName() {
    return driverName;
}

public Calendar getDateOfBirth() {
    return dateOfBirth;
}

public Calendar getDateOfIssue() {
    return dateOfIssue;
}

public boolean getIsFull() {
    return isFull;
}

public String getSerial() {
    return serial;
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          }

#test class#

public class Test {

 public static void main(String[] args) {

    CarReg registration = new CarReg();
    Map<String, CarReg> REG = CarReg.REG;
    System.out.println(s);
     }

   }

0 个答案:

没有答案