Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

You have to use comment function to describe what each line does

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class PreferenceData {

private final List<Student> students;

private final List<Project> projects;

private int[][] preferences;

private static enum ReadState {

STUDENT_MODE, PROJECT_MODE, PREFERENCE_MODE, UNKNOWN;

};

public PreferenceData() {

super();

this.students = new ArrayList<Student>();

this.projects = new ArrayList<Project>();

}

public void addStudent(Student s) {

this.students.add(s);

}

public void addStudent(String s) {

this.addStudent(Student.createStudent(s));

}

public void addProject(Project p) {

this.projects.add(p);

}

public void addProject(String p) {

this.addProject(Project.createProject(p));

}

public void createPreferenceMatrix() {

this.preferences = new int[this.students.size()][this.projects.size()];

}

public void setPreference(Student s, Project p, int preference) {

this.preferences[this.students.indexOf(s)][this.projects.indexOf(p)] = preference;

}

public void setPreference(int row, int column, int preference) {

this.preferences[row][column] = preference;

}

public void setPreferenceRow(int row, String[] prefValues) {

for (int j = 0; j < prefValues.length; j++) {

this.preferences[row][j] = Integer.parseInt(prefValues[j]);

}

}

public List<Student> getStudents() {

return students;

}

public List<Project> getProjects() {

return projects;

}

public int[][] getPreferences() {

return preferences;

}

@Override

public String toString() {

return "PreferenceData [students=" + students + ", projects=" + projects + ", preferences="

+ Arrays.toString(preferences) + "]";

}

static PreferenceData readData(String inputFile) {

PreferenceData prefs = new PreferenceData();

try(BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {

ReadState state = ReadState.UNKNOWN;

int row = 0;

while (reader.ready()) {

String line = reader.readLine();

switch (line.trim()) {

case "Students:":

state = ReadState.STUDENT_MODE;

break;

case "Projects:":

state = ReadState.PROJECT_MODE;

break;

case "Preferences:":

prefs.createPreferenceMatrix();

state = ReadState.PREFERENCE_MODE;

break;

default:

switch (state) {

case STUDENT_MODE:

prefs.addStudent(line);

break;

case PROJECT_MODE:

prefs.addProject(line);

break;

case PREFERENCE_MODE:

prefs.setPreferenceRow(row, line.split(","));

row++;

break;

default:

throw new PreferenceFormatException(line);

}

}

}

reader.close();

} catch (FileNotFoundException e) {

System.err.println("Error opening preferences file. File does not exist as specified.");

e.printStackTrace();

} catch (IOException e) {

System.err.println("Error reading from file.");

e.printStackTrace();

} catch (PreferenceFormatException e) {

System.err.println("Preference file in incorrect format. I can't tell which section I'm in.");

System.err.println("Line being read: " + e.getCurrentLine());

e.printStackTrace();

}

return prefs;

}

public int getPreference(int i, int j) {

return this.preferences[i][j];

}

public int numStudents() {

return this.students.size();

}

public int numProjects() {

return this.projects.size();

}

}

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education