Need help with Java, Sorting name alphabetical order and rate in numerical order

User Generated

Spunaybpb

Programming

Description

***************Semployees.csv***************************************

Stan Stanley,123456789,TRUE,19,FALSE,10.95
Ralph Maccio,258866445,TRUE,15,FALSE,9.55
Julie Andrews,112222445,FALSE,0,FALSE,7.25
Janice Young,558844115,TRUE,19,TRUE,11.75
Annie Song,532120049,FALSE,10,TRUE,14.55
Charles Castro,449032259,TRUE,12,TRUE,7.25
Ruby Johnson,602938575,FALSE,14,TRUE,9.92
Angela Jones,572656432,TRUE,12,TRUE,15.24
John Erwin,986276422,FALSE,20,FALSE,16.70
Socorro Gibson,762844562,TRUE,27,FALSE,13.3
In bold are the name and pay rate that need to sorted alphabetically and numerically.

**************************************main.java*****************************************************

import java.util.ArrayList;

import java.util.Scanner;

import java.io.*;

public class Main {

 static ArrayList<String> name = new ArrayList<String>();

 static ArrayList<String> id = new ArrayList<String>();

 static ArrayList<String> state_of_employee = new ArrayList<String>();

 static ArrayList<String> hourly_worked = new ArrayList<String>();

 static ArrayList<String> state_of_student_employee = new ArrayList<String>();

 static ArrayList<String> pay_rate = new ArrayList<String>();

 static Scanner in = new Scanner(System.in);

 private static Scanner inp;

 static StudentEmployee[] Obb = new StudentEmployee[50];  

 public static void main(String[] args) {

  inp = new Scanner(System.in);

  file_Operation();

  char choice;

  do{ 

   System.out.print("===================Menu=================");

    System.out.print("\nEnter:\n"+" S to Show.\n"+" A to Add.\n"+" R to Remove.\n"

      + " N to Sorted-by-name.\n"+ " T to Sorted-by-rate.\n"+" W to Student-isWorkStudy\n"

      + " H to Show-highest.\n"+" L to Show-lowest.\n"+" C to Change-rate id/name xxx\n"

      + " B to Rate x between y (bonus).\n"+" E to Exit.\n"

      + "Enter your choice : ");

         choice = inp.next().charAt(0);

   

   switch(choice){

   case 'S'  : Show(); break;

   case 'A'  : Add(); break;

   case 'R'  : Remove(); break;

   case 'W'  : StudentIsWorkStudy();break;

   case 'N'  : SortByName(); break;

   case 'T'  : SortByRate(); break;

   case 'H'  : ShowHighest(); break;

   case 'L'  : ShowLowest(); break;

   case 'C'  : ChangeRate(); break;

   case 'B'  : Rate(); break;

   case 'E'  : break;

   

   default : System.out.println("Error, Enter a valid key with capital letter"); break;

   }

     

  }while(choice != 'E'); 

  System.out.println("================================================");  

  System.out.print("Thank you");

 }

 public static void file_Operation()

 {

  String[] words = null;

  FileReader fr = null;

  BufferedReader br = null;

  String line = null ;

  try{

   fr = new FileReader("semployees.csv");

   br = new BufferedReader(fr);

   line = (String)br.readLine();

   while(line!=null){

   words = line.split(",");

   name.add(words[0]);

   id.add(words[1]);

   state_of_employee.add(words[2]);

   hourly_worked.add(words[3]);

   state_of_student_employee.add(words[4]);

   pay_rate.add(words[5]);

   line = (String)br.readLine();

   }

   br.close();

   fr.close();

  }catch(Exception ex){

   System.out.println(ex);

  }

 }

 public static void Show()

 {

  System.out.println("================================================");

  for ( int i = 0 ; i < name.size(); ++ i )

   {

   System.out.println(

     "name: "+ name.get(i) + "\nid: "+id.get(i)

     +"\nstate of employee: "+ state_of_employee.get(i) + "\nHourly Worked: "+hourly_worked.get(i)

     +"\nstate of student employee: "+ state_of_student_employee.get(i) + "\nPay Rate: "+pay_rate.get(i)

     );

   System.out.println("================================================");

   }

 }

 public static void Add()

 {   int n;

  System.out.print("Enter the number of person you want add : ");

  n = inp.nextInt();

  for(int i=0 ; i<n ; i++){

  System.out.print((i+1)+"-Enter Name: ");

  name.add((String)in.nextLine());

  System.out.print((i+1)+"-Enter ID: ");

  id.add((String)in.nextLine());

  in = new Scanner(System.in);

  System.out.print((i+1)+"-Enter state of employee: ");

  state_of_employee.add((String)in.nextLine());

  System.out.print((i+1)+"-Hourly Worked: ");

  hourly_worked.add((String)in.nextLine());

  System.out.print((i+1)+"-state of student employee: ");

  state_of_student_employee.add((String)in.nextLine());

  System.out.print((i+1)+"-Pay Rate: ");

  pay_rate.add((String)in.nextLine());

  System.out.println("================================================");

  }  

 }

 public static void Remove()

 {

  int nn;

  String empid;

  int index;

      System.out.print("Enter how many Employees you want ot remove: ");

      nn = inp.nextInt();

      for(int i=0 ; i<nn ; i++){

   System.out.print((i+1)+"-Enter ID of Employee to remove: ");

   empid = in.nextLine();    

   index = id.indexOf(empid);  

   name.remove(index);

   id.remove(index);

   hourly_worked.remove(index);

   state_of_student_employee.remove(index);

   state_of_employee.remove(index);

   pay_rate.remove(index);

   System.out.println("Deleted");

      }

 }

