Created
March 24, 2011 09:51
-
-
Save Cellane/884817 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Comparator; | |
public class DateComparator implements Comparator<Person> { | |
public int compare (Person person, Person person1) { | |
if (person.getRawBirthday ().after (person1.getRawBirthday ())) { | |
return (1); | |
} | |
if (person.getRawBirthday ().before (person1.getRawBirthday ())) { | |
return (-1); | |
} | |
return (person.compareTo (person1)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.Collator; | |
import java.util.Comparator; | |
import java.util.Locale; | |
public class FieldComparator implements Comparator<Person> { | |
public int compare (Person person, Person person1) { | |
Collator collator = Collator.getInstance (new Locale ("cs")); | |
return (collator.compare (person.getField (), person1.getField ())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
private static List<Person> list = new ArrayList<Person> (); | |
public static void main (String[] args) { | |
Main main = new Main (); | |
main.fillArray (); | |
main.printList (list); | |
main.showSort (); | |
} | |
private void fillArray () { | |
list.add (new Person ("Jan", "Novák", "běžec", "1. 1. 1990", 60.5, 1.75)); | |
list.add (new Person ("Pepa", "Vomáčka", "fotbalista", "5. 7. 1990", 65.5, 1.78)); | |
list.add (new Person ("Jan", "Hřebejk", "běžec", "1. 12. 1990", 55.5, 1.70)); | |
list.add (new Person ("Franta", "Král", "kriketista", "5. 10. 1990", 60., 2.30)); | |
list.add (new Person ("Jan", "Král", "basketbalista", "9. 11. 1990", 60.2, 2.25)); | |
list.add (new Person ("Jan", "Vorel", "floorballista", "10. 10. 1990", 70, 2.5)); | |
list.add (new Person ("Pepa", "Vorel", "floorballista", "10. 10. 1990", 70.5, 2.)); | |
list.add (new Person ("Pepa", "Skočdopole", "fotbalista", "10. 10. 1990", 70.5, 2.)); | |
list.add (new Person ("Pepa", "Nejezchleba", "fotbalista", "10. 10. 1990", 70.5, 2.)); | |
list.add (new Person ("Pepa", "Bezfantazie", "běžec", "10. 10. 1990", 70.5, 2.)); | |
} | |
private void printList (List<Person> list) { | |
for (Person person : list) { | |
System.out.println (person.getFirstName () + " " + person.getLastName () + "; " + | |
person.getBirthday () + ", " + person.getField () + ", " + | |
person.getWeight () +"kg, " + person.getWidth () + "m."); | |
} | |
System.out.println (); | |
} | |
private void showSort () { | |
Scanner scanner = new Scanner (System.in); | |
String input; | |
do { | |
System.out.println ("Podle čeho řadit? 1 – jméno, 2 – obor, 3 – narození, 4 – váha, 5 – výška."); | |
System.out.println ("9 pro otočení řazení."); | |
System.out.println ("0 pro ukončení."); | |
input = scanner.next (); | |
if (!input.equals ("0")) { | |
if (input.equals ("1")) { | |
Collections.sort (list); | |
} else if (input.equals ("2")) { | |
Collections.sort (list, new FieldComparator ()); | |
} else if (input.equals ("3")) { | |
Collections.sort (list, new DateComparator ()); | |
} else if (input.equals ("4")) { | |
Collections.sort (list, new WeightComparator ()); | |
} else if (input.equals ("5")) { | |
Collections.sort (list, new WidthComparator ()); | |
} else if (input.equals ("9")) { | |
Collections.reverse (list); | |
} else { | |
System.out.println ("Neumíš číst nebo psát?"); | |
} | |
printList (list); | |
} else { | |
break; | |
} | |
} while (true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.Collator; | |
import java.text.DateFormat; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Locale; | |
public class Person implements Comparable<Person> { | |
private String firstName; | |
private String lastName; | |
private String field; | |
private Date birthday; | |
private double weight; | |
private double width; | |
public Person (String firstName, String lastName, String field, String birthday, double weight, double width) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.field = field; | |
this.weight = weight; | |
this.width = width; | |
setBirthday (birthday); | |
} | |
public String getFirstName () { | |
return firstName; | |
} | |
public void setFirstName (String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName () { | |
return lastName; | |
} | |
public void setLastName (String lastName) { | |
this.lastName = lastName; | |
} | |
public String getField () { | |
return field; | |
} | |
public void setField (String field) { | |
this.field = field; | |
} | |
public String getBirthday () { | |
Calendar calendar = Calendar.getInstance (); | |
StringBuilder stringBuilder = new StringBuilder (); | |
String string; | |
calendar.setTime (birthday); | |
stringBuilder.append (calendar.get (Calendar.DAY_OF_MONTH)).append (". "); | |
stringBuilder.append (calendar.get (Calendar.MONTH) + 1).append (". "); | |
stringBuilder.append (calendar.get (Calendar.YEAR)); | |
string = stringBuilder.toString (); | |
return string; | |
} | |
public Date getRawBirthday () { | |
return (birthday); | |
} | |
public void setBirthday (String birthday) { | |
DateFormat format = new SimpleDateFormat ("d. M. yyyy"); | |
try { | |
this.birthday = format.parse (birthday); | |
} catch (ParseException e) { | |
System.out.println ("Špatný formát data!"); | |
} | |
} | |
public double getWeight () { | |
return weight; | |
} | |
public void setWeight (double weight) { | |
this.weight = weight; | |
} | |
public double getWidth () { | |
return width; | |
} | |
public void setWidth (double width) { | |
this.width = width; | |
} | |
public int compareTo (Person person) { | |
Collator collator = Collator.getInstance (new Locale ("cs")); | |
int result; | |
result = collator.compare (lastName, person.getLastName ()); | |
if (result == 0) { | |
result = collator.compare (firstName, person.getFirstName ()); | |
} | |
return (result); | |
} | |
public String toString () { | |
return (getFirstName () + " " + getLastName ()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Comparator; | |
public class WeightComparator implements Comparator<Person> { | |
public int compare (Person person, Person person1) { | |
if (person.getWeight () > person1.getWeight ()) { | |
return (1); | |
} | |
if (person.getWeight () < person1.getWeight ()) { | |
return (-1); | |
} | |
return (0); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Comparator; | |
public class WidthComparator implements Comparator<Person> { | |
public int compare (Person person, Person person1) { | |
if (person.getWidth () > person1.getWidth ()) { | |
return (1); | |
} | |
if (person.getWidth () < person1.getWidth ()) { | |
return (-1); | |
} | |
return (0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment