Click here to Skip to main content
14,978,864 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?

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(;
    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>();
       System.out.println("Enter Employee Name: ");
       String x =;
       System.out.println("Enter Employee Designation ('temporary or permanent'): ");
       String y =;
       System.out.println("Enter Employee Department: ");
       String z =;
       System.out.println("Enter Employee Salary: ");
       float i = in.nextFloat();
       System.out.println("Do you wish to add another employee? ('y'/'n'): ");
       Continue =;
       Count = Count + 1;
    System.out.println("Enter sorting Criterion Number: 1. Name, 2.Department, 3. Salary. ");
    SortingChoice = in.nextInt();
    if(SortingChoice == 1)
         for(int i=0; i<Names.size(); i++)
    if(SortingChoice == 2)
         for(int i=0; i<Department.size(); i++)
    if(SortingChoice == 3)
      for(int k=0; k<Salary.size(); k++)
             Salary.set(k, Salary.get(k)*1920);
         for(int i=0; i<Salary.size(); i++)
  public abstract class Employee
  public class TempEmployee extends Employee
  public class PermEmployee extends Employee
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.
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.
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

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 calls the global varible of name insted of the local variable        = 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( // Initialize scanner
      employees = new ArrayList<>() // Initialize array list

         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 (...)

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