Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. import java.util.ArrayList;  //Loads ArrayList java library for use in program
  2.  
  3. public class Roster
  4. {
  5.    
  6.     static ArrayList<Student> studentRoster = new ArrayList<>(); //initializes student roster array list
  7.    
  8.     public static void main(String[] args) //Main program
  9.         {
  10.             Roster.add("1", "John", "Smith", "John1989@gmail.com", 20, 88, 79, 59);
  11.             Roster.add("2", "Suzan", "Erickson", "Erickson_1990@gmailcom", 19, 91, 72, 85);
  12.             Roster.add("3", "Jack", "Napoli", "The_lawyer99yahoo.com", 19, 85, 84, 87);
  13.             Roster.add("4", "Erin", "Black", "Erin.black@comcast.net", 22, 91, 98, 82);
  14.             Roster.add("5", "Ryan", "Lewis", "rlewi57@wgu.edu", 28, 90, 83, 76);
  15.            
  16.             //prints all students in roster
  17.             print_all();
  18.             //prints any invalid emails along with an error message
  19.             print_invalid_emails();
  20.             //prints out all student's average grade
  21.             print_average_grade();
  22.             //removes student 3 from the roster
  23.             remove("3");
  24.             //removes student 3 from the roster, but returns error message since student 3 no longer exists
  25.             remove("3");
  26.            
  27.         }
  28.    
  29.     //Constructor for adding new students to the Student Roster
  30.     public static void add(String studentId, String firstName, String lastName, String emailAddress, int age, int grade1, int grade2, int grade3)
  31.     {
  32.         int[] grades = {grade1, grade2, grade3};
  33.         Student newStudent = new Student(studentId, firstName, lastName, emailAddress, age, grades); //Creates a new student
  34.         studentRoster.add(newStudent); //Adds the new student to the roster array
  35.     }
  36.    
  37.     //Creates remove method to remove students from the roster. Returns error message if student is not found.
  38.     public static void remove(String studentId)
  39.     {
  40.         for (Student student : studentRoster)
  41.         {
  42.             if (student.getStudentId().equals(studentId))
  43.             {
  44.                    studentRoster.remove(student);
  45.                    System.out.println("Student " + studentId + " has been removed.");
  46.                    return;
  47.             }
  48.         }
  49.         System.out.println("This Student ID cannot be found!");
  50.     }
  51.    
  52.     //Creates print_all method to print out a list of all students in the roster.  Calls the print() method from the Student class.
  53.     public static void print_all()
  54.     {
  55.         for (int i = 0; i < studentRoster.size(); i++)
  56.         {
  57.             studentRoster.get(i).print();
  58.         }
  59.     }
  60.    
  61.     //Creates print_average_grade method that calculates the average of each student's three grades
  62.     public static void print_average_grade()
  63.     {
  64.         for (Student student : studentRoster)
  65.         {
  66.             double averageGrade = (student.getGrades()[0] + student.getGrades()[1] + student.getGrades()[2]) / 3.0;
  67.             System.out.println("Student ID:\t" + student.getStudentId() + "\tAverage Grade:\t" + String.format("%.2f",averageGrade));
  68.         } //Added String.format to round the average grades up to 2 decimal points
  69.     }
  70.    
  71.     //Creates method to print out any emails that do not include either an "@" symbol or a period or contain a space.
  72.     public static void print_invalid_emails()
  73.     {
  74.         for (Student student : studentRoster)
  75.         {     //Added argument to also check the email address for any spaces, and returns them as invalid if it does.
  76.             if (!student.getEmailAddress().contains("@") || !student.getEmailAddress().contains(".") || student.getEmailAddress().contains(" "))
  77.             {
  78.                 System.out.println("Student " + student.getStudentId() + "'s email address " + student.getEmailAddress() + " is invalid!");
  79.             }
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement