How can I pass messages between objects in Java

584 Views Asked by At

I am attempting to pass messages between objects in Java with OOP concept. I created two classes named Doctor and Receptionist, and I want instances of the Receptionist class to send messages to Doctor's objects. I also want objects of class Patient to send a message (books an appointment) to objects of class Appointments.

In summary, I want to achieve a relationship and communication between different instances of different classes.

Patient Class

public class Patient 
{
    private int id;
    private String name;
    private int age;
    private String condition;

    public Patient (int id, String name, int age, String condition)
    {
        this.setId(id);
        this.setName("name");
        this.setAge(age);
        this.setCondition("condition");
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public int getId()
    {
        return this.id;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }
    public void setAge(int age)
    {
        this.age = age;
    }
    public int getAge()
    {
        return this.age;
    }
    public void setCondition(String condition)
    {
        this.condition = condition;
    }
    public String getCondition()
    {
        return this.condition;
    }
}

Appointment class

public class Appointment {
    private int appointId;
    private String date;
    private String purpose;
    
    public Appointment (int appointId, String date, String purpose)
    {
        this.setAppointId(appointId);
        this.setDate("date");
        this.setPurpose("purpose");
    }
    public void setAppointId(int appointId)
    {
        this.appointId = appointId;
    }
    public void setDate(String date)
    {
        this.date = date;
    }
    public void setPurpose(String purpose)
    {
        this.purpose = purpose;
    }
}

How can I have a method that books an appointment in the class Patient; and when the method is called, it creates an instance of Appointment?

1

There are 1 best solutions below

0
On

Your Patient class may look like this:

public class Patient {
    private int id;
    private String name;
    private int age;
    private String condition;

    public Patient( int id, String name, int age, String condition ) {
        this.setId(id);
        this.setName("name");
        this.setAge(age);
        this.setCondition("condition");
    }

    public void setId( int id ) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

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

    public String getName() {
        return this.name;
    }

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

    public int getAge() {
        return this.age;
    }

    public void setCondition( String condition ) {
        this.condition = condition;
    }

    public String getCondition() {
        return this.condition;
    }

    public Appointment bookAnAppointment( int appointId, String date, String purpose ) {
        return new Appointment(appointId, date, purpose);
    }
}