0

I am trying to enter user details into a LinkedList based off input from a JOptionPane input dialog box. But if I add the strings directly it gives an error because the LinkedList is of BabySitterPeople objects. So when i try adding the BabySitterPeople objects it outputs something like BabySitterPeople23234e@21 Please Help me fix this so it correctly prints the user details.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class BabySitterSignUp extends JFrame implements ActionListener{

    private JButton btnAdd, btnRemove, btnUp, btnDown;
    private JList<BabySitterPeople> jlPassengers;
    private DefaultListModel dlm;
    private BabySitterPeople BabySitterPerson;
    //This is where the LinkedList is created 
    private LinkedList<BabySitterPeople> babySitters = 
            new LinkedList<BabySitterPeople>();
    private File f = new File("waitlist.txt");
    private String firstName, lastName, kidNumber;

    public BabySitterSignUp(){
        //Initializing the Linked List
        BabySitterPerson = new BabySitterPeople();
        dlm = new DefaultListModel();

        //initialising a Jlist and setting its properties
        jlPassengers = new JList<BabySitterPeople>(dlm);
        jlPassengers.setVisibleRowCount(20);
        jlPassengers.setFont(new Font("Courier New", Font.PLAIN, 12));
        jlPassengers.setFixedCellWidth(250);
        jlPassengers.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jlPassengers.setSelectionMode
        (ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        //creating a logo label
        JLabel lblLogo = new JLabel();
        lblLogo.setIcon(new ImageIcon("images\\air-canada-logo.jpg"));
        lblLogo.setPreferredSize(new Dimension(350, 70));
        lblLogo.setHorizontalAlignment(SwingConstants.CENTER);

        //creating a name label
        JLabel lblName = new JLabel();
        lblName.setText("NAME");
        lblName.setPreferredSize(new Dimension(150, 20));

        //creating a priority label
        JLabel lblPriority = new JLabel();
        lblPriority.setText("KID NUMBER");
        lblPriority.setPreferredSize(new Dimension(100, 20));
        lblPriority.setHorizontalAlignment(SwingConstants.RIGHT);

        //creating a jscrollpane for the waiting list 
        JScrollPane scroll = new JScrollPane
                (jlPassengers, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setBorder(null);

        //creates the up button and declares its properties
        btnUp = new JButton("\u2191");
        btnUp.setPreferredSize(new Dimension(45, 30));
        btnUp.setBackground(Color.WHITE);
        btnUp.setFocusable(false);
        btnUp.addActionListener(this);

        //creates the down button and declares its properties
        btnDown = new JButton("\u2193");
        btnDown.setPreferredSize(new Dimension(45, 30));
        btnDown.setBackground(Color.WHITE);
        btnDown.setFocusable(false);
        btnDown.addActionListener(this);

        //creates the add button and declares its properties
        btnAdd = new JButton("ADD");
        btnAdd.setPreferredSize(new Dimension(100, 30));
        btnAdd.setBackground(Color.WHITE);
        btnAdd.setFocusable(false);
        btnAdd.addActionListener(this);

        //creates the remove button and declares its properties
        btnRemove = new JButton("REMOVE");
        btnRemove.setPreferredSize(new Dimension(100, 30));
        btnRemove.setBackground(Color.WHITE);
        btnRemove.setFocusable(false);
        btnRemove.addActionListener(this);

        //creating and setting the JPanels properties 
        //along with adding components to the panel
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 15));
        panel.setBackground(Color.WHITE);
        panel.add(lblLogo);
        panel.add(Box.createHorizontalStrut(25));
        panel.add(lblName);
        panel.add(lblPriority);
        panel.add(Box.createHorizontalStrut(25));
        panel.add(Box.createHorizontalStrut(25));
        panel.add(scroll);
        panel.add(Box.createHorizontalStrut(25));
        panel.add(btnUp);
        panel.add(btnAdd);
        panel.add(btnRemove);
        panel.add(btnDown);
        setContentPane(panel);
        setSize(350, 560);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setTitle("Waiting List");
        setResizable(false);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == btnAdd)
        { 
            firstName = JOptionPane.showInputDialog(null, 
                    "Enter your first name:\n", 
                    "BabySitter", JOptionPane.OK_OPTION + 
                    JOptionPane.INFORMATION_MESSAGE);

            // user will input a last name
            lastName = JOptionPane.showInputDialog(null, 
                    "Enter your last name:\n",
                    "BabySitter", JOptionPane.OK_OPTION + 
                    JOptionPane.INFORMATION_MESSAGE);

            //Adds first name and last name to the baby sitter
            BabySitterPerson.setFirstName(firstName);
            BabySitterPerson.setLastName(lastName);

            kidNumber = JOptionPane.showInputDialog(null, 
                    "Enter number of kids you wish to babysit:\n", 
                    "BabySitter", JOptionPane.OK_OPTION + 
                    JOptionPane.INFORMATION_MESSAGE);

            //Adds number of kids they want to babysit to the baby sitter
            BabySitterPerson.setKidNumber(Integer.parseInt(kidNumber));

            // tells user that this person has been added to the waiting list
            JOptionPane.showMessageDialog(null, firstName + " " +
                    lastName + " has been added to the waiting list!", 
                    "Waiting List",
                    JOptionPane.INFORMATION_MESSAGE);

            // clears list
            dlm.clear();

            // adds inputed first and last name to the waiting list

            babySitters.add(BabySitterPerson);

            //  displays waiting list
            for (int i = 0; i < babySitters.size(); i++)
            {
                dlm.addElement(babySitters.get(i));
            }

            if ( lastName == null || (lastName.length() == 0)){
                JOptionPane.showMessageDialog(null, 
                        "Please enter a valid name!",
                        "Error", JOptionPane.ERROR_MESSAGE);
            }

            if( firstName == null || (firstName.length() == 0)) {
                JOptionPane.showMessageDialog(null, 
                        "Please enter a valid name!",
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
        }

        if(e.getSource() == btnUp){

        }
    }
}
George Mulligan
  • 11,813
  • 6
  • 37
  • 50
M. Srom
  • 1
  • 2

0 Answers0