Advertisement
Luninariel

Roster Manipulation Generic - WIP

Feb 6th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.71 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * This program performs the following functions:
  8. * - Will load student records from input.txt into their respective variables
  9. * - Will turn those variables into a Student Object
  10. * - Calculate their average % based on test scores
  11. * - Places the Student objects into an ArrayList representing the academic class.
  12. * - Lists the class with their % score and letter grade in the class.
  13. * - Remove the records matching ID 42P4 and 45A3
  14. * - Re-list the records with the removed students.
  15. * - Add the Student Records from Additions.txt
  16. * - Calculate the Year Rank based on hours completed.
  17. * - Calculate the GPA based on Current GPA and the current class hours.
  18. * - Adding, and sorting students is done using Generic Methods.
  19. * - Calculate the grades of the students and sort the ArrayList from Highest to lowest based on % score
  20. * - To prove the generic nature of the program, a list of doubles is brought in and sorted from smallest to largest
  21. * - Print the ArrayList.
  22. *
  23. * @author Carlos
  24. * @since 01/25/2019
  25. **/
  26. public class RosterManipulations {
  27.  
  28. public static void main(String[] args) throws Exception {
  29. RosterManipulations me = new RosterManipulations();
  30. ArrayList<Student> AcademicClass = new ArrayList<Student>();
  31.  
  32. //Create an arraylist of doubles to contain doubles.txt
  33. ArrayList<Double> myDoubles = new ArrayList<Double>();
  34.  
  35. //Making a new instance of the StudentClassManager for Students
  36. StudentClassManager <Student> myStudents= me.new StudentClassManager<Student>();
  37.  
  38. //Making a new instance of the StudentClassManager for Doubles
  39. StudentClassManager<Double>genericDoubles= me.new StudentClassManager<Double>();
  40. try {
  41. Scanner input = new Scanner(new File("src/main/input.txt"));
  42.  
  43. //Gather the information from the records in input.txt and set them equal to their respective variables
  44. while (input.hasNextLine()) {
  45. String StudentID = input.next();
  46. String Studentname = input.next();
  47. int test1 = input.nextInt();
  48. int test2 = input.nextInt();
  49. int test3 = input.nextInt();
  50. int totalHours = input.nextInt();
  51. float oldGPA = input.nextFloat();
  52.  
  53. //Place those variables into a student object
  54. Student student1 = me.new Student(StudentID, Studentname, test1, test2, test3, totalHours, oldGPA);
  55.  
  56.  
  57. //Place the student object into an array list
  58. myStudents.AddStudent(AcademicClass, student1);
  59.  
  60. }
  61.  
  62. System.out.println("\nOriginal Class Roster Before Modifications: \n");
  63. //Print the contents of the ArrayList including their % score and grade before any manipulation
  64. for (int i = 0; i < AcademicClass.size(); i++) {
  65. System.out.println(AcademicClass.get(i));
  66. }
  67.  
  68. me.DeleteStudent(AcademicClass, "45A3");
  69. me.DeleteStudent(AcademicClass, "42P4");
  70. System.out.println("\nStudents 45A3 & 42P4 Have Dropped The Class. Generating New Roster: \n");
  71. //Print the contents of the ArrayList including their % score and grade AFTER the students have dropped
  72. for (int i = 0; i < AcademicClass.size(); i++) {
  73. System.out.println(AcademicClass.get(i));
  74. }
  75.  
  76. System.out.println("\n New Students Have Joined The Class. Generating Roster: \n");
  77.  
  78. Scanner secondInput = new Scanner(new File("src/main/Additions.txt"));
  79. while (secondInput.hasNextLine()) {
  80. String AddedID = secondInput.next();
  81. String AddedName = secondInput.next();
  82. int AddedTest1 = secondInput.nextInt();
  83. int AddedTest2 = secondInput.nextInt();
  84. int AddedTest3 = secondInput.nextInt();
  85. int AddedTotalHours = secondInput.nextInt();
  86. float AddedoldGPA = secondInput.nextFloat();
  87.  
  88. Student student2 = me.new Student(AddedID, AddedName, AddedTest1, AddedTest2, AddedTest3, AddedTotalHours, AddedoldGPA);
  89.  
  90. //Adding students the expected students to the Class
  91. myStudents.AddStudent(AcademicClass, student2);
  92. }
  93.  
  94. //Reprint the array with the students now added
  95.  
  96. for (int i = 0; i < AcademicClass.size(); i++) {
  97. System.out.println(AcademicClass.get(i));
  98. }
  99.  
  100. myStudents.SortLarge(AcademicClass);
  101. System.out.println("\nRegenerating Roster Sorted By Average Grade\n");
  102. //print the contents of the array sorted by Larger to Smaller based on average %
  103. for (int i = 0; i < AcademicClass.size(); i++) {
  104. System.out.println(AcademicClass.get(i));
  105. }
  106.  
  107. //Read the contents of doubles.txt
  108. Scanner inputDoubles = new Scanner(new File("src/main/doubles.txt"));
  109. //Add the contents of doubles.txt to the ArrayList for doubles
  110. while (inputDoubles.hasNextLine()){
  111. genericDoubles.AddStudent(myDoubles,inputDoubles.nextDouble());
  112. }
  113. //Sort the contents of doubles.txt
  114. genericDoubles.SortLarge(myDoubles);
  115.  
  116. //Print the sorted contents of doubles.txt from Smaller to Larger
  117. System.out.println("Proving Generic Sort By Printing Sorted Doubles From Smallest to Largest\n");
  118.  
  119. //Print the sorted contents of doubles.txt from Smaller to Larger
  120. for (int i=myDoubles.size()-1; i >= 0; i--){
  121. System.out.println(myDoubles.get(i));
  122. }
  123.  
  124.  
  125.  
  126.  
  127. } catch (FileNotFoundException e) {
  128. System.err.println("File Input.txt was not found");
  129. }
  130.  
  131. }
  132.  
  133. //Deletes a Student who has dropped the class from the array list.
  134. public static void DeleteStudent(ArrayList<Student> AcademicClass, String ID) {
  135. for (int i = 0; i < AcademicClass.size(); i++) {
  136. if (AcademicClass.get(i).getStudentID().equals(ID)) {
  137. AcademicClass.remove(i);
  138. }
  139. }
  140.  
  141.  
  142. }
  143.  
  144. //StudentClassManager class will use generics to perform the same actions regardless of the input type.
  145. public class StudentClassManager <T extends Comparable<T>> {
  146.  
  147. //Generically adds an object (in this case students) to the Academic Class ArrayList
  148. public void AddStudent(ArrayList<T> AcademicClass, T Student) {
  149. AcademicClass.add(Student);
  150. }
  151.  
  152. //Generically sorts the object from Larger to Smaller
  153. public void SortLarge(ArrayList<T> AcademicClass) {
  154. T xsave;
  155. int isw = 1;
  156. while (isw == 1) {
  157. isw = 0;
  158. for (int i = 0; i < AcademicClass.size() - 1; i++) {
  159. switch (AcademicClass.get(i).compareTo(AcademicClass.get(i + 1))) {
  160. case 1://the objects in array x are in the right order
  161. break;
  162. case -1://objects out of order, they must be changed.
  163. // This is where to the place holder is used to swap values
  164. xsave = AcademicClass.get(i);
  165. AcademicClass.set(i, AcademicClass.get(i + 1));
  166. AcademicClass.set(i + 1, xsave);
  167. isw = 1;
  168. break;
  169. default://objects are equal no change
  170. }
  171. }
  172. }
  173. }
  174. }
  175.  
  176.  
  177. //The Student class turns student records into objects ao that they an be sorted into an ArrayLIst
  178. public class Student implements Comparable<Student> {
  179. String StudentID;
  180. String Studentname;
  181. int test1;
  182. int test2;
  183. int test3;
  184. int average;
  185. int totalHours;
  186. float oldGPA;
  187.  
  188. @Override
  189. //toString so that objects can be printed.
  190. public String toString() {
  191. return " ID: " + StudentID + " Name: " + Studentname + "\n Test 1 Score: " + test1 + "\n Test 2 Score: " + test2 + "\n Test 3 Score: " + test3 + "\n Test Average: " + average + "%" + "\n Class Grade: " + averageGrade(average) + "\n Total Hours: " + totalHours + "\n GPA: " + calculatenewGPA(oldGPA, gradePoint(average), totalHours) + "\n Year Rank: " + yearRank(totalHours) + "\n";
  192. }
  193.  
  194. //Calculates the NEW GPA of a student
  195. public float calculatenewGPA(float oldGPA, int gradePoint, int totalHours) {
  196. return ((oldGPA * totalHours) + (2) * gradePoint) / (totalHours + 2);
  197. }
  198.  
  199. //Calculate the GradePoint Value of A GPA
  200. public int gradePoint(int average) {
  201. {
  202. int gradePoint = 0;
  203. if (average >= 90) {
  204. gradePoint = 4;
  205. } else if (average >= 80) {
  206. gradePoint = 3;
  207. } else if (average >= 70) {
  208. gradePoint = 2;
  209. } else if (average >= 60) {
  210. gradePoint = 1;
  211. } else if (average >= 0) {
  212. gradePoint = 0;
  213. }
  214. return gradePoint;
  215. }
  216. }
  217.  
  218. //Calculate the Year Rank of each student
  219. public String yearRank(int totalHours) {
  220. String rank = null;
  221. if (totalHours <= 30) {
  222. rank = "Freshman";
  223. } else if (totalHours <= 60) {
  224. rank = "Sophomore";
  225. } else if (totalHours <= 90) {
  226. rank = "Junior";
  227. } else if (totalHours >= 91) {
  228. rank = "Senior";
  229. }
  230. return rank;
  231. }
  232.  
  233. //Turn the average into a letter grade
  234. public String averageGrade(int average) {
  235. String averageLetter = null;
  236. if (average >= 90) {
  237. averageLetter = "A";
  238. } else if (average >= 80) {
  239. averageLetter = "B";
  240. } else if (average >= 70) {
  241. averageLetter = "C";
  242. } else if (average >= 60) {
  243. averageLetter = "D";
  244. } else if (average >= 0) {
  245. averageLetter = "F";
  246. }
  247. return averageLetter;
  248. }
  249.  
  250. //Calculate the average grade of a student
  251.  
  252.  
  253. //Constructor for Student Class
  254. public Student(String StudentID, String Studentname, int test1, int test2, int test3, int totalHours, float oldGPA) {
  255. setStudentID(StudentID);
  256. setStudentname(Studentname);
  257. setTest1(test1);
  258. setTest2(test2);
  259. setTest3(test3);
  260. int average = (test1 + test2 + test3) / 3;
  261. setAverage(average);
  262. setTotalHours(totalHours);
  263. setoldGPA(oldGPA);
  264. }
  265.  
  266. //Setters for the Student Class
  267. public void setStudentID(String StudentID) {
  268. this.StudentID = StudentID;
  269. }
  270.  
  271. public void setStudentname(String Studentname) {
  272. this.Studentname = Studentname;
  273. }
  274.  
  275. public void setTest1(int test1) {
  276. this.test1 = test1;
  277. }
  278.  
  279. public void setTest2(int test2) {
  280. this.test2 = test2;
  281. }
  282.  
  283. public void setTest3(int test3) {
  284. this.test3 = test3;
  285. }
  286.  
  287. public void setAverage(int average) {
  288. this.average = average;
  289. }
  290.  
  291. public void setTotalHours(int totalHours) {
  292. this.totalHours = totalHours;
  293. }
  294.  
  295. public void setoldGPA(float oldGPA) {
  296. this.oldGPA = oldGPA;
  297. }
  298.  
  299.  
  300. //Getters for the Student Class
  301. public String getStudentID() {
  302. return StudentID;
  303. }
  304.  
  305. public String getStudentname() {
  306. return Studentname;
  307. }
  308.  
  309. public int getTest1() {
  310. return test1;
  311. }
  312.  
  313. public int getTest2() {
  314. return test2;
  315. }
  316.  
  317. public int getTest3() {
  318. return test3;
  319. }
  320.  
  321. public int getAverage() {
  322. return average;
  323. }
  324.  
  325. public int getTotalHours() {
  326. return totalHours;
  327. }
  328.  
  329. public float getoldGPA() {
  330. return oldGPA;
  331. }
  332.  
  333.  
  334. public int compareTo(Student o) {
  335. return (this.getAverage() < o.getAverage() ? -1 :
  336. (this.getAverage() == o.getAverage() ? 0 : 1));
  337. }
  338. }
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement