Advertisement
Guest User

Untitled

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