In: Computer Science
Write a Java program thatdoes the following jobs.Step1. Creates a Schooltable with name, score, rating, and district.Step2. Reads data from theCSVfile(elementrayschool.csv)and insert the schoolsinto the table created in Step 1.Step3. Interact with the user, wheretheusercan select one of the following actions.-(Q) Quit: quit the program-(A) Add a school-(C) Calculate average rating-(S) Print a subset of the schools based on score-(D) Print a subset of the schools based on districtThe user can choose one of these actions bytyping Q,A,C, S, or Din the console. When adding a school,theusermust provide name, score, rating, and districtthrough the console(you can assume the user will provide valid inputs).When selecting a subset of the schoolsbased on score,a lowerbound score value must be provided. When selecting a subset of the schoolsbased on district,a valid district name must be provided.
Schooltable.java
public class Schooltable {
private String name;
private double score;
private int rating;
private String district;
public Schooltable()
{
this.name = this.district = "";
this.score = 0.0;
this.rating = 0;
}
public Schooltable(String name, double score, int rating, String
district) {
this.name = name;
this.score = score;
this.rating = rating;
this.district = district;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
public int getRating() {
return rating;
}
public String getDistrict() {
return district;
}
@Override
public String toString()
{
return(String.format("%-20s %-10.2f %-10d %-1s", getName(),
getScore(), getRating(), getDistrict()));
}
}
Management.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Management {
private ArrayList<Schooltable> schools;
public Management()
{
this.schools = new ArrayList<>();
}
public void readFile(String filename)
{
Scanner fileReader;
try
{
fileReader = new Scanner(new File(filename));
while(fileReader.hasNextLine())
{
String[] data = fileReader.nextLine().trim().split(",");
String name = data[0];
double score = Double.parseDouble(data[1]);
int rating = Integer.parseInt(data[2]);
String district = data[3];
schools.add(new Schooltable(name, score, rating, district));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(filename + " could not be found!
Exiting..");
System.exit(0);
}
}
public void addSchool()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine().trim();
System.out.print("Enter score: ");
double score = Double.parseDouble(sc.nextLine().trim());
System.out.print("Enter rating (1-5): ");
int rating = Integer.parseInt(sc.nextLine().trim());
while(rating < 1 || rating > 5)
{
System.out.println("Please enter a rating between 1 &
5!");
System.out.print("Enter rating (1-5): ");
rating = Integer.parseInt(sc.nextLine().trim());
}
System.out.print("Enter district: ");
String district = sc.nextLine().trim();
schools.add(new Schooltable(name, score, rating, district));
System.out.println(name + " has been added successfully.\n");
}
private void displayHeader()
{
System.out.printf("%-20s %-10s %-10s %-1s\n", "NAME", "SCORE",
"RATING", "DISTRICT");
}
public void calculateAverageRating()
{
if(schools.isEmpty())
{
System.out.println("No schools added till now!\n");
return;
}
double sum = 0.0;
for(Schooltable sc : schools)
sum += (double)sc.getRating();
double avg = (sum / (double)schools.size());
System.out.println("The average rating of the school is = " +
String.format("%.3f.\n", avg));
}
public void displaySchoolsOfSameScore()
{
if(schools.isEmpty())
{
System.out.println("No schools added till now!\n");
return;
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter a score to get all schools: ");
double targetScore =
Double.parseDouble(sc.nextLine().trim());
ArrayList<Schooltable> res = new ArrayList<>();
for(Schooltable sch : schools)
{
if(sch.getScore() == targetScore)
res.add(sch);
}
if(res.isEmpty())
System.out.println("Sorry, no such schools found!\n");
else
{
System.out.println("Total " + res.size() + " matches
found:\n");
displayHeader();
for(Schooltable s : res)
System.out.println(s);
System.out.println();
}
}
public void displaySchoolsOfSameDistrict()
{
if(schools.isEmpty())
{
System.out.println("No schools added till now!\n");
return;
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter a district to get all schools: ");
String targetDistrict = sc.nextLine().trim();
ArrayList<Schooltable> res = new ArrayList<>();
for(Schooltable sch : schools)
{
if(sch.getDistrict().compareToIgnoreCase(targetDistrict) ==
0)
res.add(sch);
}
if(res.isEmpty())
System.out.println("Sorry, no such schools found!\n");
else
{
System.out.println("Total " + res.size() + " matches
found:\n");
displayHeader();
for(Schooltable s : res)
System.out.println(s);
System.out.println();
}
}
}
Test.java (Main class)
import java.util.Scanner;
public class Test {
private static final String FILENAME =
"elementrayschool.csv";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Management management = new Management();
management.readFile(FILENAME);
char choice;
do
{
displayMenu();
choice = Character.toUpperCase(sc.nextLine().charAt(0));
switch(choice)
{
case 'A':
{
System.out.println("\nADD A SCHOOL:\n"
+ "-------------");
management.addSchool();
break;
}
case 'C':
{
System.out.println("\nCALCULATE AVERAGE RATING:\n"
+ "-------------------------");
management.calculateAverageRating();
break;
}
case 'S':
{
System.out.println("\nSUBSET OF SCHOOLS BASED ON SCORE:\n"
+ "---------------------------------");
management.displaySchoolsOfSameScore();
break;
}
case 'D':
{
System.out.println("\nSUBSET OF SCHOOLS BASED ON DISTRICT:\n"
+ "------------------------------------");
management.displaySchoolsOfSameDistrict();
break;
}
case 'Q':
{
System.out.println("\nThanks for visiting our
school..Goodbye!\n");
System.exit(0);
}
default:
System.out.println("\nInvalid selection!\n");
}
}while(choice != 'Q');
}
private static void displayMenu()
{
System.out.print("Choose from the following options:\n"
+ "A. Add a school\n"
+ "C. Calculate average rating\n"
+ "S. Print a subset of the schools based on score\n"
+ "D. Print a subset of the schools based on district\n"
+ "Q. Quit\n"
+ "Your selection >> ");
}
}
******************************************************** SCREENSHOT ********************************************************
INPUT FILE (elementrayschool.csv) - This file needs to be created before running the code and this file should be created within the same working projct directory where the above .java files will be residing.
CONSOLE OUTPUT :