Question

I have created a class, constructors and accessors. I am wondering if there is an easier way to do this?

I have a Patient class:

public Patient(final String ptNo, final String ptName,
        final String procDate, final int procType, final String injury,
        final String drName) throws IOException
{
    Patient.ptNo = getPtNo();
    Patient.ptName = getPtName();
    Patient.procDate = getProcDate();
    Patient.procType = getProcType();
    Patient.injury = getPtNotes();
    Patient.drName = getDrName();
}

And a getter for this class.

public static Patient getNewPt(String ptNo, String ptName,
        String procDate, int procType, String 
        injury, String drName) throws IOException
{
    Patient newPt = new Patient (ptNo,
            ptName, procDate, procType, injury, drName);

    return newPt;
}

So I could create new patients:

    public static void main(String[] args) throws IOException
    {

        Patient.getNewPt(null, null, null, 0, null, null);
        // creating an array of 5 patients
        Patient patients[] = new Patient[5];

        int i = 0;

        for (i = 0; i < 5; i++)
        {
            patients[i] = Patient.getNewPt(null, null, null, i, null, null);

        }
        Patient.getOption();
    }

And also create new patients through a menu option:

public static String getOption() throws IOException
{
    System.out.println("bla bla");

    option = stdin.readLine();

    switch (option)
    {
    case ("N"):
    Patient newPt = new Patient (ptNo,
            ptName, procDate, procType, injury, drName);
        break;// and so on

I asked another question For loop accepting an extra array member and then realised something that I thought might make a helpful Q&A for newcomers like myself.

Was it helpful?

Solution

This is wrong:

public Patient(final String ptNo, final String ptName,
        final String procDate, final int procType, final String injury,
        final String drName) throws IOException
{
    Patient.ptNo = getPtNo();
    Patient.ptName = getPtName();
    Patient.procDate = getProcDate();
    Patient.procType = getProcType();
    Patient.injury = getPtNotes();
    Patient.drName = getDrName();
}

As you're completely ignoring all values passed in as parameters. Instead do:

public Patient(final String ptNo, final String ptName,
        final String procDate, final int procType, final String injury,
        final String drName) throws IOException
{
    Patient.ptNo = ptNo;
    Patient.ptName = ptName;
    Patient.procDate = procDate;
    Patient.procType = procType;
    Patient.injury = injury;
    Patient.drName = drName;
}

Where here you're setting your class's fields with the parameter values.


A simple example of what I mean using a class with two fields:

Client.java

public class Client {
   private String clientNo;
   private String clientName;

   public Client(String clientNo, String clientName) {
      this.clientNo = clientNo;
      this.clientName = clientName;
   }

   public String getClientNo() {
      return clientNo;
   }

   public void setClientNo(String clientNo) {
      this.clientNo = clientNo;
   }

   public String getClientName() {
      return clientName;
   }

   public void setClientName(String clientName) {
      this.clientName = clientName;
   }

   @Override
   public String toString() {
      return "Client [clientNo=" + clientNo + ", clientName=" + clientName
            + "]";
   }

}

Note that there is no user interaction in the above code, none.

And a separate class for user interface, here quite simple and all in the main method:

ClientTest.java

import java.util.Scanner;

public class ClientTest {
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);

      Client[] clients = new Client[5];

      for (int i = 0; i < clients.length; i++) {
         System.out.print("Enter Client Number: ");
         String clientNumber = scanner.nextLine();
         System.out.print("Enter Client Name: ");
         String name = scanner.nextLine();

         clients[i] = new Client(clientNumber, name);
      }

      scanner.close();

      for (Client client : clients) {
         System.out.println(client);
      }
   }
}

OTHER TIPS

I realise I don't need getNewPt. I can create new Patients like so:

Patient Management class:

public static void main(String[] args) throws IOException
{
    // creating an array of 5 patients
    Patient patients[] = new Patient[5];

    int i = 0;

    for (i = 0; i < 5; i++)
    {
        patients[i] = new Patient(null, null, null, i, null, null);

    }
    Patient.getOption();
}

and so:

public static String getOption() throws IOException
{
    System.out.println("bla bla");

    option = stdin.readLine();

    switch (option)
    {
    case ("N"):
        new Patient (ptNo, ptName,
                procDate, procType, injury, drName);
        break; //and so on
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top