Java Code here I need cleaned up. I\'ve been working out the kinks for days to n
ID: 668327 • Letter: J
Question
Java Code here I need cleaned up. I've been working out the kinks for days to no avail so to anyone who can help THANK YOU!!! I am taking information from a text file about a students class status (online or on campus) and outputting it to another .txt file with the individuals name and tuition. There are Four classes I have created for this and Main is the only one giving me issues.
// MAIN////
package tuition;
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) {
new Main().run();
}
public class run{
private abstract class implements run<> {
ArrayList<Sorter> studentList = new ArrayList<Sorter>();
try {
studentList = readFile();
calcTuition(studentList);
Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING);
writeFile(studentList);
} catch (FileNotFoundException e)
{
System.err.println("Sorry, could not open 'p02-students.txt' for reading.");
System.exit(-1);
}
}
}
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, lname, fname;
id = pIn.next();
lname = pIn.next();
fname = pIn.next();
}
private void calcTuition(ArrayList<Student> pStudentList) {
for (Student s : pStudentList) {
s.calcTuition();
}
}
private ArrayList<Student> readFile() throws FileNotFoundException {
ArrayList<Student> studentList = new ArrayList<Student>();
info.add(type, id, lname, fname, resstat, prfee, credits);
String fName = "p02-students.txt";
Scanner in = new Scanner(new File(fName));
String studentType;
while (in.hasNext()) {
studentList.add();
if (in.next() == "C") {
studentList.add(readOnCampusStudent(in));
}
studentList.add(readOnlineStudent(in));
in.nextLine();
}
in.close();
return studentList;
}
private void writeFile(ArrayList<Student> 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();
}
}
//STUDENT///
package tuition;
public abstract class Student implements Comparable<Student> {
private int mCredits;
private String mFname;
private String mLname;
private int mId;
private double mTuition;
//constructor of Student class
public Student(String pFname, String pLname) {
mFname = pFname;
mLname = pLname;
}
//Implement this method in the subclass
//of student class
public abstract void calcTuition();
//Set credits of student
public void setCredits(int pCredits) {
mCredits = pCredits;
}
//Returns the credits of student
public int getCredits() {
return mCredits;
}
//Set first name
public void setFname(String pFname) {
mFname = pFname;
}
//Returns first name
public String getFirstName() {
return mFname;
}
//Set id
public void setid(int pId) {
mId = pId;
}
//Retunrns id
public int getid() {
return mId;
}
//Set last name
public void setLname(String pLname) {
mLname = pLname;
}
//Returns last name
public String getLastName() {
return mLname;
}
//Returns the tuition fee
public void setTuition(double pTuition) {
mTuition = pTuition;
}
//Returns the tuition fee
public double getTuition() {
return mTuition;
}
@Override
public int compareTo(Student pStudnet) {
if (getid() < pStudnet.getid()) {
return -1;
} else if (getid() > pStudnet.getid()) {
return 1;
} else {
return 0;
}
}
}
/// TUITION ///
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>
}
///// SORTER /// to output the .txt file appropriately
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<Student> 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<Student> 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<Student> pList, int pIndex1, int pIndex2) {
Student temp = pList.get(pIndex1);
pList.set(pIndex1, pList.get(pIndex2));
pList.set(pIndex2, temp);
}
}
//// .txt DOCUMENT ////
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
Explanation / Answer
Modified files:
// Main class
package tuition;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import static java.lang.System.in;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main().run();
}
private onlineStudent readOnlineStudent(Scanner pIn) {
int id;
String lname, fname;
id = pIn.nextInt();
lname = pIn.next();
fname = pIn.next();
Student onlineStudent(id, fname, lname) {
@Override
public void calcTuition()
{
}
};
return OnlineStudent;
}
private void calcTuition(ArrayList<Student> pStudentList) {
for (Student s : pStudentList) {
s.calcTuition();
}
}
private void run() {
ArrayList<Student> studentList = new ArrayList<Student>();
try {
studentList = readFile();
calcTuition(studentList);
Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING);
writeFile(studentList);
} catch (FileNotFoundException e)
{
System.err.println("Sorry, could not open 'p02-students.txt' for reading.");
System.exit(-1);
}
}
private onCampusStudent readOnCampusStudent(Scanner pIn) {
int id = pIn.nextInt();
String lname = pIn.next();
String fname = pIn.next();
Student onCampusStudent(id, fname, lname) {
@Override
public void calcTuition() {
}
};
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 ArrayList<Student> readFile() throws FileNotFoundException {
readOnCampusStudent (in) ArrayList<Student> studentList = new ArrayList<Student>();
//info.add(type, id, lname, fname, resstat, prfee, credits);
String fName = "p02-students.txt";
Scanner in = new Scanner(new File(fName));
String studentType;
while (in.hasNext()) {
// studentList.add(type, id, lname, fname, resstat, prfee, credits);
if ("C".equals(in.next())) {
studentList.add(readOnCampusStudent(in));
}
studentList.add(readOnlineStudent(in));
in.nextLine();
}
in.close();
return studentList;
}
private void writeFile(ArrayList<Student> 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());
}
out.close();
}
}
// Student
package tuition;
public abstract class Student implements Comparable<Student> {
private int mCredits;
private String mFname;
private String mLname;
private int mId;
private double mTuition;
//constructor of Student class
public Student(int pId, String pFname, String pLname) {
mId=pId;
mFname = pFname;
mLname = pLname;
}
//Implement this method in the subclass
//of student class
public abstract void calcTuition();
//Set credits of student
public void setCredits(int pCredits) {
mCredits = pCredits;
}
//Returns the credits of student
public int getCredits() {
return mCredits;
}
//Set first name
public void setFname(String pFname) {
mFname = pFname;
}
//Returns first name
public String getFirstName() {
return mFname;
}
//Set id
public void setid(int pId) {
mId = pId;
}
//Retunrns id
public int getid() {
return mId;
}
//Set last name
public void setLname(String pLname) {
mLname = pLname;
}
//Returns last name
public String getLastName() {
return mLname;
}
//Returns the tuition fee
public void setTuition(double pTuition) {
mTuition = pTuition;
}
//Returns the tuition fee
public double getTuition() {
return mTuition;
}
@Override
public int compareTo(Student pStudnet) {
if (getid() < pStudnet.getid()) {
return -1;
} else if (getid() > pStudnet.getid()) {
return 1;
} else {
return 0;
}
}
int getId() {
return mId;
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
String getLName() {
return mLname;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
String getFName() {
return mFname;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setResidency(boolean b) {
}
void setProgramFee(double fee) {
}
}
abstract class onCampusStudent extends Student
{
boolean mResident;
double mProgramFee;
onCampusStudent(int pId, String pFname, String pLname)
{
super(pId,pFname,pLname);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.