Please help with this project. There are 3 Java Files... 1. Main.java, 2. Sorter
ID: 667515 • Letter: P
Question
Please help with this project. There are 3 Java Files... 1. Main.java, 2. Sorter.java and 3. TuitionConstants.java (all files are seperated by ---------------- lines). The sorter.java and TuitionConstant.java file are already completed but the Main.java still needs to be written. I also included the Design requirements all the way on the bottom.
Your program shall meet these requirements.
1. Student information for Springfield State University is stored in a text file named p02-students.txt. There is one student record per line, where the format of a student record for an on-campus student is:
C id last-name first-name residency program-fee credits
where:
'C' Identifies the student as an on-campus student.
id The student identifier number. A string of 13 digits.
last-name The student's last name. A contiguous string of characters.
first-name The student's first name. A contiguous string of characters.
residency 'R' if the student is a resident, 'N' if the student is a non-resident.
program-fee A program fee, which may be zero.
credits The number of credit hours for which the student is enrolled.
The format of a student record for an online student is:
O id last-name first-name tech-fee credits
where 'O' identifies the student as an online student, and id, last-name, first-name, and credits are the same as for an on-campus student. The tech-fee field is 'T' if the student is to be assessed the technology fee or '-' if the student is not assessed the technology fee.
Here is an example p02-students.txt file:
Sample p02-students.txt
C 8230123345450 Flintstone Fred R 0 12
C 3873472785863 Simpson Lisa N 750 18
C 4834324308675 Jetson George R 0 20
O 1384349045225 Szyslak Moe - 6
O 5627238253456 Flanders Ned T 3
2. The program shall read the contents of p02-students.txt and calculate the tuition for each student.
3. The program shall write the tuition results to an output file named p02-tuition.txt formatted thusly:
id last-name first-name tuition
id last-name first-name tuition
...
where tuition is the computed tuition for the student. The tuition shall be displayed with two digits after the
decimal point. For example:
Sample p02-tuition.txt
1384349045225 Szyslak Moe 5250.00
3873472785863 Simpson Lisa 12950.00
4834324308675 Jetson George 6200.00
5627238253456 Flanders Ned 2750.00
8230123345450 Flintstone Fred 5500.00
4. The records in the output file shall be sorted in ascending order by id.
5. If the input file p02-students.txt cannot be opened for reading (because it does not exist) then display an error message on the output window and immediately terminate the program, e.g., run program
Sorry, could not open 'p02-students.txt' for reading. Stopping.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
/**
* Instantiate a Main object and call run() on the object.
*/
public static void main(String[] args) {
???
}
/**
* Calculates the tuition for each student. Write an enhanced for loop that iterates over each Student in
* pStudentList. For each Student, call calcTuition() on that Student. Note: this is a polymorphic method
* call.
*
* PSEUDOCODE
* EnhancedFor each student in pStudentList Do
* student.calcTuition()
* End EnhancedFor
*/
private void calcTuition(ArrayList pStudentList) {
???
}
/**
* Reads the student information from "p02-students.txt" and returns the list of students as an ArrayList
* object.
*
* PSEUDOCODE
* Declare and create an ArrayList object named studentList.
* Open "p02-students.txt" for reading using a Scanner object named in.
* While in.hasNext() returns true Do
* String studentType <- read next string from in
* If studentType is "C" Then
* studentList.add(readOnCampusStudent(in))
* Else
* studentList.add(readOnlineStudent(in))
* End If
* End While
* Close the scanner
* Return studentList
*/
private ArrayList readFile() throws FileNotFoundException {
???
}
/**
* Reads the information for an on-campus student.
*
* PSEUDOCODE
* Declare String object id and assign pIn.next() to id
* Declare String object named lname and assign pIn.next() to lname
* Declare String object named fname and assign pIn.next() to fname
* Declare and create an OnCampusStudent object. Pass id, fname, and lname as params to ctor.
* Declare String object named res and assign pIn.next() to res
* Declare double variable named fee and assign pIn.nextDouble() to fee
* Declare int variable named credits and assign pIn.nextInt() to credits
* If res.equals("R") Then
* Call setResidency(true) on student
* Else
* Call setResidency(false) on student
* End If
* Call setProgramFee(fee) on student
* Call setCredits(credits) on student
* Return student
*/
private OnCampusStudent readOnCampusStudent(Scanner pIn) {
???
}
/**
* Reads the information for an online student.
*
* PSEUDOCODE
* Declare String object id and assign pIn.next() to id
* Declare String object named lname and assign pIn.next() to lname
* Declare String object named fname and assign pIn.next() to fname
* Declare and create an OnlineStudent object. Pass id, fname, lname as params to the ctor.,
* Declare String object named fee and assign pIn.next() to fee
* Declare int variable named credits and assign pIn.nextInt() to credits
* If fee.equals("T")) Then
* Call setTechFee(true) on student
* Else
* Call setTechFee(false) on student
* End If
* Call setCredits(credits) on student
* Return student
*/
private OnlineStudent readOnlineStudent(Scanner pIn) {
???
}
/**
* Calls other methods to implement the sw requirements.
*
* PSEUDOCODE
* Declare ArrayList object named studentList
* try
* studentList = readFile()
* calcTuition(studentList)
* Call Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING) to sort the list
* writeFile(studentList)
* catch FileNotFoundException
* Print "Sorry, could not open 'p02-students.txt' for reading. Stopping."
* Call System.exit(-1)
*/
private void run() {
???
}
/**
* Writes the output file to "p02-tuition.txt" per the software requirements.
*
* PSEUDOCODE
* Declare and create a PrintWriter object named out. Open "p02-tuition.txt" for writing.
* EnhancedFor each student in pStudentList Do
* out.print(student id + " " + student last name + " " + student first name)
* out.printf("%.2f%n" student tuition)
* End EnhancedFor
* Close the output file
*/
private void writeFile(ArrayList pStudentList) throws FileNotFoundException {
???
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package tuition;
import java.util.ArrayList;
public class Sorter {
public static final int SORT_ASCENDING = 0;
public static final int SORT_DESCENDING = 1;
/**
* Sorts pList into ascending (pOrder = SORT_ASCENDING) or descending
* (pOrder = SORT_DESCENDING) order using the insertion sort algorithm.
*/
public static void insertionSort(ArrayList pList, int pOrder) {
for (int i = 1; i < pList.size(); ++i) {
for (int j = i; keepMoving(pList, j, pOrder); --j) {
swap(pList, j, j - 1);
}
}
}
/**
* Returns true if we need to continue moving the element at pIndex until it
* reaches its proper location.
*/
private static boolean keepMoving(ArrayList pList, int pIndex, int pOrder) {
if (pIndex < 1) {
return false;
}
Student after = pList.get(pIndex);
Student before = pList.get(pIndex - 1);
return (pOrder == SORT_ASCENDING) ? after.compareTo(before) < 0 : after.compareTo(before) > 0;
}
/**
* Swaps the elements in pList at pIndex1 and pIndex2.
*/
private static void swap(ArrayList pList, int pIndex1, int pIndex2) {
Student temp = pList.get(pIndex1);
pList.set(pIndex1, pList.get(pIndex2));
pList.set(pIndex2, temp);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package tuition;
public class TuitionConstants {
public static final int> public static final int MAX_CREDITS = 18;
public static final int> public static final int> public static final int> public static final int>
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Design Requirements.
5.1 Main Class A template for Main is included in the zip archive. The Main class shall contain the main) method which shall instantiate an object of the Main class and call ru) oni that object. Complete the code by rading the comments and implementing the pseudocode. 5.2 TuitionConstants Class The complete TuitionConstants class is included in the zip archive. This class simply declares some publie static constants that are used in other classes. 5.3 Sorter Class We shall discuss sorting later n the course, so this code may not make perfect sense at this time. However, I have provided all of it for you. sclss sorting later Im the course, 0 The Sorter class coutains a public method insertionSort that can be called to sort a list of Array List Student. When sorting Students we need to be able to compare one Student A to another Student B to determine if A is less than or greater than B. Since we are sortig by student id, we have the abstract Student class mplement the Comparable Student, interface aid we define Student A to be less than Student B if the ,rld field of A is leo than the tnJd field of B. This is how we sor the ArrayListStudent list by student idExplanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main m = new Main();
m.run();
}
private void calcTuition(ArrayList pStudentList) {
for (Student s : pStudentList){
s.calcTution();
}
}
private ArrayList readFile() throws FileNotFoundException {
List<Student> studentList = new ArrayList<>(Student);
File file = new File("p02-students.txt");
try {
Scanner in= new Scanner(file);
while (sc.hasNext()) {
String studentType = sc.next();
if(studentType.equals("C")){
studentList.add(readOnCampusStudent(in));
}
else{
studentList.add(readOnlineStudent(in))
}
}
sc.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
return studentList;
}
private OnCampusStudent readOnCampusStudent(Scanner pIn) {
String id = pIn.next();
String lname = pIn.next();
String fname = pIn.next();
Student OnCampusStudent(id,fname,lname);
String res = pIn.next();
double fee = pIn.nextDouble();
int credits = pIn.nextInt();
if(res.equals("R")){
onCampusStudent .setResidency(true)
}
else{
onCampusStudent.setResidency(false)
}
onCampusStudent.setProgramFee(fee);
onCampusStudent.setCredits(credits);
return onCampusStudent;
}
private OnlineStudent readOnlineStudent(Scanner pIn) {
String id = pIn.next();
String lname = pIn.next();
String fname = pIn.next();
Student OnlineStudent(id,fname,lname);
String fee = pIn.next();
int credits = pIn.nextInt();
if(fee.equals("T")){
onlineStudent.setTechFee(true)
}
else{
onlineStudent.setTechFee(false)
}
onlineStudent.setCredits(credits);
return onlineStudent;
}
private void run() {
List<Student> studentList = new ArrayList<>(Student);
try{
studentList = readFile();
calcTuition(studentList);
Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING);
writeFile(studentList);
}
catch (FileNotFoundException e) {
System.out.println("Sorry, could not open 'p02-students.txt' for reading. Stopping.");
System.exit(-1);
}
}
private void writeFile(ArrayList pStudentList) throws FileNotFoundException {
File file = new File ("p02-tuition.txt");
PrintWriter out = new PrintWriter (file);
for (Student s : pStudentList){
out.print(s.getId() + " " + s.getLName() + " " + s.getFName());
out.printf("%.2f%n" s.getTuition());
}
printWriter.close ();
}
}