Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.Scanner;
- public class Gui {
- /**
- * The Main class is where we load all the files with the persons and the locals on a object file.
- * If the object file does not exist we load the persons from a .txt file and the locals from other .txt file.
- */
- private ArrayList<People> PeopleList = new ArrayList<>();
- private ArrayList<People> Enrolled_Event = new ArrayList<>();
- private ArrayList<Locals> LocalsList = new ArrayList<>();
- public static void main(String[] args) {
- new Gui();
- }
- Gui() {
- copyFromObjectFile();
- First_Menu first_menu = new First_Menu();
- }
- class First_Menu extends JFrame {
- private javax.swing.JLabel username;
- private javax.swing.JLabel password;
- private javax.swing.JButton createUser;
- private javax.swing.JButton login;
- private javax.swing.JTextField username_text;
- private javax.swing.JTextField password_text;
- private String user, pass;
- private Register_menu register_menu;
- private First_Menu first_menu;
- public First_Menu() {
- this.setPreferredSize(new Dimension(350, 250));
- this.setTitle("Menu");
- this.setLocation(100, 100);
- this.setVisible(true);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setLayout(new FlowLayout());
- username = new JLabel("Username:");
- this.add(username);
- username_text = new JTextField();
- this.add(username_text);
- username_text.setPreferredSize(new Dimension(60, 20));
- password = new JLabel("Password:");
- this.add(password);
- password_text = new JTextField();
- this.add(password_text);
- password_text.setPreferredSize(new Dimension(60, 20));
- login = new JButton("Login");
- login.addActionListener(actionEvent -> {
- });
- this.add(login);
- createUser = new JButton("Create User");
- createUser.addActionListener(actionEvent -> {
- this.setVisible(false);
- register_menu = new Register_menu();
- register_menu.setSize(300,250);
- username_text.setText("");
- password_text.setText("");
- register_menu.setVisible(true);
- });
- this.add(createUser);
- this.pack();
- }
- }
- class Register_menu extends JFrame {
- private javax.swing.JLabel username;
- private javax.swing.JLabel password;
- private javax.swing.JTextField username_text;
- private javax.swing.JTextField password_text;
- private javax.swing.JButton regist;
- public Register_menu() {
- this.setPreferredSize(new Dimension(350, 250));
- this.setTitle("Register");
- this.setLocation(100, 100);
- this.setVisible(false);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setLayout(new FlowLayout());
- username = new JLabel("Username: ");
- this.add(username);
- username_text = new JTextField();
- this.add(username_text);
- username_text.setPreferredSize((new Dimension(60, 20)));
- password = new JLabel("Password:");
- this.add(password);
- password_text = new JTextField();
- this.add(password_text);
- password_text.setPreferredSize(new Dimension(60, 20));
- regist = new JButton("Register");
- this.add(regist);
- regist.addActionListener(e -> {
- People temp;
- if ((temp = findPersonUser(username_text.getText())) != null) {
- temp.setPassword(password_text.getText());
- JOptionPane.showMessageDialog(new Frame(),"Password Created/Updated");
- this.setVisible(false);
- first_menu = new First_Menu();
- first_menu.setVisible(true);
- } else {
- JOptionPane.showMessageDialog(new Frame(), "Username doesnt exist");
- }
- });
- }
- }
- /**
- * Check if a person is on a event.
- *
- * @param person Person to check if is on event.
- * @return True is it is, ou False if not.
- */
- private boolean check_if_Enrolled(People person) {
- int i = 0;
- while (i < Enrolled_Event.size()) {
- if (person == Enrolled_Event.get(i)) {
- return true;
- }
- i++;
- }
- return false;
- } // Checks if person is on the enrolled List
- private People findperson(String Username, String password) {
- int i = 0;
- while (i < PeopleList.size()) {
- if (PeopleList.get(i).getUsername().equalsIgnoreCase(Username) && PeopleList.get(i).getPassword().equalsIgnoreCase(password)) {
- return PeopleList.get(i);
- }
- i++;
- }
- return null;
- } //Finds a person based on keyword for Login
- private People findPersonUser(String user) {
- for(int i = 0; i<PeopleList.size(); i++) {
- if(PeopleList.get(i).getUsername().equalsIgnoreCase(user)) {
- return PeopleList.get(i);
- }
- }
- return null;
- }
- private boolean Usernamecheck(String Username) {
- int i = 0;
- while (i < PeopleList.size()) {
- if (PeopleList.get(i).getUsername().equals(Username)) {
- return true;
- }
- i++;
- }
- return false;
- }
- private void Chosing_locals(People person) { // alterar para nao se inscrever duas vezes no mesmo local
- System.out.printf("/Locals Choosen/\n");
- System.out.println(person.getLocals_votes());
- Scanner sc = new Scanner(System.in);
- ArrayList<Locals> temp = person.getLocals_votes();
- while (true) {
- System.out.printf("Which Local would u like to Change? [1-5|6->exit]\n");
- String x = sc.nextLine();
- switch (x) {
- case "1":
- Locals local = Search_Locals();
- if (temp.get(0) != null) {
- Locals old_local = temp.get(0);
- old_local.Change_votes(0);
- }
- temp.set(0, local);
- local.Change_votes(1);
- break;
- case "2":
- local = Search_Locals();
- if (temp.get(1) != null) {
- Locals old_local = temp.get(1);
- old_local.Change_votes(0);
- }
- temp.set(1, local);
- local.Change_votes(1);
- break;
- case "3":
- local = Search_Locals();
- if (temp.get(2) != null) {
- Locals old_local = temp.get(2);
- old_local.Change_votes(0);
- }
- temp.set(2, local);
- local.Change_votes(1);
- break;
- case "4":
- local = Search_Locals();
- if (temp.get(3) != null) {
- Locals old_local = temp.get(3);
- old_local.Change_votes(0);
- }
- temp.set(3, local);
- local.Change_votes(1);
- break;
- case "5":
- local = Search_Locals();
- if (temp.get(4) != null) {
- Locals old_local = temp.get(4);
- old_local.Change_votes(0);
- }
- temp.set(4, local);
- local.Change_votes(1);
- break;
- case "6":
- break;
- default:
- System.out.printf("Error -> Invalid Input\n");
- }
- System.out.printf("Wanna change another Local?[y/n]\n");
- String y = sc.nextLine();
- if (y.equalsIgnoreCase("y")) continue;
- else {
- System.out.printf("New votes --> \n");
- print_locals_choosen(person);
- break;
- }
- }
- }
- private Locals Search_Locals() {
- // asks which local the user would like to add and searches it in the list returning it when found
- printlocals();
- System.out.printf("-->Chose a Local!\n");
- Scanner sc = new Scanner(System.in);
- while (true) {
- String x = sc.nextLine();
- if (x.matches(".*\\d.*")) System.out.printf("\nError -> Invalid Input\n");
- else {
- int i = 0;
- while (i < LocalsList.size()) {
- if (LocalsList.get(i).getName().equalsIgnoreCase(x)) { //Search by name (unique) and returns it
- return LocalsList.get(i); //Returns Local
- }
- i++;
- }
- System.out.printf("Ups, Its seems like that Local doesn't exist!\nTry again!\n-->");
- }
- }
- }
- //Files Methods
- private void copyFromObjectFile() {
- try {
- ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("Object.dat")));
- ArrayList<People> temp_list_people = (ArrayList<People>) in.readObject();
- PeopleList = temp_list_people;
- ArrayList<Locals> temp_list_locals = (ArrayList<Locals>) in.readObject();
- LocalsList = temp_list_locals;
- ArrayList<People> temp_list_enrolled = (ArrayList<People>) in.readObject();
- Enrolled_Event = temp_list_enrolled;
- in.close();
- } catch (FileNotFoundException not_found) {
- System.out.printf("Error -> People.dat Not found!\n");
- copyPeopleFromFile();
- copyLocalsFromFile();
- } catch (IOException e) {
- System.out.printf("Error ->IOEx->CopyObjectFromFile\n");
- } catch (ClassNotFoundException e) {
- System.out.printf("Class not Found");
- }
- }
- private void copyToObjectFile() {
- try {
- ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("Object.dat")));
- out.writeObject(PeopleList);
- out.writeObject(LocalsList);
- out.writeObject(Enrolled_Event);
- out.close();
- } catch (FileNotFoundException not_Found) {
- System.out.printf("Error -> People.dat not found\n");
- } catch (IOException e) {
- System.out.printf("Error -> IOex->CopyPeopleToFile\n");
- }
- }
- private void copyPeopleToFile() {
- try {
- BufferedWriter out = new BufferedWriter(new FileWriter(new File("People.txt")));
- System.out.printf("-->PeopleFile Opened Successfully!\n");
- int i, j;
- for (i = 0; i < PeopleList.size(); i++) {
- People p = PeopleList.get(i);
- String name = p.getName();
- String user = p.getUsername();
- String pass = p.getPassword();
- String profile = p.getProfile();
- String l = name + " " + user + " " + pass + " " + profile + " ";
- if (p.getCode_type() == 1) {
- Students s = (Students) p;
- String prog = s.getProgram();
- l += "1" + " " + prog + " ";//Concatenates strings
- } else if (p.getCode_type() == 2) {
- Teachers t = (Teachers) p;
- String type = t.getType();
- l += "2" + " " + type + " ";
- } else if (p.getCode_type() == 3) {
- Officials o = (Officials) p;
- String worktime = o.getWorktime();
- l += "3" + " " + worktime + " ";
- }
- out.write(l + "\n");// Writes on File the Following String
- /* name keyword profile [program/type/worktime]
- [program/type/worktime] --> depends on type of people
- */
- }
- out.close(); //closes File
- } catch (FileNotFoundException not_Found) {
- System.out.printf("ERROR\n -->People File Not Found");
- } catch (IOException e) {
- System.out.printf("IOE->ERROR->CopyPeopleToFile\n"); // to know where error occurred
- }
- }
- private void copyPeopleFromFile() {
- try {
- BufferedReader in = new BufferedReader(new FileReader(new File("People.txt")));
- String line;
- while ((line = in.readLine()) != null) {
- String[] parts = line.split(" "); // divides String into several SubStrings
- /*First 3 and 5 are always the same!*/
- String name = parts[0];
- String kw = parts[1];
- String pass = parts[2];
- String profile = parts[3];
- int code = Integer.parseInt(parts[4]);
- if (code == 1) { //Student
- String program = parts[5];
- Students s = new Students(name, kw, pass, profile, program);
- PeopleListadd(s); // Adds person to list
- } else if (code == 2) { //Teacher
- String type = parts[5];
- Teachers t = new Teachers(name, kw, pass, profile, type);
- PeopleListadd(t);
- }
- if (code == 3) { //Official
- String worktime = parts[5];
- Officials o = new Officials(name, kw, pass, profile, worktime);
- PeopleListadd(o);
- }
- }
- in.close();
- } catch (IOException e) {
- System.out.printf("IOE -> ERROR -> copyPeopleFromFile");
- }
- }
- private void copyLocalsToFile() {
- try {
- BufferedWriter out = new BufferedWriter(new FileWriter(new File("People.txt")));
- System.out.printf("-->PeopleFile Opened Successfully!\n");
- int i, j;
- for (i = 0; i < LocalsList.size(); i++) {
- Locals temp = LocalsList.get(i);
- String name = temp.getName();
- double lat = temp.getLatitude();
- double longe = temp.getLongitude();
- int code = temp.getCode_type();
- String line = name + " " + Double.toString(lat) + " " + Double.toString(longe) + " " + Integer.toString(code) + " ";
- if (code == 1) {
- Exhibitions ex = (Exhibitions) temp;
- String art = ex.getArtform();
- line += art + " ";//Concatenates strings
- } else if (code == 2) {
- Gardens ga = (Gardens) temp;
- String area = ga.getArea();
- line += area + " ";
- } else if (code == 3) {
- SportArea sp = (SportArea) temp;
- String sport = sp.getSports();
- line += sport + " ";
- } else if (code == 4) {
- Bars ba = (Bars) temp;
- int max = ba.getMaxppl();
- double min = ba.getMinconsume();
- double percentage = ba.getPercentage();
- line += Integer.toString(max) + " " + Double.toString(min) + " " + percentage + " ";
- }
- out.write(line + "\n");
- }
- out.close();
- } catch (FileNotFoundException not_found) {
- System.out.printf("Error -> Locals.txt Not found");
- } catch (IOException e) {
- System.out.printf("Error -> IOEx -> copyLocalsToFile");
- }
- }
- private void copyLocalsFromFile() {
- try {
- BufferedReader in = new BufferedReader(new FileReader(new File("Locals.txt")));
- String line;
- while ((line = in.readLine()) != null) {
- String[] parts = line.split(" "); // divides String into several SubStrings
- /*First 4 are always the same!*/
- String name = parts[0];
- double latitude = Double.parseDouble(parts[1]);
- double longitude = Double.parseDouble(parts[2]);
- int code = Integer.parseInt(parts[3]);
- if (code == 1) { // Exhibitions
- String art = parts[4];
- double entrance_fee = Double.parseDouble(parts[5]);
- Exhibitions temp = new Exhibitions(name, latitude, longitude, art, entrance_fee);
- LocalListadd(temp);
- }
- if (code == 2) { //Gardens
- String area = parts[4];
- Gardens temp = new Gardens(name, latitude, longitude, area);
- LocalListadd(temp);
- }
- if (code == 3) { // SportsArea
- String sport = parts[4];
- SportArea temp = new SportArea(name, latitude, longitude, sport);
- LocalListadd(temp);
- }
- if (code == 4) { // Exhibitions
- int maxpeople = Integer.parseInt(parts[4]);
- double min_consume = Double.parseDouble(parts[5]);
- double percentage = Double.parseDouble(parts[6]);
- Bars temp = new Bars(name, latitude, longitude, maxpeople, min_consume, percentage);
- LocalListadd(temp);
- }
- }
- in.close();
- } catch (FileNotFoundException not_found) {
- System.out.printf("Error -> Locals.txt Not Found!\n");
- } catch (IOException e) {
- System.out.printf("Error -> IOEx -> copyLocalsFromFile\n");
- }
- }
- //Prints Methods
- private void prints2() {
- int i = 0;
- while (i < Enrolled_Event.size()) {
- System.out.printf("\n[%s]\n", Enrolled_Event.get(i).getName());
- i++;
- }
- } // Temporary
- private void prints() {
- int i = 0;
- while (i < PeopleList.size()) {
- System.out.printf("\n[%s]\n", PeopleList.get(i).getName());
- i++;
- }
- } // Temporary
- private void printlocals() {
- int i = 0, j = 1;
- while (i < LocalsList.size()) {
- System.out.printf("\n%d--> [%s*\n", j, LocalsList.get(i).toString());
- i++;
- j++;
- }
- }
- //Change this function
- private void print_locals_votes() {
- ArrayList<Locals> temp = LocalsList;
- if (temp.size() == 0) {
- System.out.println("No votes yet");
- return;
- }
- Locals x = LocalsList.get(0);
- temp.sort(Comparator.comparing(Locals::getAmount_votes));
- System.out.println(temp);
- }
- private void print_locals_choosen(People person) {
- System.out.printf("/Locals Choosen/\n");
- for (int i = 0; i < 5; i++) {
- if (person.getLocals_votes().get(i) == null) System.out.printf("%d --> [Empty]\n", i);
- else
- System.out.printf("%d --> [%s]\n", i, person.getLocals_votes().get(i).getName());
- }
- }
- //List adds Methods
- private void PeopleListadd(People x) {
- PeopleList.add(x);
- }
- private void Enrolled_eventadd(People x) {
- Enrolled_Event.add(x);
- }
- private void LocalListadd(Locals x) {
- LocalsList.add(x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement