Advertisement
Guest User

Untitled

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