Lotto Kuma
Lotto Kuma

Reputation: 38

How to pass or retrieve linked list from one method to another method?

First i want to retrieve patient linked list from AddPatient() method and show it on ListPatient() Method.

I try to retrieve by changing public static void ListPatient(); method to public static void ListPatient(ListInterface<PatientDetails> patient) but it doesn't work

package dsa;
import dsa.LList;
import dsa.ListInterface;
import java.sql.Time;
import java.util.Date;
import java.util.Scanner;

public class EmergencyClinic {


    public static void main(String[] args){

        MainMenu();

    }

    public static void MainMenu(){

        int n = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Welcome to Emergency Clinic!");
        System.out.println("1. Add Patient");
        System.out.println("2. Serve Patient");
        System.out.println("3. List Patient");
        System.out.print("Please choose your option :");
        n = scan.nextInt();

        switch(n){
            case 1: AddPatient();
                break;
            case 2: ServePatient();
                break;
            case 3: ListPatient();
                break;
            default : System.out.println("Sorry! Invalid Input. Returning to main menu...\n"); MainMenu();
                break;
        }


    }

    public static void AddPatient(){

        ListInterface<PatientDetails> patient = new LList<PatientDetails>();
        Scanner scan = new Scanner(System.in);
        int num=0;

        System.out.print("Please Enter Name :");
        String name = scan.nextLine();
        System.out.print("Please Enter IC No :");
        String ic = scan.nextLine();       
        System.out.print("Please Enter Contact Number :");
        String contactNum = scan.nextLine();
        System.out.print("Please Enter Gender :");
        String gender = scan.nextLine();
        Date date = new Date();
        Long time = date.getTime();
        System.out.print("Please Enter Reason :");
        String reason = scan.nextLine();
        System.out.print("Please Enter Seriousness :");
        String seriousness = scan.nextLine();

        if(patient.isEmpty())
        {
            patient.add(new PatientDetails(name, ic, contactNum, gender,date ,time ,reason,seriousness ));
        }

        MainMenu();


    }

    public static void ServePatient(){

    }

    public static void ListPatient(){

        ListInterface<PatientDetails> patient = new LList<PatientDetails>();
        System.out.println(patient.getLength());
        if (!patient.isEmpty())
        {
            for(int i=0;i<patient.getLength();i++){
            patient.getEntry(i);
            }
        }
        else
        {
            System.out.println("Error in list patients!");
        }
    }
}

Upvotes: 0

Views: 315

Answers (2)

Kent
Kent

Reputation: 195079

It seems that the add, list and serve are three functions. All your methods are static, then you need a static PatientList variable. That is, when user picked add, he added elements in the list, when he chose list, the same list objects would be displayed.

In codes just in your class declare:

private static ListInterface<PatientDetails> patient = new LList<PatientDetails>();

In your add and list method, use this variable directly.

Upvotes: 1

Leprechaun
Leprechaun

Reputation: 971

All your methods are marked as void. That means the have no return value. One might say, they are procedures, not functions. If you want to return a List, you have to change the signature:

public static List AddPatient()

Then you can return your list from the method using keyword return.

return patient;

The parameters in brackets () are all input parameters.

This is a very basic concept of methods/functions. I suggest reading a book for begginers to understand the fundamentals of Java.

Also Java has it's own general-purpose implementation of linked list. You should use it, if you don't have any special requirements for it's implementation.

Upvotes: 0

Related Questions