0

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • you can create a method with `Appointment` as return type. inside the method you create a `new` `Appointment ` and return it. Just rememeber that if you create this method in `Patient` you will have to ìnclude` Àppointment` in it in order to be recognized – Ivan Jul 07 '21 at 09:02
  • Actually, this is where dependency injection comes into play. But if you just wanna see then I am providing an example below. – Amimul Ehsan Rahi Jul 07 '21 at 09:08
  • “passing messages” in Java = calling methods. – Konrad Rudolph Jul 07 '21 at 09:17
  • Does this answer your question? [What is message passing in OOP?](https://stackoverflow.com/questions/34765555/what-is-message-passing-in-oop) – kaya3 Jul 07 '21 at 09:45
  • Thank you all for the comments and the help. @AmimulEhsanRahi, I really appreciate your answer below. It was helpful. Please, how can I have a mechanism that detects when an instance of Appointment is created and also stores it in an array. – Celestine Akpanoko Jul 16 '21 at 01:42
  • 1
    @CelestineAkpanoko you can know appointment was created when you invoke the bookAnAppointment() method.........the method will return an appointment object.......so, you can store the return value of bookAnAppointment() in an array......... – Amimul Ehsan Rahi Jul 16 '21 at 06:19

1 Answers1

0

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