Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a java program that stores health professionals record and allows them to create a schedule book for each individual professionals.

here's my record class to record appointments

Java
import java.time.LocalDate;
import java.time.LocalTime;

/**
 * Performs actions on the list's nodes
 *
 * @author AC12001 Team
 * @version January 2018
 */
public class record
{
    // instance variables
    private String treatment;
    private LocalDate date;
    private LocalTime endTime;
    private LocalTime startTime;

    /**
     * Constructor for objects of class List
     */
    public record(String treatment,LocalDate date,LocalTime endTime,LocalTime startTime)
    {
        // initialise instance variables
        this.treatment = treatment;
        this.date=date;
        this.endTime = endTime;
        this.startTime=startTime;

    }

    public LocalTime getStartTime() {
        return startTime;
    }

    /**
     * Get the student ID
     *
     * @return student at this node
     */
    public String getTreatment()
    {
        return treatment;
    }

    /**
     * Get the mark
     *
     * @return mark at this node
     */
    public LocalTime getEndTime()
    {
        return endTime;
    }

    public LocalDate getDate()
    {
        return date;
    }

    /**
     * Return a string containing the data from this node, formatted
     */
    public String printRec()
    {
        String info;

        info = "Treatement Type:  " + treatment +" start time:  "+startTime + " end time : "+endTime;

        return info;
    }
}


Here's the health professional class

Java
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
/**
 * Write a description of class doctor here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class doctor
{
    // instance variables - replace the example below with your own
    private String name;
    private String profession;
    private String office;
    public record parent;
    final ArrayList<record> records = new ArrayList<>();
    LocalDate date;
    LocalTime time;
    /**
     * Constructor for objects of class doctor
     */
    public doctor(String name,String profession,String office)
    {
        // initialise instance variables
        this.name=name;
        this.profession=profession;
        this.office=office;
    }

    public String getName()
    {
        return name;
    }

    public String getProfession()
    {
        return profession;
    }

    public String getOffice()
    {
        return office;
    }

    record addRecord(String treatment, LocalDate date, LocalTime start, LocalTime end) {
        // Create a new appointment
        final record holdRecord = new record(treatment, date, end, start);

        // Add it to the doctor appointments
        records.add(holdRecord);
        return holdRecord;
    } 
    
    // Create a getter for all the records created by this doctor
    List<record> getRecords()
    {
        
        return records;
    }
    public void setOffice(String location) {
        
        this.office=location;
      
    }
    public void setName(String name) {
          this.name=name;
        
    }
    public void setProfession(String prof) {
        this.profession=prof;
    }
    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
   
    public String printInfo()
    {
        String info;

        info = "name: " + name +"; profession: "+profession + "; office: "+office;

        return info;
    }
}


and here's the main class that handles the program

Java
import java.util.ArrayList;
import java.util.Iterator;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter; 
/**
 * Write a description of class appointment here.
 * @author (your name)
 * @version (a version number or a date)
 */

public class runCode
{
    LocalDate date;
    LocalTime time;
    // instance variables - replace the example below with your own
    private doctor doc;
    ArrayList<record>profRec=new ArrayList();
    ArrayList<doctor>healthProf=new ArrayList();
    public void run()
    {
        addProf();
        edit();
        print();
    }

    public void addProf()
    {  String firstName=getInput("Enter first name: ").toLowerCase().trim();
        String lastName=getInput("Enter last name: ").toLowerCase().trim();
        String name=firstName+" "+lastName;

        String profession=getInput("Enter profession: ").trim();
        String office=getInput("Enter your work address: ").trim();

        doc=new doctor(name,profession,office);
        healthProf.add(doc);

        System.out.println("added");

    }

    public void delete()
    {
        System.out.println("Which health professional's record would you like to delete ");
        int x=Genio.getInteger()-1;
        
        if(healthProf.size()==0)
            System.out.println("The list is empty, add a value ");
        else if(healthProf.size() !=0 && x>=0 && x<=healthProf.size())
        {
            healthProf.remove(x);
             System.out.println("deleted ");
        }
        else
        {
            System.out.println("Wrong input try again");
        }
    }

