import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.ListIterator;
public class SortingLL {
private String MTitle;
private String MIdentifier;
private String ATitle;
private String DateSet;
private String DueDate;
private String AAuthor;
private String Weighting;
static LinkedList<SortingLL> assignmentList;
static Date date;
static Date dateCheck;
static Date tempDate;
static int index;
static int firstIndex;
public SortingLL(String MTitle, String MIdentifier, String ATitle, String DateSet, String DueDate, String AAuthor, String Weighting)
{
this.MTitle = MTitle;
this.MIdentifier = MIdentifier;
this.ATitle = ATitle;
this.DateSet = DateSet;
this.DueDate = DueDate;
this.AAuthor = AAuthor;
this.Weighting = Weighting;
}
public String getDateSet()
{
return DateSet;
}
public void setDateSet(String DateSet)
{
this.DateSet = DateSet;
}
public String toString() {
// return MTitle + "\n" + MIdentifier + "\n" + ATitle + "\n" + DateSet + "\n" + DueDate + "\n" + AAuthor + "\n" + Weighting + "\n";
return MTitle + " " + MIdentifier + " " + ATitle + " " + DateSet + " " + DueDate + " "
+ AAuthor + " " + Weighting + "\n";
}
public static class Registry
{
public Registry()
{
assignmentList = new LinkedList<SortingLL>();
}
public void addAssignment(SortingLL aAssignment)
{
assignmentList.add(aAssignment);
}
public void orderSet()
{
SimpleDateFormat ValidFormat = new SimpleDateFormat("dd/MM/yyyy");
ListIterator<SortingLL> iterator = assignmentList.listIterator(1);
boolean sorted = false;
while(!sorted)
{
while(iterator.hasNext()) {
firstIndex = iterator.previousIndex();
String previousDate = assignmentList.get(firstIndex).getDateSet();
try {
date = ValidFormat.parse(previousDate);
}
catch(ParseException pe) {
System.out.println("ParseException" + pe);
}
index = assignmentList.indexOf(iterator.next());
String checkSetDate = assignmentList.get(index).getDateSet();
try {
dateCheck = ValidFormat.parse(checkSetDate);
}
catch(ParseException pe) {
System.out.println("ParseException" + pe);
}
// System.out.println("Testing inner while loop");
System.out.println("previous date = " + previousDate + " current date = " + checkSetDate);
if(dateCheck.before(date)) {
int previous = firstIndex;//Index of previous assignment
int current = index; // Index of current assignment
SortingLL temporary = assignmentList.get(firstIndex);
// System.out.println("tempIdx =" + temp + " int currentIdx= " + current);
System.out.println("setting previous=" + previous + " to " + assignmentList.get(index));
assignmentList.set(previous, assignmentList.get(index));
System.out.println("setting current=" + current + " to " + temporary);
assignmentList.set(current, temporary);
}
}
sorted = true;
}
System.out.println("After aL=" + assignmentList.toString());
}
}
public static void main(String[] args) {
Registry newAssignment = new Registry();
newAssignment.addAssignment(new SortingLL("Java 1","ASM 1001","Java assignment 1","28/02/2012","10/05/2012","Mr Joe Bloggs","20%"));
newAssignment.addAssignment(new SortingLL("Java 2","ASM 1002","Java assignment 2","27/02/2012","19/05/2012","Mr Joe Bloggs","20%"));
newAssignment.addAssignment(new SortingLL("Java 3","ASM 1003","Java assignment 3","23/02/2012","22/05/2012","Mr Joe Bloggs","20%"));
newAssignment.addAssignment(new SortingLL("Java 4","ASM 1004","Java assignment 4","29/02/2012","22/05/2012","Mr Joe Bloggs","20%"));
System.out.println("Before aL=" + SortingLL.assignmentList.toString());
System.out.println("Testing sort");
System.out.println("************\n");
newAssignment.orderSet();
}
}