Java 期望值:是“;[Ghouse心脏病学25,人类心脏病学15,Sunil心脏病学15]”;但是:“是的。”;[Doctor@74a14482,Doctor@4554617c,Doctor@1540e19d]&引用;

Java 期望值:是“;[Ghouse心脏病学25,人类心脏病学15,Sunil心脏病学15]”;但是:“是的。”;[Doctor@74a14482,Doctor@4554617c,Doctor@1540e19d]&引用;,java,assertion,Java,Assertion,在测试用例中 将这3个医生数据添加到医生中 Ghouse心脏病学25、Manzoor心脏病学15、Sunil心脏病学15 使用 列出医生名单。。。。。。; 添加(新医生(“Ghouse”,“心脏病学”,25)); 在添加剩余的两个 在运行代码时,我在代码中提到的某一行出现了错误,即生成的错误 博士班 class Doctor implements Comparable<Doctor> { private String name; private String speciality;

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

列出医生名单。。。。。。; 添加(新医生(“Ghouse”,“心脏病学”,25)); 在添加剩余的两个

在运行代码时,我在代码中提到的某一行出现了错误,即生成的错误

博士班

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
}
类医生实现可比较{
私有字符串名称;
私家弦专业;
私人int经验;
博士(字符串名称、字符串专业、国际经验){
this.name=name;
这个。专业=专业;
这个。经验=经验;
}
//getter和setter方法
公共内部比较(d2医生){
//比较要排序的对象
}
}

用于获取特定医生数据的类

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
}
类博士服务{
私人名单医生资料库;
公共博士服务(列表博士存储库){
this.doctorsRepository=doctorsRepository;
}
公共列表getExperiencedDoctors(int expr){
List expDoc=new ArrayList();
对于(int j=0;j=expr){
字符串strnm=(doctorsRepository.get(j)).getName();
字符串strspc=(doctorsRepository.get(j)).getSpeciality();
int dxp=(doctorsRepository.get(j)).getExp();
expDoc.add(新医生(strnm、strspc、dxp));
}
}
Collections.sort(expDoc);
return expDoc;//此行生成错误
}
公共集getSpecialityDoctor(字符串spc){
Set spcDoc=new HashSet();
//与列表中的上述循环相同
return spcDoc;//此行生成错误
}
}

主类

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");
        }
    }
}
公共类源代码{
私有静态字符串doctorsData;
静止的{
StringBuilder=新建StringBuilder();
附加(“Amy-Pediatrics-16;”;
医生。附加(“约翰皮肤病学-10
doctorsData=doctors.toString();
}
公共静态void main(字符串[]args){
字符串[]docStr=doctorsData.split(“;”);
ArrayList医生=新建ArrayList();

对于(int i=0;i您需要在
Doctor
类中重写
toString()
方法:

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

您的错误消息听起来像是来自单元测试,但您的帖子中没有显示任何单元测试。(您使用了断言标记,但没有包含任何断言…)错误的堆栈跟踪是什么?函数似乎返回了它应该返回的列表。至于您的输出问题,请重写“String Doctor.toString()'以便让医生对象打印您想要的内容。