    public void edit()
    {

        System.out.println("Which health proffessional's record would you like to edit");
        int x=Genio.getInteger()-1;
        System.out.println("What do you like to edit");
        if(healthProf.size()==0)
            System.out.println("The list is empty, add a value ");
        else if(healthProf.size() !=0 && x>=0 && x<healthProf.size())
        {
            System.out.println("enter 1 to change name");
            System.out.println("enter 2 to change office");
            System.out.println("enter 3 to change profession");
            int d=Genio.getInteger();
            if(d==1)
            {
                String firstName=getInput("Enter first name: ").toLowerCase().trim();
                String lastName=getInput("Enter last name: ").toLowerCase().trim();
                String newName=firstName+" "+lastName;
                healthProf.get(x).setName(newName);
            }
            else if(d==2)
            {
                System.out.println("Enter your new work addreess: ");
                String newOffice=Genio.getString();
                healthProf.get(x).setOffice(newOffice);
            }
            else if(d==3)
            {
                System.out.println("Enter your new profession: ");
                String newProfession=Genio.getString();
                healthProf.get(x).setProfession(newProfession);
            }
            else
            {
                System.out.println("Wrong input try again");
            }
            System.out.println("change saved");
        }
        else
            System.out.println("Wrong input try again");
    }

    public LocalDate date() 
    {
        while (true)
        {
            try
            {

                System.out.println("Input the date :");

                String input = Genio.getString();
                String[] store = input.split("/");
                int day = Integer.parseInt(store[0]);
                int month = Integer.parseInt(store[1]);
                int year = Integer.parseInt(store[2]);
                return date = date.of(year, month, day);
            }catch(Exception de)
            {
                System.out.println("An error occured try again");
                continue;
            }
        }
    }

    public LocalTime time() {
        while (true)
        {
            try
            {
                System.out.println("Input the time use 24-clock format :");

                String inputTime = Genio.getString();
                String[] storeTime = inputTime.split(":");
                int hour = Integer.parseInt(storeTime[0]);
                int minute = Integer.parseInt(storeTime[1]);

                time = time.of(hour, minute);
                break;
            }catch(Exception de)
            {
                System.out.println("An error occured try again");
                continue;
            }

        }
        return time;
    }

    public void print()
    {
        int count=0;
        for(doctor doc:healthProf)
        {
            count++;
            System.out.println(count+" "+doc.printInfo());

        }
        count=0;
    }

    public void printAppointment()
    {
      System.out.println("Select the health professional: ");
      int x=Genio.getInteger()-1;
      int count=0;
      
      Iterator<record> it = healthProf.get(x).getRecords().iterator();
      Iterator<record> it2 = healthProf.get(x).getRecords().iterator();
    while(it.hasNext()) {
      while(it2.hasNext())
      {
          System.out.println(it2.next());
       }
     
      }
      
    }
    
    public void makeAppointment()
    {
        System.out.println("Select the health professional: ");
        int x=Genio.getInteger()-1;
      
        LocalTime start=time();
        LocalTime end=time();
        LocalDate date=date();
        System.out.println("Input the treatment :");

        String treatment= Genio.getString();
        healthProf.get(x).addRecord(treatment,date,end,start);
        
    }

    public String getInput(String prompt)
    {
        System.out.println(prompt);

        return Genio.getString();
    }
}


So basically when you run the program you can create several health professional instances, each health professional is able to create appointment booking using the makeAppointment method. my problem is printing out the appointment for each health professional.

I tried printing it out with the print() method in the doctor class. it works but it prints out all the appointment bookings made not for the specific professional's appointment book.

Also, I tried printing it out with the printAppointment () method in the runCode class, but it prints out record@672f158f and is stuck in an infinite loop

What I have tried:

Java
public void print()
    {
        int count=0;
        for(Record rec:records)
        {
            count++;
            System.out.println(count+" "+rec.printInfo());

        }
        count=0;
    }



I've also tried
Java
public void printAppointment()
    {
        System.out.println("Select the health professional: ");
        int x=Genio.getInteger()-1;
        int count=0;

        Iterator<Record> it2 = healthProf.get(x).getRecords().iterator();
        
        while(it2.hasNext())
        {
            System.out.println(it2.next());
        }

      
    }
Posted
Updated 4-Jun-21 1:12am
v2
Comments
[no name] 4-Jun-21 3:04am    
I didn't see you "adding" any appointments / Records ... so there's nothing to print.

1 solution

The record class should have an override of the toString method, which returns a printable string containing the full details of the record. The doctor class should have a method that prints all the records, by iterating the List and calling using the record.toString method. Your main program can then call that method for each, or any one, doctor in the List.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900