All pastes #4276 Raw Edit

eXistenZi

public java v1 · immutable
#4276 ·published 2005-01-15 23:23 UTC
rendered paste body
public class List{    static StudentRecord initial = null; //for the purpose of starting with the first node    public static void main(String[] args) throws IOException    {        String input;        BufferedReader br = new BufferedReader (new InputStreamReader (System.in));        boolean loop = true;        System.out.println("Last name         First name        Grade Phone Number   Criminal Record");        enrollStudent("Alex", "Kravtsov", 99, "4167309664", true);        enrollStudent("Do", "Juan", 56, "6669", true);        enrollStudent("Jesus", "Christ", 99, "777", false);        viewStudentRecords();        System.out.println("Now entering new grade");        enterGrade("Alex", "Kravtsov", 93);        viewStudentRecords();    }        public static void viewStudentRecords()    {    StudentRecord currentpos = initial;    while (currentpos != null)    {        System.out.println (currentpos.returnFormattedString ());        currentpos = currentpos.nextStudentRecord ();    }    }              public static void enrollStudent(String firstname, String lastname, int grade, String phonenumber, boolean criminalrecord)  {    StudentRecord newStudentRecord = new StudentRecord(firstname, lastname, grade, phonenumber, criminalrecord);      if (initial != null)     {    	StudentRecord searchResult = searchByName(firstname, lastname);		newStudentRecord.nextLink (searchResult.nextStudentRecord());		searchResult.nextLink (newStudentRecord);    } else {    	        initial = newStudentRecord;    	newStudentRecord.nextLink(null);	    }      }    public static void enterGrade(String firstname, String lastname, int newgrade)  {  	if (initial != null)  	{  		System.out.println(initial.returnlName);  		if (((initial.returnlName).equals(lastname)) && ((initial.returnfName).equals(firstname))) {  			initial.assignGrade(newgrade);  		} else {  			StudentRecord searchResult = searchByName(firstname, lastname);  			searchResult.assignGrade(newgrade);  		}  	} else {  		  		System.out.println ("Error: No entries exist in database");  	}  	  	  	  	  }      //Performs a search for the name   private static StudentRecord searchByName(String firstname, String lastname)   {   boolean foundpos = false; 	StudentRecord current = initial;               while ((current.nextStudentRecord () != null) && !foundpos) //Start the search            {				if (lastname.equals ((current.nextStudentRecord ()).returnlName ()))				{					foundpos = true;				} else {				                if (lastname.compareTo ((current.nextStudentRecord ()).returnlName ()) > 0) //Compare the entry and the existing                    {                 current = current.nextStudentRecord (); //Advance to the next one                } else {                 foundpos = true; //or indicate the position is found                }                }            }			return current;    }            }      class StudentRecord{    String fName, lName, phoneNum;    int grade;    boolean crimRecord;    StudentRecord next;    public StudentRecord (String fname1, String lName1, int grade1, String phoneNum1, boolean crimRecord1)    {        fName = fname1;        lName = lName1;        grade = grade1;        phoneNum = phoneNum1;        crimRecord = crimRecord1;    }    public void addStudentRecord (String fName2, String lName2, int grade2, String phoneNum2, boolean crimRecord2)    {        next = new StudentRecord (fName2, lName2, grade2, phoneNum2, crimRecord2);    }    public StudentRecord nextStudentRecord()    {        return next;    }    public String returnlName()    {        return lName;    }    public String returnfName()    {        return fName;    }    public void nextLink (StudentRecord newLink)    {        next = newLink;    }    public int returnGrade ()    {        return grade;    }    public void assignGrade (int newGrade)    {        grade = newGrade;    }    public String returnFormattedString ()    {        return (lName + generateSpaces(18 - lName.length()) + fName + generateSpaces(18 - fName.length()) + grade + generateSpaces(6 - String.valueOf(grade).length()) + phoneNum + generateSpaces(15 - phoneNum.length()) + crimRecord);          }    private String generateSpaces(int numspaces)     {        String spaces = "";        for (int i = 0; i < numspaces; i++)         {            spaces = spaces + " ";          }        return spaces;    }}