I'm a first year CompSci student creating a hospital management system for an assignment. I've created an arraylist for patients which the user populates. I'm also creating a pre-defined arraylist for doctors. The issue is that the ArrayList doesn't recognise the class Doctor defined immediately below it. This also causes problems further down the code where i'm attempting to call the Array to link doctors and patients in a HashMap.
The code is below:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.*;
public class AandEDepartment {
public static void main(String[] args) {
ArrayList<Doctor> doctors = new ArrayList<>();
doctors.add(new Doctor("James", "Cardiovascular"));
doctors.add(new Doctor("Richard", "Dermatology"));
doctors.add(new Doctor("Peter", "Orthopedics"));
class Doctor {
String name;
String specialisation;
public Doctor(String name, String specialisation){
this.name = name;
this.specialisation = specialisation;
}
//create getters for each of these variables
public String getName() {
return name;
}
public String getSpecialisation() {
return specialisation;
}
class Patient {
int patientID;
String patientFirstName;
String patientLastName;
int patientAge;
String condition;
double cost;
public Patient (int patientID, String patientFirstName, String patientLastName, int patientAge, String condition, double cost)
Is this something to do with having multiple classes (doctor/patient)? I've tried redefining 'doctor' and 'patient' as public, but this creates a different error (illegal modifier). Any ideas?
Thanks.
CodePudding user response:
In java, the best practice is to seperate each classes in their own file. You are currently defining Doctor
and Patient
within AandEDepartment
Both of them should be in a different file. Your IDE should automatically import those classes when called in you main
class.
Also, you should not declare classes within a method. Refer to https://www.w3schools.com/java/java_inner_classes.asp if you absolutly want to declare Doctor
and Patient
in AandEDepartment
even though you shouldn't take that kind of habit in your programming journey.
Hope this helped
CodePudding user response:
As others commented:
Your Doctor
and Patient
classes are core parts of your business logic. They are likely to be used in various parts of your app. So they should be stored in their own .java
files rather than nested within another class.
Also, if the main purpose of your class is to communicate data transparently and immutably, define your class as a record. In a record, the compiler implicitly creates the constructor, getters, equals
& hashCode
, and toString
.
In a separate Doctor.java
file:
package work.basil.example.hospital;
public record Doctor(String name , String specialization ) { }
In a separate Patient.java
file:
package work.basil.example.hospital;
public record Patient(
int patientID ,
String patientFirstName ,
String patientLastName ,
int patientAge ,
String condition ,
double cost
) { }
By the way, your Patient
class looks like you are conflating two different entities, the patient (person) versus their treatment. But I will ignore that issue here.
Define a class for the department. Again, use a record if and only if the main purpose of the class is to communicate data transparently and immutably. Otherwise rewrite to be a conventional class.
In a separate Department.java
file:
package work.basil.example.hospital;
import java.util.Set;
public record Department( String name , Set < Doctor > doctors ) { }
Write an app class to populate some data.
In a separate App.java
file:
package work.basil.example.hospital;
import java.util.Set;
public class App
{
public static void main ( String[] args )
{
String deptName = "A&E";
Set < Doctor > doctorSet =
Set.of(
new Doctor( "James" , "Cardiovascular" ) ,
new Doctor( "Richard" , "Dermatology" ) ,
new Doctor( "Peter" , "Orthopedics" )
);
Department aAndE = new Department( deptName , doctorSet );
System.out.println( "aAndE = " aAndE );
}
}
When run:
aAndE = Department[name=A&E, doctors=[Doctor[name=James, specialization=Cardiovascular], Doctor[name=Peter, specialization=Orthopedics], Doctor[name=Richard, specialization=Dermatology]]]