JAVA基础编程跟constructors有关

完成以下编程要求

您必须实现以下类:
请记住,对于所有类,您必须添加:
构造函数
二传手
吸气剂
toString 方法
具有以下属性和方法的类医院(0.5m)
名称:字符串
地址:字符串
容量:整数
患者:患者类的数组列表(见下文)
医生:医生类的数组列表(见下文)
护士类 Nurse 的数组列表(见下文)
addDoctor 它将所有必需的属性作为输入来创建一个新的 Doctor 实例。该方法必须将新实例添加到医生数组列表中。
addPatient 将所有必需的属性作为输入来创建 Patient 的新实例。如果当前患者数量小于容量,则该方法必须将新实例添加到患者数组列表中。
addNurse 将所有必需的属性作为输入来创建 Nurse 的新实例。该方法必须将新实例添加到护士数组列表中。
具有以下属性的类 Person (0.2m)
名称:字符串
年龄:整数
使用以下属性和方法扩展类 Person 的类 Staff (0.4m)
部门:字符串
一个名为“earnings”的抽象方法返回一个双精度值表示员工的收入
使用以下内容扩展类 Person 的类 Patient (0.3m)
案例描述:字符串
扩展类 Staff 的类 Doctor 具有以下属性和方法:(0.3m)
特长:字符串
基本工资:双倍
实现将基本工资作为医生收入返回的方法收入
使用以下属性和方法扩展 Staff 类的 Nurse 类:(0.3m)
每周小时数:双倍
小时率:双倍
方法收入的实现返回 hoursPerWeek*hourRate 作为护士的收入

img

img

二传手,吸气剂哈哈哈哈哈哈


public class Hospital {
    private String name;
    private String address;
    private Integer capacity;
    private List<Patient> patients = new ArrayList<Patient>();
    private List<Doctor> doctors = new ArrayList<Doctor>();
    private List<Nurse> nurses = new ArrayList<Nurse>();

    public Hospital(String name, String address, Integer capacity) {
        this.name = name;
        this.address = address;
        this.capacity = capacity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getCapacity() {
        return capacity;
    }

    public void setCapacity(Integer capacity) {
        this.capacity = capacity;
    }

    public void addDoctor(Doctor doctor){
        doctors.add(doctor);
    }

    public void addPatient(Patient patient){
        if(capacity > patients.size()){
            patients.add(patient);
        }
    }

    public void addNurse(Nurse nurse){
        nurses.add(nurse);
    }

    @Override
    public String toString() {
        return "Hospital{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", capacity=" + capacity +
                ", patients=" + patients +
                ", doctors=" + doctors +
                ", nurses=" + nurses +
                '}';
    }
}

public class Person {
    private String name;
    private Integer age;

