-
-
Save dave-martinez/5c7b3a7bbca9e031c6ba to your computer and use it in GitHub Desktop.
Assignment for Sorting
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.ArrayList; | |
import java.util.Collections; | |
import java.util.Scanner; | |
public class Runner { | |
public static ArrayList<Person> pList = new ArrayList<Person>(); | |
public static Scanner scan = new Scanner(System.in); | |
public static void lineBreak(){ | |
System.out.println("------------------------"); | |
} | |
public static void displayChoice(){ | |
System.out.println("[1] Add person\n" | |
+ "[2] Edit\n" | |
+ "[3] Remove\n" | |
+ "[4] Search\n" | |
+ "[5] Sort\n" | |
+ "[6] View\n" | |
+ "[7] Exit\n"); | |
} | |
public static int promptUser(){ | |
int choice; | |
System.out.print("Enter choice: "); | |
choice = scan.nextInt(); | |
scan.nextLine(); | |
return choice; | |
} | |
public static void verifyChoice(int choice){ | |
int index = 0, age; | |
String name; | |
switch(choice){ | |
case 1: | |
System.out.println("Add a person"); | |
lineBreak(); | |
System.out.print("Enter name: "); | |
name = scan.nextLine(); | |
System.out.print("Enter age: "); | |
age = scan.nextInt(); scan.nextLine(); | |
Person dummy = new Person(age, name); | |
pList.add(dummy); | |
break; | |
case 2: | |
System.out.println("Edit a person"); | |
lineBreak(); | |
System.out.print("Enter index: "); | |
index = scan.nextInt(); scan.nextLine(); | |
System.out.print("Enter new name: "); | |
name = scan.nextLine(); | |
System.out.print("Enter new age: "); | |
age = scan.nextInt(); scan.nextLine(); | |
pList.get(index).setName(name); | |
pList.get(index).setAge(age); | |
break; | |
case 3: | |
System.out.print("Remove a person"); | |
lineBreak(); | |
System.out.print("Enter index: "); | |
index = scan.nextInt(); scan.nextLine(); | |
break; | |
case 4: | |
boolean isExist = false; | |
System.out.print("Search a person"); | |
lineBreak(); | |
System.out.print("Enter name: "); | |
name = scan.nextLine(); | |
for(int i = 0; i < pList.size(); i++){ | |
if(pList.get(i).getName().toLowerCase().equals(name.toLowerCase())){ | |
isExist = true; | |
break; | |
} | |
} | |
System.out.println("Name existing: " + isExist); | |
break; | |
case 5: | |
System.out.println("Sorting List"); | |
lineBreak(); | |
// Sort Start | |
for(int i = 0; i < pList.size()-1; i++){ | |
name = pList.get(i).getName(); | |
age = pList.get(i).getAge(); | |
System.out.println("Pass: " + i + "-> " + name + " - " + age); | |
for(int j = i+1; j < pList.size(); j++){ | |
if(age == pList.get(j).getAge() && name.compareToIgnoreCase(pList.get(j).getName()) < 0){ | |
name = pList.get(j).getName(); | |
index = j; | |
System.out.println("Sort by name"); | |
} else if(age > pList.get(j).getAge()){ | |
age = pList.get(j).getAge(); | |
index = j; | |
System.out.println("Sort by age"); | |
} | |
System.out.println("Index - > " + index); | |
Collections.swap(pList, i, index); | |
} | |
} | |
// End Sort | |
break; | |
case 6: | |
System.out.println("Display list"); | |
lineBreak(); | |
System.out.println("Name\t\tAge"); | |
lineBreak(); | |
for(int i = 0; i < pList.size();i++){ | |
System.out.println(pList.get(i).getName() + "\t\t" + pList.get(i).getAge()); | |
} | |
break; | |
case 7: | |
System.out.println("---------\nProgram exit"); | |
break; | |
default: | |
System.out.println("Unknown input"); | |
break; | |
} | |
} | |
public static void main(String[] args){ | |
int choice = 0; | |
do{ | |
displayChoice(); | |
choice = promptUser(); | |
verifyChoice(choice); | |
} while(choice != 7); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment