Question

In: Computer Science

Write a Java program thatdoes the following jobs.Step1. Creates a Schooltable with name, score, rating, and...

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.

Solutions

Expert Solution

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 :


Related Solutions

IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to...
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to the specifications that the user desires. It walks the user through ordering, giving the user choices, which the program then uses to decide how to make the pizza and how much the cost of the pizza will be. Note: Use dialog boxes for communicating with the user. Remember to use the JOptionPane class which is the graphical user interface (GUI) and Comments that are...
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to...
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to the specifications that the user desires. It walks the user through ordering, giving the user choices, which the program then uses to decide how to make the pizza and how much the cost of the pizza will be. Note: Use dialog boxes for communicating with the user. Remember to use the JOptionPane class which is the graphical user interface (GUI) and Comments that are...
Java - Write a test program that creates an Account object with an account number of...
Java - Write a test program that creates an Account object with an account number of AC1111, a balance of $25,000, and an annual interest rate of 3.5. Use the withdraw method to withdraw $3,500, use the deposit method to deposit $3,500, and print the balance, the monthly interest, and the date when this account was created.
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
C++ : Write a program that creates a login name for a user, given the user's...
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the...
Write a Java program for a restaurant with the following features: ◦ Customer: Name, Surname, ID...
Write a Java program for a restaurant with the following features: ◦ Customer: Name, Surname, ID (incremental ID by 1 for each new customer), Email, Phone, Address. ◦ Service: ID (incremental ID by1 for each group),CustomerID, Priority (High, Medium, Low, Other), ResolutionTimeFrame (Measured in Man hours), AssignedUser, Status(resolved or not), Fee. ◦ User (simple user of system): ID, Name, Surname, Username and Password (insert from code five fixed users), Address , PhoneNumber ◦ Manager: Name, Surname, Username and Password (insert...
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside file there should be items: Open, Save, and Save As. Selecting open prompts the user to input a file name to a txt document containing employee information and displays a Jtable with the information, which can be edited. With column headers {"First Name" , "Last Name" , "Occupation" , "Office #"} Example: Gary Osbourn Teacher 113 Michelle Ramirez Teacher 101 Ava Gomez Principal 120...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT