Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

create software for an occupation list object that uses a linked list instead of an array, then substitute this new class for the array-based occupation

create software for an occupation list object that uses a linked list instead of an array, then substitute this new class for the array-based occupation list class in a copy of the Occupation List assignment you have been working on. You will also need to add a list node class to the project.
You should start by making a copy of the first IntelliJ Occupation List project, then work with that copy. You do not need to work with the project that involves menus, just the original Occupation List project.
the new Occupation List class will have properties and methods with names matching those in the array-based Occupation List class, so you should be able to substitute the linked list class into the project in place of the array-based class without changing the main executable class or the Occupation class.
You will need to create a ListNode class in addition to the new Occupation List class
package OccupationsPackage;
import java.io.File;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
public class OccupationList {
//Array of Occupations
private Occupations[] list;
/*number of occupations in the list as properties*/
int numberOfOccupations;
//Default constructor
public OccupationList() {
list = new Occupations[1000];
numberOfOccupations = 0;
}
public void readLines() throws Exception {
File unsorted = new File("occupations.txt");
Scanner infile = new Scanner(unsorted);
while (infile.hasNextLine()) {
//create object of Occupations
Occupations occupations = new Occupations(infile.nextLine(),
infile.nextLine(),
Integer.parseInt(String.valueOf(NumberFormat.getNumberInstance(Locale.US).parse(infile.nextLine()))),
Integer.parseInt(String.valueOf(NumberFormat.getNumberInstance(Locale.US).parse(infile.nextLine()))));
// add to list
list[numberOfOccupations] = occupations;
numberOfOccupations++;
}
infile.close();
}
//No need of this method not asked in question
/* public static void sortStringArray(String[] a, int count) {
boolean swapped = true;
int i;
String temp;
while (swapped){
swapped = false;
for(i = 0 ; (i< count-1); i++){
if (a[i+i].compareTo(a[i])< 0) {
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
swapped = true;
}
}
}
}*/
//Printing array of Occupations on screen
public void displayLines() {
int i;
for (i = 0; i < numberOfOccupations; i++) {
System.out.println(list[i]);
}
}
// allow the user to search array
/**
* asks the user for a COS code, then either displays the data for that
* code, or says the code list.
*
*/
public void searchOccupationByCOS() {
String cos;
// asks the user for a COS code
Scanner input = new Scanner(System.in);
System.out.print("Enter Occupation's COS Code: ");
cos = input.nextLine();
for (int i = 0; i < numberOfOccupations; i++) {
if (list[i].getCOS().equals(cos)) {
System.out.println(list[i]);
return;
}
}
System.out.println("check code list");
}
//Write data to output file
public void writeLines() throws Exception {
File tut = new File("occupations-3.txt");
PrintWriter outfile = new PrintWriter(tut);
for (int i = 0; i < numberOfOccupations; i++) {
outfile.println(list[i]);
}
outfile.close();
}
// method to the list class to calculate and return the total number of // people employed across all of the occupations listed public int getTotalEmployees(){ int total = 0; for (int i = 0; i < numberOfOccupations; i++) { total = total+list[i].getEmployment(); } return total; } // method to the list class to calculate and return the // average of the salaries across all occupations. public double getAverageSalary(){ int total = 0; for (int i = 0; i < numberOfOccupations; i++) { total = total+list[i].getSalary(); } return (double) total/(double) numberOfOccupations; }
}
package OccupationsPackage;
import java.util.Scanner;
/**
* The executable class (with the same name as the package)
*/
public class OccupationPackage {
public static void main(String[] args) throws Exception {
// instantiate an instance of the OccupationList class
OccupationList occupationList = new OccupationList();
/**
* load the data from the data file into the array of occupations.
*/
occupationList.readLines();
while(true){ // print menu int option = getMenuOption(); if(option==5) break; switch(option){ case 1: occupationList.displayLines(); break; case 2: int employees = occupationList.getTotalEmployees(); System.out.println("Total number of employees: "+employees); break; case 3: double salary = occupationList.getAverageSalary(); System.out.println("Total number of employees: "+salary); break; case 4: occupationList.searchOccupationByCOS(); break; default: System.out.println("Invalid option. Try again"); break; } }
occupationList.writeLines();
}
private static int getMenuOption() { System.out.println("MENU"); System.out.println("====="); System.out.println(" 1. Print all occupations"); System.out.println(" 2. Print total employees"); System.out.println(" 3. Print total average salary"); System.out.println(" 4. Search occupation"); System.out.println(" 5. Exit"); Scanner in = new Scanner(System.in); System.out.print("Enter an option: "); int option = in.nextInt(); return option; }
}
package OccupationsPackage;
import java.io.File;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
public class OccupationList {
//Array of Occupations
private Occupations[] list;
/*number of occupations in the list as properties*/
int numberOfOccupations;
//Default constructor
public OccupationList() {
list = new Occupations[1000];
numberOfOccupations = 0;
}
public void readLines() throws Exception {
File unsorted = new File("occupations.txt");
Scanner infile = new Scanner(unsorted);
while (infile.hasNextLine()) {
//create object of Occupations
Occupations occupations = new Occupations(infile.nextLine(),
infile.nextLine(),
Integer.parseInt(String.valueOf(NumberFormat.getNumberInstance(Locale.US).parse(infile.nextLine()))),
Integer.parseInt(String.valueOf(NumberFormat.getNumberInstance(Locale.US).parse(infile.nextLine()))));
// add to list
list[numberOfOccupations] = occupations;
numberOfOccupations++;
}
infile.close();
}
//No need of this method not asked in question
/* public static void sortStringArray(String[] a, int count) {
boolean swapped = true;
int i;
String temp;
while (swapped){
swapped = false;
for(i = 0 ; (i< count-1); i++){
if (a[i+i].compareTo(a[i])< 0) {
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
swapped = true;
}
}
}
}*/
//Printing array of Occupations on screen
public void displayLines() {
int i;
for (i = 0; i < numberOfOccupations; i++) {
System.out.println(list[i]);
}
}
// allow the user to search array
/**
* asks the user for a COS code, then either displays the data for that
* code, or says the code list.
*
*/
public void searchOccupationByCOS() {
String cos;
// asks the user for a COS code
Scanner input = new Scanner(System.in);
System.out.print("Enter Occupation's COS Code: ");
cos = input.nextLine();
for (int i = 0; i < numberOfOccupations; i++) {
if (list[i].getCOS().equals(cos)) {
System.out.println(list[i]);
return;
}
}
System.out.println("check code list");
}
//Write data to output file
public void writeLines() throws Exception {
File tut = new File("occupations-3.txt");
PrintWriter outfile = new PrintWriter(tut);
for (int i = 0; i < numberOfOccupations; i++) {
outfile.println(list[i]);
}
outfile.close();
}
// method to the list class to calculate and return the total number of // people employed across all of the occupations listed public int getTotalEmployees(){ int total = 0; for (int i = 0; i < numberOfOccupations; i++) { total = total+list[i].getEmployment(); } return total; } // method to the list class to calculate and return the // average of the salaries across all occupations. public double getAverageSalary(){ int total = 0; for (int i = 0; i < numberOfOccupations; i++) { total = total+list[i].getSalary(); } return (double) total/(double) numberOfOccupations; }
}
package OccupationsPackage;
import java.util.Scanner;
/**
* The executable class (with the same name as the package)
*/
public class OccupationPackage {
public static void main(String[] args) throws Exception {
// instantiate an instance of the OccupationList class
OccupationList occupationList = new OccupationList();
/**
* load the data from the data file into the array of occupations.
*/
occupationList.readLines();
while(true){ // print menu int option = getMenuOption(); if(option==5) break; switch(option){ case 1: occupationList.displayLines(); break; case 2: int employees = occupationList.getTotalEmployees(); System.out.println("Total number of employees: "+employees); break; case 3: double salary = occupationList.getAverageSalary(); System.out.println("Total number of employees: "+salary); break; case 4: occupationList.searchOccupationByCOS(); break; default: System.out.println("Invalid option. Try again"); break; } }
occupationList.writeLines();
}
private static int getMenuOption() { System.out.println("MENU"); System.out.println("====="); System.out.println(" 1. Print all occupations"); System.out.println(" 2. Print total employees"); System.out.println(" 3. Print total average salary"); System.out.println(" 4. Search occupation"); System.out.println(" 5. Exit"); Scanner in = new Scanner(System.in); System.out.print("Enter an option: "); int option = in.nextInt(); return option; }
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions