Advertisement
Guest User

Untitled

a guest
Dec 10th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 41.81 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.    
  43. /**
  44.  * classe de criação do convivio
  45.  */
  46.     public String nome_convivio;
  47.    
  48.     //public List<Locais> locais = new ArrayList<>();
  49.     //public List<Elemento_dei> registados = new ArrayList<>();
  50.    
  51.    
  52.     public Convivio(String nome_convivio){
  53.         this.nome_convivio = nome_convivio;
  54.     }
  55.      /**
  56.      * Por enquanto ainda nao serve para nada
  57.      *
  58.      */
  59.     public void login(){
  60.        
  61.     } //Pedir username e password
  62.     public void inscricao_convivio(String nome, String username,String password,String perfil,int idade,List<Locais> locais){
  63.    
  64.         List<Inscrito> inscritos = new ArrayList<>();
  65.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  66.             inscritos = (ArrayList) oin.readObject();
  67.         }catch(IOException | ClassNotFoundException e){
  68.             System.out.println(e.getMessage());
  69.         }
  70.        
  71.         Inscrito x = new Inscrito(perfil, nome, idade,username,password,locais);
  72.         inscritos.add(x);
  73.        
  74.         Ficheiros_Elementos_dei f = new Ficheiros_Elementos_dei();
  75.         f.escreve_ficheiro_dat_inscritos(inscritos);
  76.    
  77.     } //Tem que pedir sempre o nome para verificar se pertence ao DEI  //Verificar sempre se ha username igual
  78.     public void show_locais(){ //Mostrar sempre depois da inscricao, ver numero atual de inscritos de cada um e a lotacao (se aplicavel)
  79.         List<Locais> locais = new ArrayList<>();
  80.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Locais_obj.txt"))){
  81.             locais = (ArrayList) oin.readObject();
  82.         }catch(IOException | ClassNotFoundException e){
  83.             System.out.println(e.getMessage());
  84.         }
  85.         for (Locais l : locais){
  86.             l.print();
  87.         }
  88.     }
  89.     public void escolha_locais(){
  90.    
  91.     } //Na interface so ter 5 espacos //Ter um ficheiro objetos dos inscritos em cada local (secalhar)
  92.     public void show_guests(){ //Nao sei se mete aqui ou se se mete na class locais
  93.        
  94.     } //Ficheiro objetos para cada guest de cada bar
  95.     /*public double receita(){ //Nao interessa o que esta ca dentro por enquanto
  96.         List<Locais> locais = new ArrayList<>();
  97.         double receita = 0;
  98.         for (Locais l: locais){
  99.            double receita_local = 0;
  100.            receita += l.receita;
  101.         }
  102.         return receita;
  103.     }*/
  104.    
  105.     public int conta_inscritos(String nome_local){
  106.         int num_inscritos = 0;
  107.        
  108.         List<Inscrito> inscritos = new ArrayList<>();
  109.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  110.             inscritos = (ArrayList) oin.readObject();
  111.         }catch(IOException | ClassNotFoundException e){
  112.             System.out.println(e.getMessage());
  113.         }
  114.        
  115.         for (Inscrito i : inscritos){
  116.             for (Locais l : i.locais){
  117.                 if(l.nome.indexOf(nome_local) >= 0){
  118.                     num_inscritos += 1;
  119.                 }
  120.             }
  121.         }
  122.         return num_inscritos;
  123.     }
  124. }
  125.  
  126.  
  127. /*----------------------------LOCAIS CONVIVIO-----------------------------------------*/
  128. class Locais implements Serializable{
  129.     private String coord_gps;
  130.     public String nome;
  131.    
  132.     public Locais(String coord_gps,String nome){
  133.         this.coord_gps = coord_gps;
  134.         this.nome = nome;
  135.     }
  136.    
  137.     public void print(){
  138.         System.out.printf("%s\t%s\n",nome,coord_gps);    
  139.     }
  140. }
  141.  
  142. abstract class Parques extends Locais{
  143.  
  144.     public Parques(String coord_gps, String nome) {
  145.         super(coord_gps, nome);
  146.     }
  147. }
  148.  
  149. class Jardins extends Parques{
  150.     private int area; //em metros quadrados
  151.  
  152.     public Jardins(String coord_gps, String nome,int area) {
  153.         super(coord_gps, nome);
  154.         this.area = area;
  155.     }
  156.    
  157.    public int receita_prevista(List<Inscrito> inscritos){
  158.        return 0;
  159.    }
  160.    public String info(){
  161.        return "";
  162.    }
  163. }
  164.  
  165. class Areas_desportivas extends Parques{
  166.     private String desportos[];
  167.  
  168.     public Areas_desportivas(String coord_gps, String nome,String desportos[]) {
  169.         super(coord_gps, nome);
  170.         this.desportos = desportos;
  171.     }
  172.  
  173.     @Override
  174.     public void print(){
  175.         System.out.printf("%s\n",nome);
  176.         for (int i = 0; i < desportos.length; i++){
  177.             System.out.printf("%s", desportos[i]);
  178.         }
  179.     }
  180.    
  181.     public int receita_prevista(List<Inscrito> inscritos){
  182.         return 0;
  183.     }
  184. }
  185.  
  186. class Exposicoes extends Locais{
  187.     public String forma_artistica;
  188.     public int custo_ingresso;
  189.    
  190.     public Exposicoes(String coord_gps,String nome,String forma_artistica, int custo_ingresso) {
  191.         super(coord_gps, nome);
  192.         this.custo_ingresso = custo_ingresso;
  193.         this.forma_artistica = forma_artistica;
  194.     }
  195.    
  196.     public int receita_prevista(){ //Fazer
  197.         return 0;
  198.     }
  199.    
  200. }
  201.  
  202. class Bares extends Locais{
  203.        
  204.     private int lotacao;  
  205.     private int consumo_minimo; //Por pessoa
  206.    
  207.     public List<Inscrito> guest = new ArrayList<>();
  208.  
  209.     public Bares(String coord_gps,String nome, int lotacao, int consumo_minimo) {
  210.         super(coord_gps, nome);
  211.         this.lotacao = lotacao;
  212.         this.consumo_minimo = consumo_minimo;
  213.     }
  214.    
  215.     public void add_guest(Inscrito i,List<Inscrito> guest){
  216.         int maximo_guest = (int)(lotacao * 0.3);
  217.         if (guest.size() == maximo_guest){
  218.             if (i.perfil.indexOf("Boemio") >= 0){
  219.                 for (Inscrito ins : inscritos){
  220.                    
  221.                 }
  222.             }
  223.         }
  224.         else{
  225.             inscritos.add(i);
  226.         }
  227.         Ficheiros_Elementos_dei f = new Ficheiros_Elementos_dei();
  228.         f.escreve_ficheiro_dat_inscritos(inscritos);
  229.     } //Fazer
  230.    
  231.     public int receita_prevista(String nome_local){
  232.         Convivio c = new Convivio("Mega Convivio");
  233.        
  234.         int n_inscritos;
  235.         n_inscritos = c.conta_inscritos(nome_local);
  236.        
  237.         return consumo_minimo * n_inscritos;
  238.     }
  239.    
  240. }
  241.  
  242. /*----------------------------ELEMENTOS DEI-----------------------------------------*/
  243. class Elemento_dei implements Serializable{
  244.     public String perfil;
  245.     public String nome;
  246.     public int idade;
  247.    
  248.     public Elemento_dei(String perfil, String nome,int idade){
  249.         this.perfil = perfil;
  250.         this.nome = nome;
  251.         this.idade = idade;
  252.     }
  253.    
  254.     public void print(){
  255.         System.out.printf("%s\t%d\t%s\n", nome,idade,perfil);
  256.     }
  257.    
  258. }
  259.  
  260. class Inscrito extends Elemento_dei{
  261.     public String username;
  262.     public String password;
  263.    
  264.     public List<Locais> locais = new ArrayList<>(); //Maximo de 5 locais
  265.  
  266.     public Inscrito(String perfil, String nome, int idade, String username,String password,List<Locais> locais) {
  267.         super(perfil, nome, idade);
  268.         this.username = username;
  269.         this.password = password;
  270.         this.locais = locais;
  271.     }
  272.    
  273.     @Override
  274.     public void print(){
  275.         System.out.printf("%s\t%s\t%s\t%s\t%s\n", nome,perfil,idade,username,password);
  276.     }
  277. }
  278.  
  279. class Estudante extends Elemento_dei{
  280.     private String curso;
  281.  
  282.     public Estudante(String perfil, String nome,int idade,String curso) {
  283.         super(perfil, nome, idade);
  284.         this.curso = curso;
  285.     }
  286.    
  287.     @Override
  288.     public String toString(){
  289.         return nome + perfil + idade;
  290.     }
  291. }
  292.  
  293. class Professor extends Elemento_dei{
  294.     private String tipo_prof;
  295.  
  296.     public Professor(String perfil, String nome, int idade, String tipo_prof) {
  297.         super(perfil, nome, idade);
  298.         this.tipo_prof = tipo_prof;
  299.     }
  300.    
  301.     @Override
  302.     public String toString(){
  303.         return nome;
  304.     }
  305. }
  306.  
  307. class Funcionario extends Elemento_dei{
  308.     private String tipo_func;
  309.  
  310.     public Funcionario(String perfil, String nome, int idade, String tipo_func) {
  311.         super(perfil, nome, idade);
  312.         this.tipo_func = tipo_func;
  313.     }
  314.    
  315.     @Override
  316.     public String toString(){
  317.         return nome;
  318.     }
  319. }
  320.  
  321. /*---------------------------------FICHEIROS-----------------------------------------*/
  322. class Ficheiros_Locais{
  323.    
  324.     public void ler_ficheiro_locais(){
  325.         List<Locais> locais = new ArrayList<>();
  326.         try{
  327.             BufferedReader reader = new BufferedReader(new FileReader("Locais.txt"));
  328.             String line;
  329.             while ((line = reader.readLine()) != null){
  330.                 String nome;
  331.                 String coordenadas;
  332.                 String tipo_local;
  333.                 String[] info = line.split("\t");
  334.                 nome = info[0];
  335.                 coordenadas = info[1];
  336.                 tipo_local = info[2];
  337.                 if(tipo_local.indexOf("parque") >= 0){
  338.                     String tipo_parque = info[3];
  339.                     if(tipo_parque.indexOf("jardim") >= 0){
  340.                         int area = Integer.valueOf(info[4].trim());
  341.                         Locais l = new Jardins(coordenadas,nome,area);
  342.                         locais.add(l);
  343.                     }
  344.                     else if(tipo_parque.indexOf("desportivo") >= 0){
  345.                         String desportos[] = new String[info.length - 4];
  346.                         int n = 0;
  347.                         for (int i = 4; i < info.length ; i++){
  348.                            desportos[n] = info[i];
  349.                            n++;
  350.                         }
  351.                         Locais l = new Areas_desportivas(coordenadas,nome,desportos);
  352.                         locais.add(l);
  353.                     }
  354.                 }
  355.                 else if(tipo_local.indexOf("bar") >= 0){
  356.                     int lotacao = Integer.valueOf(info[3].trim());
  357.                     int consumo_minimo = Integer.valueOf(info[4].trim());
  358.                     Locais l = new Bares(coordenadas,nome,lotacao,consumo_minimo);
  359.                     locais.add(l);
  360.                 }
  361.                 else if(tipo_local.indexOf("exposicao") >= 0){
  362.                     int custo_ingresso = Integer.valueOf(info[3].trim());
  363.                     String forma_artistica = info[4];
  364.                     Locais l = new Exposicoes(coordenadas,nome,forma_artistica,custo_ingresso);
  365.                     locais.add(l);
  366.                 }
  367.        
  368.         }
  369.            
  370.         }catch(IOException e){
  371.             System.out.print("ERRO");
  372.             System.out.printf("Ocorreu a exceçao %s ao ler o ficheiro\n", e);
  373.         }
  374.        
  375.         escreve_ficheiro_dat_locais(locais);
  376.        
  377.        
  378.        
  379.     }
  380.     public void escreve_ficheiro_dat_locais(List<Locais> locais){
  381.          try{
  382.             ObjectOutputStream oS = new ObjectOutputStream(new FileOutputStream("Locais_obj.txt"));
  383.             oS.writeObject(locais);
  384.             oS.close();
  385.  
  386.         }catch(IOException e){
  387.             System.out.print("ERRO");
  388.             System.out.printf("Ocorreu a exceçao %s ao escrever no ficheiro\n", e);
  389.         }
  390.     }
  391. }
  392.  
  393. class Ficheiros_Elementos_dei{  
  394.    
  395.     public void ler_ficheiro_Elementos_dei(){
  396.         List<Elemento_dei> elementos_dei = new ArrayList<>(); //Ver se meter isto aqui esta correto
  397.         try{
  398.             BufferedReader reader = new BufferedReader(new FileReader("Elementos_DEI.txt"));
  399.             String line;
  400.             while ((line = reader.readLine()) != null){
  401.                 String nome;
  402.                 String perfil;
  403.                 int idade;
  404.                 String profissao;
  405.                 String[] info = line.split(";");
  406.                 nome = info[0];
  407.                 perfil = info[1];
  408.                 idade = Integer.valueOf(info[2].trim());
  409.                 profissao = info[3];
  410.                 if(profissao.indexOf("aluno") >= 0){
  411.                     String curso = info[4];
  412.                     Elemento_dei e = new Estudante(perfil,nome,idade,curso);
  413.                     elementos_dei.add(e);
  414.                    
  415.                 }
  416.                 else if(profissao.indexOf("professor") >= 0){
  417.                     String tipo_prof = info[4];
  418.                     Elemento_dei e = new Professor(perfil, nome,idade,tipo_prof);
  419.                     elementos_dei.add(e);
  420.                    
  421.                 }
  422.                 else if(profissao.indexOf("funcionario") >= 0){
  423.                     String tipo_func = info[4];
  424.                     Elemento_dei e = new Funcionario(perfil,nome,idade, tipo_func);
  425.                     elementos_dei.add(e);
  426.                    
  427.                 }
  428.                 else{
  429.                     System.out.print("ERRO\n");
  430.                 }
  431.             }
  432.         }
  433.         catch(IOException e){
  434.             System.out.printf("Ocorreu a exceçao %s ao ler o ficheiro\n", e);
  435.         }
  436.        
  437.         escreve_ficheiro_dat_pessoas(elementos_dei);
  438.     }
  439.     public void escreve_ficheiro_dat_pessoas(List<Elemento_dei> pessoas){
  440.          try{
  441.             ObjectOutputStream oS = new ObjectOutputStream(new FileOutputStream("Pessoas_obj.txt"));
  442.             oS.writeObject(pessoas);
  443.             oS.close();
  444.  
  445.         }catch(IOException e){
  446.             System.out.printf("Ocorreu a exceçao %s ao escrever mo ficheiro\n", e);
  447.         }
  448.     } //Ver como fazer para ir atualizando
  449.     public void escreve_ficheiro_dat_inscritos(List<Inscrito> inscritos){
  450.          try{
  451.             ObjectOutputStream oS = new ObjectOutputStream(new FileOutputStream("Inscritos_obj.txt"));
  452.             oS.writeObject(inscritos);
  453.             oS.close();
  454.  
  455.         }catch(IOException e){
  456.             System.out.printf("Ocorreu a exceçao %s ao escrever mo ficheiro\n", e);
  457.         }
  458.     } //Ver como fazer para ir atualizando
  459. }
  460.  
  461. /*----------------------------------------------------INTERFACE-------------------------------------------------------*/
  462.  
  463.  
  464. ////////////////////////////////////////////MENU////////////////////////////////////////////
  465.  
  466. class MenuInicial extends JFrame{ //O que por no registar? (opcoes)
  467.    
  468.     private final JLabel welcome; //Texto a dar as boas vindas
  469.     private final JButton botLogin;
  470.     private final JButton botRegistar;
  471.     private final JButton botSair;
  472.    
  473.     public MenuInicial(Convivio c){
  474.         this.setPreferredSize (new Dimension(450,300));
  475.         this.setTitle("Menu Inicial");
  476.         this.setLocation (600,300); //Mais ou menos centrado
  477.         this.setVisible(true);
  478.         this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  479.         this.setLayout (new GridLayout (4,2,20,20));
  480.    
  481.         welcome = new JLabel ("                                          Bem vindo ao Mega Convivio do DEI");
  482.         this.add (welcome);
  483.        
  484.         botLogin = new JButton("Login");
  485.         this.add(botLogin);
  486.         botLogin.addActionListener(new ActionListener() {
  487.         @Override
  488.         public void actionPerformed(ActionEvent e) {
  489.             Login Interface = new Login(c);
  490.             dispose();
  491.         }
  492.     });
  493.        
  494.         botRegistar = new JButton("Registar");
  495.         this.add(botRegistar);
  496.         botRegistar.addActionListener(new ActionListener() {
  497.         @Override
  498.         public void actionPerformed(ActionEvent e) {
  499.             Registar r = new Registar(c);
  500.             dispose();
  501.         }
  502.     });
  503.        
  504.         botSair = new JButton("Sair");
  505.         this.add(botSair);
  506.         botSair.addActionListener(new ActionListener() {
  507.         @Override
  508.         public void actionPerformed(ActionEvent e) {
  509.             System.exit(0);
  510.         }
  511.     });
  512.        
  513.         this.pack();
  514.     }
  515. }
  516.  
  517. ///////////////////////////////////////////REGISTAR////////////////////////////////////
  518.  
  519. class Registar extends JFrame{
  520.    
  521.     private final JTextField nome;
  522.     private final JTextField username;
  523.     private final JPasswordField password;
  524.     private final JPasswordField confirmapass;
  525.    
  526.     private final JComboBox showpass;
  527.    
  528.     private final JLabel label0;
  529.     private final JLabel label1;
  530.     private final JLabel label2;
  531.     private final JLabel label3;
  532.  
  533.    
  534.     private final JButton botEntrar; //Para fazer login
  535.     private final JButton botLimpa; //Decidir se limpa tudo ou apenas um dos campos
  536.     private final JButton botSai;
  537.     private final JButton botRetroceder;
  538.    
  539.     public String nome1;
  540.     public String username1;
  541.     public String password1;
  542.  
  543.     public Registar(Convivio c) {
  544.        
  545.         this.setPreferredSize (new Dimension(600,450));
  546.         this.setTitle("Registar");
  547.         this.setLocation (600,300); //Mais ou menos centrado
  548.         this.setVisible(true);
  549.         this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  550.         //this.setLayout(new FlowLayout());
  551.  
  552.  
  553.         //final JPanel grid = new JPanel(new FlowLayout());    
  554.         this.setLayout (new GridLayout (7,2,15,15));
  555.         //this.add(grid);
  556.  
  557.         label0 = new JLabel ("Nome");
  558.         this.add (label0);
  559.  
  560.        
  561.         nome = new JTextField();
  562.         this.add(nome);
  563.         nome.setPreferredSize (new Dimension(100,20));
  564.         nome.addActionListener (new ActionListener() {
  565.             @Override
  566.             public void actionPerformed(ActionEvent e) {
  567.                 nome1 = nome.getText();
  568.             }
  569.         }); //Mudar o método abstrato
  570.  
  571.         label1 = new JLabel ("Escolher Username");
  572.         this.add (label1);
  573.  
  574.         username = new JTextField();
  575.         this.add(username);
  576.         username.setPreferredSize (new Dimension(100,20));
  577.         username.addActionListener (new ActionListener() {
  578.             @Override
  579.             public void actionPerformed(ActionEvent e) {
  580.                 username1 = username.getText();
  581.             }
  582.         }); //Mudar o método abstrato
  583.  
  584.  
  585.         label2 = new JLabel ("Escolher Password");
  586.         this.add (label2);
  587.  
  588.         password = new JPasswordField(); //cria novo objeto
  589.         this.add(password); //adiciona ao container  
  590.         password.setPreferredSize (new Dimension(100,20));
  591.         password.addActionListener (new ActionListener() {
  592.             @Override
  593.             public void actionPerformed(ActionEvent e) {              
  594.  
  595.                 password.setToolTipText("A password tem que ter pelo menos 5 carateres");
  596.  
  597.             }
  598.         }); //Mudar o método abstrato
  599.  
  600.  
  601.         label3 = new JLabel ("Confirmar Password");
  602.         this.add (label3);
  603.  
  604.         confirmapass = new JPasswordField();
  605.         this.add(confirmapass);
  606.         confirmapass.setPreferredSize (new Dimension(100,20));
  607.         confirmapass.addActionListener (new ActionListener() {
  608.             @Override
  609.             public void actionPerformed(ActionEvent e) {
  610.  
  611.                 if ((password.getPassword()).equals(confirmapass)){
  612.  
  613.                 }
  614.                 else{
  615.                     System.out.println("As passwords tem que ser iguais");
  616.                 }
  617.             }
  618.         }); //Mudar o método abstrato
  619.  
  620.         //SHOW PASSWORDDDDDDDDDDDDDDDD
  621.  
  622.         //char[] pass = password.getPassword();
  623.         //String pass = new String(password.getPassword());
  624.         //char[] correctpass = {'b','a','t','a','t','a'};
  625.         //String correctpass = "batata";
  626.        
  627.         //REGISTO
  628.        
  629.        
  630.         botEntrar = new JButton("Registar");
  631.         this.add(botEntrar);
  632.         botEntrar.addActionListener(new ActionListener() {
  633.             @Override
  634.             public void actionPerformed(ActionEvent e) {
  635.            
  636.                 username.getText();
  637.                 password.getPassword();
  638.                 confirmapass.getPassword();
  639.                 String pass = "";
  640.                
  641.                 List<Elemento_dei> pessoas = new ArrayList<>();
  642.                 try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Pessoas_obj.txt"))){
  643.                     pessoas = (ArrayList) oin.readObject();
  644.                 }catch(IOException | ClassNotFoundException n){
  645.                     System.out.println(n.getMessage());
  646.                 }
  647.                
  648.                 List<Inscrito> inscritos = new ArrayList<>();
  649.                 try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  650.                     inscritos = (ArrayList) oin.readObject();
  651.                 }catch(IOException | ClassNotFoundException n){
  652.                     System.out.println(n.getMessage());
  653.                 }
  654.                 int  n = 0;
  655.                 String perfil = null;
  656.                 int idade = 0;
  657.                 String nome1 = null;
  658.                 int y = 0;
  659.                 if (Arrays.equals(password.getPassword(),confirmapass.getPassword())) {
  660.                    
  661.                     char [] pass_char = password.getPassword();
  662.                     for (int i = 0; i < pass_char.length ; i++){
  663.                         pass += pass_char[i];
  664.                        
  665.                     }
  666.                     password1 = pass;
  667.                     for (Elemento_dei el : pessoas){
  668.                         if ((nome.getText().indexOf(el.nome)) >= 0){
  669.                            
  670.                             n = 1;
  671.                             y = 0;
  672.                             for (Inscrito i : inscritos){
  673.                                
  674.                                 if(!username.getText().toUpperCase().equals(i.username.toUpperCase()) && !nome.getText().toUpperCase().equals(i.nome.toUpperCase())){
  675.                                     y += 1;  
  676.                                 }
  677.                                 else{
  678.                                     n = 1;
  679.                                     break;
  680.                                 }  
  681.                             }
  682.                             if (y == inscritos.size()){ //Se nao houver nenhum igual
  683.                                 n = 2;
  684.                                 perfil = el.perfil;
  685.                                 nome1 = el.nome;
  686.                                 idade = el.idade;
  687.                             }
  688.                            
  689.                         }
  690.                     }
  691.                    
  692.                     if(n == 1){
  693.                         JOptionPane.showMessageDialog(rootPane, "Username ja escolhido ou ja esta inscrito no convivio", "Erro", JOptionPane.ERROR_MESSAGE);
  694.                         Registar r = new Registar(c);
  695.                         dispose();
  696.                     }
  697.                     else if (n == 0){
  698.                         JOptionPane.showMessageDialog(rootPane, "Nome nao consta na base de dados!", "Erro", JOptionPane.ERROR_MESSAGE);
  699.                         Registar r = new Registar(c);
  700.                         dispose();
  701.                     }
  702.                     else if (y == inscritos.size()){//Se nao houver nenhum igual
  703.                         List<Locais> locais = new ArrayList<>();
  704.                         c.inscricao_convivio(nome1,username.getText(),password1,perfil,idade,locais);
  705.                         JOptionPane.showMessageDialog(rootPane, "Registo bem sucedido!", ":)", JOptionPane.INFORMATION_MESSAGE);
  706.                         Login escolhe = new Login(c);
  707.                         dispose();
  708.                     }
  709.                 }
  710.                 else {
  711.                     JOptionPane.showMessageDialog(rootPane, "As passwords têm que ser iguais", "Erro", JOptionPane.ERROR_MESSAGE);
  712.                    
  713.                 }              
  714.             }
  715.         });
  716.         //Dentro do metodo abstrato, verificar se o username esta nos registados
  717.         //e se a password corresponde a password desse username
  718.         //Se sim, login successful Se nao, erro
  719.  
  720.  
  721.         botLimpa = new JButton("Limpa");
  722.             this.add(botLimpa);
  723.             botLimpa.addActionListener(new ActionListener() {
  724.                 @Override
  725.                 public void actionPerformed(ActionEvent e) {
  726.                         username.setText("");
  727.                         password.setText("");
  728.                 }
  729.             });
  730.  
  731.  
  732.         botSai = new JButton ("Sai");
  733.             this.add(botSai);
  734.             botSai.addActionListener(new ActionListener() {
  735.                 @Override
  736.                 public void actionPerformed(ActionEvent e) {
  737.                     System.exit(0);
  738.             }
  739.             });            
  740.  
  741.  
  742.         botRetroceder = new JButton ("Retroceder");
  743.             this.add(botRetroceder);
  744.             botRetroceder.addActionListener(new ActionListener() {
  745.                 @Override
  746.                 public void actionPerformed(ActionEvent e) {
  747.                     MenuInicial m = new MenuInicial(c);
  748.                     dispose();
  749.             }
  750.             });
  751.  
  752.  
  753.         JFrame self = this;
  754.  
  755.         String [] opcao = new String []{"Mostrar Password","Esconder Password"};
  756.        // JComboBox<String> oplist = new JComboBox<>(opcao);
  757.  
  758.  
  759.         showpass = new JComboBox (opcao);
  760.         showpass.addActionListener(new ActionListener(){
  761.             @Override
  762.             public void actionPerformed(ActionEvent event) {
  763.                 JComboBox <String> oplist = (JComboBox<String>)event.getSource();
  764.                 String opcao = (String) oplist.getSelectedItem();
  765.  
  766.                 if (opcao.equals("Mostrar Password")){
  767.                     password.setEchoChar((char)0);
  768.                     confirmapass.setEchoChar((char)0);
  769.                 }
  770.                 else{
  771.                     password.setEchoChar('•');
  772.                     confirmapass.setEchoChar('•');
  773.                 }
  774.  
  775.  
  776.  
  777.             }
  778.         });
  779.         this.add(showpass);
  780.  
  781.         this.pack();
  782.         }
  783.     }
  784.  
  785. ///////////////////////////////////////LOGIN//////////////////////////////////////////////
  786.  
  787. class Login extends JFrame{
  788.    
  789.     private final JTextField username;
  790.     //private final JTextField password;
  791.    
  792.     private final JPasswordField password;
  793.    
  794.     private final JComboBox showpass;
  795.    
  796.     private final JLabel label1;
  797.     private final JLabel label2;
  798.    
  799.     private final JButton botEntrar; //Para fazer login
  800.     private final JButton botLimpa; //Decidir se limpa tudo ou apenas um dos campos
  801.     private final JButton botSai;
  802.     private final JButton botRetroceder;
  803.  
  804.     public Login(Convivio c) {
  805.         this.setPreferredSize (new Dimension(600,450));
  806.         this.setTitle("Login");
  807.         this.setLocation (600,300); //Mais ou menos centrado
  808.         this.setVisible(true);
  809.         this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  810.         //this.setLayout(new FlowLayout());
  811.  
  812.  
  813.         //final JPanel grid = new JPanel(new FlowLayout());    
  814.         this.setLayout (new GridLayout (7,2,15,15));
  815.         //this.add(grid);
  816.  
  817.  
  818.         label1 = new JLabel ("Username");
  819.         this.add (label1);
  820.         username = new JTextField();
  821.         this.add(username);
  822.         username.setPreferredSize (new Dimension(100,20));
  823.         username.addActionListener (new ActionListener() {
  824.             @Override
  825.             public void actionPerformed(ActionEvent e) {
  826.                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  827.             }
  828.         }); //Mudar o método abstrato
  829.  
  830.  
  831.         label2 = new JLabel ("Password");
  832.         this.add (label2);
  833.         password = new JPasswordField(); //cria novo objeto
  834.  
  835.         //password.setEchoChar((char)0);
  836.         //password = new JTextField();
  837.         this.add(password); //adiciona ao container  
  838.         password.setPreferredSize (new Dimension(100,20));
  839.         password.addActionListener (new ActionListener() {
  840.             @Override
  841.             public void actionPerformed(ActionEvent e) {              
  842.  
  843.                 password.setToolTipText("A password tem que ter pelo menos 5 carateres");
  844.  
  845.             }
  846.         }); //Mudar o método abstrato    
  847.  
  848.         //char[] pass = password.getPassword();
  849.         //String pass = new String(password.getPassword());
  850.  
  851.         botEntrar = new JButton("Entrar");
  852.         this.add(botEntrar);
  853.         botEntrar.addActionListener(new ActionListener() {
  854.             @Override
  855.             public void actionPerformed(ActionEvent e) {
  856.                 String pass = "";
  857.                
  858.                 List<Inscrito> inscritos = new ArrayList<>();
  859.                 try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  860.                     inscritos = (ArrayList) oin.readObject();
  861.                 }catch(IOException | ClassNotFoundException n){
  862.                     System.out.println(n.getMessage());
  863.                 }
  864.                
  865.                 username.getText();
  866.                 password.getPassword();
  867.                
  868.                 char [] pass_char = password.getPassword();
  869.                 for (int i = 0; i < pass_char.length ; i++){
  870.                     pass += pass_char[i];
  871.                 }
  872.                
  873.                 String nome = null;
  874.                 int idade = 0;
  875.                 String username1 = null;
  876.                 String password1 = null;
  877.                 String perfil = null;
  878.                 List<Locais> locais = new ArrayList<>();
  879.                
  880.                
  881.                 int n = 0;
  882.                 for (Inscrito i : inscritos){
  883.                     if(i.username.indexOf(username.getText()) >= 0 && i.password.indexOf(pass) >= 0){
  884.                         n = 1;
  885.                         nome = i.nome;
  886.                         idade = i.idade;
  887.                         username1 = i.username;
  888.                         password1 = i.password;
  889.                         perfil = i.perfil;
  890.                         locais = i.locais;
  891.                     }
  892.                 }        
  893.  
  894.                 if(n == 1){
  895.                    
  896.                     dispose();
  897.                     Inscrito i = new Inscrito(perfil,nome,idade,username1,password1,locais);
  898.                     MenuEscolha escolhe = new MenuEscolha(i,c);
  899.                 } else {
  900.                     JOptionPane.showMessageDialog(rootPane, "Password ou username errados!", "Erro", JOptionPane.ERROR_MESSAGE);
  901.                     dispose();
  902.                     Login l = new Login(c);
  903.                     }
  904.  
  905.  
  906.             }
  907.         });
  908.         //Dentro do metodo abstrato, verificar se o username esta nos registados
  909.         //e se a password corresponde a password desse username
  910.         //Se sim, login successful Se nao, erro
  911.  
  912.  
  913.         botLimpa = new JButton("Limpa");
  914.             this.add(botLimpa);
  915.             botLimpa.addActionListener(new ActionListener() {
  916.                 @Override
  917.                 public void actionPerformed(ActionEvent e) {
  918.                         username.setText("");
  919.                         password.setText("");
  920.                 }
  921.             });
  922.  
  923.         botSai = new JButton ("Sai");
  924.             this.add(botSai);
  925.             botSai.addActionListener(new ActionListener() {
  926.                 @Override
  927.                 public void actionPerformed(ActionEvent e) {
  928.                     System.exit(0);
  929.             }
  930.             });
  931.  
  932.         botRetroceder = new JButton ("Retroceder");
  933.             this.add(botRetroceder);
  934.             botRetroceder.addActionListener(new ActionListener() {
  935.                 @Override
  936.                 public void actionPerformed(ActionEvent e) {
  937.                     MenuInicial m = new MenuInicial(c);
  938.                     //this.dispose();  //Remove JFrame 1
  939.                     //JFrame2.setVisible(true) //Show other frame
  940.                     dispose();
  941.                 }
  942.             });
  943.  
  944.  
  945.         JFrame self = this;
  946.  
  947.          String [] opcao = new String []{"Mostrar Password","Esconder Password"};
  948.        // JComboBox<String> oplist = new JComboBox<>(opcao);
  949.  
  950.         showpass = new JComboBox (opcao);
  951.         showpass.addActionListener(new ActionListener(){
  952.             @Override
  953.             public void actionPerformed(ActionEvent event) {
  954.                 JComboBox <String> oplist = (JComboBox<String>)event.getSource();
  955.                 String opcao = (String) oplist.getSelectedItem();
  956.  
  957.                 if (opcao.equals("Mostrar Password")){
  958.                     password.setEchoChar((char)0);
  959.                 }
  960.                 else{
  961.                     password.setEchoChar('•');
  962.                 }
  963.             }
  964.         });
  965.         this.add(showpass);
  966.  
  967.         this.pack();
  968.         } //Ja feito em principio
  969. }
  970.  
  971. //////////////////////////////////MENU ESCOLHA///////////////////////////////////////////
  972.  
  973. class MenuEscolha extends JFrame{
  974.        
  975.         private final JButton botMostraLocais;
  976.         private final JButton botShowGuest;
  977.         private final JButton botReceitaMin;
  978.         private final JButton botRetroceder;
  979.        
  980.         public MenuEscolha(Inscrito i, Convivio c){
  981.             this.setPreferredSize (new Dimension(500,400));
  982.             this.setTitle("Menu Escolha");
  983.             this.setLocation (600,300); //Mais ou menos centrado
  984.             this.setVisible(true);
  985.             this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  986.             this.setLayout(new GridLayout (5,1,30,30));
  987.        
  988.             botMostraLocais = new JButton ("Mostra Locais");
  989.             this.add(botMostraLocais);
  990.             botMostraLocais.addActionListener(new ActionListener() {
  991.             @Override
  992.             public void actionPerformed(ActionEvent e) {
  993.                 MostraLocais m = new MostraLocais(i,c);
  994.                 dispose();
  995.             }
  996.         });  
  997.            
  998.             botShowGuest = new JButton ("Mostra Guest Lists");
  999.             this.add(botShowGuest);
  1000.            
  1001.             botReceitaMin = new JButton ("Receita Minima Prevista");
  1002.             this.add(botReceitaMin);
  1003.            
  1004.             botRetroceder = new JButton ("Retroceder");
  1005.             this.add(botRetroceder);
  1006.             botRetroceder.addActionListener(new ActionListener() {
  1007.             @Override
  1008.             public void actionPerformed(ActionEvent e) {
  1009.                 Login Interface = new Login(c);
  1010.                 dispose();
  1011.             }
  1012.         });  
  1013.            
  1014.         JFrame self = this;
  1015.    
  1016.         this.pack();
  1017.         }      
  1018.     }
  1019.  
  1020. ///////////////////////////////////////MOSTRA LOCAIS//////////////////////////////////////////////
  1021.  
  1022. class MostraLocais extends JFrame{
  1023.    
  1024.     private final JTextArea box;
  1025.    
  1026.     private final JButton botEscolhe;
  1027.     private final JButton botRetroceder;
  1028.    
  1029.     private static final int SPACE = 3;
  1030.    
  1031.    
  1032.     public MostraLocais(Inscrito i,Convivio c){
  1033.  
  1034.         this.setTitle("Menu");
  1035.         this.setPreferredSize (new Dimension(600, 450));
  1036.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  1037.         this.setLocation(600,300);
  1038.         this.setVisible(true);
  1039.         //this.setLayout(new GridLayout (4,2,60,60));
  1040.        
  1041.         box = new JTextArea (20,20);        
  1042.         box.setEditable(false);
  1043.         this.add(box);      
  1044.        
  1045.         botEscolhe = new JButton ("Escolher Locais");
  1046.         this.add(botEscolhe);
  1047.         botEscolhe.addActionListener(new ActionListener() {
  1048.             @Override
  1049.             public void actionPerformed(ActionEvent e) {
  1050.                 EscolheLocais esc = new EscolheLocais(i);
  1051.                 dispose();
  1052.             }
  1053.         });  
  1054.        
  1055.         botRetroceder = new JButton ("Retroceder");
  1056.             this.add(botRetroceder);
  1057.             botRetroceder.addActionListener(new ActionListener() {
  1058.             @Override
  1059.             public void actionPerformed(ActionEvent e) {
  1060.                 MenuEscolha s = new MenuEscolha(i,c);
  1061.                 dispose();
  1062.             }
  1063.         });  
  1064.            
  1065.            
  1066.         //JPanel
  1067.         JPanel textAreaGrid = new JPanel(new GridLayout(1, 0, SPACE, SPACE));
  1068.         textAreaGrid.add(new JScrollPane(box));
  1069.        
  1070.         JPanel buttonPanel = new JPanel(new GridLayout(2, 0, SPACE, SPACE));
  1071.         buttonPanel.add(botEscolhe);
  1072.         buttonPanel.add(botRetroceder);
  1073.        
  1074.         getRootPane().setBorder(BorderFactory.createEmptyBorder(SPACE, SPACE, SPACE, SPACE));
  1075.         setLayout(new BorderLayout(SPACE, SPACE));
  1076.         add(textAreaGrid, BorderLayout.CENTER);
  1077.         add(buttonPanel, BorderLayout.PAGE_END);
  1078.        
  1079.         box.add(new JScrollPane(new JTextArea(50, 50)), BorderLayout.PAGE_START);
  1080.         JScrollPane scrollPane = new JScrollPane (box);
  1081.         JScrollPane scroll = new JScrollPane (box,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  1082.         this.add(scroll);
  1083.         this.setVisible (true);
  1084.        
  1085.        
  1086.         List<Locais> locais = new ArrayList<>();
  1087.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Locais_obj.txt"))){
  1088.             locais = (ArrayList) oin.readObject();
  1089.         }catch(IOException | ClassNotFoundException e){
  1090.             System.out.println(e.getMessage());
  1091.         }
  1092.      
  1093.         for (Locais l : locais){
  1094.             box.append(l.nome);
  1095.             box.append("\n\n");
  1096.         }
  1097.        
  1098.         this.pack();
  1099.     }
  1100. }
  1101.  
  1102. //////////////////////////////////////ESCOLHA LOCAIS//////////////////////////////////////////
  1103.    
  1104. class EscolheLocais extends JFrame{
  1105.    
  1106.     private final JComboBox local1;
  1107.     private final JComboBox local2;
  1108.     private final JComboBox local3;
  1109.     private final JComboBox local4;
  1110.     private final JComboBox local5;
  1111.  
  1112.     private final JLabel label1;
  1113.     private final JLabel label2;    
  1114.     private final JLabel label3;
  1115.     private final JLabel label4;
  1116.     private final JLabel label5;
  1117.    
  1118.     private final JButton botRetroceder;
  1119.     private final JButton botGuardar;
  1120.    
  1121.    
  1122.     public EscolheLocais(Inscrito i,Convivio c){
  1123.         this.setPreferredSize (new Dimension(600,450));
  1124.         this.setTitle("Escolhe Locais");
  1125.         this.setLocation (600,300); //Mais ou menos centrado
  1126.         this.setVisible(true);
  1127.         this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  1128.         this.setLayout(new GridLayout (7,2,20,20));
  1129.        
  1130.         List<Locais> locais = new ArrayList<>();
  1131.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Locais_obj.txt"))){
  1132.             locais = (ArrayList) oin.readObject();
  1133.         }catch(IOException | ClassNotFoundException e){
  1134.             System.out.println(e.getMessage());
  1135.         }
  1136.        
  1137.                
  1138.         label1 = new JLabel ("Local 1");
  1139.         this.add (label1);
  1140.         local1 = new JComboBox ();
  1141.         for (Locais l : locais){
  1142.             local1.addItem(l.nome);
  1143.         }
  1144.         this.add(local1);
  1145.      
  1146.         label2 = new JLabel ("Local 2");
  1147.         this.add (label2);
  1148.         local2 = new JComboBox ();
  1149.         for (Locais l : locais){
  1150.             local2.addItem(l.nome);
  1151.         }
  1152.         this.add(local2);
  1153.        
  1154.         label3 = new JLabel ("Local 3");
  1155.         this.add (label3);
  1156.         local3 = new JComboBox ();
  1157.         for (Locais l : locais){
  1158.             local3.addItem(l.nome);
  1159.         }
  1160.         this.add(local3);
  1161.        
  1162.         label4 = new JLabel ("Local 4");
  1163.         this.add (label4);
  1164.         local4 = new JComboBox ();
  1165.         for (Locais l : locais){
  1166.             local4.addItem(l.nome);
  1167.         }
  1168.         this.add(local4);
  1169.        
  1170.         label5 = new JLabel ("Local 5");
  1171.         this.add (label5);
  1172.         local5 = new JComboBox ();
  1173.         for (Locais l : locais){
  1174.             local5.addItem(l.nome);
  1175.         }
  1176.         this.add(local5);
  1177.        
  1178.        
  1179.         botGuardar = new JButton ("Guardar");
  1180.         this.add(botGuardar);
  1181.         botGuardar.addActionListener(new ActionListener() {
  1182.             @Override
  1183.             public void actionPerformed(ActionEvent e) {
  1184.  
  1185.             }
  1186.         });
  1187.        
  1188.        
  1189.         botRetroceder = new JButton ("Retroceder");
  1190.         this.add(botRetroceder);
  1191.         botRetroceder.addActionListener(new ActionListener() {
  1192.             @Override
  1193.             public void actionPerformed(ActionEvent e) {
  1194.                 MostraLocais m = new MostraLocais(i,c);
  1195.                 dispose();
  1196.             }
  1197.         });  
  1198.        
  1199.        
  1200.         this.pack();
  1201.     }
  1202. }
  1203.  
  1204.  
  1205. /*--------------------------------------MAIN----------------------------------------------*/
  1206.  
  1207. public class projeto_racunho {
  1208.    
  1209.     public static void main(String[] args) {
  1210.        
  1211.         /*INICIALIZAR*/
  1212.         Convivio c = new Convivio("Mega Convivio");
  1213.        
  1214.         Ficheiros_Elementos_dei a = new Ficheiros_Elementos_dei();
  1215.         a.ler_ficheiro_Elementos_dei();
  1216.         Ficheiros_Locais l = new Ficheiros_Locais();
  1217.         l.ler_ficheiro_locais();
  1218.        
  1219.         /*INTERFACE*/
  1220.         MenuInicial m = new MenuInicial(c);
  1221.        
  1222.         List<Inscrito> inscritos = new ArrayList<>();
  1223.         try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Inscritos_obj.txt"))){
  1224.             inscritos = (ArrayList) oin.readObject();
  1225.         }catch(IOException | ClassNotFoundException n){
  1226.             System.out.println(n.getMessage());
  1227.         }
  1228.      
  1229.         for (Inscrito i : inscritos){
  1230.             i.print();
  1231.         }
  1232.     }  
  1233.    
  1234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement