Advertisement
Guest User

Untitled

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