MnMWizard

db day 10

Mar 26th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. //Mason Marnell day 10
  2. //DatabaseProject class
  3.  
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.PrintWriter;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9.  
  10. import javax.swing.JOptionPane;
  11.  
  12. public class DatabaseProject {
  13.  
  14.     static ArrayList LoricArray = new ArrayList<Loric>();
  15.    
  16.     public static void readInList() throws Exception{
  17.         Scanner file = new Scanner(new File("./src/listofcharacters"));
  18.         while(file.hasNextLine()){
  19.             String line = file.nextLine();
  20.             String Splitline[] = line.split(",");
  21.             int num = Integer.parseInt(Splitline[0]);
  22.             String name = Splitline[1];
  23.             String leg1 = Splitline[2];
  24.             String leg2 = Splitline[3];
  25.             Loric temp = new Loric(num,name,leg1,leg2);
  26.             System.out.println(temp);
  27.             LoricArray.add(temp);
  28.         }
  29.         System.out.println("Done reading in file.");
  30.                 }
  31.    
  32.     public static void Save() throws Exception{
  33.         PrintWriter output = new PrintWriter(new FileWriter("./src/listofcharacters"));
  34.         for (int i = 0; i < LoricArray.size(); i++) {
  35.             output.println(LoricArray.get(i));
  36.         }
  37.            
  38.        
  39.         System.out.println("File saved.");
  40.         output.close();
  41.     }
  42.     public static void mainMenu() throws Exception{
  43.         String choice = "";
  44.         while(! choice.equals("X")){
  45.             String menu = "Main Menu \n"
  46.                         + "A: Read in Data \n"
  47.                         + "B: Display Menu \n"
  48.                         + "C: Search Menu \n"
  49.                         + "D: Sort Menu \n"
  50.                         + "E: Add and Delete \n"
  51.                         + "H: Change \n"
  52.                         + "F: Save \n"
  53.                         + "X: Exit";
  54.             choice = JOptionPane.showInputDialog(menu);
  55.             if(choice.equals("A"))
  56.                 readInList();
  57.             if(choice.equals("B"))
  58.                 DisplayMenu();
  59.             if(choice.equals("C"))
  60.                 SearchMenu();
  61.             if(choice.equals("D"))
  62.                 SortMenu();
  63.             if(choice.equals("E"))
  64.                 AddAndDelete();
  65.             if(choice.equals("F"))
  66.                 Save();
  67.             if(choice.equals("H"))
  68.                 ChangeClass.Change(LoricArray);
  69.         }
  70.     }
  71.    
  72.     public static void DisplayMenu() throws Exception{
  73.         String choice = "";
  74.             String menu = "Display Menu \n"
  75.                         + "A: Number  Legacy 1  Legacy 2 \n"
  76.                         + "B: Number  Name  Legacy 1 \n"
  77.                         + "C: Name  Legacy 1  Legacy 2 \n"
  78.                         + "X: Exit";
  79.             choice = JOptionPane.showInputDialog(menu);
  80.             if(choice.equals("A"))
  81.                 DisplayClass.NumberLegacy1Legacy2(LoricArray);
  82.             if(choice.equals("B"))
  83.                 DisplayClass.NumberNameLegacy1(LoricArray);
  84.             if(choice.equals("C"))
  85.                 DisplayClass.NameLegacy1Legacy2(LoricArray);
  86.            
  87.         }
  88.    
  89.    
  90.     public static void SearchMenu() throws Exception{
  91.         String choice = "";
  92.             String menu = "Search Menu \n"
  93.                         + "A: Search by name \n"
  94.                         + "B: Search by number \n"
  95.                         + "C: Search by legacy \n"
  96.                         + "X: Exit";
  97.             choice = JOptionPane.showInputDialog(menu);
  98.             if(choice.equals("A"))
  99.                 SearchClass.Name(LoricArray);
  100.             if(choice.equals("B"))
  101.                 SearchClass.Number(LoricArray);
  102.             if(choice.equals("C"))
  103.                 SearchClass.Legacy(LoricArray);
  104.            
  105.         }
  106.    
  107.    
  108.     public static void SortMenu() throws Exception{
  109.         String choice = "";
  110.             String menu = "Sort Menu \n"
  111.                         + "A: Sort alphabetically \n"
  112.                         + "B: Sort by number \n"
  113.                         + "C: Sort by number in reverse \n"
  114.                         + "X: Exit";
  115.             choice = JOptionPane.showInputDialog(menu);
  116.             if(choice.equals("A"))
  117.                 SortClass.Name(LoricArray);
  118.             if(choice.equals("B"))
  119.                 SortClass.Number(LoricArray);
  120.             if(choice.equals("C"))
  121.                 SortClass.NumberR(LoricArray);
  122.            
  123.         }
  124.    
  125.    
  126.     public static void AddAndDelete() throws Exception{
  127.         String choice = "";
  128.             String menu = "Add and Delete Menu \n"
  129.                         + "A: Add \n"
  130.                         + "B: Delete \n"
  131.                         + "X: Exit";
  132.             choice = JOptionPane.showInputDialog(menu);
  133.             if(choice.equals("A"))
  134.                 AddAndDelete.Add(LoricArray);
  135.             if(choice.equals("B"))
  136.                 AddAndDelete.Delete(LoricArray);
  137.            
  138.         }
  139.    
  140.    
  141.     public static void main(String[] args) throws Exception{
  142.         readInList();
  143.         mainMenu();
  144.  
  145.     }
  146.  
  147. }
  148.  
  149. //----------------------------------------------------------------------------------------------------------------------
  150.  
  151. //ChangeClass class
  152.  
  153. import java.util.ArrayList;
  154.  
  155. import javax.swing.JOptionPane;
  156.  
  157. public class ChangeClass {
  158.  
  159. public static void Change(ArrayList<Loric> theloric){
  160.        
  161.         String name = JOptionPane.showInputDialog("Enter a character's name");
  162.         for (int i = 0; i < theloric.size(); i++) {
  163.             if(theloric.get(i).getName().contains(name)){
  164.                 String toDisp = "Name: " +theloric.get(i).getName() +"\n";
  165.                 toDisp += "Number: " +theloric.get(i).getNumber() +"\n";
  166.                 toDisp += "Legacy 1: " +theloric.get(i).getLegacy1() +"\n";
  167.                 toDisp += "Legacy 2: " +theloric.get(i).getLegacy2() +"\n";
  168.                
  169.                 String ans = JOptionPane.showInputDialog(toDisp + "Do you want to change (Y) ?");
  170.                 if(ans.contains("Y")){
  171.                     int num = Integer.parseInt(JOptionPane.showInputDialog(toDisp + "Enter a number"));
  172.                     String name2 = JOptionPane.showInputDialog(toDisp + "Enter a name");
  173.                     String leg1 = JOptionPane.showInputDialog(toDisp + "Enter the main (1st) legacy");
  174.                     String leg2 = JOptionPane.showInputDialog(toDisp + "Enter the sub (2nd) legacy");
  175.                     theloric.get(i).setNumber(num);
  176.                     theloric.get(i).setName(name2);
  177.                     theloric.get(i).setLegacy1(leg1);
  178.                     theloric.get(i).setLegacy1(leg2);
  179.                 }
  180.                
  181.             }
  182.         }
  183.         JOptionPane.showMessageDialog(null, "Finished with search.");
  184.  
  185.     }
  186.    
  187.    
  188. }
Add Comment
Please, Sign In to add comment