    public Person() {
        super();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public abstract class Staff extends Person{
    private String department;
    public abstract double earnings();

    public Staff() {
        super();
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Staff{" +
                "department='" + department + '\'' +
                '}';
    }
}

public class Patient extends Person{
    private String caseDescription;

    public Patient(String caseDescription) {
        this.caseDescription = caseDescription;
    }

    public void setCaseDescription(String caseDescription) {
        this.caseDescription = caseDescription;
    }

    public String getCaseDescription() {
        return caseDescription;
    }

    @Override
    public String toString() {
        return "Patient{" +
                "caseDescription='" + caseDescription + '\'' +
                '}';
    }
}

public class Doctor extends Staff{
    private String speciality;
    private double baseSalary;

    public Doctor(String speciality, double baseSalary) {
        this.speciality = speciality;
        this.baseSalary = baseSalary;
    }

    public String getSpeciality() {
        return speciality;
    }

    public void setSpeciality(String speciality) {
        this.speciality = speciality;
    }

    public double getBaseSalary() {
        return baseSalary;
    }

    public void setBaseSalary(double baseSalary) {
        this.baseSalary = baseSalary;
    }

    @Override
    public double earnings(){
        return this.baseSalary;
    }

    @Override
    public String toString() {
        return "Doctor{" +
                "speciality='" + speciality + '\'' +
                ", baseSalary=" + baseSalary +
                '}';
    }
}

public class Nurse extends Staff{
    private double hoursPerWeek;
    private double hourRate;

    public Nurse(double hoursPerWeek, double hourRate) {
        this.hoursPerWeek = hoursPerWeek;
        this.hourRate = hourRate;
    }

    public double getHoursPerWeek() {
        return hoursPerWeek;
    }

    public void setHoursPerWeek(double hoursPerWeek) {
        this.hoursPerWeek = hoursPerWeek;
    }

    public double getHourRate() {
        return hourRate;
    }

    public void setHourRate(double hourRate) {
        this.hourRate = hourRate;
    }

    @Override
    public double earnings(){
        return hoursPerWeek*hourRate;
    }

    @Override
    public String toString() {
        return "Nurse{" +
                "hoursPerWeek=" + hoursPerWeek +
                ", hourRate=" + hourRate +
                '}';
    }
}

import java.util.*;

class Hospital{
    private String name;
    private String address;
    private int capacity;
    private ArrayList<Patient> patients;
    private ArrayList<Doctor> doctors;
    private ArrayList<Nurse> nurses;
    public Hospital(){
        this("","",0,null,null,null);
    }
    public Hospital(String name,String address,int capacity,ArrayList<Patient> patients,ArrayList<Doctor> doctors,ArrayList<Nurse> nurses){
        this.name = name;
        this.address = address;
        this.capacity = capacity;
        this.patients = patients;
        this.doctors = doctors;
        this.nurses = nurses;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setAddress(String address){
        this.address = address;
    }
    public String getAddress(){
        return address;
    }
    public void setCapacity(int capacity){
        this.capacity = capacity;
    }
    public int getCapacity(){
        return capacity;
    }
    public void setPatients(ArrayList<Patient> patients){
        this.patients = patients;
    }
    public ArrayList<Patient> getPatients(){
        return patients;
    }
    public void setDoctors(ArrayList<Doctor> doctors){
        this.doctors = doctors;
    }
    public ArrayList<Doctor> getDoctors(){
        return doctors;
    }
    public void setNurses(ArrayList<Nurse> nurses){
        this.nurses = nurses;
    }
    public ArrayList<Nurse> getNurses(){
        return nurses;
    }
    
    public void addDoctor(Doctor doctor){
        doctors.add(doctor);
    }
    public void addPatient(Patient patient){
        if(patients.size() < capacity){
            patients.add(patient);
        }else{
            System.out.println("Hospital is full!");
        } 
    }
    public void addNurse(Nurse nurse){
        nurses.add(nurse);
    }
    
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(" name:"+name);
        sb.append(" address:"+address); 
        sb.append(" capacity:"+capacity);
        return sb.toString();
    }
}

class Person{
    private String name;
    private int age;
    public Person(){
        this("",0);
    }
    public Person(String name , int age){
        this.name = name;
        this.age= age;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setAge(int age){
        this.age= age;
    }
    public int getAge(){
        return age;
    } 
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(" name:"+name);
        sb.append(" age:"+age);  
        return sb.toString();
    }
}

abstract class Staff extends Person{
    private String department;
    public Staff(){
        this("",0,"");
    }
    public Staff(String name , int age,String department){
        super(name,age);
        this.department = department; 
    }
    public void setDepartment(String department){
        this.department = department;
    }
    public String getDepartment(){
        return department;
    } 
    
    public abstract double earnings(); 
    
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(" name:"+getName());
        sb.append(" age:"+getAge());   
        sb.append(" department:"+department); 
        return sb.toString();
    }
}

class Patient extends Person{
    private String caseDescription;
    public Patient(){
        this("",0,"");
    }
    public Patient(String name , int age,String caseDescription){
        super(name,age);
        this.caseDescription = caseDescription; 
    } 
    public void setCaseDescription(String caseDescription){
        this.caseDescription = caseDescription;
    }
    public String getCaseDescription(){
        return caseDescription;
    } 
     
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(" name:"+getName());
        sb.append(" age:"+getAge());   
        sb.append(" caseDescription:"+caseDescription); 
        return sb.toString();
    }
}


class Doctor extends Staff{
    private String speciality;
    private double baseSalary;
    public Doctor(){
        this("",0,"","",0);
    }
    public Doctor(String name , int age,String department,String speciality,double baseSalary){
        super(name,age,department);
        this.speciality = speciality; 
        this.baseSalary = baseSalary; 
    } 
    public void setSpeciality(String speciality){
        this.speciality = speciality;
    }
    public String getSpeciality(){
        return speciality;
    } 
    public void setBaseSalary(double baseSalary){
        this.baseSalary = baseSalary;
    }
    public double getBaseSalary(){
        return baseSalary;
    } 
    
    public double earnings(){
        return baseSalary;
    } 
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(" name:"+getName());
        sb.append(" age:"+getAge());   
        sb.append(" department:"+getDepartment()); 
        sb.append(" speciality:"+speciality);   
        sb.append(" baseSalary:"+baseSalary); 
        return sb.toString();
    }
}

class Nurse extends Staff{
    private double hourPerWeek;
    private double hourRate;
    public Nurse(){
        this("",0,"",0,0);
    }
    public Nurse(String name , int age,String department,double hourPerWeek,double hourRate){
        super(name,age,department);
        this.hourPerWeek = hourPerWeek; 
        this.hourRate = hourRate; 
    } 
    public void setHourPerWeek(double hourPerWeek){
        this.hourPerWeek = hourPerWeek;
    }
    public double getHourPerWeek(){
        return hourPerWeek;
    } 
    public void setHourRate(double hourRate){
        this.hourRate = hourRate;
    }
    public double getHourRate(){
        return hourRate;
    } 
    
    public double earnings(){
        return hourRate*hourRate;
    } 
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(" name:"+getName());
        sb.append(" age:"+getAge());   
        sb.append(" department:"+getDepartment()); 
        sb.append(" hourPerWeek:"+hourPerWeek);   
        sb.append(" hourRate:"+hourRate); 
        return sb.toString();
    }
}
public class JavaConstructors{
    public static void main(String[] args){
        Doctor d1 = new Doctor("doctor1",40,"department1","speciality1",1000);
        ArrayList<Doctor> doctors = new ArrayList<Doctor>(); 
        Hospital h = new Hospital("TONGJI","shanghai",5,null,doctors,null);
        h.addDoctor(d1);
        System.out.println(h.toString());
        System.out.println(h.getDoctors().get(0).toString());
    }
}