I'm working on a homework my OOP class and I just can't figure out this last part. The assignment calls for us to write five different classes with parent class StaffMember. I'm pretty sure I've that at the subclassess (Volunteer, HourlyEmployee and FullTimeEmployee) correct so I'm not going to post those here. I'm having problems with the StaffMemberParser class and how to use it to write the code for the driver class. The driver class should create the objects using the parser (split with "/") and store them in an ArrayList. I feel like I've tried everything and just need some help. The trouble spot is under case A. Additionally, under case D, the program is to search for a member ID and return found if it matches. I've got a couple of ideas on how to do this, but I don't think it will work until I get the first part. Without doing my homework for me, some pointers in the right direction would be very much appreciated. Thank you.

Here's the parser class...

public class StaffMemberParser {

        public static StaffMember parseStringToMember(String lineToParse){

                String type;
                String fN;
                String lN;
                String iD;
                double r = 0.0;
                double b = 0.0;
                int hW = 0;

                String[] empInfo = lineToParse.split("/");
                type = empInfo[0].toLowerCase();
                fN = empInfo[1];
                lN = empInfo[2];
                iD = empInfo[3];

                if (empInfo[0].toLowerCase() == "volunteer")     {
                        System.out.println(empInfo[0].toLowerCase());
                        return new Volunteer(fN,lN,iD);
                }
                else if (empInfo[0].toLowerCase() == "fulltimeemployee"){
                        r= Double.parseDouble(empInfo[4]);
                        b = Double.parseDouble(empInfo[5]);
                        return new FullTimeEmployee(fN,lN,iD,r,b);
                }
                else if (empInfo[0].toLowerCase() == "hourlyemployee"){
                        r = Double.parseDouble(empInfo[4]);
                        hW = Integer.parseInt(empInfo[5]);
                        return new HourlyEmployee(fN,lN, iD,r,hW);
                }
                else{
                        System.out.println("Returned a null");
                        return null;
                }
        }
	}

and here's the driver class....

import java.io.*;         //to use InputStreamReader and BufferedReader
import java.util.*;       //to use ArrayList

public class Assignment5
 {
  public static void main (String[] args)
   {
     char input1;
     String inputInfo = new String();
     String line = new String();
     boolean found = false;

     // ArrayList object is used to store member objects
     ArrayList<StaffMember> memberList = new ArrayList();

     try
      {
       printMenu();     // print out menu

       // create a BufferedReader object to read input from a keyboard
       InputStreamReader isr = new InputStreamReader (System.in);
       BufferedReader stdin = new BufferedReader (isr);

       do
        {
         System.out.println("What action would you like to perform?");
         line = stdin.readLine().trim();
         input1 = line.charAt(0);
         input1 = Character.toUpperCase(input1);

         if (line.length() == 1)
          {
           switch (input1)
            {
             case 'A':   //Add Member
               System.out.print("Please enter a member information to add:\n");
               inputInfo = stdin.readLine().trim();
              	
                memberList.add(inputInfo);
                memberList.parseStringToMember(inputInfo);  <--error here



               break;
             case 'C':   //Compute Pay
            	 for (int i=0; i<memberList.size(); i++)
            		((StaffMember)memberList.get(i)).computePay();



             System.out.print("pay computed\n");
               break;



             case 'D':   //Search for Member
               System.out.print("Please enter a memberID to search:\n");
               inputInfo = stdin.readLine().trim();



                if (found == true)
                 System.out.print("member found\n");
                else
                 System.out.print("member not found\n");
               break;
             case 'L':   //List Members
            	 if (memberList.size()>0)
            	   {for (int i=0; i<memberList.size(); i++)
            		   System.out.println (memberList.get(i));
            	   }
            	 else
            		 System.out.println ("no member\n");
            	   break;
             case 'Q':   //Quit
               break;
             case '?':   //Display Menu
               printMenu();
               break;
             default:
               System.out.print("Unknown action\n");
               break;
            }
         }
        else
         {
           System.out.print("Unknown action\n");
          }
        } while (input1 != 'Q'); // stop the loop when Q is read
      }
     catch (IOException exception)
      {
        System.out.println("IO Exception");
      }
  }

  /** The method printMenu displays the menu to a user **/
  public static void printMenu()
   {
     System.out.print("Choice\t\tAction\n" +
                      "------\t\t------\n" +
                      "A\t\tAdd Member\n" +
                      "C\t\tCompute Pay\n" +
                      "D\t\tSearch for Member\n" +
                      "L\t\tList Members\n" +
                      "Q\t\tQuit\n" +
                      "?\t\tDisplay Help\n\n");
  }
}