  public static void Rate(){

         int x=0;

         int y=0;

         System.out.println("================================================");

    System.out.println("Show the student employees that with the hourly rate between x and y");

    System.out.println("Note, Should x<y ");

    System.out.print("Enter x : ");

    in = new Scanner(System.in);

    x = in.nextInt();  

    System.out.print("Enter y : ");

    in = new Scanner(System.in);

    y = in.nextInt();      

    System.out.println("===Employees's rate between =="+x+" and "+y+"=========");

    for ( int i = 0 ; i < name.size(); ++ i ){          

    if(Integer.parseInt(hourly_worked.get(i)) >= x  && Integer.parseInt(hourly_worked.get(i)) <= y){

    System.out.println("name: "+ name.get(i));    

    }          

    }  

    System.out.println("================================================"); 

           

     }

   

  public static void ShowHighest(){  

     int max = Integer.parseInt(hourly_worked.get(0));

     int IndexHigh=0;    

     for ( int i = 0 ; i < name.size(); ++ i ){    

     if(Integer.parseInt(hourly_worked.get(i)) > max){      

     max = Integer.parseInt(hourly_worked.get(i));

     IndexHigh = i;

     }  

        }

     System.out.println("================================================"); 

     System.out.println("The student employee with highest hourly rate is: "+ name.get(IndexHigh));      

       }

  public static void ShowLowest (){  

   int min = Integer.parseInt(hourly_worked.get(0));

   int IndexLow=0;    

   for( int i = 0 ; i < name.size(); ++ i ){    

   if(Integer.parseInt(hourly_worked.get(i)) < min){      

   min = Integer.parseInt(hourly_worked.get(i));

   IndexLow = i;

     }  

         }

   System.out.println("================================================"); 

   System.out.println("The student employee with lowest hourly rate is: "+ name.get(IndexLow));      

  } 

  public static void StudentIsWorkStudy(){

   System.out.println("All the students that are eligible for work study status");  

   for ( int i = 0 ; i < name.size(); ++ i ){    

      if(Boolean.parseBoolean(state_of_student_employee.get(i))){

   System.out.println("Name : "+name.get(i));  

      }

    }      

    }

   

  public static void ChangeRate(){

   int idd=0;

   System.out.println("================================================");

   System.out.print("Enter the id of the Employee that's you want to change here rate: ");

   in = new Scanner(System.in);

   idd = in.nextInt(); 

   int i=0;

   while(idd != Integer.parseInt(id.get(i))){          

   i++; }        

   double newPayrate;

   System.out.print("Enter the new rate : ");    

   in = new Scanner(System.in);

   newPayrate = in.nextDouble();    

   pay_rate.set(i, Double.toString(newPayrate));

   System.out.println("Changed");

  } 

  public static void SortByName(){

    System.out.println("================================================");

    for ( int i = 0 ; i < name.size(); ++ i ){

      System.out.println("name: "+ name.get(i));}

      System.out.println("================================================");    

  } 

  public static void SortByRate(){  

    System.out.println("================================================");

    for ( int i = 0 ; i < name.size(); ++ i ){

      System.out.println("Rate : "+ pay_rate.get(i));}

      System.out.println("================================================");

  }  

}

     

**********************************************employee.java**********************************************************************

public class Employee

{

//Private instance variable fields

   private String employeeName;

   private int employeeId;

   private boolean isWorking;   // true if currently working; false otherwise

       

// Constructor - All args passed in as Strings

    public Employee(String name, String id, String isworking) {

      employeeName = name;

      employeeId = Integer.parseInt(id);

      isWorking = Boolean.parseBoolean(isworking);

   }

    public String getName() {

      return employeeName;

   }

   

    public void setName(String name) {

      employeeName = name;

   }

    public int getEmployeeId() {

      return employeeId;

   }

   

    public void setEmployeeId(int id) {

      employeeId = id;

   }

    public boolean isWorking() {

      return isWorking;

   }

    public void setIsWorking(boolean employed) {

      isWorking = employed;

   }

   

    public String toString() {

      return employeeName + "\t" + employeeId + "\t" + isWorking;

   }

}


*******************************************************StudentEmployee.java*********************************************************

public class StudentEmployee extends Employee {

       

  private int hoursWorked;

  private boolean isWorkStudy;

  private double payRate;

  public StudentEmployee(String name, String id, String isworking,

                         String hoursworked,String workstudy,String payrate) {

    super(name, id, isworking);

    hoursWorked =Integer.parseInt(hoursworked); 

    isWorkStudy = Boolean.parseBoolean(workstudy);

    payRate = Double.parseDouble(payrate); 

   

 }

  public void setHoursWorked(int hours){ 

    hoursWorked = hours;

  }

  public void setIsWorkkStudy(boolean isWork){

    isWorkStudy = isWork;

  }

   

  public void setPayRate(double rate){

    payRate = rate;

  }

     

  public int getHoursWorked(){ 

    return hoursWorked;

  }

  public boolean getIsWorkkStudy(){

    return isWorkStudy;

  } 

     

  public double getPayRate(){

    return payRate;

  }

  public String toString() {

    return hoursWorked + "\t" + isWorkStudy + "\t" + payRate;

  }

}



User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer


Anonymous
Nice! Really impressed with the quality.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags