Skip to content

Instantly share code, notes, and snippets.

@stopfaner
Created July 21, 2014 19:30
Show Gist options
  • Save stopfaner/565e22cc0f2da1d95ecf to your computer and use it in GitHub Desktop.
Save stopfaner/565e22cc0f2da1d95ecf to your computer and use it in GitHub Desktop.
Fuck Yeah number two
import java.io.File;
import java.io.IOException;
import java.util.*;
public class main {
//Arrays declarations
//Array of Human objects
public static List<Human> humans = new ArrayList<Human>();
//Array of types of sorting
public static List<String> sortingTypes = new ArrayList<String>();
/*
Main function, all actions start here
*/
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new File("file.txt"));
//Filling array of Humans
while(scanner.hasNext()){
humans.add(new Human(scanner.next()));
}
//Filling array of sorting types
sortingTypes.add("именам");
sortingTypes.add("полам");
sortingTypes.add("возрастам");
sortingTypes.add("оценкам");
sortingTypes.add("семестрам");
//Caling functions of sorting and rendering
sortByNames(humans);
renderHuman(humans,sortingTypes.get(0));
sortByGender(humans);
renderHuman(humans,sortingTypes.get(1));
sortByAges(humans);
renderHuman(humans,sortingTypes.get(2));
sortByRating(humans);
renderHuman(humans,sortingTypes.get(3));
sortBySemester(humans);
renderHuman(humans,sortingTypes.get(4));
}
//------------------------------------------------
static class Human{
/*
Human data variebles
*/
private String nameHuman;
private String genderHuman;
private int ageHuman;
private double ratingHuman;
private int semesterHuman;
//--------------------------------------------------
//Defaut constructor
Human(String string){
String []parts = string.split(",");
nameHuman = parts[0];
genderHuman = parts[1];
ageHuman = Integer.parseInt(parts[2]);
ratingHuman = Double.parseDouble(parts[3]);
semesterHuman = Integer.parseInt(parts[4]);
}
//---------Getters---------------------------------
public String getNameHuman(){
return nameHuman;
}
public String getGenderHuman(){
return genderHuman;
}
public int getAgeHuman(){
return ageHuman;
}
public double getRatingHuman(){
return ratingHuman;
}
public int getSemesterHuman(){
return semesterHuman;
}
//-------------end---------------------------------
}
/*
Sorting functions
*/
//-----------------start-----------------------
static void sortByNames(List <Human> humanData){
for(int i = 0; i < humanData.size(); i++){
for (int j = humanData.size() - 1; j > i; j--){
if(humanData.get(j-1).getNameHuman().
compareTo(humanData.get(j).getNameHuman())>0){
Collections.swap(humanData, j-1, j);
}
}
}
}
//--------------------------------------------------------------------
static void sortByGender(List <Human> humanData){
for(int i = 0; i < humanData.size(); i++){
for (int j = humanData.size() - 1; j > i; j--){
if(humanData.get(j-1).getGenderHuman().
compareTo(humanData.get(j).getGenderHuman())>0){
Collections.swap(humanData, j-1, j);
}
}
}
}
//---------------------------------------------------------
static void sortByAges(List <Human> humanData){
for(int i = 0; i < humanData.size(); i++){
for (int j = humanData.size() - 1; j > i; j--){
if(humanData.get(j-1).getAgeHuman()>
humanData.get(j).getAgeHuman()){
Collections.swap(humanData, j-1, j);
}
}
}
}
//-----------------------------------------------------------
static void sortByRating(List <Human> humanData){
for(int i = 0; i < humanData.size(); i++){
for (int j = humanData.size() - 1; j > i; j--){
if(humanData.get(j-1).getRatingHuman()>
humanData.get(j).getRatingHuman()){
Collections.swap(humanData, j-1, j);
}
}
}
}
//-------------------------------------------------
static void sortBySemester(List <Human> humanData){
for(int i = 0; i < humanData.size(); i++){
for (int j = humanData.size() - 1; j > i; j--){
if(humanData.get(j-1).getSemesterHuman()>
humanData.get(j).getSemesterHuman()){
Collections.swap(humanData, j-1, j);
}
}
}
}
//-----------------end---------------------------
/*
Render function
*/
static void renderHuman(List <Human> humanData, String dataType){
System.out.println("Сортировка по " + dataType + ":");
for(Human human:humanData){
System.out.println(human.getNameHuman()+" "
+human.getGenderHuman()+" "+human.getAgeHuman()+" "
+human.getRatingHuman()+" "+human.getSemesterHuman());
}
System.out.println();
}
//-----------------------------------------------------
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment