All pastes #4336 Raw Edit

Anonymous

public java v1 · immutable
#4336 ·published 2005-01-17 00:11 UTC
rendered paste body
import java.io.*;class Wait {  public static void oneSec() {     try {       Thread.currentThread().sleep(1000);       }     catch (InterruptedException e) {       e.printStackTrace();       }     }    public static void manySec(long s) {     try {       Thread.currentThread().sleep(s * 1000);       }     catch (InterruptedException e) {       e.printStackTrace();       }     }}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", "ASDasd", 99, "4167309664", true);        enrollStudent("Jesus", "Christ", 5, "777", false);        enrollStudent("Steve", "Spurrier", 66, "1111111", true);        enrollStudent("Your", "Mother", 39, "133123434", true);        enrollStudent("Don", "Juan", 56, "6669", true);        viewStudentRecords();        System.out.println("Now order by grades");        sortByGrade();        viewStudentRecords();                   }      public static void viewStudentRecords()    {    StudentRecord currentpos = initial;    while (currentpos != null)    {        System.out.println (currentpos.returnFormattedString ());        currentpos = currentpos.nextStudentRecord ();    }    }      public static void sortByGrade ()  {  	StudentRecord currentpos;  	StudentRecord templink;  	StudentRecord secondpos;  	StudentRecord lastpos;  	StudentRecord previouslink;  	StudentRecord currentpostemp;  	boolean sort = true;  	int debugcount = 0;  	//Sort it now (alter the link list)  	  	while (sort)  	{  		sort = false;  		//Sorting the first one  		if (initial.returnGrade() < initial.nextStudentRecord().returnGrade())   		{  			  			templink = initial.nextStudentRecord();  			initial.nextLink(templink.nextStudentRecord());  			templink.nextLink(initial);  			initial = templink;  			//System.out.println(initial.returnlName());  			System.out.println("IS THIS INITIAL RIGHT? " + initial.returnlName());  			  				  		}  		previouslink = initial;  		//System.out.println(initial.nextStudentRecord().returnlName());  		currentpos = initial.nextStudentRecord();  		System.out.println("IS THIS CURRENTPOS RIGHT? " + currentpos.returnlName());  		while (currentpos.nextStudentRecord() != null)  		{  			  			currentpostemp = currentpos;  			if (currentpos.nextStudentRecord() != null)  {  			secondpos = currentpos.nextStudentRecord();  			//System.out.println ("secondpos: " + secondpos.returnlName());  			} else {  			secondpos = null;  			System.out.println ("secondpos: null");	  				  				  				  			}  			  			if (secondpos.nextStudentRecord() != null)   			{  				lastpos = secondpos.nextStudentRecord();  			} else {  				lastpos = null;  			}  			  			viewStudentRecords();  			if (currentpos.returnGrade() < secondpos.returnGrade())   			{	  				System.out.println("previouslink("+previouslink.returnlName()+").nextLink("+secondpos.returnlName()+")");  				previouslink.nextLink(secondpos);  				if (secondpos.nextStudentRecord() != null)   				{  					System.out.println("secondpos(" + secondpos.returnlName() + ").nextLink("+currentpos.returnlName()+")");  				secondpos.nextLink(currentpos);  				System.out.println("currentpos(" + currentpos.returnlName()+ ").nextLink(" + lastpos.returnlName() + ")");  				currentpos.nextLink(lastpos);  				}  				sort = true;  				previouslink = currentpostemp;				currentpos = currentpostemp.nextStudentRecord();								//System.out.println("NEW PREVIOUS POSITION: " + previouslink.returnlName());				System.out.println("(MADE CHANGE) NEW CURRENT POSITION: " + currentpos.returnlName());  			} else {  				previouslink = currentpos;  				currentpos = currentpostemp.nextStudentRecord(); 				  				  				//System.out.println("NEW PREVIOUS POSITION: " + previouslink.returnlName());				System.out.println("(NO CHANGE) NEW CURRENT POSITION: " + currentpos.returnlName());  				  			}  			  			  	System.out.println ("-----------");  				Wait.oneSec();  				//previouslink = currentpostemp;  			//currentpos = currentpos.nextStudentRecord();  		}   		debugcount++;  		//System.out.println(debugcount + "sort = " + sort);			  	}  }  	  	  	  	              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)     {    	if (lastname.compareTo (initial.returnlName ()) < 0)		{		newStudentRecord.nextLink(initial);		initial = newStudentRecord;		} else {		StudentRecord searchResult = searchByName(firstname, lastname);		newStudentRecord.nextLink (searchResult.nextStudentRecord());		searchResult.nextLink (newStudentRecord);		}    } else {    	        initial = newStudentRecord;    	newStudentRecord.nextLink(null);	    }      }    //Expells the student  //The searchByName cannot be used (as there is no link to the previous node)  public static void expellStudent(String firstname, String lastname)  {  	if (initial != null)  	{  		if (((initial.returnlName()).equals(lastname)) && ((initial.returnfName()).equals(firstname)))  		{  			//First check if the first user in the list is a match  			initial = initial.nextStudentRecord();		  		} else {  			StudentRecord currentpos = initial;  			boolean foundpos = false;  			//Checks for two records ahead so that it's possible to expel the last student in the list  			while ((currentpos.nextStudentRecord()).nextStudentRecord() != null && !foundpos)   			{  				if (lastname.compareTo ((currentpos.nextStudentRecord ()).returnlName ()) > 0)  				{  					currentpos = currentpos.nextStudentRecord();  				} else {  					foundpos = true;  				}  			}  			currentpos.nextLink(currentpos.nextStudentRecord().nextStudentRecord());  		}  		  		  		  	} else {  		  		System.out.println ("No students to expell");  		  	}  	  	  }    public static int classAverage()  {  	StudentRecord currentpos = initial;  	int gradeTotal = 0;  	int numStudents = 0;  	while (currentpos.nextStudentRecord() != null)   	{	  		gradeTotal = gradeTotal + currentpos.returnGrade();  		numStudents++;  		currentpos = currentpos.nextStudentRecord();  	}  	return gradeTotal / numStudents;  }      //Enters a new grade for a student (no output)  public static void enterGrade(String firstname, String lastname, int newgrade)  {  	if (initial != null)  	{  		if (((initial.returnlName()).equals(lastname)) && ((initial.returnfName()).equals(firstname)))   		{  			initial.assignGrade(newgrade);  		} else {  			StudentRecord searchResult = searchByName(firstname, lastname);  			searchResult.nextStudentRecord().assignGrade(newgrade);  		}  	} else {  		  		System.out.println ("Error: No entries exist in database");  	}  	  	  	  	  }      //Performs a search for the name (outputs the search result)   private static StudentRecord searchByName(String firstname, String lastname)   {   boolean foundpos = false; 	StudentRecord current = initial;			if (lastname.compareTo (current.returnlName ()) < 0)			{				foundpos = true;											}               while (((current.nextStudentRecord ()) != null) && !foundpos) //Start the search            {				                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;    }}