Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList; //Loads ArrayList java library for use in program
- public class Roster
- {
- static ArrayList<Student> studentRoster = new ArrayList<>(); //initializes student roster array list
- public static void main(String[] args) //Main program
- {
- Roster.add("1", "John", "Smith", "John1989@gmail.com", 20, 88, 79, 59);
- Roster.add("2", "Suzan", "Erickson", "Erickson_1990@gmailcom", 19, 91, 72, 85);
- Roster.add("3", "Jack", "Napoli", "The_lawyer99yahoo.com", 19, 85, 84, 87);
- Roster.add("4", "Erin", "Black", "Erin.black@comcast.net", 22, 91, 98, 82);
- Roster.add("5", "Ryan", "Lewis", "rlewi57@wgu.edu", 28, 90, 83, 76);
- //prints all students in roster
- print_all();
- //prints any invalid emails along with an error message
- print_invalid_emails();
- //prints out all student's average grade
- print_average_grade();
- //removes student 3 from the roster
- remove("3");
- //removes student 3 from the roster, but returns error message since student 3 no longer exists
- remove("3");
- }
- //Constructor for adding new students to the Student Roster
- public static void add(String studentId, String firstName, String lastName, String emailAddress, int age, int grade1, int grade2, int grade3)
- {
- int[] grades = {grade1, grade2, grade3};
- Student newStudent = new Student(studentId, firstName, lastName, emailAddress, age, grades); //Creates a new student
- studentRoster.add(newStudent); //Adds the new student to the roster array
- }
- //Creates remove method to remove students from the roster. Returns error message if student is not found.
- public static void remove(String studentId)
- {
- for (Student student : studentRoster)
- {
- if (student.getStudentId().equals(studentId))
- {
- studentRoster.remove(student);
- System.out.println("Student " + studentId + " has been removed.");
- return;
- }
- }
- System.out.println("This Student ID cannot be found!");
- }
- //Creates print_all method to print out a list of all students in the roster. Calls the print() method from the Student class.
- public static void print_all()
- {
- for (int i = 0; i < studentRoster.size(); i++)
- {
- studentRoster.get(i).print();
- }
- }
- //Creates print_average_grade method that calculates the average of each student's three grades
- public static void print_average_grade()
- {
- for (Student student : studentRoster)
- {
- double averageGrade = (student.getGrades()[0] + student.getGrades()[1] + student.getGrades()[2]) / 3.0;
- System.out.println("Student ID:\t" + student.getStudentId() + "\tAverage Grade:\t" + String.format("%.2f",averageGrade));
- } //Added String.format to round the average grades up to 2 decimal points
- }
- //Creates method to print out any emails that do not include either an "@" symbol or a period or contain a space.
- public static void print_invalid_emails()
- {
- for (Student student : studentRoster)
- { //Added argument to also check the email address for any spaces, and returns them as invalid if it does.
- if (!student.getEmailAddress().contains("@") || !student.getEmailAddress().contains(".") || student.getEmailAddress().contains(" "))
- {
- System.out.println("Student " + student.getStudentId() + "'s email address " + student.getEmailAddress() + " is invalid!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement