/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package project; import java.sql.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSetMetaData; import java.util.Scanner; //import java.util.Collections; import java.util.LinkedList; import java.util.ListIterator; //import java.util.List; /** *represents a general blueprint defining state of a person - student, is composed of two other objects * @author ivan */ class Student { int ID; String lastName; String firstName; String birthday; Major major; int earnedCredits; /** * initializes a Student based on user input * @param id int corresponding to position in student linkedlist * @param lname string last name * @param fname string first name * @param bday string birthday * @param majorKey pointer to Major object * @param credits int credits earned */ Student(int id, String lname, String fname, String bday, Major majorKey, int credits) { ID = id; lastName = lname; firstName = fname; birthday = bday; major = majorKey; earnedCredits = credits; } /** * method on Student instance that forms readable string containing all its member * values; uses chained calls to respective display()on its member objects * @return string containing fields on Student object in readable format */ public String displayStudent() { return "Student ID: "+ID+ "\nName: "+lastName+", "+firstName+ "\nBirthdate: "+birthday+ "\nTotal Credits earned: "+earnedCredits+"\n"+ major.displayMajor(); } /** * static method on Student class that takes arbitrary Student and returns credit-hours needed to graduate based on their major * @param student takes in a Student object * @return returns int representing deficit in credits */ public static int creditsNeeded(Student student) { return student.major.creditHoursReq - student.earnedCredits; } } /** * represents a general blueprint defining state of an educational major * @author ivan */ class Major { int ID; String name; int creditHoursReq; DepartmentHead deptHead; boolean transfers; /** * initializes a Major object based on user input * @param id int corresponding to index in containing linkedList * @param majorName string * @param creditHours int hours req to graduate * @param headKey pointer to DeptHead object * @param transferrable boolean; are transfers typical */ Major(int id, String majorName, int creditHours, DepartmentHead headKey, boolean transferrable) { ID = id; name = majorName; creditHoursReq = creditHours; deptHead = headKey; transfers = transferrable; } /** * method on Major instance that forms readable string containing all its member * values; is called in chain when displatStudent() is called * @return string containing fields on Major object in readable format */ public String displayMajor() { return "Major ID: "+ID+" "+ "Major title: "+name+" "+ "Credits required to graduate: "+creditHoursReq+" "+ "Transferrable: "+transfers+"\n"+ deptHead.displayDepartmentHead(); } } /** * represents a general blueprint defining state of a person heading a department * @author ivan */ class DepartmentHead { int ID; String lastName; String firstName; String office; String phone; String email; /** * initializes a department head object based on user input * @param id int matching index of position n linkedlist * @param lname string last name * @param fname string first name * @param officeLocation string location of office * @param phoneNumber string * @param emailAdd string */ DepartmentHead(int id, String lname, String fname, String officeLocation, String phoneNumber, String emailAdd) { ID = id; lastName = lname; firstName = fname; office = officeLocation; phone = phoneNumber; email = emailAdd; } /** * method on DepartmentHead instance that forms readable string containing all its member * values; is called last in chain when displatStudent() is called * @return string containing fields on DeptHead object in readable format */ public String displayDepartmentHead() { return "Faculty ID: "+ID+" "+ "Name: "+lastName+", "+firstName+" "+ "Office: "+office+" "+ "Phone: "+phone+" "+ "email: "+email; } } /** * utility class containing main() * @author ivan */ public class StudentTracking { private static String dbURL = "jdbc:derby://localhost:1527/contact;create=true;user=nbuser;password=nbuser"; private static String tableName = "students"; // jdbc Connection private static Connection conn = null; private static Statement stmt = null; /** * comprises logic of studenttracking program; construction of objects and respective lists happens here, as well as calls to * utility and object methods based on user input collected * @param args */ public static void main(String[] args) { //createConnection(); Scanner scan = new Scanner(System.in); //creates Student, Major, and DepartmentHead lists LinkedList studentList = new LinkedList(); LinkedList majorList = new LinkedList(); LinkedList deptHeadList = new LinkedList(); //populates 2 lists with references to object components of Student super object and creates 3rd main list of composite Student objects deptHeadList.add(new DepartmentHead(deptHeadList.size(), "Knuth", "Donald", "123 Sennot sq", "412-555-9087", "foo@example.com")); deptHeadList.add(new DepartmentHead(deptHeadList.size(), "Chalmers", "David", "456 5th Ave", "412-555-5676", "foo@example.com")); deptHeadList.add(new DepartmentHead(deptHeadList.size(), "Feynman", "Richard", "789 Poplar Rd", "412-555-7867", "foo@example.com")); deptHeadList.add(new DepartmentHead(deptHeadList.size(), "Weinberg", "Steven", "012 Caltech Rd", "412-555-6756", "foo@example.com")); majorList.add(new Major(majorList.size(), "Physics", 65, deptHeadList.get(2), true)); majorList.add(new Major(majorList.size(), "Computer Science", 60, deptHeadList.get(0), true)); majorList.add(new Major(majorList.size(), "Philosophy", 70, deptHeadList.get(1), true)); majorList.add(new Major(majorList.size(), "Mathematics", 70, deptHeadList.get(3), true)); studentList.add(new Student(studentList.size(), "Smith", "Mary", "01/03/1992", majorList.get(0), 21)); studentList.add(new Student(studentList.size(), "Adams", "Sara", "06/03/1994", majorList.get(1), 34)); studentList.add(new Student(studentList.size(), "Jenkins", "Jennifer", "01/28/1991", majorList.get(2), 18)); studentList.add(new Student(studentList.size(), "Smith", "Emily", "06/29/1991", majorList.get(3), 18)); //initializes loop variable to give true String userResponse = "y"; while(userResponse.equals("y") || userResponse.equals("Y")) { System.out.println("Press enter to print all students."); scan.nextLine(); //uses method on studentList to construct its iterator and creates pointer to it ListIterator iterator = studentList.listIterator(); //while loop using iterator to call displayStudent() on each student object in list while (iterator.hasNext()) { System.out.println(iterator.next().displayStudent()); System.out.println(); } System.out.println("Press enter to compute all student credits and average credits earned."); scan.nextLine(); //pass list of students to compute total and average credits System.out.println(getAverageCreditsEarned(studentList)); System.out.println(); System.out.println("Press enter to get highest and lowest credits earned"); scan.nextLine(); //pass list of students to get highest and lowest credits earned System.out.println(highLow(studentList)); System.out.println(); //recreate listIterator for studentList so cursor is back at beginning iterator = studentList.listIterator(); //while loop using iterator to access all student fields and pass each student to static creditsNeeded() //experimented here with alternating calls to next() and previous() on iterator to repeatedly acces same student //instead of using pointer while (iterator.hasNext()) { System.out.println(iterator.next().firstName+" "+iterator.previous().lastName+" has "+iterator.next().major.name+ " declared as a major and has "+Student.creditsNeeded(iterator.previous())+" more credit-hours left to graduate."); iterator.next(); } //gather user input on whether to create and add new student to list System.out.println(); System.out.println("Would you like to add a student? Y/N"); userResponse = scan.next(); //gathers userinput for new student fields and constructs a new student and adds to list //creates majorList iterator to print index values to inform user input if (userResponse.equals("y") || userResponse.equals("Y")) { ListIterator listIterator = majorList.listIterator(); int credits; String fname; String lname; String bday; int major; System.out.println("Student's first name?"); fname = scan.next(); System.out.println("Student's last name?"); lname = scan.next(); System.out.println("Birthdate? 00/00/0000"); bday = scan.next(); System.out.println("How many credits has this student earned?"); credits = scan.nextInt(); System.out.println("What is the student's declard major? Enter corresponding integer"); while (listIterator.hasNext()) { System.out.println(listIterator.next().ID+"...."+listIterator.previous().name); listIterator.next(); } major = scan.nextInt(); studentList.add(new Student(studentList.size(), lname, fname, bday, majorList.get(major), credits)); System.out.println("New entry completed. New student has ID "+studentList.getLast().ID); } //end of while loop. loop terminates if user inuts anything other than y or Y System.out.println("Run again? Y/N"); userResponse = scan.next(); shutdown(); } } /** * iterates through the studentList and computes total and average credits earned of all student objects * @param studentList takes in a Student LinkedList as an argument * @return returns a String containing the results of the computation */ public static String getAverageCreditsEarned(LinkedList studentList){ ListIterator listIterator = studentList.listIterator(); int totalCredits = 0; while (listIterator.hasNext()) { totalCredits += listIterator.next().earnedCredits; } return "There are "+studentList.size()+" students with a credit total of "+totalCredits+" and the avg. credits earned is " +totalCredits/studentList.size()+"."; } /** * iterates through the studentList and computes highest and lowest credits earned of all student objects * @param studentList takes in a Student LinkedList as an argument * @return returns a String containing the results of the computation */ public static String highLow(LinkedList studentList) { int highest = 0; int lowest = Integer.MAX_VALUE; Student currentStudent; ListIterator listIterator; listIterator = studentList.listIterator(); while (listIterator.hasNext()) { currentStudent = listIterator.next(); if (currentStudent.earnedCredits > highest) highest = currentStudent.earnedCredits; if (currentStudent.earnedCredits < lowest) lowest = currentStudent.earnedCredits; } return "most credits earned is: "+highest+" and least credits earned is: "+lowest; } private static void createConnection() { try { Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); //Get a connection conn = DriverManager.getConnection(dbURL); } catch (Exception except) { except.printStackTrace(); } } private static void shutdown() { try { if (stmt != null) { stmt.close(); } if (conn != null) { DriverManager.getConnection(dbURL + ";shutdown=true"); conn.close(); } } catch (SQLException sqlExcept) { } } }