预期:是“[GhouseCardiology25,ManzoorCardiology15,SunilCardiology15]”但是:是“[Doctor@74a14482,Doctor@4554617c,Doctor@1540e19d]”

Expected: is "[GhouseCardiology25,ManzoorCardiology15,SunilCardiology15]" but: was "[Doctor@74a14482,Doctor@4554617c,Doctor@1540e19d]"

在测试用例中 将这 3 个医生数据添加到 Doctors Ghouse 心脏病学 25、Manzoor 心脏病学 15、Sunil 心脏病学 15 使用

列出医生......; doctors.add(新医生("Ghouse", "Cardiology", 25)); 添加剩余的两者后

虽然 运行 代码在我在代码中提到的某行有错误生成。

医生Class

class Doctor implements Comparable<Doctor> {

private String name;
private String speciality;
private int experience;

Doctor(String name, String speciality, int experience){
    this.name=name;
    this.speciality=speciality;
    this.experience=experience;
}

//getters and setters methods

public int compareTo(Doctor d2) {
    //comparing objects for sorting
}

}

Class获取特定医生数据

class DoctorService {   
private List<Doctor> doctorsRepository;

public DoctorService(List<Doctor> doctorsRepository) {
    this.doctorsRepository = doctorsRepository;
}

public List<Doctor> getExperiencedDoctors(int expr){
    List<Doctor> expDoc = new ArrayList<Doctor>();

    for (int j=0; j<doctorsRepository.size(); j++){
        if (doctorsRepository.get(j).getExp()>=expr){
            String strnm = (doctorsRepository.get(j)).getName();
            String strspc = (doctorsRepository.get(j)).getSpeciality();
            int dxp = (doctorsRepository.get(j)).getExp();
            expDoc.add(new Doctor(strnm, strspc, dxp));
        }
    }

    Collections.sort(expDoc);
    return expDoc;                   //THIS LINE GENERATES ERROR
}

public Set<Doctor> getSpecialityDoctor(String spc){
    Set<Doctor> spcDoc = new HashSet<Doctor>();

    //same for loop as above in list<Doctor>

    return spcDoc;            //THIS LINE GENERATES ERROR
}

}

主要Class

public class Source {

    private static String doctorsData;

    static {
        StringBuilder doctors = new StringBuilder();
        doctors.append("Amy-Pediatrics-16;");
        doctors.append("John-Dermatology-10;"
        doctorsData = doctors.toString();
    }

    public static void main(String[] args) {
        String[] docStr = doctorsData.split(";");
        ArrayList<Doctor> Doctors=new ArrayList<Doctor>();

        for(int i=0; i<docStr.length; i++){ 
            String dd= docStr[i];
            Doctors.add(new Doctor(docStr[i].split("-")[0], docStr[i].split("-")[1], Integer.parseInt(docStr[i].split("-")[2])));
        }

        DoctorService ds = new DoctorService(Doctors);
        Scanner sc = new Scanner(System.in);
        int ch = sc.nextInt();

        if(ch==1){
            int lmt = sc.nextInt();
            List<Doctor> filtDoc=new ArrayList<Doctor>();
            filtDoc = ds.getExperiencedDoctors(lmt);
            for (int k=0; k<filtDoc.size(); k++){
                System.out.println(filtDoc.get(k).getName() + " " +filtDoc.get(k).getSpeciality()+ " " + filtDoc.get(k).getExp() );
            }
        }else if(ch==2){
           //printing from set as above part in if
        }else{
            System.out.println("Invalid Choice");
        }
    }
}

您需要 override toString() 方法 Doctor class:

class Doctor{
   ...
   @Override
   public String toString() {
      return name+speciality+experience;
   }
}