英文:
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<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 for get specific doctor data
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
}
}
Main 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");
}
}
}
答案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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论