我有一個ArrayList,需要爲管理員添加到醫院的每位醫生生成醫生的ID。所以每次增加一名新的醫生時,該醫生將在醫生課堂的構造師中被分配一個特定的ID。 問題是,每次我添加一位新醫生時,每位醫生都會生成相同的id,即id = 1,這意味着id不會遞增。 請幫我改正這個錯誤。如何爲java中的adminintrator添加到醫院的每位醫生生成一個唯一的ID?
public class Doctor {
private String d_fname;
private String d_lname;
private int d_id;
private String d_qualification;
private String d_specialisation;
private String d_email;
private int d_contact;
public int dcount;
private String d_username;
private String d_pass;
private String spass;
public Doctor(){
}
public Doctor(String d_fname, String d_lname, String d_qualification,String d_specialisation,String d_email, int d_contact, int dcount){
dcount++;
this.d_id=dcount;
this.d_fname=d_fname;
this.d_lname=d_lname;
this.d_qualification=d_qualification;
this.d_specialisation=d_specialisation;
this.d_email=d_email;
this.d_contact=d_contact;
}
public void setID(int dcount){
this.d_id=dcount;
}
public void setFirstName(String d_fname){
this.d_fname=d_fname;
}
public void setLastName(String d_lname){
this.d_lname=d_lname;
}
public void setQualification(String d_qualification){
this.d_qualification=d_qualification;
}
public void setSpecialisation(String d_specialisation){
this.d_specialisation=d_specialisation;
}
public void setEmail(String d_email){
this.d_email=d_email;
}
public void setContact(int d_contact){
this.d_contact=d_contact;
}
public String getFirstName(){
return this.d_fname;
}
public String getLastName(){
return this.d_lname;
}
public int getID(){
return this.d_id;
}
public String getQualification(){
return this.d_qualification;
}
public String getSpecialisation(){
return this.getSpecialisation();
}
public static void main(String[] args) throws FileNotFoundException,
ParseException, IOException {
Scanner input = new Scanner(System.in);
// ArrayList <Patient> patient1 = new ArrayList<Patient>();
ArrayList <Doctor> doctor1 = new ArrayList<Doctor>();
int s=0;
int sh = 0;
int ch = 0;
String pres="";
String d_fname, d_lname,d_qualification, d_specialization, d_email;
int d_contact;
String p_fname, p_lname, p_gender, p_occupation, p_cnic, p_email,p_dob,
p_bloodGroup,p_maritalStatus;
int p_contact,p_id,dcount=0,pcount=0,d_id = 0, app_no=0;
String date;
do{
System.out.println("press 0 to exit");
ch = input.nextInt();
if(ch==0){
System.exit(0);
}
else{
Login login =new Login();
if(login.check()==true){
if(login.getType()==1)
{
do{
System.out.println("press 1 to add the new doctor's record");
System.out.println("press 2 to delete a doctor's record");
System.out.println("Press 3 to log-out");
ch = input.nextInt();
if(ch==1)
{
System.out.println("enter doctor's first name");
d_fname = input.next();
System.out.println("enter doctor's last name");
d_lname = input.next();
System.out.println("enter doctor's qualification");
d_qualification = input.next();
System.out.println("enter doctor's sepcialization");
d_specialization = input.next();
System.out.println("enter doctor's email address");
d_email = input.next();
System.out.println("enter doctor's contact number");
d_contact = input.nextInt();
Doctor d= new
Doctor(d_fname,d_lname,d_qualification,d_specialization,d_email,d_contant
,dcount
);
doctor1.add(d);
System.out.println("id number generated : " + doctor1.get(d_id).getID()); //
here i am getting id number 1 each time i am adding a new doctor
}
if(ch==2)
{
System.out.println("Which doctor do you want to remove? ");
d_id =input.nextInt();
doctor1.remove(doctor1.get(d_id-1));
System.out.println("Doctor with id number: " + d_id + " is removed
successfully.");
}
}
while(ch!=3);
}
}
}
在你的類中有一些靜態字段,比如'numberOfDoctors',它在你的構造函數中增加並且用於分配一個非靜態的id。這樣,每個實例類都知道程序中存在多少個「Doctor」對象,因此它們都可以被分配一個唯一的ID號。 –
1)在工廠方法創建醫生,能跟蹤醫生數增加的方法。 2)請儘量發佈格式良好的代碼。記住你要求志願者來幫助你,所以你永遠不應該讓閱讀和理解你的代碼變得更加困難。 –
嘗試將您的問題重新命名爲考慮您正在嘗試解決的一般編程問題(與您的具體實現(如_doctors_和_hospital_)相反)。這可能會使您的問題更容易讓其他人瞭解,並且還可以幫助未來的訪問者加入到SO中。 –