memberList.parseStringToMember(inputInfo); <--error here

Please copy and paste here the full text of the error messages.

C:\Users\Documents\Assignment5.java:51: error: no suitable method found for add(String)
              	memberList.add(inputInfo);
              	          ^
    method ArrayList.add(int,StaffMember) is not applicable
      (actual and formal argument lists differ in length)
    method ArrayList.add(StaffMember) is not applicable
      (actual argument String cannot be converted to StaffMember by method invocation conversion)
C:\Users\Documents\Assignment5.java:52: error: cannot find symbol
              	memberList.parseStringToMember(inputInfo);
              	          ^
  symbol:   method parseStringToMember(String)
  location: variable memberList of type ArrayList<StaffMember>

Note: C:\Users\Documents\Assignment5.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

Tool completed with exit code 1

error: no suitable method found for add(String)
ArrayList<StaffMember> memberList

memberList contains StaffMember objects, not Strings.

memberList.parseStringToMember(inputInfo);

What class does parseStringToMember belong to? You must use a reference to that class when calling the method. memberList is an ArrayList

Alright, I've referenced the StaffMemberClass and created a new parser and then attempted to use it to it to create SalesMember objects. The new code is below and compiles. However, when I attempt to add a member while running the application, all it will return is "null" I've posted the updated Staff Member class below as well (it also compiles).

case 'A':   //Add Member
               System.out.print("Please enter a member information to add:\n");
               inputInfo = stdin.readLine().trim();



				StaffMemberParser parser=new StaffMemberParser();
				StaffMember sM=parser.parseStringToMember(inputInfo);
				memberList.add(sM);



public class StaffMemberParser {

        public static StaffMember parseStringToMember(String lineToParse){

                String type;
                String fN;
                String lN;
                String iD;
                double r = 0.0;
                double b = 0.0;
                int hW = 0;




                String[] empInfo = lineToParse.split("/");


                type=empInfo[0];
                fN = empInfo[1];
                lN = empInfo[2];
                iD = empInfo[3];

                if (empInfo[0].toLowerCase() == "volunteer")     {
                        System.out.println(empInfo[0].toLowerCase());
                        return new Volunteer(fN,lN,iD);
                }
                else if (empInfo[0].toLowerCase() == "fulltimeemployee"){
                        r= Double.parseDouble(empInfo[4]);
                        b = Double.parseDouble(empInfo[5]);
                        return new FullTimeEmployee(fN,lN,iD,r,b);
                }
                else if (empInfo[0].toLowerCase() == "hourlyemployee"){
                        r = Double.parseDouble(empInfo[4]);
                        hW = Integer.parseInt(empInfo[5]);
                        return new HourlyEmployee(fN,lN, iD,r,hW);
                }
                else{
                        System.out.println("null");
                        return null;
                }



               break;

when I attempt to add a member while running the application, all it will return is "null"

I see that the method does return null on line 52.

At what line does the method execute a return?
I see 4 return statements. Which one is executed?

A problem is that you are using == to compare Strings. You should use the equals() method.

Another possible problem is using literal Strings in the if statement instead of a variable with the value. If you have to enter the same String at many places in the program chances are you will misspell it sometime. The compiler will not tell you of the spelling error.
Better to define a final String and use that everywhere:
final String HourlyEmployee = "hourlyemployee"; //Define it once at beginning of class

someVariable = HourlyEmployee; // set it

..
if(someVariable.equals(HourlyEmployee)) ... // test it

Worked perfectly. I should be able to take it from here. Thank you so much for your help. I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.