简体   繁体   中英

Storing several different bits of data into a linked list

I am needing to store 3 different bits of data into a linked list. the first is a name, which i have working, then i need to store the employee number and their occupation. I can't find anything about linking a node to a set of data. any help would be greatly appreciated.

This is the code that runs, TeamMemberTest:

package teammember;

import java.util.*;
import java.util.Scanner;

public class TeamMemberTest {
    public static void main(String args[]) {
    /* Linked List Declaration */
    LinkedList<String> linkedlist = new LinkedList<String>();

    Scanner input = new Scanner(System.in);
    boolean mainloop = true;
    String name;
    int employeeNo;
    String position;
    int choice;
    while(true){
        System.out.println("Menu");
        System.out.print("1. add project \n");
        System.out.print("2. display project \n");
        System.out.print("3. remove project \n");
        System.out.print("4. display all projects \n");
        System.out.print("\nEnter your option: ");

        choice = input.nextInt();

        switch(choice){
            case 1:
                /*add(String Element) is used for adding 
                * the elements to the linked list*/
                name = Input.getString("name: ");
                employeeNo = Input.getInteger("Enter employee number: ");
                position = Input.getString("Enter employee position: ");
                linkedlist.add(name);
                System.out.println("LinkedList after deletion of first and last element: " +linkedlist);
                break;
            case 2:
                name = Input.getString("name: ");
                if(linkedlist.contains(name)){
                    System.out.println("LinkedList does contain " + name);
                }
                break;
            case 3:
                name = Input.getString("name: ");
                linkedlist.remove(name);
                System.out.println("LinkedList after deletion of first and last element: " +linkedlist);
                break;
            case 4:
                System.out.println("Linked List Content: " +linkedlist);
                break;
            default :
                System.out.println("This is not a valid option try again.");
                break;
            }
        }
    }
}

this code below is TeamMember.java:

package teammember;

public class TeamMember {
    //variables
    private String name;
    private int employeeNo;
    private String position;

    //the default constant
    public TeamMember(){
        name = " ";
        employeeNo = 0;
        position = " ";
    }
    //overload the constructor
    public TeamMember(String name, int employeeNo, String position){
        this.name = name;
        this.employeeNo = employeeNo;
        this.position = position;
    }
    //the methods
    public void setName(String name){
        this.name = name;
    }
    public void setemployeeNo(int employeeNo){
        this.employeeNo = employeeNo;
    }
    public void setPosition(String position){
        this.position = position;
    }
    public String getName(){
        return name;
    }
    public int getemployeeNo(){
        return employeeNo;
    }
    public String getPosition(){
        return position;
    }
}

and this is the input code:

package teammember;

import java.io.*;
public class Input{
    private static BufferedReader input=new BufferedReader(new InputStreamReader(System.in));

    public static Character getCharacter(String prompt){
        Character value;
        System.out.print(prompt);
        try{
            value=Input.input.readLine().charAt(0);
        }
        catch(Exception error){
            // error condition
            value=null;
        }
        return value;
    }

public static Double getDouble(String prompt){
    Double value;
    System.out.print(prompt);
    try{
        value=Double.parseDouble(Input.input.readLine());
    }
    catch(Exception error){
        // error condition
        throw new NumberFormatException();
    }
    return value;
}

public static Integer getInteger(String prompt){
    Integer value;
    System.out.print(prompt);
    try{
        value=Integer.parseInt(Input.input.readLine());
    }
    catch(Exception error){
        // error condition
        throw new NumberFormatException();
    }
    return value;
}

public static String getString(String prompt){
    String string;
    System.out.print(prompt);
    try{
        string=Input.input.readLine();
    }
    catch(Exception error){
        // error condition
        string=null;
    }
    return string;
}

}

Instead of creating a LinkedList of String , you need to create a LinkedList of TeamMember as follows:

LinkedList<TeamMember> linkedlist = new LinkedList<TeamMember>();

Then you need to change the cases accordingly eg

choice = input.nextInt();
boolean found;
switch (choice) {
case 1:
    name = Input.getString("name: ");
    employeeNo = Input.getInteger("Enter employee number: ");
    position = Input.getString("Enter employee position: ");
    linkedlist.add(new TeamMember(name, employeeNo, position));
    break;

case 2:
    found = false;
    name = Input.getString("name: ");
    for (TeamMember teamMember : linkedlist) {
        if (teamMember.getName().equalsIgnoreCase(name)) {
            System.out.println("Name: " + teamMember.getName() + "Employee No.: "
                    + teamMember.getemployeeNo() + "Position: " + teamMember.getPosition());
            found = true;
            break;
        }
    }
    if (!found) {
        System.out.println("The team member was not found.");
    }
    break;

case 3:
    found = false;
    name = Input.getString("name: ");
    for (TeamMember teamMember : linkedlist) {
        if (teamMember.getName().equalsIgnoreCase(name)) {
            linkedlist.remove(teamMember);
            found = true;
            break;
        }
    }
    if (found) {
        System.out.println("LinkedList after deletion of " + name + "'s record" + ": " + linkedlist);
    } else {
        System.out.println("The team member was not found.");
    }
    break;

case 4:
    System.out.println("Linked List Content: " + linkedlist);
    break;

default:
    System.out.println("This is not a valid option try again.");
    break;
}

Make sure to add a toString() method in the class, TeamMember so that you can simply do like System.out.println(an-object-of-TeamMember); . Your IDE can generate the toString() method on the click of a button. Given below is an autogenerated toString() method for your class by eclipse IDE:

@Override
public String toString() {
    return "TeamMember [name=" + name + ", employeeNo=" + employeeNo + ", position=" + position + "]";
}

Check this to learn more about toString() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM