//Mason Marnell day 10
//DatabaseProject class
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class DatabaseProject {
static ArrayList LoricArray = new ArrayList<Loric>();
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 Save() throws Exception{
PrintWriter output = new PrintWriter(new FileWriter("./src/listofcharacters"));
for (int i = 0; i < LoricArray.size(); i++) {
output.println(LoricArray.get(i));
}
System.out.println("File saved.");
output.close();
}
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"
+ "D: Sort Menu \n"
+ "E: Add and Delete \n"
+ "H: Change \n"
+ "F: Save \n"
+ "X: Exit";
choice = JOptionPane.showInputDialog(menu);
if(choice.equals("A"))
readInList();
if(choice.equals("B"))
DisplayMenu();
if(choice.equals("C"))
SearchMenu();
if(choice.equals("D"))
SortMenu();
if(choice.equals("E"))
AddAndDelete();
if(choice.equals("F"))
Save();
if(choice.equals("H"))
ChangeClass.Change(LoricArray);
}
}
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 SortMenu() throws Exception{
String choice = "";
String menu = "Sort Menu \n"
+ "A: Sort alphabetically \n"
+ "B: Sort by number \n"
+ "C: Sort by number in reverse \n"
+ "X: Exit";
choice = JOptionPane.showInputDialog(menu);
if(choice.equals("A"))
SortClass.Name(LoricArray);
if(choice.equals("B"))
SortClass.Number(LoricArray);
if(choice.equals("C"))
SortClass.NumberR(LoricArray);
}
public static void AddAndDelete() throws Exception{
String choice = "";
String menu = "Add and Delete Menu \n"
+ "A: Add \n"
+ "B: Delete \n"
+ "X: Exit";
choice = JOptionPane.showInputDialog(menu);
if(choice.equals("A"))
AddAndDelete.Add(LoricArray);
if(choice.equals("B"))
AddAndDelete.Delete(LoricArray);
}
public static void main(String[] args) throws Exception{
readInList();
mainMenu();
}
}
//----------------------------------------------------------------------------------------------------------------------
//ChangeClass class
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class ChangeClass {
public static void Change(ArrayList<Loric> 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";
String ans = JOptionPane.showInputDialog(toDisp + "Do you want to change (Y) ?");
if(ans.contains("Y")){
int num = Integer.parseInt(JOptionPane.showInputDialog(toDisp + "Enter a number"));
String name2 = JOptionPane.showInputDialog(toDisp + "Enter a name");
String leg1 = JOptionPane.showInputDialog(toDisp + "Enter the main (1st) legacy");
String leg2 = JOptionPane.showInputDialog(toDisp + "Enter the sub (2nd) legacy");
theloric.get(i).setNumber(num);
theloric.get(i).setName(name2);
theloric.get(i).setLegacy1(leg1);
theloric.get(i).setLegacy1(leg2);
}
}
}
JOptionPane.showMessageDialog(null, "Finished with search.");
}
}
Comments