Created
August 21, 2018 08:24
-
-
Save luchtech/39de9b9f31d3752cd6f51e253028c587 to your computer and use it in GitHub Desktop.
Laboratory Activity: List and Sorting Algorithm
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.Vector; | |
public class Magic { | |
// DUPLICATE CHECKERS | |
static boolean isDuplicate(String s, Vector<String> arr) { // String Array | |
for(String x: arr) { | |
if(x.equalsIgnoreCase(s)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
static boolean isDuplicate(int s, Vector<Integer> arr) { // Integer Array | |
for(int x: arr) { | |
if(x == s) { | |
return true; | |
} | |
} | |
return false; | |
} | |
static boolean isDuplicate(double s, Vector<Double> arr) { // Double Array | |
for(double x: arr) { | |
if(x == s) { | |
return true; | |
} | |
} | |
return false; | |
} | |
static boolean isDuplicate(char s, Vector<Character> arr) { // Character Array | |
for(char x: arr) { | |
if(x == s) { | |
return true; | |
} | |
} | |
return false; | |
} | |
static boolean isDuplicate(String s, ArrayList<String> arr) { // String Array | |
for(String x: arr) { | |
if(x.equalsIgnoreCase(s)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
static boolean isDuplicate(int s, ArrayList<Integer> arr) { // Integer Array | |
for(int x: arr) { | |
if(x == s) { | |
return true; | |
} | |
} | |
return false; | |
} | |
static boolean isDuplicate(double s, ArrayList<Double> arr) { // Double Array | |
for(double x: arr) { | |
if(x == s) { | |
return true; | |
} | |
} | |
return false; | |
} | |
static boolean isDuplicate(char s, ArrayList<Character> arr) { // Character Array | |
for(char x: arr) { | |
if(x == s) { | |
return true; | |
} | |
} | |
return false; | |
} | |
//STRING CLEANER | |
static String cleanString(String s) { // CONVERTS RAW STRING TO A CLEAN STRING | |
if(s.equals("")) return s; | |
String temp = "", hold = ""; | |
char c; | |
for (int i = 0; i < s.length(); i++) { // REMOVES INAPPROPRIATE WHITESPACES | |
c = s.charAt(i); | |
if(Character.isLetter(c)) { | |
temp+=c; | |
} | |
else if(Character.isWhitespace(c) && temp.length() > 0 && !Character.isWhitespace(temp.charAt(temp.length()-1))) { | |
temp+=" "; | |
} | |
} | |
String[] arr = temp.split(" "); | |
for (int i = 0; i < arr.length; i++) { // CONVERTS FIRST LETTER FOR EACH WORD | |
arr[i] = arr[i].toUpperCase().charAt(0)+arr[i].toLowerCase().substring(1); | |
hold+=arr[i]; | |
if(i != arr.length-1) { | |
hold+=" "; | |
} | |
} | |
return hold; | |
} | |
//SOMETHING | |
static int toInteger(String s) { // CONVERTS STRING TO INTEGER | |
String hold = ""; | |
char c; | |
for(int i = 0; i < s.length(); i++) { | |
c = s.charAt(i); | |
if(Character.isDigit(c)) { | |
hold+=c; | |
} | |
else if(c == '-' && hold.equals("")) { | |
hold+=c; | |
} | |
} | |
if(hold.equals("")) return -1; | |
else return Integer.parseInt(hold); | |
} | |
static Double toDouble(String s) { // CONVERTS STRING TO DOUBLE | |
String hold = ""; | |
int ctr = 0; | |
char c; | |
for(int i = 0; i < s.length(); i++) { | |
c = s.charAt(i); | |
if(Character.isDigit(c)) { | |
hold+=c; | |
} | |
else if(c == '-' && hold.equals("")) { | |
hold+=c; | |
} | |
else if(c == '.' && ctr != 1) { | |
hold+=c; | |
ctr++; | |
} | |
} | |
if(hold.equals("")) return 0.0; | |
else return Double.parseDouble(hold); | |
} | |
static boolean allWhite(String s) { // CHECKS WHITESPACE INPUTS | |
char c; | |
for(int i = 0; i < s.length(); i++) { | |
c = s.charAt(i); | |
if(!Character.isWhitespace(c)) { | |
return false; | |
} | |
} | |
return 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.util.ArrayList; | |
import javax.swing.JOptionPane; | |
import javax.swing.JTextArea; | |
public class PetRecordsSystem { | |
static ArrayList<String> myPetList = new ArrayList<String>(); | |
static JOptionPane jop = new JOptionPane(); | |
public static void main(String[] args) { | |
String menu[] = {"Add", "Insert", "Search", "Edit", "View", "Delete", "Count", "Terminate"}; | |
String choice = "", message = "", header = "", name = "", name2 = ""; | |
int index = 0; | |
for(;;) { | |
try { | |
choice = jop.showInputDialog(null, "Please choose one:", "Pet List", 1, null, menu, menu[0]).toString(); | |
}catch (Exception e) { | |
jop.showMessageDialog(null, "Good bye!", "Terminating...", 1); | |
System.exit(0); | |
} | |
switch (choice) { | |
case "Add": | |
header = "Result:\n\n"; | |
name = Magic.cleanString(jop.showInputDialog("Enter student name:")); | |
if(Magic.allWhite(name)) { | |
message = "Invalid input!"; | |
} else if(Magic.isDuplicate(name, myPetList)) { | |
message = name+" already exists!"; | |
} | |
else { | |
myPetList.add(name); | |
message = name+" is successfully added!"; | |
} | |
break; | |
case "Insert": | |
header = "Result:\n\n"; | |
name = Magic.cleanString(jop.showInputDialog("Enter student name:")); | |
if(Magic.allWhite(name)) { | |
message = "Invalid input!"; | |
} | |
else if(Magic.isDuplicate(name, myPetList)) { | |
message = "Cannot insert "+name+" because it is found at index "+myPetList.indexOf(name)+"."; | |
} | |
else { | |
index = Magic.toInteger(jop.showInputDialog("Enter index from 0 to "+(myPetList.size()-1)+":")); | |
if(index < 0 || index >= myPetList.size()) { | |
message = "Invalid index!"; | |
} | |
else { | |
myPetList.add(index, name); | |
message = name+" is successfully added at index "+index+"."; | |
} | |
} | |
break; | |
case "Search": | |
header = "Result:\n\n"; | |
name = Magic.cleanString(jop.showInputDialog("Enter student name:")); | |
if(Magic.allWhite(name)) { | |
message = "Invalid input!"; | |
} | |
else if(Magic.isDuplicate(name, myPetList)) { | |
message = name+" is at index "+myPetList.indexOf(name)+"."; | |
} | |
else message = name+" is not found."; | |
break; | |
case "Edit": | |
header = "Result:\n\n"; | |
name = Magic.cleanString(jop.showInputDialog("Enter student name:")); | |
if(Magic.allWhite(name)) { | |
message = "Invalid input!"; | |
} | |
else if(Magic.isDuplicate(name, myPetList)) { | |
name2 = Magic.cleanString(jop.showInputDialog(name+" is at index "+myPetList.indexOf(name)+".\nWhat is the new name?")); | |
if(Magic.allWhite(name)) { | |
message = "Invalid input!"; | |
} | |
else if(Magic.isDuplicate(name2, myPetList)) { | |
message = "Cannot add "+name2+" because it already exists at index "+myPetList.indexOf(name2)+"."; | |
} | |
else { | |
myPetList.set(myPetList.indexOf(name), name2); | |
message = name+" is replaced with "+name2+"."; | |
} | |
} | |
else message = name+" is not found."; | |
break; | |
case "View": | |
message = ""; | |
if(myPetList.size() == 0) { | |
header = "Result\n\n"; | |
message = "The database is empty."; | |
} | |
else { | |
header = "Name\tIndex\n"; | |
for (String x : myPetList) { | |
message+=x+"\t"+myPetList.indexOf(x)+"\n"; | |
} | |
} | |
break; | |
case "Delete": | |
header = "Result:\n\n"; | |
name = Magic.cleanString(jop.showInputDialog("Enter student name:")); | |
if(Magic.allWhite(name)) { | |
message = "Invalid input!"; | |
} | |
else if(Magic.isDuplicate(name, myPetList)) { | |
myPetList.remove(myPetList.indexOf(name)); | |
message = name+" is at successfully deleted."; | |
} | |
else message = name+" is not found."; | |
break; | |
case "Count": | |
header = "Result:\n\n"; | |
message = "Current Population: "+myPetList.size(); | |
break; | |
case "Terminate": | |
jop.showMessageDialog(null, "Good bye!", "Terminating...", 1); | |
System.exit(0); | |
break; | |
} | |
jop.showMessageDialog(null, new JTextArea(header+message)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment