Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 56.64 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package projeto_rascunho;
  7.  
  8. import java.awt.BorderLayout;
  9. import java.util.Random;
  10. import java.util.Scanner;
  11. import java.lang.Math;
  12. import java.util.StringTokenizer;
  13. import java.util.List;
  14. import java.util.ListIterator;
  15. import java.util.ArrayList;
  16. import java.io.*;
  17. import java.lang.Math;
  18. import java.util.StringTokenizer;
  19. //Para a interface
  20. import java.awt.Dimension;
  21. import java.awt.FlowLayout;
  22. import java.awt.GridLayout;
  23. import java.awt.event.ActionEvent;
  24. import java.awt.event.ActionListener;
  25. import java.awt.event.WindowEvent;
  26. import java.util.Arrays;
  27. import javax.swing.JFrame;
  28. import javax.swing.JPasswordField;//Esconder password
  29. import javax.swing.JFileChooser; //Escolher ficheiro
  30. import javax.swing.*;
  31.  
  32. /**Classe principal do programa
  33.  * @author João Perdigão e Rodrigo Oliveira
  34.  * @version 1.0
  35.  * @since Release 17/12/2017
  36.  */
  37.  
  38.  
  39. /*----------------------------CONVIVIO NO DEI----------------------------------------*/
  40. class Convivio {    
  41. /**
  42.  * classe de criação do convivio
  43.  */
  44.     public String nome_convivio;
  45.     //public List<Locais> locais = new ArrayList<>();
  46.     //public List<Elemento_dei> registados = new ArrayList<>();
  47.     public Convivio(String nome_convivio){
  48.         this.nome_convivio = nome_convivio;
  49.     }
  50.    
  51.      /**
  52.      * Por enquanto ainda nao serve para nada
  53.      *
  54.      */
  55.     public void login(){
  56.        
  57.     } //Pedir username e password
  58.     public void inscricao_convivio(String nome, String username,String password,String perfil,int idade,List<Locais> locais,String tipo){
  59.    
  60.         List<Inscrito> inscritos = new ArrayList<>();
  61.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  62.             inscritos = (ArrayList) oin.readObject();
  63.         }catch(IOException | ClassNotFoundException e){
  64.             System.out.println(e.getMessage());
  65.         }
  66.        
  67.         Inscrito x = new Inscrito(perfil, nome, idade,username,password,locais,tipo);
  68.         inscritos.add(x);
  69.        
  70.         Ficheiros_Elementos_dei f = new Ficheiros_Elementos_dei();
  71.         f.escreve_ficheiro_dat_inscritos(inscritos);
  72.    
  73.     } //Tem que pedir sempre o nome para verificar se pertence ao DEI  //Verificar sempre se ha username igual
  74.     public void show_locais(){ //Mostrar sempre o numero atual de inscritos de cada um e a lotacao (se aplicavel)
  75.         List<Locais> locais = new ArrayList<>();
  76.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Locais_obj.txt"))){
  77.             locais = (ArrayList) oin.readObject();
  78.         }catch(IOException | ClassNotFoundException e){
  79.             System.out.println(e.getMessage());
  80.         }
  81.         for (Locais l : locais){
  82.             l.print();
  83.         }
  84.     }
  85.     public void escolha_locais(){
  86.    
  87.     } //Na interface so ter 5 espacos //Ter um ficheiro objetos dos inscritos em cada local (secalhar)
  88.     public int add_guest(Inscrito i,Bares b){
  89.  
  90.         int maximo_guest = (int)(b.lotacao * 0.1);
  91.         int n = 0;
  92.         String nome = null;
  93.        
  94.         if (b.guest.size() == maximo_guest){
  95.             if (i.perfil.indexOf("Boemio") >= 0){
  96.                 for (Inscrito ins : b.guest){
  97.                     if (ins.perfil.indexOf("Boemio") < 0){ //ver o nome do ultimo a entrar que nao seja "boemio" e se há
  98.                        nome = ins.nome;
  99.                     }
  100.                     else{ //Nao pode ser inscrito
  101.                         n = 0;
  102.                     }
  103.                 }
  104.                 for (Inscrito ins : b.guest){
  105.                     if (nome.indexOf(ins.nome) >= 0){ //verifcar se nome != null para podeser realizar a troca
  106.                         b.guest.remove(ins); //Remove o ultimo inscrito que nao e "boemio"
  107.                         b.guest.add(i); //Adiciona o inscrito que e "boemio"
  108.                         n = 1;
  109.                     }
  110.                     else{ //Nao pode ser inscrito
  111.                         n = 0;
  112.                     }
  113.                 }
  114.             }
  115.             else{ //Nao pode ser inscrito
  116.                 n = 0;  
  117.             }
  118.         }
  119.         else{ //Pode ser inscrito
  120.             b.guest.add(i);
  121.             n = 1;
  122.         }
  123.        
  124.         return n;
  125.     }
  126.     public int conta_inscritos(String nome_local){
  127.         int num_inscritos = 0;
  128.        
  129.         List<Inscrito> inscritos = new ArrayList<>();
  130.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  131.             inscritos = (ArrayList) oin.readObject();
  132.         }catch(IOException | ClassNotFoundException e){
  133.             System.out.println(e.getMessage());
  134.         }
  135.        
  136.         for (Inscrito i : inscritos){
  137.             for (Locais l : i.locais){
  138.                 if(l.nome.indexOf(nome_local) >= 0){
  139.                     num_inscritos += 1;
  140.                 }
  141.             }
  142.         }
  143.         return num_inscritos;
  144.     }
  145. }
  146.  
  147.  
  148. /*----------------------------LOCAIS CONVIVIO-----------------------------------------*/
  149. class Locais implements Serializable{
  150.     private String coord_gps;
  151.     public String nome;
  152.     public String tipo_local;
  153.    
  154.     public Locais(String coord_gps,String nome,String tipo_local){
  155.         this.coord_gps = coord_gps;
  156.         this.nome = nome;
  157.         this.tipo_local = tipo_local;
  158.     }
  159.     public int receita_prevista(Convivio c){
  160.         return 0;
  161.     }
  162.     public void print(){
  163.         System.out.printf("%s\n",nome);    
  164.     }
  165.     public int receita_minima_prevista_total(Convivio c){
  166.         int receita = 0;
  167.        
  168.         List<Locais> locais = new ArrayList<>();
  169.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Locais_obj.txt"))){
  170.             locais = (ArrayList) oin.readObject();
  171.         }catch(IOException | ClassNotFoundException e){
  172.             System.out.println(e.getMessage());
  173.         }
  174.        
  175.         for (Locais l : locais){
  176.             receita += l.receita_prevista(c);
  177.         }
  178.         return receita;
  179.     }
  180. }
  181.  
  182. abstract class Parques extends Locais{
  183.  
  184.     public Parques(String coord_gps, String nome,String tipo_local) {
  185.         super(coord_gps, nome,tipo_local);
  186.     }
  187. }
  188.  
  189. class Jardins extends Parques{
  190.     private int area; //em metros quadrados
  191.  
  192.     public Jardins(String coord_gps, String nome,int area,String tipo_local) {
  193.         super(coord_gps, nome,tipo_local);
  194.         this.area = area;
  195.     }
  196.     @Override
  197.     public int receita_prevista(Convivio c){
  198.         return 0;
  199.     }
  200.     @Override
  201.      public void print(){
  202.         System.out.printf("%s\n",nome);    
  203.     }
  204. }
  205.  
  206. class Areas_desportivas extends Parques{
  207.     private String desportos[];
  208.  
  209.     public Areas_desportivas(String coord_gps, String nome,String desportos[],String tipo_local) {
  210.         super(coord_gps, nome,tipo_local);
  211.         this.desportos = desportos;
  212.     }
  213.     @Override
  214.     public void print(){
  215.         System.out.printf("%s:\n",nome);
  216.         for (int i = 0; i < desportos.length; i++){
  217.             System.out.printf("  -%s", desportos[i]);
  218.         }
  219.     }
  220.     @Override
  221.     public int receita_prevista(Convivio c){
  222.         return 0;
  223.     }
  224. }
  225.  
  226. class Exposicoes extends Locais{
  227.     public String forma_artistica;
  228.     public int custo_ingresso;
  229.    
  230.     public Exposicoes(String coord_gps,String nome,String forma_artistica, int custo_ingresso,String tipo_local) {
  231.         super(coord_gps, nome,tipo_local);
  232.         this.custo_ingresso = custo_ingresso;
  233.         this.forma_artistica = forma_artistica;
  234.     }
  235.     @Override
  236.     public int receita_prevista(Convivio c){
  237.         int receita = 0;
  238.        
  239.         List<Inscrito> inscritos = new ArrayList<>();
  240.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  241.             inscritos = (ArrayList) oin.readObject();
  242.         }catch(IOException | ClassNotFoundException e){
  243.             System.out.println(e.getMessage());
  244.         }
  245.        
  246.         for (Inscrito i : inscritos){
  247.             for (Locais loc : i.locais){
  248.                 if (nome.indexOf(loc.nome) >= 0 && i.tipo.indexOf("aluno") >= 0){
  249.                     receita += (custo_ingresso * 0.90);
  250.                 }
  251.                 else if(nome.indexOf(loc.nome) >= 0 && i.tipo.indexOf("aluno") < 0){
  252.                     receita += custo_ingresso;
  253.                 }
  254.             }
  255.         }
  256.        
  257.         return receita;
  258.     }  
  259.     @Override
  260.      public void print(){
  261.         System.out.printf("%s\n",nome);    
  262.     }
  263. }
  264.  
  265.  
  266. class Bares extends Locais{
  267.        
  268.     public int lotacao;  
  269.     private int consumo_minimo; //Por pessoa
  270.     public List<Inscrito> guest = new ArrayList<>();
  271.    
  272.     public Bares(String coord_gps,String nome, int lotacao, int consumo_minimo, String tipo_local,List<Inscrito> guest) {
  273.         super(coord_gps, nome,tipo_local);
  274.         this.lotacao = lotacao;
  275.         this.consumo_minimo = consumo_minimo;
  276.         this.guest = guest;
  277.     }  
  278.     @Override
  279.     public int receita_prevista(Convivio c){
  280.  
  281.         int n_inscritos;
  282.         n_inscritos = c.conta_inscritos(nome);
  283.        
  284.         return consumo_minimo * n_inscritos;
  285.     }
  286.     @Override
  287.     public void print(){
  288.         System.out.printf("%s com lotacao:%d\n",nome,lotacao);    
  289.     }
  290. }
  291.  
  292. /*----------------------------ELEMENTOS DEI-----------------------------------------*/
  293. class Elemento_dei implements Serializable{
  294.     public String perfil;
  295.     public String nome;
  296.     public int idade;
  297.     public String tipo;
  298.    
  299.     public Elemento_dei(String perfil, String nome,int idade,String tipo){
  300.         this.perfil = perfil;
  301.         this.nome = nome;
  302.         this.idade = idade;
  303.         this.tipo = tipo;
  304.     }  
  305.     public void print(){
  306.         System.out.printf("%s\t%d\n", nome,idade);
  307.     }  
  308. }
  309.  
  310. class Inscrito extends Elemento_dei{
  311.     public String username;
  312.     public String password;
  313.     public List<Locais> locais = new ArrayList<>(); //Maximo de 5 locais
  314.  
  315.     public Inscrito(String perfil, String nome, int idade, String username,String password,List<Locais> locais,String tipo) {
  316.         super(perfil, nome, idade,tipo);
  317.         this.username = username;
  318.         this.password = password;
  319.         this.locais = locais;
  320.     }
  321.     @Override
  322.     public void print(){
  323.         System.out.printf("%s\t%s\t%s\t%s\t%s\n", nome,perfil,idade,username,password);
  324.     }
  325. }
  326.  
  327. class Estudante extends Elemento_dei{
  328.     private String curso;
  329.    
  330.     public Estudante(String perfil, String nome,int idade,String curso,String tipo) {
  331.         super(perfil, nome, idade,tipo);
  332.         this.curso = curso;
  333.         this.tipo = tipo;
  334.     }
  335.     @Override
  336.     public void print(){
  337.         System.out.printf("%s\t%s\t%s\n", nome,perfil,curso);
  338.     }
  339. }
  340.  
  341. class Professor extends Elemento_dei{
  342.     private String tipo_prof;
  343.  
  344.     public Professor(String perfil, String nome, int idade, String tipo_prof,String tipo) {
  345.         super(perfil, nome, idade,tipo);
  346.         this.tipo_prof = tipo_prof;
  347.     }
  348.     @Override
  349.     public void print(){
  350.         System.out.printf("%s\t%s\n", nome,perfil);
  351.     }
  352. }
  353.  
  354. class Funcionario extends Elemento_dei{
  355.     private String tipo_func;
  356.  
  357.     public Funcionario(String perfil, String nome, int idade, String tipo_func,String tipo) {
  358.         super(perfil, nome, idade,tipo);
  359.         this.tipo_func = tipo_func;
  360.     }
  361.     @Override
  362.     public void print(){
  363.         System.out.printf("%s\t%s\n", nome,perfil);
  364.     }
  365. }
  366.  
  367. /*---------------------------------FICHEIROS-----------------------------------------*/
  368. class Ficheiros_Locais{
  369.    
  370.     public void ler_ficheiro_locais(){
  371.         List<Locais> locais = new ArrayList<>();
  372.         List<Bares> bares = new ArrayList<>();
  373.         try{
  374.             BufferedReader reader = new BufferedReader(new FileReader("Locais.txt"));
  375.             String line;
  376.             while ((line = reader.readLine()) != null){
  377.                 String nome;
  378.                 String coordenadas;
  379.                 String tipo_local;
  380.                 String[] info = line.split("\t");
  381.                 nome = info[0];
  382.                 coordenadas = info[1];
  383.                 tipo_local = info[2];
  384.                 if(tipo_local.indexOf("parque") >= 0){
  385.                     String tipo_parque = info[3];
  386.                     if(tipo_parque.indexOf("jardim") >= 0){
  387.                         int area = Integer.valueOf(info[4].trim());
  388.                         Locais l = new Jardins(coordenadas,nome,area,tipo_parque);
  389.                         locais.add(l);
  390.                     }
  391.                     else if(tipo_parque.indexOf("desportivo") >= 0){
  392.                         String desportos[] = new String[info.length - 4];
  393.                         int n = 0;
  394.                         for (int i = 4; i < info.length ; i++){
  395.                            desportos[n] = info[i];
  396.                            n++;
  397.                         }
  398.                         Locais l = new Areas_desportivas(coordenadas,nome,desportos,tipo_parque);
  399.                         locais.add(l);
  400.                     }
  401.                 }
  402.                 else if(tipo_local.indexOf("bar") >= 0){
  403.                     List<Inscrito> guest = new ArrayList<>();
  404.                     int lotacao = Integer.valueOf(info[3].trim());
  405.                     int consumo_minimo = Integer.valueOf(info[4].trim());
  406.                     Locais l = new Bares(coordenadas,nome,lotacao,consumo_minimo,tipo_local,guest);
  407.                     locais.add(l);
  408.                     Bares b = new Bares(coordenadas,nome,lotacao,consumo_minimo,tipo_local,guest);
  409.                     bares.add(b);
  410.                    
  411.                 }
  412.                 else if(tipo_local.indexOf("exposicao") >= 0){
  413.                     int custo_ingresso = Integer.valueOf(info[3].trim());
  414.                     String forma_artistica = info[4];
  415.                     Locais l = new Exposicoes(coordenadas,nome,forma_artistica,custo_ingresso,tipo_local);
  416.                     locais.add(l);
  417.                 }
  418.        
  419.         }
  420.            
  421.         }catch(IOException e){
  422.             System.out.print("ERRO");
  423.             System.out.printf("Ocorreu a exceçao %s ao ler o ficheiro\n", e);
  424.         }
  425.        
  426.         escreve_ficheiro_dat_locais(locais);
  427.         escreve_ficheiro_dat_locais_bares(bares);
  428.        
  429.     }
  430.     public void escreve_ficheiro_dat_locais(List<Locais> locais){
  431.          try{
  432.             ObjectOutputStream oS = new ObjectOutputStream(new FileOutputStream("Locais_obj.txt"));
  433.             oS.writeObject(locais);
  434.             oS.close();
  435.  
  436.         }catch(IOException e){
  437.             System.out.print("ERRO");
  438.             System.out.printf("Ocorreu a exceçao %s ao escrever no ficheiro\n", e);
  439.         }
  440.     }
  441.     public void escreve_ficheiro_dat_locais_bares(List<Bares> bares){
  442.          try{
  443.             ObjectOutputStream oS = new ObjectOutputStream(new FileOutputStream("Bares_obj.txt"));
  444.             oS.writeObject(bares);
  445.             oS.close();
  446.  
  447.         }catch(IOException e){
  448.             System.out.print("ERRO");
  449.             System.out.printf("Ocorreu a exceçao %s ao escrever no ficheiro\n", e);
  450.         }
  451.     }
  452. }
  453.  
  454. class Ficheiros_Elementos_dei{  
  455.    
  456.     public void ler_ficheiro_Elementos_dei(){
  457.         List<Elemento_dei> elementos_dei = new ArrayList<>(); //Ver se meter isto aqui esta correto
  458.         try{
  459.             BufferedReader reader = new BufferedReader(new FileReader("Elementos_DEI.txt"));
  460.             String line;
  461.             while ((line = reader.readLine()) != null){
  462.                 String nome;
  463.                 String perfil;
  464.                 int idade;
  465.                 String profissao;
  466.                 String[] info = line.split(";");
  467.                 nome = info[0];
  468.                 perfil = info[1];
  469.                 idade = Integer.valueOf(info[2].trim());
  470.                 profissao = info[3];
  471.                 if(profissao.indexOf("aluno") >= 0){
  472.                     String curso = info[4];
  473.                     Elemento_dei e = new Estudante(perfil,nome,idade,curso,profissao);
  474.                     elementos_dei.add(e);
  475.                    
  476.                 }
  477.                 else if(profissao.indexOf("professor") >= 0){
  478.                     String tipo_prof = info[4];
  479.                     Elemento_dei e = new Professor(perfil, nome,idade,tipo_prof,profissao);
  480.                     elementos_dei.add(e);
  481.                    
  482.                 }
  483.                 else if(profissao.indexOf("funcionario") >= 0){
  484.                     String tipo_func = info[4];
  485.                     Elemento_dei e = new Funcionario(perfil,nome,idade,tipo_func,profissao);
  486.                     elementos_dei.add(e);
  487.                    
  488.                 }
  489.                 else{
  490.                     System.out.print("ERRO\n");
  491.                 }
  492.             }
  493.         }
  494.         catch(IOException e){
  495.             System.out.printf("Ocorreu a exceçao %s ao ler o ficheiro\n", e);
  496.         }
  497.        
  498.         escreve_ficheiro_dat_pessoas(elementos_dei);
  499.     }
  500.     public void escreve_ficheiro_dat_pessoas(List<Elemento_dei> pessoas){
  501.          try{
  502.             ObjectOutputStream oS = new ObjectOutputStream(new FileOutputStream("Pessoas_obj.txt"));
  503.             oS.writeObject(pessoas);
  504.             oS.close();
  505.  
  506.         }catch(IOException e){
  507.             System.out.printf("Ocorreu a exceçao %s ao escrever mo ficheiro\n", e);
  508.         }
  509.     } //Ver como fazer para ir atualizando
  510.     public void escreve_ficheiro_dat_inscritos(List<Inscrito> inscritos){
  511.          try{
  512.             ObjectOutputStream oS = new ObjectOutputStream(new FileOutputStream("Inscritos_obj.txt"));
  513.             oS.writeObject(inscritos);
  514.             oS.close();
  515.  
  516.         }catch(IOException e){
  517.             System.out.printf("Ocorreu a exceçao %s ao escrever mo ficheiro\n", e);
  518.         }
  519.     }
  520. }
  521.  
  522. /*----------------------------------------------------INTERFACE-------------------------------------------------------*/
  523.  
  524.  
  525. ////////////////////////////////////////////MENU////////////////////////////////////////////
  526.  
  527. class MenuInicial extends JFrame{ //O que por no registar? (opcoes)
  528.    
  529.     private final JLabel welcome; //Texto a dar as boas vindas
  530.     private final JButton botLogin;
  531.     private final JButton botRegistar;
  532.     private final JButton botSair;
  533.    
  534.     public MenuInicial(Convivio c){
  535.         this.setPreferredSize (new Dimension(450,300));
  536.         this.setTitle("Menu Inicial");
  537.         this.setLocation (600,300); //Mais ou menos centrado
  538.         this.setVisible(true);
  539.         this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  540.         this.setLayout (new GridLayout (4,2,20,20));
  541.    
  542.         welcome = new JLabel ("                                          Bem vindo ao Mega Convivio do DEI");
  543.         this.add (welcome);
  544.        
  545.         botLogin = new JButton("Login");
  546.         this.add(botLogin);
  547.         botLogin.addActionListener(new ActionListener() {
  548.         @Override
  549.         public void actionPerformed(ActionEvent e) {
  550.             Login Interface = new Login(c);
  551.             dispose();
  552.         }
  553.     });
  554.        
  555.         botRegistar = new JButton("Registar");
  556.         this.add(botRegistar);
  557.         botRegistar.addActionListener(new ActionListener() {
  558.         @Override
  559.         public void actionPerformed(ActionEvent e) {
  560.             Registar r = new Registar(c);
  561.             dispose();
  562.         }
  563.     });
  564.        
  565.         botSair = new JButton("Sair");
  566.         this.add(botSair);
  567.         botSair.addActionListener(new ActionListener() {
  568.         @Override
  569.         public void actionPerformed(ActionEvent e) {
  570.             System.exit(0);
  571.         }
  572.     });
  573.        
  574.         this.pack();
  575.     }
  576. }
  577.  
  578. ///////////////////////////////////////////REGISTAR////////////////////////////////////
  579.  
  580. class Registar extends JFrame{
  581.    
  582.     private final JTextField nome;
  583.     private final JTextField username;
  584.     private final JPasswordField password;
  585.     private final JPasswordField confirmapass;
  586.     private final JComboBox showpass;
  587.     private final JLabel label0;
  588.     private final JLabel label1;
  589.     private final JLabel label2;
  590.     private final JLabel label3;
  591.     private final JButton botEntrar; //Para fazer login
  592.     private final JButton botLimpa; //Decidir se limpa tudo ou apenas um dos campos
  593.     private final JButton botSai;
  594.     private final JButton botRetroceder;
  595.     public String nome1;
  596.     public String username1;
  597.     public String password1;
  598.  
  599.     public Registar(Convivio c) {
  600.        
  601.         this.setPreferredSize (new Dimension(600,450));
  602.         this.setTitle("Registar");
  603.         this.setLocation (600,300); //Mais ou menos centrado
  604.         this.setVisible(true);
  605.         this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  606.         //this.setLayout(new FlowLayout());
  607.  
  608.  
  609.         //final JPanel grid = new JPanel(new FlowLayout());    
  610.         this.setLayout (new GridLayout (7,2,15,15));
  611.         //this.add(grid);
  612.  
  613.         label0 = new JLabel ("Nome");
  614.         this.add (label0);
  615.  
  616.        
  617.         nome = new JTextField();
  618.         this.add(nome);
  619.         nome.setPreferredSize (new Dimension(100,20));
  620.         nome.addActionListener (new ActionListener() {
  621.             @Override
  622.             public void actionPerformed(ActionEvent e) {
  623.                 nome1 = nome.getText();
  624.             }
  625.         }); //Mudar o método abstrato
  626.  
  627.         label1 = new JLabel ("Escolher Username");
  628.         this.add (label1);
  629.  
  630.         username = new JTextField();
  631.         this.add(username);
  632.         username.setPreferredSize (new Dimension(100,20));
  633.         username.addActionListener (new ActionListener() {
  634.             @Override
  635.             public void actionPerformed(ActionEvent e) {
  636.                 username1 = username.getText();
  637.             }
  638.         }); //Mudar o método abstrato
  639.  
  640.  
  641.         label2 = new JLabel ("Escolher Password");
  642.         this.add (label2);
  643.  
  644.         password = new JPasswordField(); //cria novo objeto
  645.         this.add(password); //adiciona ao container  
  646.         password.setPreferredSize (new Dimension(100,20));
  647.         password.addActionListener (new ActionListener() {
  648.             @Override
  649.             public void actionPerformed(ActionEvent e) {              
  650.  
  651.                 password.setToolTipText("A password tem que ter pelo menos 5 carateres");
  652.  
  653.             }
  654.         }); //Mudar o método abstrato
  655.  
  656.  
  657.         label3 = new JLabel ("Confirmar Password");
  658.         this.add (label3);
  659.  
  660.         confirmapass = new JPasswordField();
  661.         this.add(confirmapass);
  662.         confirmapass.setPreferredSize (new Dimension(100,20));
  663.         confirmapass.addActionListener (new ActionListener() {
  664.             @Override
  665.             public void actionPerformed(ActionEvent e) {
  666.  
  667.                 if ((password.getPassword()).equals(confirmapass)){
  668.  
  669.                 }
  670.                 else{
  671.                     System.out.println("As passwords tem que ser iguais");
  672.                 }
  673.             }
  674.         }); //Mudar o método abstrato
  675.  
  676.         botEntrar = new JButton("Registar");
  677.         this.add(botEntrar);
  678.         botEntrar.addActionListener(new ActionListener() {
  679.             @Override
  680.             public void actionPerformed(ActionEvent e) {
  681.            
  682.                 username.getText();
  683.                 password.getPassword();
  684.                 confirmapass.getPassword();
  685.                 String pass = "";
  686.                
  687.                 List<Elemento_dei> pessoas = new ArrayList<>();
  688.                 try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Pessoas_obj.txt"))){
  689.                     pessoas = (ArrayList) oin.readObject();
  690.                 }catch(IOException | ClassNotFoundException n){
  691.                     System.out.println(n.getMessage());
  692.                 }
  693.                
  694.                 List<Inscrito> inscritos = new ArrayList<>();
  695.                 try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  696.                     inscritos = (ArrayList) oin.readObject();
  697.                 }catch(IOException | ClassNotFoundException n){
  698.                     System.out.println(n.getMessage());
  699.                 }
  700.                 int  n = 0;
  701.                 String perfil = null;
  702.                 int idade = 0;
  703.                 String nome1 = null;
  704.                 int y = 0;
  705.                 String tipo = null;
  706.                
  707.                 if (Arrays.equals(password.getPassword(),confirmapass.getPassword())) {
  708.                     char [] pass_char = password.getPassword();
  709.                     for (int i = 0; i < pass_char.length ; i++){
  710.                         pass += pass_char[i];
  711.                     }
  712.                     password1 = pass;
  713.                     for (Elemento_dei el : pessoas){
  714.                         if ((nome.getText().indexOf(el.nome)) >= 0){
  715.                             n = 1;
  716.                             y = 0;
  717.                             for (Inscrito i : inscritos){    
  718.                                 if(!username.getText().toUpperCase().equals(i.username.toUpperCase()) && !nome.getText().toUpperCase().equals(i.nome.toUpperCase())){
  719.                                     y += 1;  
  720.                                 }
  721.                                 else{
  722.                                     n = 1;
  723.                                     break;
  724.                                 }  
  725.                             }
  726.                             if (y == inscritos.size()){ //Se nao houver nenhum igual
  727.                                 n = 2;
  728.                                
  729.                                 perfil = el.perfil;
  730.                                 nome1 = el.nome;
  731.                                 idade = el.idade;
  732.                                 tipo = el.tipo;
  733.                             }
  734.                         }
  735.                     }
  736.                     if(n == 1){
  737.                         JOptionPane.showMessageDialog(rootPane, "Username ja escolhido ou ja esta inscrito no convivio", "Erro", JOptionPane.ERROR_MESSAGE);
  738.                         Registar r = new Registar(c);
  739.                         dispose();
  740.                     }
  741.                     else if (n == 0){
  742.                         JOptionPane.showMessageDialog(rootPane, "Nome nao consta na base de dados!", "Erro", JOptionPane.ERROR_MESSAGE);
  743.                         Registar r = new Registar(c);
  744.                         dispose();
  745.                     }
  746.                     else if (y == inscritos.size()){//Se nao houver nenhum igual
  747.                         List<Locais> locais = new ArrayList<>();
  748.                         System.out.println(tipo);
  749.                         c.inscricao_convivio(nome1,username.getText(),password1,perfil,idade,locais,tipo);
  750.                         JOptionPane.showMessageDialog(rootPane, "Registo bem sucedido!", ":)", JOptionPane.INFORMATION_MESSAGE);
  751.                         Login escolhe = new Login(c);
  752.                         dispose();
  753.                     }
  754.                 }
  755.                 else {
  756.                     JOptionPane.showMessageDialog(rootPane, "As passwords têm que ser iguais", "Erro", JOptionPane.ERROR_MESSAGE);
  757.                    
  758.                 }              
  759.             }
  760.         });
  761.         //Dentro do metodo abstrato, verificar se o username esta nos registados
  762.         //e se a password corresponde a password desse username
  763.         //Se sim, login successful Se nao, erro
  764.  
  765.  
  766.         botLimpa = new JButton("Limpa");
  767.             this.add(botLimpa);
  768.             botLimpa.addActionListener(new ActionListener() {
  769.                 @Override
  770.                 public void actionPerformed(ActionEvent e) {
  771.                         username.setText("");
  772.                         password.setText("");
  773.                 }
  774.             });
  775.  
  776.  
  777.         botSai = new JButton ("Sai");
  778.             this.add(botSai);
  779.             botSai.addActionListener(new ActionListener() {
  780.                 @Override
  781.                 public void actionPerformed(ActionEvent e) {
  782.                     System.exit(0);
  783.             }
  784.             });            
  785.  
  786.  
  787.         botRetroceder = new JButton ("Retroceder");
  788.             this.add(botRetroceder);
  789.             botRetroceder.addActionListener(new ActionListener() {
  790.                 @Override
  791.                 public void actionPerformed(ActionEvent e) {
  792.                     MenuInicial m = new MenuInicial(c);
  793.                     dispose();
  794.             }
  795.             });
  796.  
  797.  
  798.         JFrame self = this;
  799.  
  800.         String [] opcao = new String []{"Mostrar Password","Esconder Password"};
  801.        // JComboBox<String> oplist = new JComboBox<>(opcao);
  802.  
  803.  
  804.         showpass = new JComboBox (opcao);
  805.         showpass.addActionListener(new ActionListener(){
  806.             @Override
  807.             public void actionPerformed(ActionEvent event) {
  808.                 JComboBox <String> oplist = (JComboBox<String>)event.getSource();
  809.                 String opcao = (String) oplist.getSelectedItem();
  810.  
  811.                 if (opcao.equals("Mostrar Password")){
  812.                     password.setEchoChar((char)0);
  813.                     confirmapass.setEchoChar((char)0);
  814.                 }
  815.                 else{
  816.                     password.setEchoChar('•');
  817.                     confirmapass.setEchoChar('•');
  818.                 }
  819.  
  820.  
  821.  
  822.             }
  823.         });
  824.         this.add(showpass);
  825.  
  826.         this.pack();
  827.         }
  828.     }
  829.  
  830. ///////////////////////////////////////LOGIN//////////////////////////////////////////////
  831.  
  832. class Login extends JFrame{
  833.    
  834.     private final JTextField username;
  835.     //private final JTextField password;
  836.     private final JPasswordField password;
  837.     private final JComboBox showpass;
  838.     private final JLabel label1;
  839.     private final JLabel label2;
  840.     private final JButton botEntrar; //Para fazer login
  841.     private final JButton botLimpa; //Decidir se limpa tudo ou apenas um dos campos
  842.     private final JButton botSai;
  843.     private final JButton botRetroceder;
  844.  
  845.     public Login(Convivio c) {
  846.         this.setPreferredSize (new Dimension(600,450));
  847.         this.setTitle("Login");
  848.         this.setLocation (600,300); //Mais ou menos centrado
  849.         this.setVisible(true);
  850.         this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  851.        
  852.         this.setLayout (new GridLayout (7,2,15,15));
  853.  
  854.         label1 = new JLabel ("Username");
  855.         this.add (label1);
  856.         username = new JTextField();
  857.         this.add(username);
  858.         username.setPreferredSize (new Dimension(100,20));
  859.         username.addActionListener (new ActionListener() {
  860.             @Override
  861.             public void actionPerformed(ActionEvent e) {
  862.                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  863.             }
  864.         }); //Mudar o método abstrato
  865.  
  866.  
  867.         label2 = new JLabel ("Password");
  868.         this.add (label2);
  869.         password = new JPasswordField(); //cria novo objeto
  870.        
  871.         this.add(password); //adiciona ao container  
  872.         password.setPreferredSize (new Dimension(100,20));
  873.         password.addActionListener (new ActionListener() {
  874.             @Override
  875.             public void actionPerformed(ActionEvent e) {              
  876.  
  877.                 password.setToolTipText("A password tem que ter pelo menos 5 carateres");
  878.  
  879.             }
  880.         }); //Mudar o método abstrato    
  881.  
  882.         botEntrar = new JButton("Entrar");
  883.         this.add(botEntrar);
  884.         botEntrar.addActionListener(new ActionListener() {
  885.             @Override
  886.             public void actionPerformed(ActionEvent e) {
  887.                 String pass = "";
  888.                
  889.                 List<Inscrito> inscritos = new ArrayList<>();
  890.                 try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  891.                     inscritos = (ArrayList) oin.readObject();
  892.                 }catch(IOException | ClassNotFoundException n){
  893.                     System.out.println(n.getMessage());
  894.                 }
  895.                
  896.                 username.getText();
  897.                 password.getPassword();
  898.                
  899.                 char [] pass_char = password.getPassword();
  900.                 for (int i = 0; i < pass_char.length ; i++){
  901.                     pass += pass_char[i];
  902.                 }
  903.                
  904.                 String nome = null;
  905.                 int idade = 0;
  906.                 String username1 = null;
  907.                 String password1 = null;
  908.                 String perfil = null;
  909.                 List<Locais> locais = new ArrayList<>();
  910.                 String tipo = null;
  911.                
  912.                 int n = 0;
  913.                 for (Inscrito i : inscritos){
  914.                     if(!username.getText().equals(""))
  915.                         if(i.username.indexOf(username.getText()) >= 0 && i.password.indexOf(pass) >= 0 ){
  916.                             n = 1;
  917.  
  918.                             nome = i.nome;
  919.                             idade = i.idade;
  920.                             username1 = i.username;
  921.                             password1 = i.password;
  922.                             perfil = i.perfil;
  923.                             locais = i.locais;
  924.                             tipo = i.tipo;
  925.                            
  926.                             break;
  927.                         }
  928.                         else{
  929.                             n = 0;
  930.                         }
  931.                     else{
  932.                         n = 0;
  933.                     }
  934.                 }        
  935.  
  936.                 if(n == 1){
  937.                    
  938.                     dispose();
  939.                     Inscrito i = new Inscrito(perfil,nome,idade,username1,password1,locais,tipo);
  940.                     MenuEscolha escolhe = new MenuEscolha(i,c);
  941.                    
  942.                 }
  943.                 else {
  944.                     JOptionPane.showMessageDialog(rootPane, "Password ou username errados!", "Erro", JOptionPane.ERROR_MESSAGE);
  945.                     dispose();
  946.                     Login l = new Login(c);
  947.                 }
  948.  
  949.  
  950.             }
  951.         });
  952.         //Dentro do metodo abstrato, verificar se o username esta nos registados
  953.         //e se a password corresponde a password desse username
  954.         //Se sim, login successful Se nao, erro
  955.  
  956.  
  957.         botLimpa = new JButton("Limpa");
  958.             this.add(botLimpa);
  959.             botLimpa.addActionListener(new ActionListener() {
  960.                 @Override
  961.                 public void actionPerformed(ActionEvent e) {
  962.                         username.setText("");
  963.                         password.setText("");
  964.                 }
  965.             });
  966.  
  967.         botSai = new JButton ("Sai");
  968.             this.add(botSai);
  969.             botSai.addActionListener(new ActionListener() {
  970.                 @Override
  971.                 public void actionPerformed(ActionEvent e) {
  972.                     System.exit(0);
  973.             }
  974.             });
  975.  
  976.         botRetroceder = new JButton ("Retroceder");
  977.             this.add(botRetroceder);
  978.             botRetroceder.addActionListener(new ActionListener() {
  979.                 @Override
  980.                 public void actionPerformed(ActionEvent e) {
  981.                     MenuInicial m = new MenuInicial(c);
  982.                     //this.dispose();  //Remove JFrame 1
  983.                     //JFrame2.setVisible(true) //Show other frame
  984.                     dispose();
  985.                 }
  986.             });
  987.  
  988.  
  989.         JFrame self = this;
  990.  
  991.         String [] opcao = new String []{"Mostrar Password","Esconder Password"};
  992.        // JComboBox<String> oplist = new JComboBox<>(opcao);
  993.  
  994.         showpass = new JComboBox (opcao);
  995.         showpass.addActionListener(new ActionListener(){
  996.             @Override
  997.             public void actionPerformed(ActionEvent event) {
  998.                 JComboBox <String> oplist = (JComboBox<String>)event.getSource();
  999.                 String opcao = (String) oplist.getSelectedItem();
  1000.  
  1001.                 if (opcao.equals("Mostrar Password")){
  1002.                     password.setEchoChar((char)0);
  1003.                 }
  1004.                 else{
  1005.                     password.setEchoChar('•');
  1006.                 }
  1007.             }
  1008.         });
  1009.         this.add(showpass);
  1010.  
  1011.         this.pack();
  1012.         } //Ja feito em principio
  1013. }
  1014.  
  1015. //////////////////////////////////MENU ESCOLHA///////////////////////////////////////////
  1016.  
  1017. class MenuEscolha extends JFrame{
  1018.        
  1019.         private final JButton botMostraLocais;
  1020.         private final JButton botShowGuest;
  1021.         private final JButton botReceitaMin;
  1022.         private final JButton botRetroceder;
  1023.        
  1024.         public MenuEscolha(Inscrito i, Convivio c){
  1025.             this.setPreferredSize (new Dimension(500,400));
  1026.             this.setTitle("Menu Escolha");
  1027.             this.setLocation (600,300); //Mais ou menos centrado
  1028.             this.setVisible(true);
  1029.             this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  1030.             this.setLayout(new GridLayout (5,1,30,30));
  1031.        
  1032.             botMostraLocais = new JButton ("Mostra Locais");
  1033.             this.add(botMostraLocais);
  1034.             botMostraLocais.addActionListener(new ActionListener() {
  1035.             @Override
  1036.             public void actionPerformed(ActionEvent e) {
  1037.                 MostraLocais m = new MostraLocais(i,c);
  1038.                 dispose();
  1039.             }
  1040.         });  
  1041.            
  1042.             botShowGuest = new JButton ("Guest Lists");
  1043.             this.add(botShowGuest);
  1044.             botShowGuest.addActionListener(new ActionListener() {
  1045.             @Override
  1046.             public void actionPerformed(ActionEvent e) {
  1047.                 Guests g = new Guests(i,c);
  1048.                 dispose();
  1049.             }
  1050.         });
  1051.            
  1052.             botReceitaMin = new JButton ("Receita Minima Prevista");
  1053.             this.add(botReceitaMin);
  1054.            
  1055.             botRetroceder = new JButton ("Retroceder");
  1056.             this.add(botRetroceder);
  1057.             botRetroceder.addActionListener(new ActionListener() {
  1058.             @Override
  1059.             public void actionPerformed(ActionEvent e) {
  1060.                 Login Interface = new Login(c);
  1061.                 dispose();
  1062.             }
  1063.         });  
  1064.            
  1065.         JFrame self = this;
  1066.    
  1067.         this.pack();
  1068.         }      
  1069.     }
  1070.  
  1071. ///////////////////////////////////////MOSTRA LOCAIS//////////////////////////////////////////////
  1072.  
  1073. class MostraLocais extends JFrame{
  1074.    
  1075.     private final JTextArea box;
  1076.     private final JButton botEscolhe;
  1077.     private final JButton botRetroceder;
  1078.     private static final int SPACE = 3;
  1079.    
  1080.    
  1081.     public MostraLocais(Inscrito i,Convivio c){
  1082.  
  1083.         this.setTitle("Menu");
  1084.         this.setPreferredSize (new Dimension(600, 450));
  1085.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  1086.         this.setLocation(600,300);
  1087.         this.setVisible(true);
  1088.         //this.setLayout(new GridLayout (4,2,60,60));
  1089.        
  1090.         box = new JTextArea (20,20);        
  1091.         box.setEditable(false);
  1092.         this.add(box);      
  1093.        
  1094.         botEscolhe = new JButton ("Escolher Locais");
  1095.         this.add(botEscolhe);
  1096.         botEscolhe.addActionListener(new ActionListener() {
  1097.             @Override
  1098.             public void actionPerformed(ActionEvent e) {
  1099.                 EscolheLocais esc = new EscolheLocais(i,c);
  1100.                 dispose();
  1101.             }
  1102.         });  
  1103.        
  1104.         botRetroceder = new JButton ("Retroceder");
  1105.         this.add(botRetroceder);
  1106.         botRetroceder.addActionListener(new ActionListener() {
  1107.             @Override
  1108.             public void actionPerformed(ActionEvent e) {
  1109.                 MenuEscolha s = new MenuEscolha(i,c);
  1110.                 dispose();
  1111.             }
  1112.         });  
  1113.            
  1114.            
  1115.         //JPanel
  1116.         JPanel textAreaGrid = new JPanel(new GridLayout(1, 0, SPACE, SPACE));
  1117.         textAreaGrid.add(new JScrollPane(box));
  1118.        
  1119.         JPanel buttonPanel = new JPanel(new GridLayout(2, 0, SPACE, SPACE));
  1120.         buttonPanel.add(botEscolhe);
  1121.         buttonPanel.add(botRetroceder);
  1122.        
  1123.         getRootPane().setBorder(BorderFactory.createEmptyBorder(SPACE, SPACE, SPACE, SPACE));
  1124.         setLayout(new BorderLayout(SPACE, SPACE));
  1125.         add(textAreaGrid, BorderLayout.CENTER);
  1126.         add(buttonPanel, BorderLayout.PAGE_END);
  1127.        
  1128.         box.add(new JScrollPane(new JTextArea(50, 50)), BorderLayout.PAGE_START);
  1129.         JScrollPane scrollPane = new JScrollPane (box);
  1130.         JScrollPane scroll = new JScrollPane (box,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  1131.         this.add(scroll);
  1132.         this.setVisible (true);
  1133.        
  1134.        
  1135.         List<Locais> locais = new ArrayList<>();
  1136.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Locais_obj.txt"))){
  1137.             locais = (ArrayList) oin.readObject();
  1138.         }catch(IOException | ClassNotFoundException e){
  1139.             System.out.println(e.getMessage());
  1140.         }
  1141.      
  1142.         for (Locais l : locais){ //Ver da lotacao e da ordenacao
  1143.          
  1144.             box.append(l.nome + " com " + c.conta_inscritos(l.nome) + " inscrito(s)." );
  1145.             box.append("\n\n");
  1146.          
  1147.         }
  1148.        
  1149.         this.pack();
  1150.     }
  1151. }
  1152.  
  1153. //////////////////////////////////////ESCOLHA LOCAIS//////////////////////////////////////////
  1154.    
  1155. class EscolheLocais extends JFrame{
  1156.    
  1157.     private final JComboBox local1;
  1158.     private final JComboBox local2;
  1159.     private final JComboBox local3;
  1160.     private final JComboBox local4;
  1161.     private final JComboBox local5;
  1162.  
  1163.     private final JTextArea box_ins;
  1164.  
  1165.  
  1166.     private final JLabel label1;
  1167.     private final JLabel label2;    
  1168.     private final JLabel label3;
  1169.     private final JLabel label4;
  1170.     private final JLabel label5;
  1171.    
  1172.     private final JButton botRetroceder;
  1173.     private final JButton botGuardar;
  1174.    
  1175.     private static final int SPACE = 3;
  1176.    
  1177.     String escolha1 = null;
  1178.     String escolha2 = null;
  1179.     String escolha3 = null;
  1180.     String escolha4 = null;
  1181.     String escolha5 = null;
  1182.    
  1183.     public EscolheLocais(Inscrito i, Convivio c){
  1184.         this.setPreferredSize (new Dimension(600,450));
  1185.         this.setTitle("Escolhe Locais");
  1186.         this.setLocation (600,300); //Mais ou menos centrado
  1187.         this.setVisible(true);
  1188.         this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  1189.         //this.setLayout(new GridLayout (7,2,SPACE, SPACE)); //Ver ao certo para que serve
  1190.        
  1191.         List<Locais> locais = new ArrayList<>();
  1192.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Locais_obj.txt"))){
  1193.             locais = (ArrayList) oin.readObject();
  1194.         }catch(IOException | ClassNotFoundException e){
  1195.             System.out.println(e.getMessage());
  1196.         }
  1197.                
  1198.         label1 = new JLabel ("Local 1");
  1199.         local1 = new JComboBox ();
  1200.         for (Locais l : locais){
  1201.             local1.addItem(l.nome);
  1202.         }
  1203.         local1.addActionListener(new ActionListener(){
  1204.             @Override
  1205.             public void actionPerformed(ActionEvent event) {
  1206.                 //JComboBox <String> oplist = (JComboBox<String>)event.getSource();
  1207.                 escolha1 = (String) local1.getSelectedItem();              
  1208.             }
  1209.         });
  1210.      
  1211.         label2 = new JLabel ("Local 2");
  1212.         local2 = new JComboBox ();
  1213.         for (Locais l : locais){
  1214.             local2.addItem(l.nome);
  1215.         }
  1216.         local2.addActionListener(new ActionListener(){
  1217.             @Override
  1218.             public void actionPerformed(ActionEvent event) {
  1219.                 //JComboBox <String> oplist = (JComboBox<String>)event.getSource();
  1220.                 escolha2 = (String) local2.getSelectedItem();              
  1221.             }
  1222.         });
  1223.        
  1224.         label3 = new JLabel ("Local 3");
  1225.         local3 = new JComboBox ();
  1226.         for (Locais l : locais){
  1227.             local3.addItem(l.nome);
  1228.         }
  1229.         local3.addActionListener(new ActionListener(){
  1230.             @Override
  1231.             public void actionPerformed(ActionEvent event) {
  1232.                 //JComboBox <String> oplist = (JComboBox<String>)event.getSource();
  1233.                 escolha3 = (String) local3.getSelectedItem();              
  1234.             }
  1235.         });
  1236.        
  1237.        
  1238.         label4 = new JLabel ("Local 4");
  1239.         local4 = new JComboBox ();
  1240.         for (Locais l : locais){
  1241.             local4.addItem(l.nome);
  1242.         }
  1243.         local4.addActionListener(new ActionListener(){
  1244.             @Override
  1245.             public void actionPerformed(ActionEvent event) {
  1246.                 //JComboBox <String> oplist = (JComboBox<String>)event.getSource();
  1247.                 escolha4 = (String) local4.getSelectedItem();              
  1248.             }
  1249.         });
  1250.        
  1251.         label5 = new JLabel ("Local 5");
  1252.         local5 = new JComboBox ();
  1253.         for (Locais l : locais){
  1254.             local5.addItem(l.nome);
  1255.         }
  1256.         local5.addActionListener(new ActionListener(){
  1257.             @Override
  1258.             public void actionPerformed(ActionEvent event) {
  1259.                 //JComboBox <String> oplist = (JComboBox<String>)event.getSource();
  1260.                 escolha5 = (String) local5.getSelectedItem();              
  1261.             }
  1262.         });
  1263.        
  1264.         /*for (Locais l : i.locais){
  1265.             box_ins.append(l.nome + '\n');
  1266.         }*/
  1267.        
  1268.         botGuardar = new JButton ("Guardar");
  1269.         this.add(botGuardar);
  1270.         botGuardar.addActionListener(new ActionListener(){
  1271.             @Override
  1272.             public void actionPerformed(ActionEvent event) {
  1273.                 List<Inscrito> inscritos = new ArrayList<>();
  1274.                 try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  1275.                     inscritos = (ArrayList) oin.readObject();
  1276.                 }catch(IOException | ClassNotFoundException n){
  1277.                     System.out.println(n.getMessage());
  1278.                 }
  1279.                
  1280.                 /************************Adicionar locais*******************************/
  1281.                 box_ins.setText(null);
  1282.                
  1283.                 List<Locais> locais_novos = new ArrayList<>();
  1284.                
  1285.                 List<Locais> locais = new ArrayList<>();
  1286.                 try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Locais_obj.txt"))){
  1287.                     locais = (ArrayList) oin.readObject();
  1288.                 }catch(IOException | ClassNotFoundException e){
  1289.                     System.out.println(e.getMessage());
  1290.                 }
  1291.                
  1292.                 /**************Local 1************/
  1293.                 for (Locais l : locais){
  1294.                     if (l.nome.equals(escolha1)){
  1295.                         if (!locais_novos.contains(l)){
  1296.                             locais_novos.add(l);
  1297.                             box_ins.append(escolha1 + '\n');
  1298.                         }
  1299.                     }
  1300.                 }
  1301.                 /**************Local 2************/
  1302.                 for (Locais l : locais){
  1303.                     if (l.nome.equals(escolha2)){
  1304.                         if (!locais_novos.contains(l)){
  1305.                             locais_novos.add(l);
  1306.                             box_ins.append(escolha2 + '\n');
  1307.                         }
  1308.                     }
  1309.                 }
  1310.                 /************Local 3**************/
  1311.                 for (Locais l : locais){
  1312.                     if (l.nome.equals(escolha3)){
  1313.                         if (!locais_novos.contains(l)){
  1314.                             locais_novos.add(l);
  1315.                             box_ins.append(escolha3 + '\n');
  1316.                         }
  1317.                     }
  1318.                 }
  1319.                 /************Local 4**************/
  1320.                 for (Locais l : locais){
  1321.                     if (l.nome.equals(escolha4)){
  1322.                         if (!locais_novos.contains(l)){
  1323.                             locais_novos.add(l);
  1324.                             box_ins.append(escolha4 + '\n');
  1325.                         }
  1326.                     }
  1327.                 }
  1328.                 /************Local 5**************/
  1329.                 for (Locais l : locais){
  1330.                     if (l.nome.equals(escolha5)){
  1331.                         if (!locais_novos.contains(l)){
  1332.                             locais_novos.add(l);
  1333.                             box_ins.append(escolha5 + '\n');
  1334.                         }
  1335.                     }
  1336.                 }
  1337.                
  1338.                 /****************Gravar locais***************/
  1339.                 for (Inscrito ins : inscritos){
  1340.                     if(ins.nome.indexOf(i.nome) >= 0){
  1341.                         ins.locais = locais_novos;
  1342.                          
  1343.                     }
  1344.                 }
  1345.                 Ficheiros_Elementos_dei f = new Ficheiros_Elementos_dei();
  1346.                 f.escreve_ficheiro_dat_inscritos(inscritos);
  1347.                
  1348.                 /************************Adicionar locais*******************************/
  1349.             }
  1350.         });
  1351.        
  1352.        
  1353.         botRetroceder = new JButton ("Retroceder");
  1354.         this.add(botRetroceder);
  1355.         botRetroceder.addActionListener(new ActionListener() {
  1356.             @Override
  1357.             public void actionPerformed(ActionEvent e) {
  1358.                 MostraLocais m = new MostraLocais(i,c);
  1359.                 dispose();
  1360.             }
  1361.         });  
  1362.        
  1363.         box_ins = new JTextArea (5,5);        
  1364.         box_ins.setEditable(false);
  1365.        
  1366.         //JPanel
  1367.         JPanel textAreaGrid = new JPanel(new GridLayout(1,0, SPACE, SPACE));
  1368.         JScrollPane scroll = new JScrollPane (box_ins,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  1369.         textAreaGrid.add (scroll);      
  1370.        
  1371.        
  1372.         JPanel buttonPanel = new JPanel (new GridLayout(2,0, SPACE, SPACE));
  1373.         buttonPanel.add(botGuardar);
  1374.         buttonPanel.add(botRetroceder);
  1375.        
  1376.        
  1377.         JPanel locPanel  = new JPanel (new GridLayout (5, 0, SPACE, SPACE));
  1378.         locPanel.add(label1);
  1379.         locPanel.add(local1);
  1380.  
  1381.         locPanel.add(label2);
  1382.         locPanel.add(local2);
  1383.        
  1384.         locPanel.add(label3);
  1385.         locPanel.add(local3);
  1386.  
  1387.         locPanel.add(label4);
  1388.         locPanel.add(local4);
  1389.        
  1390.         locPanel.add(label5);        
  1391.         locPanel.add(local5);
  1392.        
  1393.        
  1394.         //this.getRootPane().setBorder(BorderFactory.createEmptyBorder(SPACE, SPACE, SPACE, SPACE));
  1395.         //this.setLayout(new BorderLayout(SPACE, SPACE));
  1396.        
  1397.         this.add(locPanel, BorderLayout.PAGE_START);
  1398.         this.add(textAreaGrid, BorderLayout.CENTER);
  1399.         this.add(buttonPanel, BorderLayout.PAGE_END);
  1400.        
  1401.         this.pack();
  1402.     }
  1403. }
  1404.  
  1405. /////////////////////////////////////GUESTS////////////////////////////////////////////////
  1406.  
  1407. class Guests extends JFrame{
  1408.    
  1409.     private final JTextArea box;
  1410.     private final JButton botInscreve_Guest;
  1411.     private final JButton botRetroceder;
  1412.     private static final int SPACE = 3;
  1413.    
  1414.     public Guests(Inscrito i,Convivio c){
  1415.         this.setTitle("Menu");
  1416.         this.setPreferredSize (new Dimension(600, 450));
  1417.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  1418.         this.setLocation(600,300);
  1419.         this.setVisible(true);
  1420.        
  1421.         box = new JTextArea (20,20);        
  1422.         box.setEditable(false);
  1423.         this.add(box);
  1424.        
  1425.         botInscreve_Guest = new JButton ("Inscreve Guest");
  1426.         this.add(botInscreve_Guest);
  1427.         botInscreve_Guest.addActionListener(new ActionListener() {
  1428.             @Override
  1429.             public void actionPerformed(ActionEvent e) {
  1430.                 Inscreve_Guest g = new Inscreve_Guest(i,c);
  1431.             }
  1432.         });
  1433.        
  1434.         botRetroceder = new JButton ("Retroceder");
  1435.         this.add(botRetroceder);
  1436.         botRetroceder.addActionListener(new ActionListener() {
  1437.             @Override
  1438.             public void actionPerformed(ActionEvent e) {
  1439.                 MenuEscolha s = new MenuEscolha(i,c);
  1440.                 dispose();
  1441.             }
  1442.         });  
  1443.        
  1444.         JPanel textAreaGrid = new JPanel(new GridLayout(1, 0, SPACE, SPACE));
  1445.         textAreaGrid.add(new JScrollPane(box));
  1446.        
  1447.         JPanel buttonPanel = new JPanel(new GridLayout(2, 0, SPACE, SPACE));
  1448.         buttonPanel.add(botInscreve_Guest);
  1449.         buttonPanel.add(botRetroceder);
  1450.        
  1451.         getRootPane().setBorder(BorderFactory.createEmptyBorder(SPACE, SPACE, SPACE, SPACE));
  1452.         setLayout(new BorderLayout(SPACE, SPACE));
  1453.         add(textAreaGrid, BorderLayout.CENTER);
  1454.         add(buttonPanel, BorderLayout.PAGE_END);
  1455.        
  1456.         box.add(new JScrollPane(new JTextArea(50, 50)), BorderLayout.PAGE_START);
  1457.         JScrollPane scrollPane = new JScrollPane (box);
  1458.         JScrollPane scroll = new JScrollPane (box,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  1459.         this.add(scroll);
  1460.         this.setVisible (true);
  1461.        
  1462.         List<Bares> bares = new ArrayList<>();
  1463.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Bares_obj.txt"))){
  1464.             bares = (ArrayList) oin.readObject();
  1465.         }catch(IOException | ClassNotFoundException e){
  1466.             System.out.println(e.getMessage());
  1467.         }
  1468.        
  1469.         for (Bares l : bares){
  1470.                 box.append(l.nome + ":\n");
  1471.                 for (Inscrito insc : l.guest){
  1472.                    
  1473.                     box.append("\t-" + insc.nome + insc.perfil);
  1474.                    
  1475.                 }  
  1476.             }
  1477.             box.append("\n\n"); //FAZER COM GUESTS COMO ARRAYS E OS BARES EM
  1478.        
  1479.         this.pack();
  1480.     }
  1481. }
  1482.  
  1483. ///////////////////////////INSCREVE GUEST///////////////////////////////////////////////
  1484.  
  1485. class Inscreve_Guest extends JFrame{
  1486.    
  1487.     private final JButton botRetroceder;
  1488.     private final JButton botInscrever;
  1489.    
  1490.     private final JLabel titulo;
  1491.     private final JLabel labelbar;
  1492.    
  1493.     private final JTextField nomebar;
  1494.    
  1495.     private static final int SPACE = 3;
  1496.  
  1497.    
  1498.     public Inscreve_Guest(Inscrito i, Convivio c){
  1499.      
  1500.         this.setPreferredSize (new Dimension(400,250));
  1501.         this.setTitle("Inscrição no Bar");
  1502.         this.setLocation (600,300); //Mais ou menos centrado
  1503.         this.setVisible(true);
  1504.         this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  1505.        
  1506.         this.setLayout (new GridLayout (5,2,10,10));
  1507.        
  1508.         titulo = new JLabel ("Escolha o bar pretendido", SwingConstants.CENTER);
  1509.         this.add (titulo);
  1510.        
  1511.         labelbar = new JLabel ("Nome do Bar: ");
  1512.         //this.add (labelbar);
  1513.        
  1514.         nomebar = new JTextField();
  1515.         //this.add(nomebar);
  1516.         nomebar.setPreferredSize (new Dimension(5,10));
  1517.         nomebar.addActionListener (new ActionListener() {
  1518.             @Override
  1519.             public void actionPerformed(ActionEvent e) {
  1520.                 //Guardar nome do bar numa variável
  1521.             }
  1522.         }); //Mudar o método abstrato
  1523.        
  1524.         JPanel panel = new JPanel(new BorderLayout());
  1525.         panel.add(labelbar,BorderLayout.WEST);
  1526.         panel.add(nomebar,BorderLayout.CENTER);
  1527.         this.add(panel);
  1528.        
  1529.         botInscrever = new JButton ("Inscrever");
  1530.         this.add(botInscrever);
  1531.         botInscrever.addActionListener(new ActionListener() {
  1532.             @Override
  1533.             public void actionPerformed(ActionEvent e) {
  1534.                
  1535.                 dispose();
  1536.             }
  1537.         });
  1538.        
  1539.        
  1540.         botRetroceder = new JButton ("Retroceder");
  1541.             this.add(botRetroceder);
  1542.             botRetroceder.addActionListener(new ActionListener() {
  1543.             @Override
  1544.             public void actionPerformed(ActionEvent e) {
  1545.                
  1546.                 dispose();
  1547.             }
  1548.         });
  1549.        
  1550.         this.pack();
  1551.     }
  1552. }
  1553.  
  1554. /*--------------------------------------MAIN----------------------------------------------*/
  1555.  
  1556. public class projeto_racunho {
  1557.    
  1558.     public static void main(String[] args) {
  1559.        
  1560.         /*INICIALIZAR*/
  1561.         Convivio c = new Convivio("Mega Convivio");
  1562.        
  1563.         Ficheiros_Elementos_dei a = new Ficheiros_Elementos_dei();
  1564.         a.ler_ficheiro_Elementos_dei();
  1565.         Ficheiros_Locais l = new Ficheiros_Locais();
  1566.         l.ler_ficheiro_locais();
  1567.        
  1568.         /*INTERFACE*/
  1569.         MenuInicial m = new MenuInicial(c);
  1570.        
  1571.         List<Inscrito> inscritos = new ArrayList<>();
  1572.        
  1573.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  1574.             inscritos = (ArrayList) oin.readObject();
  1575.         }catch(IOException | ClassNotFoundException n){
  1576.             System.out.println(n.getMessage());
  1577.         }
  1578.      
  1579.         for (Inscrito i : inscritos){
  1580.             i.print();
  1581.         }
  1582.     }  
  1583.    
  1584. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement