//Mason Marnell //Day 6 // DatabaseProject import java.io.File; import java.util.ArrayList; import java.util.Scanner; import javax.swing.JOptionPane; public class DatabaseProject { static ArrayList LoricArray = new ArrayList(); public static void readInList() throws Exception{ Scanner file = new Scanner(new File("./src/listofcharacters")); while(file.hasNextLine()){ String line = file.nextLine(); String Splitline[] = line.split(","); int num = Integer.parseInt(Splitline[0]); String name = Splitline[1]; String leg1 = Splitline[2]; String leg2 = Splitline[3]; Loric temp = new Loric(num,name,leg1,leg2); System.out.println(temp); LoricArray.add(temp); } System.out.println("Done reading in file."); } public static void mainMenu() throws Exception{ String choice = ""; while(! choice.equals("X")){ String menu = "Main Menu \n" + "A: Read in Data \n" + "B: Display Menu \n" + "C: Search Menu \n" + "X: Exit"; choice = JOptionPane.showInputDialog(menu); if(choice.equals("A")) readInList(); if(choice.equals("B")) DisplayMenu(); if(choice.equals("C")) SearchMenu(); } } public static void DisplayMenu() throws Exception{ String choice = ""; String menu = "Display Menu \n" + "A: Number Legacy 1 Legacy 2 \n" + "B: Number Name Legacy 1 \n" + "C: Name Legacy 1 Legacy 2 \n" + "X: Exit"; choice = JOptionPane.showInputDialog(menu); if(choice.equals("A")) DisplayClass.NumberLegacy1Legacy2(LoricArray); if(choice.equals("B")) DisplayClass.NumberNameLegacy1(LoricArray); if(choice.equals("C")) DisplayClass.NameLegacy1Legacy2(LoricArray); } public static void SearchMenu() throws Exception{ String choice = ""; String menu = "Search Menu \n" + "A: Search by name \n" + "B: Search by number \n" + "C: Search by legacy \n" + "X: Exit"; choice = JOptionPane.showInputDialog(menu); if(choice.equals("A")) SearchClass.Name(LoricArray); if(choice.equals("B")) SearchClass.Number(LoricArray); if(choice.equals("C")) SearchClass.Legacy(LoricArray); } public static void main(String[] args) throws Exception{ readInList(); mainMenu(); } } //--------------------------------------------------------------------------------------------------------------------------------- // SearchClass import java.util.ArrayList; import javax.swing.JOptionPane; public class SearchClass { public static void Name(ArrayList theloric){ String name = JOptionPane.showInputDialog("Enter a character's name"); for (int i = 0; i < theloric.size(); i++) { if(theloric.get(i).getName().contains(name)){ String toDisp = "Name: " +theloric.get(i).getName() +"\n"; toDisp += "Number: " +theloric.get(i).getNumber() +"\n"; toDisp += "Legacy 1: " +theloric.get(i).getLegacy1() +"\n"; toDisp += "Legacy 2: " +theloric.get(i).getLegacy2() +"\n"; JOptionPane.showMessageDialog(null, toDisp); } } JOptionPane.showMessageDialog(null, "FInished with search."); } public static void Number(ArrayList theloric){ int number = Integer.parseInt(JOptionPane.showInputDialog("Enter a character's number")); for (int i = 0; i < theloric.size(); i++) { if(theloric.get(i).getNumber() == number){ String toDisp = "Name: " +theloric.get(i).getName() +"\n"; toDisp += "Number: " +theloric.get(i).getNumber() +"\n"; toDisp += "Legacy 1: " +theloric.get(i).getLegacy1() +"\n"; toDisp += "Legacy 2: " +theloric.get(i).getLegacy2() +"\n"; JOptionPane.showMessageDialog(null, toDisp); } } JOptionPane.showMessageDialog(null, "Finished with search."); } public static void Legacy(ArrayList theloric){ String leg = JOptionPane.showInputDialog("Enter a character's legacy"); for (int i = 0; i < theloric.size(); i++) { if(theloric.get(i).getLegacy1().contains(leg) || theloric.get(i).getLegacy2().contains(leg)){ String toDisp = "Name: " +theloric.get(i).getName() +"\n"; toDisp += "Number: " +theloric.get(i).getNumber() +"\n"; toDisp += "Legacy 1: " +theloric.get(i).getLegacy1() +"\n"; toDisp += "Legacy 2: " +theloric.get(i).getLegacy2() +"\n"; JOptionPane.showMessageDialog(null, toDisp); } } JOptionPane.showMessageDialog(null, "Finished with search."); } }