Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I've finished my code which gets employee information as input and it can sort based on what the user wants. But for part of the requirements, I have two use an abstract class "Employee" with two subclasses "TempEmployee" and "Perm
employee". What of my code do I put under the abstract classes for this to work? Also, when I sort I want all the information to go with it, so If I sort by salary, I want the names and department of the people who make that salary when it orders it, not just an ordered list of the numbers. Is this possible as well?

Java
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;


public class Assignment55_000848913
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    String Continue = "y";
    int Count = 0;
    int SortingChoice;
    ArrayList<String> Names       = new ArrayList<String>();
    ArrayList<String> Department  = new ArrayList<String>();
    ArrayList<String> Designation = new ArrayList<String>();
    ArrayList<Float>  Salary      = new ArrayList<Float>();
    
    //******************************************************//
    
    do
    {
       System.out.println("Enter Employee Name: ");
       String x = in.next();
       Names.add(x);
    
       System.out.println("Enter Employee Designation ('temporary or permanent'): ");
       String y = in.next();
       Designation.add(y);
    
       System.out.println("Enter Employee Department: ");
       String z = in.next();
       Department.add(z);
    
       System.out.println("Enter Employee Salary: ");
       float i = in.nextFloat();
       Salary.add(i);
    
       System.out.println("Do you wish to add another employee? ('y'/'n'): ");
       Continue = in.next();
       
       Count = Count + 1;
    }
    while(Continue.equals("y"));
    
//***********************************************************************//
    
    System.out.println("Enter sorting Criterion Number: 1. Name, 2.Department, 3. Salary. ");
    SortingChoice = in.nextInt();
    
    if(SortingChoice == 1)
    {
      Collections.sort(Names);
         for(int i=0; i<Names.size(); i++)
         {
            System.out.println(Names.get(i));
         }
    }
    
    if(SortingChoice == 2)
    {
      Collections.sort(Department);
         for(int i=0; i<Department.size(); i++)
         {
            System.out.println(Department.get(i));
         }
    }
    
    if(SortingChoice == 3)
    {
      for(int k=0; k<Salary.size(); k++)
      {
        if(Designation.get(k).equals("temporary"))
          {
             Salary.set(k, Salary.get(k)*1920);
          }
      }
           
          
      Collections.sort(Salary);
         for(int i=0; i<Salary.size(); i++)
         {
           System.out.print("$");
           System.out.println(Salary.get(i));
         }
    }
  }
}
 
  public abstract class Employee
  {
    
  }
  
  public class TempEmployee extends Employee
  {
    
  }
  
  public class PermEmployee extends Employee
  {
    
  }
Posted
Comments
PIEBALDconsult 11-Nov-15 23:44pm    
Sounds like homework. In the real world you wouldn't do it that way; there isn't enough difference between term and perm employees.
Member 12133028 12-Nov-15 0:22am    
yes its an assignment, doesn;t mean i still don't need help however.
Sergey Alexandrovich Kryukov 12-Nov-15 3:00am    
The whole approach is wrong. Classes are not placeholders for your code. This is not how programming is done. The answer would be: none of your code. Instead, real code should be written. Throw out all your code and start thinking. It will be the shortest way.
—SA
Richard MacCutchan 12-Nov-15 3:34am    
Your Employee calss should contain the basic properties and methods that are common to all inheritors. Then you can add the specific items to Temp and Perm to make the application do what you want. See http://docs.oracle.com/javase/tutorial/java/javaOO/index.html.

1 solution

You have written all of your code inside the of the entrypoint method (public static main) as Sergey pointed out. This is generally a really bad way of doing it, especially in a object oriented programming language (aka OOP) like Java. An OOP language is a language where you can sort your code into classes and functions.
The so called 'correct' way of doing this, would be to setup the code like this:

class Employee
   
   public String name
   public String department
   public String designation // You could use a boolean here, since you only use two values
   public float  salary

   // The constructor for creating the object, needs to be passed the name, department,  designation & salary
   
   public Employee (String name, String department, String designation , float salary)
      // Using this.name calls the global varible of name insted of the local variable
      this.name        = name
      this.department  = department
      this.designation = designation
      this.salary      = salary
   
   // The different sorting methods
   public void sortingMethod1 ()
   public void sortingMethod2 () 
   public void sortingMethod3 ()


I don't see how you would implement abstract classes for this, since this will take care of the job. You'll also have you polish up the code since I have left some braces and terminators. Now from the main class, the assignment one, you can store an array of employees and change their values.

class Assignment55_000848913
   ArrayList<employee> employees;
   Scanner scan;

   public static main (String[] args)
      scan = new Scanner(System.in) // Initialize scanner
      employees = new ArrayList<>() // Initialize array list

      do
      
         sysout("Enter employee data using this format: name,department,designation,salary") // Short for System.out.print

         String data = scan.nextLine();
         String[] tokens = data.split(",");

         employees.add(new Employee(tokens[0], tokens[1], tokens[2], tokens[3]))
         
         // Ask if user would like to keep adding employees...
      while (...)
         
</employee>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900