Advertisement
Luninariel

Roster Manipulation - Main

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