Advertisement
Guest User

Untitled

a guest
Dec 4th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.12 KB | None | 0 0
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.*;
  7. import java.util.ArrayList;
  8. import java.util.Comparator;
  9. import java.util.Scanner;
  10.  
  11. public class Gui {
  12.     /**
  13.      * The Main class is where we load all the files with the persons and the locals on a object file.
  14.      * If the object file does not exist we load the persons from a .txt file and the locals from other .txt file.
  15.      */
  16.     private ArrayList<People> PeopleList = new ArrayList<>();
  17.     private ArrayList<People> Enrolled_Event = new ArrayList<>();
  18.     private ArrayList<Locals> LocalsList = new ArrayList<>();
  19.  
  20.  
  21.     public static void main(String[] args) {
  22.         new Gui();
  23.     }
  24.  
  25.     Gui() {
  26.         copyFromObjectFile();
  27.         First_Menu first_menu = new First_Menu();
  28.  
  29.     }
  30.  
  31.     class First_Menu extends JFrame {
  32.         private javax.swing.JLabel username;
  33.         private javax.swing.JLabel password;
  34.  
  35.         private javax.swing.JButton createUser;
  36.         private javax.swing.JButton login;
  37.  
  38.         private javax.swing.JTextField username_text;
  39.         private javax.swing.JTextField password_text;
  40.  
  41.         private String user, pass;
  42.  
  43.         private Register_menu register_menu;
  44.         private First_Menu first_menu;
  45.  
  46.  
  47.         public First_Menu() {
  48.             this.setPreferredSize(new Dimension(350, 250));
  49.             this.setTitle("Menu");
  50.             this.setLocation(100, 100);
  51.             this.setVisible(true);
  52.             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  53.             this.setLayout(new FlowLayout());
  54.  
  55.  
  56.             username = new JLabel("Username:");
  57.             this.add(username);
  58.             username_text = new JTextField();
  59.             this.add(username_text);
  60.             username_text.setPreferredSize(new Dimension(60, 20));
  61.  
  62.  
  63.             password = new JLabel("Password:");
  64.             this.add(password);
  65.             password_text = new JTextField();
  66.             this.add(password_text);
  67.             password_text.setPreferredSize(new Dimension(60, 20));
  68.  
  69.             login = new JButton("Login");
  70.             login.addActionListener(actionEvent -> {
  71.  
  72.  
  73.             });
  74.             this.add(login);
  75.  
  76.  
  77.             createUser = new JButton("Create User");
  78.             createUser.addActionListener(actionEvent -> {
  79.                 this.setVisible(false);
  80.                 register_menu = new Register_menu();
  81.                 register_menu.setSize(300,250);
  82.                 username_text.setText("");
  83.                 password_text.setText("");
  84.                 register_menu.setVisible(true);
  85.             });
  86.             this.add(createUser);
  87.  
  88.  
  89.             this.pack();
  90.         }
  91.  
  92.  
  93.     }
  94.  
  95.     class Register_menu extends JFrame {
  96.         private javax.swing.JLabel username;
  97.         private javax.swing.JLabel password;
  98.         private javax.swing.JTextField username_text;
  99.         private javax.swing.JTextField password_text;
  100.  
  101.         private javax.swing.JButton regist;
  102.  
  103.  
  104.         public Register_menu() {
  105.             this.setPreferredSize(new Dimension(350, 250));
  106.             this.setTitle("Register");
  107.             this.setLocation(100, 100);
  108.             this.setVisible(false);
  109.             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  110.             this.setLayout(new FlowLayout());
  111.  
  112.             username = new JLabel("Username: ");
  113.             this.add(username);
  114.             username_text = new JTextField();
  115.             this.add(username_text);
  116.             username_text.setPreferredSize((new Dimension(60, 20)));
  117.  
  118.             password = new JLabel("Password:");
  119.             this.add(password);
  120.             password_text = new JTextField();
  121.             this.add(password_text);
  122.             password_text.setPreferredSize(new Dimension(60, 20));
  123.  
  124.             regist = new JButton("Register");
  125.             this.add(regist);
  126.             regist.addActionListener(e -> {
  127.                 People temp;
  128.  
  129.                 if ((temp = findPersonUser(username_text.getText())) != null) {
  130.                     temp.setPassword(password_text.getText());
  131.                     JOptionPane.showMessageDialog(new Frame(),"Password Created/Updated");
  132.                     this.setVisible(false);
  133.                     first_menu = new First_Menu();
  134.                     first_menu.setVisible(true);
  135.                 } else {
  136.                     JOptionPane.showMessageDialog(new Frame(), "Username doesnt exist");
  137.                 }
  138.             });
  139.         }
  140.  
  141.     }
  142.  
  143.  
  144.     /**
  145.      * Check if a person is on a event.
  146.      *
  147.      * @param person Person to check if is on event.
  148.      * @return True is it is, ou False if not.
  149.      */
  150.     private boolean check_if_Enrolled(People person) {
  151.         int i = 0;
  152.         while (i < Enrolled_Event.size()) {
  153.             if (person == Enrolled_Event.get(i)) {
  154.                 return true;
  155.             }
  156.             i++;
  157.         }
  158.         return false;
  159.     } // Checks if person is on the enrolled List
  160.  
  161.     private People findperson(String Username, String password) {
  162.         int i = 0;
  163.         while (i < PeopleList.size()) {
  164.             if (PeopleList.get(i).getUsername().equalsIgnoreCase(Username) && PeopleList.get(i).getPassword().equalsIgnoreCase(password)) {
  165.                 return PeopleList.get(i);
  166.             }
  167.             i++;
  168.         }
  169.         return null;
  170.     } //Finds a person based on keyword for Login
  171.  
  172.     private People findPersonUser(String user) {
  173.         for(int i = 0; i<PeopleList.size(); i++) {
  174.             if(PeopleList.get(i).getUsername().equalsIgnoreCase(user)) {
  175.                 return PeopleList.get(i);
  176.             }
  177.         }
  178.  
  179.         return null;
  180.     }
  181.  
  182.     private boolean Usernamecheck(String Username) {
  183.         int i = 0;
  184.         while (i < PeopleList.size()) {
  185.             if (PeopleList.get(i).getUsername().equals(Username)) {
  186.                 return true;
  187.             }
  188.             i++;
  189.         }
  190.         return false;
  191.     }
  192.  
  193.     private void Chosing_locals(People person) { // alterar para nao se inscrever duas vezes no mesmo local
  194.         System.out.printf("/Locals Choosen/\n");
  195.         System.out.println(person.getLocals_votes());
  196.  
  197.  
  198.         Scanner sc = new Scanner(System.in);
  199.         ArrayList<Locals> temp = person.getLocals_votes();
  200.  
  201.         while (true) {
  202.             System.out.printf("Which Local would u like to Change? [1-5|6->exit]\n");
  203.             String x = sc.nextLine();
  204.  
  205.             switch (x) {
  206.                 case "1":
  207.                     Locals local = Search_Locals();
  208.                     if (temp.get(0) != null) {
  209.                         Locals old_local = temp.get(0);
  210.                         old_local.Change_votes(0);
  211.                     }
  212.                     temp.set(0, local);
  213.                     local.Change_votes(1);
  214.                     break;
  215.                 case "2":
  216.                     local = Search_Locals();
  217.                     if (temp.get(1) != null) {
  218.                         Locals old_local = temp.get(1);
  219.                         old_local.Change_votes(0);
  220.                     }
  221.                     temp.set(1, local);
  222.                     local.Change_votes(1);
  223.                     break;
  224.                 case "3":
  225.                     local = Search_Locals();
  226.                     if (temp.get(2) != null) {
  227.                         Locals old_local = temp.get(2);
  228.                         old_local.Change_votes(0);
  229.                     }
  230.                     temp.set(2, local);
  231.                     local.Change_votes(1);
  232.                     break;
  233.                 case "4":
  234.                     local = Search_Locals();
  235.                     if (temp.get(3) != null) {
  236.                         Locals old_local = temp.get(3);
  237.                         old_local.Change_votes(0);
  238.                     }
  239.                     temp.set(3, local);
  240.                     local.Change_votes(1);
  241.                     break;
  242.                 case "5":
  243.                     local = Search_Locals();
  244.                     if (temp.get(4) != null) {
  245.                         Locals old_local = temp.get(4);
  246.                         old_local.Change_votes(0);
  247.                     }
  248.                     temp.set(4, local);
  249.                     local.Change_votes(1);
  250.                     break;
  251.                 case "6":
  252.                     break;
  253.                 default:
  254.                     System.out.printf("Error -> Invalid Input\n");
  255.             }
  256.             System.out.printf("Wanna change another Local?[y/n]\n");
  257.             String y = sc.nextLine();
  258.             if (y.equalsIgnoreCase("y")) continue;
  259.  
  260.             else {
  261.                 System.out.printf("New votes --> \n");
  262.                 print_locals_choosen(person);
  263.                 break;
  264.             }
  265.         }
  266.     }
  267.  
  268.     private Locals Search_Locals() {
  269.         // asks which local the user would like to add and searches it in the list returning it when found
  270.         printlocals();
  271.         System.out.printf("-->Chose a Local!\n");
  272.         Scanner sc = new Scanner(System.in);
  273.         while (true) {
  274.             String x = sc.nextLine();
  275.             if (x.matches(".*\\d.*")) System.out.printf("\nError -> Invalid Input\n");
  276.  
  277.             else {
  278.                 int i = 0;
  279.                 while (i < LocalsList.size()) {
  280.                     if (LocalsList.get(i).getName().equalsIgnoreCase(x)) { //Search by name (unique) and returns it
  281.                         return LocalsList.get(i); //Returns Local
  282.                     }
  283.                     i++;
  284.                 }
  285.                 System.out.printf("Ups, Its seems like that Local doesn't exist!\nTry again!\n-->");
  286.             }
  287.         }
  288.     }
  289.  
  290.     //Files Methods
  291.     private void copyFromObjectFile() {
  292.         try {
  293.             ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("Object.dat")));
  294.             ArrayList<People> temp_list_people = (ArrayList<People>) in.readObject();
  295.  
  296.             PeopleList = temp_list_people;
  297.  
  298.             ArrayList<Locals> temp_list_locals = (ArrayList<Locals>) in.readObject();
  299.  
  300.             LocalsList = temp_list_locals;
  301.  
  302.             ArrayList<People> temp_list_enrolled = (ArrayList<People>) in.readObject();
  303.  
  304.             Enrolled_Event = temp_list_enrolled;
  305.  
  306.  
  307.             in.close();
  308.         } catch (FileNotFoundException not_found) {
  309.             System.out.printf("Error -> People.dat Not found!\n");
  310.             copyPeopleFromFile();
  311.             copyLocalsFromFile();
  312.         } catch (IOException e) {
  313.             System.out.printf("Error ->IOEx->CopyObjectFromFile\n");
  314.         } catch (ClassNotFoundException e) {
  315.             System.out.printf("Class not Found");
  316.         }
  317.     }
  318.  
  319.     private void copyToObjectFile() {
  320.         try {
  321.             ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("Object.dat")));
  322.             out.writeObject(PeopleList);
  323.             out.writeObject(LocalsList);
  324.             out.writeObject(Enrolled_Event);
  325.  
  326.             out.close();
  327.         } catch (FileNotFoundException not_Found) {
  328.             System.out.printf("Error -> People.dat not found\n");
  329.         } catch (IOException e) {
  330.             System.out.printf("Error -> IOex->CopyPeopleToFile\n");
  331.         }
  332.     }
  333.  
  334.     private void copyPeopleToFile() {
  335.         try {
  336.             BufferedWriter out = new BufferedWriter(new FileWriter(new File("People.txt")));
  337.             System.out.printf("-->PeopleFile Opened Successfully!\n");
  338.  
  339.  
  340.             int i, j;
  341.             for (i = 0; i < PeopleList.size(); i++) {
  342.                 People p = PeopleList.get(i);
  343.                 String name = p.getName();
  344.                 String user = p.getUsername();
  345.                 String pass = p.getPassword();
  346.                 String profile = p.getProfile();
  347.                 String l = name + " " + user + " " + pass + " " + profile + " ";
  348.  
  349.  
  350.                 if (p.getCode_type() == 1) {
  351.                     Students s = (Students) p;
  352.                     String prog = s.getProgram();
  353.                     l += "1" + " " + prog + " ";//Concatenates strings
  354.                 } else if (p.getCode_type() == 2) {
  355.                     Teachers t = (Teachers) p;
  356.                     String type = t.getType();
  357.                     l += "2" + " " + type + " ";
  358.                 } else if (p.getCode_type() == 3) {
  359.                     Officials o = (Officials) p;
  360.                     String worktime = o.getWorktime();
  361.                     l += "3" + " " + worktime + " ";
  362.                 }
  363.  
  364.  
  365.                 out.write(l + "\n");// Writes on File the Following String
  366.                 /* name keyword profile [program/type/worktime]
  367.                 [program/type/worktime] --> depends on type of people
  368.                 */
  369.             }
  370.             out.close(); //closes File
  371.         } catch (FileNotFoundException not_Found) {
  372.             System.out.printf("ERROR\n -->People File Not Found");
  373.         } catch (IOException e) {
  374.             System.out.printf("IOE->ERROR->CopyPeopleToFile\n"); // to know where error occurred
  375.         }
  376.     }
  377.  
  378.     private void copyPeopleFromFile() {
  379.         try {
  380.             BufferedReader in = new BufferedReader(new FileReader(new File("People.txt")));
  381.             String line;
  382.             while ((line = in.readLine()) != null) {
  383.                 String[] parts = line.split(" "); // divides String into several SubStrings
  384.                 /*First 3 and 5 are always the same!*/
  385.                 String name = parts[0];
  386.                 String kw = parts[1];
  387.                 String pass = parts[2];
  388.                 String profile = parts[3];
  389.                 int code = Integer.parseInt(parts[4]);
  390.  
  391.                 if (code == 1) { //Student
  392.                     String program = parts[5];
  393.                     Students s = new Students(name, kw, pass, profile, program);
  394.                     PeopleListadd(s); // Adds person to list
  395.  
  396.                 } else if (code == 2) { //Teacher
  397.                     String type = parts[5];
  398.                     Teachers t = new Teachers(name, kw, pass, profile, type);
  399.                     PeopleListadd(t);
  400.  
  401.                 }
  402.                 if (code == 3) { //Official
  403.                     String worktime = parts[5];
  404.                     Officials o = new Officials(name, kw, pass, profile, worktime);
  405.                     PeopleListadd(o);
  406.                 }
  407.             }
  408.             in.close();
  409.         } catch (IOException e) {
  410.             System.out.printf("IOE -> ERROR -> copyPeopleFromFile");
  411.         }
  412.     }
  413.  
  414.     private void copyLocalsToFile() {
  415.         try {
  416.             BufferedWriter out = new BufferedWriter(new FileWriter(new File("People.txt")));
  417.             System.out.printf("-->PeopleFile Opened Successfully!\n");
  418.  
  419.  
  420.             int i, j;
  421.             for (i = 0; i < LocalsList.size(); i++) {
  422.                 Locals temp = LocalsList.get(i);
  423.                 String name = temp.getName();
  424.                 double lat = temp.getLatitude();
  425.                 double longe = temp.getLongitude();
  426.                 int code = temp.getCode_type();
  427.                 String line = name + " " + Double.toString(lat) + " " + Double.toString(longe) + " " + Integer.toString(code) + " ";
  428.  
  429.                 if (code == 1) {
  430.                     Exhibitions ex = (Exhibitions) temp;
  431.                     String art = ex.getArtform();
  432.                     line += art + " ";//Concatenates strings
  433.                 } else if (code == 2) {
  434.                     Gardens ga = (Gardens) temp;
  435.                     String area = ga.getArea();
  436.                     line += area + " ";
  437.                 } else if (code == 3) {
  438.                     SportArea sp = (SportArea) temp;
  439.                     String sport = sp.getSports();
  440.                     line += sport + " ";
  441.                 } else if (code == 4) {
  442.                     Bars ba = (Bars) temp;
  443.                     int max = ba.getMaxppl();
  444.                     double min = ba.getMinconsume();
  445.                     double percentage = ba.getPercentage();
  446.                     line += Integer.toString(max) + " " + Double.toString(min) + " " + percentage + " ";
  447.                 }
  448.                 out.write(line + "\n");
  449.             }
  450.             out.close();
  451.         } catch (FileNotFoundException not_found) {
  452.             System.out.printf("Error -> Locals.txt Not found");
  453.         } catch (IOException e) {
  454.             System.out.printf("Error -> IOEx -> copyLocalsToFile");
  455.         }
  456.     }
  457.  
  458.     private void copyLocalsFromFile() {
  459.  
  460.         try {
  461.             BufferedReader in = new BufferedReader(new FileReader(new File("Locals.txt")));
  462.             String line;
  463.             while ((line = in.readLine()) != null) {
  464.                 String[] parts = line.split(" "); // divides String into several SubStrings
  465.                 /*First 4 are always the same!*/
  466.                 String name = parts[0];
  467.                 double latitude = Double.parseDouble(parts[1]);
  468.                 double longitude = Double.parseDouble(parts[2]);
  469.                 int code = Integer.parseInt(parts[3]);
  470.  
  471.                 if (code == 1) { // Exhibitions
  472.                     String art = parts[4];
  473.                     double entrance_fee = Double.parseDouble(parts[5]);
  474.  
  475.                     Exhibitions temp = new Exhibitions(name, latitude, longitude, art, entrance_fee);
  476.                     LocalListadd(temp);
  477.                 }
  478.  
  479.                 if (code == 2) { //Gardens
  480.                     String area = parts[4];
  481.  
  482.                     Gardens temp = new Gardens(name, latitude, longitude, area);
  483.                     LocalListadd(temp);
  484.                 }
  485.  
  486.                 if (code == 3) { // SportsArea
  487.                     String sport = parts[4];
  488.  
  489.                     SportArea temp = new SportArea(name, latitude, longitude, sport);
  490.                     LocalListadd(temp);
  491.                 }
  492.  
  493.                 if (code == 4) { // Exhibitions
  494.                     int maxpeople = Integer.parseInt(parts[4]);
  495.                     double min_consume = Double.parseDouble(parts[5]);
  496.                     double percentage = Double.parseDouble(parts[6]);
  497.  
  498.                     Bars temp = new Bars(name, latitude, longitude, maxpeople, min_consume, percentage);
  499.                     LocalListadd(temp);
  500.                 }
  501.             }
  502.  
  503.             in.close();
  504.         } catch (FileNotFoundException not_found) {
  505.             System.out.printf("Error -> Locals.txt Not Found!\n");
  506.         } catch (IOException e) {
  507.             System.out.printf("Error -> IOEx -> copyLocalsFromFile\n");
  508.         }
  509.     }
  510.  
  511.  
  512.     //Prints Methods
  513.     private void prints2() {
  514.         int i = 0;
  515.         while (i < Enrolled_Event.size()) {
  516.             System.out.printf("\n[%s]\n", Enrolled_Event.get(i).getName());
  517.             i++;
  518.         }
  519.     } // Temporary
  520.  
  521.     private void prints() {
  522.         int i = 0;
  523.         while (i < PeopleList.size()) {
  524.             System.out.printf("\n[%s]\n", PeopleList.get(i).getName());
  525.             i++;
  526.         }
  527.     } // Temporary
  528.  
  529.     private void printlocals() {
  530.         int i = 0, j = 1;
  531.         while (i < LocalsList.size()) {
  532.             System.out.printf("\n%d--> [%s*\n", j, LocalsList.get(i).toString());
  533.             i++;
  534.             j++;
  535.         }
  536.     }
  537.  
  538.     //Change this function
  539.     private void print_locals_votes() {
  540.         ArrayList<Locals> temp = LocalsList;
  541.  
  542.         if (temp.size() == 0) {
  543.             System.out.println("No votes yet");
  544.             return;
  545.         }
  546.  
  547.         Locals x = LocalsList.get(0);
  548.  
  549.         temp.sort(Comparator.comparing(Locals::getAmount_votes));
  550.  
  551.         System.out.println(temp);
  552.     }
  553.  
  554.     private void print_locals_choosen(People person) {
  555.         System.out.printf("/Locals Choosen/\n");
  556.  
  557.         for (int i = 0; i < 5; i++) {
  558.             if (person.getLocals_votes().get(i) == null) System.out.printf("%d --> [Empty]\n", i);
  559.             else
  560.                 System.out.printf("%d --> [%s]\n", i, person.getLocals_votes().get(i).getName());
  561.         }
  562.  
  563.  
  564.     }
  565.  
  566.     //List adds Methods
  567.     private void PeopleListadd(People x) {
  568.         PeopleList.add(x);
  569.     }
  570.  
  571.     private void Enrolled_eventadd(People x) {
  572.         Enrolled_Event.add(x);
  573.     }
  574.  
  575.     private void LocalListadd(Locals x) {
  576.         LocalsList.add(x);
  577.     }
  578.  
  579. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement