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

huangapple go评论90阅读模式
英文:

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

问题

以下是翻译好的部分:

测试用例中,在将这3名医生数据添加到Doctors中后,使用以下代码:

List<Doctor> doctors ......;
doctors.add(new Doctor("Ghouse", "Cardiology", 25));

在添加剩余医生数据后,在运行代码时,某些行出现错误,我在代码中标记为"error generated"。

医生类:

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;
    }

    //获取器和设置器方法
    
    public int compareTo(Doctor 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;                   //此行生成错误
    }
       
    public Set<Doctor> getSpecialityDoctor(String spc){
        Set<Doctor> spcDoc = new HashSet<Doctor>();

        //与上面的列表<Doctor>中的循环相同

        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){
            //与上述if部分类似地从set中打印
        } else {
            System.out.println("无效的选择");
        }
    }
}
英文:

In test cases
adding this 3 doctor data into Doctors
Ghouse Cardiology 25, Manzoor Cardiology 15, Sunil Cardiology 15
using

List<Doctor> doctors ......;
doctors.add(new Doctor("Ghouse", "Cardiology", 25) );
after adding remaining both

while running code there is error at some line which I mentioned in code as error generated.

Doctor Class

class Doctor implements Comparable&lt;Doctor&gt; {
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 for get specific doctor data

class DoctorService {	
private List&lt;Doctor&gt; doctorsRepository;
public DoctorService(List&lt;Doctor&gt; doctorsRepository) {
this.doctorsRepository = doctorsRepository;
}
public List&lt;Doctor&gt; getExperiencedDoctors(int expr){
List&lt;Doctor&gt; expDoc = new ArrayList&lt;Doctor&gt;();
for (int j=0; j&lt;doctorsRepository.size(); j++){
if (doctorsRepository.get(j).getExp()&gt;=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&lt;Doctor&gt; getSpecialityDoctor(String spc){
Set&lt;Doctor&gt; spcDoc = new HashSet&lt;Doctor&gt;();
//same for loop as above in list&lt;Doctor&gt;
return spcDoc;            //THIS LINE GENERATES ERROR
}

}

Main Class

public class Source {
private static String doctorsData;
static {
StringBuilder doctors = new StringBuilder();
doctors.append(&quot;Amy-Pediatrics-16;&quot;);
doctors.append(&quot;John-Dermatology-10;&quot;
doctorsData = doctors.toString();
}
public static void main(String[] args) {
String[] docStr = doctorsData.split(&quot;;&quot;);
ArrayList&lt;Doctor&gt; Doctors=new ArrayList&lt;Doctor&gt;();
for(int i=0; i&lt;docStr.length; i++){ 
String dd= docStr[i];
Doctors.add(new Doctor(docStr[i].split(&quot;-&quot;)[0], docStr[i].split(&quot;-&quot;)[1], Integer.parseInt(docStr[i].split(&quot;-&quot;)[2])));
}
DoctorService ds = new DoctorService(Doctors);
Scanner sc = new Scanner(System.in);
int ch = sc.nextInt();
if(ch==1){
int lmt = sc.nextInt();
List&lt;Doctor&gt; filtDoc=new ArrayList&lt;Doctor&gt;();
filtDoc = ds.getExperiencedDoctors(lmt);
for (int k=0; k&lt;filtDoc.size(); k++){
System.out.println(filtDoc.get(k).getName() + &quot; &quot; +filtDoc.get(k).getSpeciality()+ &quot; &quot; + filtDoc.get(k).getExp() );
}
}else if(ch==2){
//printing from set as above part in if
}else{
System.out.println(&quot;Invalid Choice&quot;);
}
}
}

答案1

得分: 1

你需要在你的Doctor类中override toString()方法:

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

You need to override toString() method in your Doctor class:

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

huangapple
  • 本文由 发表于 2020年4月6日 18:17:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/61057522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定