Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package projeto_rascunho;
- import java.util.Random;
- import java.util.Scanner;
- import java.lang.Math;
- import java.util.StringTokenizer;
- import java.util.List;
- import java.util.ListIterator;
- import java.util.ArrayList;
- import java.io.*;
- import java.lang.Math;
- import java.util.StringTokenizer;
- //Para a interface
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowEvent;
- import java.util.Arrays;
- import javax.swing.JFrame;
- import javax.swing.JPasswordField;//Esconder password
- import javax.swing.JFileChooser; //Escolher ficheiro
- import javax.swing.*;
- /*----------------------------CONVIVIO NO DEI----------------------------------------*/
- class Convivio{
- public String nome_convivio;
- //public List<Locais> locais = new ArrayList<>();
- //public List<Elemento_dei> registados = new ArrayList<>();
- public Convivio(String nome_convivio){
- this.nome_convivio = nome_convivio;
- }
- public void login(){
- } //Pedir username e password
- public void inscricao_convivio(){ //Login e registar // ao registar verificar se ja existe o username
- } //Tem que pedir sempre o nome para verificar se pertence ao DEI //Verificar sempre se ha username igual
- public void show_locais(){ //Mostrar sempre depois da inscricao, ver numero atual de inscritos de cada um e a lotacao (se aplicavel)
- List<Locais> locais = new ArrayList<>();
- try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Locais_obj.txt"))){
- locais = (ArrayList) oin.readObject();
- }catch(IOException | ClassNotFoundException e){
- System.out.println(e.getMessage());
- }
- for (Locais l : locais){
- l.print();
- }
- }
- public void escolha_locais(){ //ate cinco
- } //Na interface so ter 5 espacos //Ter um ficheiro objetos dos inscritos em cada local (secalhar)
- public void show_guests(){ //Nao sei se mete aqui ou se se mete na class locais
- } //Ficheiro objetos para cada guest de cada bar
- /*public double receita(){ //Nao interessa o que esta ca dentro por enquanto
- List<Locais> locais = new ArrayList<>();
- double receita = 0;
- for (Locais l: locais){
- double receita_local = 0;
- receita += l.receita;
- }
- return receita;
- }*/
- }
- /*----------------------------LOCAIS CONVIVIO-----------------------------------------*/
- class Locais implements Serializable{
- private String coord_gps;
- public String nome;
- public List<Inscrito> inscritos = new ArrayList<>();
- public Locais(String coord_gps,String nome){
- this.coord_gps = coord_gps;
- this.nome = nome;
- }
- public void print(){
- System.out.printf("%s\t%s\n",nome,coord_gps);
- }
- }
- abstract class Parques extends Locais{
- public Parques(String coord_gps, String nome) {
- super(coord_gps, nome);
- }
- }
- class Jardins extends Parques{
- private int area; //em metros quadrados
- public Jardins(String coord_gps, String nome,int area) {
- super(coord_gps, nome);
- this.area = area;
- }
- }
- class Areas_desportivas extends Parques{
- private String desportos[];
- public Areas_desportivas(String coord_gps, String nome,String desportos[]) {
- super(coord_gps, nome);
- this.desportos = desportos;
- }
- @Override
- public void print(){
- System.out.printf("%s\n",nome);
- for (int i = 0; i < desportos.length; i++){
- System.out.printf("%s", desportos[i]);
- }
- }
- }
- class Exposicoes extends Locais{
- public String forma_artistica;
- public int custo_ingresso;
- public Exposicoes(String coord_gps,String nome,String forma_artistica, int custo_ingresso) {
- super(coord_gps, nome);
- this.custo_ingresso = custo_ingresso;
- this.forma_artistica = forma_artistica;
- }
- }
- class Bares extends Locais{
- private int lotacao;
- private int consumo_minimo; //Por pessoa
- public List<Elemento_dei> guest = new ArrayList<>();
- public Bares(String coord_gps,String nome, int lotacao, int consumo_minimo) {
- super(coord_gps, nome);
- this.lotacao = lotacao;
- this.consumo_minimo = consumo_minimo;
- }
- public void add_guest(){ //Fazer, meter num array e meter num ficheiro de obj
- }
- }
- /*----------------------------ELEMENTOS DEI-----------------------------------------*/
- class Elemento_dei implements Serializable{
- public String perfil;
- public String nome;
- public int idade;
- public Elemento_dei(String perfil, String nome,int idade){
- this.perfil = perfil;
- this.nome = nome;
- this.idade = idade;
- }
- public void print(){
- System.out.printf("%s\t%d\t%s\n", nome,idade,perfil);
- }
- }
- class Inscrito extends Elemento_dei{ //NOVO
- public String username;
- public String password;
- public Inscrito(String perfil, String nome, int idade, String username,String password) {
- super(perfil, nome, idade);
- this.username = username;
- this.password = password;
- }
- }
- class Estudante extends Elemento_dei{
- private String curso;
- public Estudante(String perfil, String nome,int idade,String curso) {
- super(perfil, nome, idade);
- this.curso = curso;
- }
- @Override
- public String toString(){
- return nome + perfil + idade;
- }
- }
- class Professor extends Elemento_dei{
- private String tipo_prof;
- public Professor(String perfil, String nome, int idade, String tipo_prof) {
- super(perfil, nome, idade);
- this.tipo_prof = tipo_prof;
- }
- @Override
- public String toString(){
- return nome;
- }
- }
- class Funcionario extends Elemento_dei{
- private String tipo_func;
- public Funcionario(String perfil, String nome, int idade, String tipo_func) {
- super(perfil, nome, idade);
- this.tipo_func = tipo_func;
- }
- @Override
- public String toString(){
- return nome;
- }
- }
- /*---------------------------------FICHEIROS-----------------------------------------*/
- class Ficheiros_Locais{
- public void ler_ficheiro_locais(){
- List<Locais> locais = new ArrayList<>();
- try{
- BufferedReader reader = new BufferedReader(new FileReader("Locais.txt"));
- String line;
- while ((line = reader.readLine()) != null){
- String nome;
- int receita;
- String coordenadas;
- String tipo_local;
- String[] info = line.split("\t");
- nome = info[0];
- coordenadas = info[1];
- tipo_local = info[2];
- if(tipo_local.indexOf("parque") >= 0){
- String tipo_parque = info[3];
- if(tipo_parque.indexOf("jardim") >= 0){
- int area = Integer.valueOf(info[4].trim());
- Locais l = new Jardins(coordenadas,nome,area);
- locais.add(l);
- }
- else if(tipo_parque.indexOf("desportivo") >= 0){
- String desportos[] = new String[info.length - 4];
- int n = 0;
- for (int i = 4; i < info.length ; i++){ //Ver como se faz para meter varios desportos
- desportos[n] = info[i];
- n++;
- }
- Locais l = new Areas_desportivas(coordenadas,nome,desportos);
- locais.add(l);
- }
- }
- else if(tipo_local.indexOf("bar") >= 0){
- int lotacao = Integer.valueOf(info[3].trim());
- int consumo_minimo = Integer.valueOf(info[4].trim());
- Locais l = new Bares(coordenadas,nome,lotacao,consumo_minimo);
- locais.add(l);
- }
- else if(tipo_local.indexOf("exposicao") >= 0){
- int custo_ingresso = Integer.valueOf(info[3].trim());
- String forma_artistica = info[4];
- Locais l = new Exposicoes(coordenadas,nome,forma_artistica,custo_ingresso);
- locais.add(l);
- }
- }
- }catch(IOException e){
- System.out.print("ERRO");
- System.out.printf("Ocorreu a exceçao %s ao ler o ficheiro\n", e);
- }
- escreve_ficheiro_dat_locais(locais);
- }
- public void escreve_ficheiro_dat_locais(List<Locais> locais){
- try{
- ObjectOutputStream oS = new ObjectOutputStream(new FileOutputStream("Locais_obj.txt"));
- oS.writeObject(locais);
- oS.close();
- }catch(IOException e){
- System.out.print("ERRO");
- System.out.printf("Ocorreu a exceçao %s ao escrever no ficheiro\n", e);
- }
- }
- }
- class Ficheiros_Elementos_dei{
- public void ler_ficheiro_Elementos_dei(){
- List<Elemento_dei> elementos_dei = new ArrayList<>(); //Ver se meter isto aqui esta correto
- try{
- BufferedReader reader = new BufferedReader(new FileReader("Elementos_DEI.txt"));
- String line;
- while ((line = reader.readLine()) != null){
- String nome;
- String perfil;
- int idade;
- String profissao;
- String[] info = line.split("\t");
- nome = info[0];
- perfil = info[1];
- idade = Integer.valueOf(info[2].trim());
- profissao = info[3];
- if(profissao.indexOf("aluno") >= 0){
- String curso = info[4];
- Elemento_dei e = new Estudante(perfil,nome,idade,curso);
- elementos_dei.add(e);
- }
- else if(profissao.indexOf("professor") >= 0){
- String tipo_prof = info[4];
- Elemento_dei e = new Professor(perfil, nome,idade,tipo_prof);
- elementos_dei.add(e);
- }
- else if(profissao.indexOf("funcionario") >= 0){
- String tipo_func = info[4];
- Elemento_dei e = new Funcionario(perfil,nome,idade, tipo_func);
- elementos_dei.add(e);
- }
- else{
- System.out.print("ERRO\n");
- }
- }
- }
- catch(IOException e){
- System.out.printf("Ocorreu a exceçao %s ao ler o ficheiro\n", e);
- }
- escreve_ficheiro_dat_pessoas(elementos_dei);
- }
- public void escreve_ficheiro_dat_pessoas(List<Elemento_dei> inscritos){
- try{
- ObjectOutputStream oS = new ObjectOutputStream(new FileOutputStream("Pessoas_obj.txt"));
- oS.writeObject(inscritos);
- oS.close();
- }catch(IOException e){
- System.out.printf("Ocorreu a exceçao %s ao escrever mo ficheiro\n", e);
- }
- } //Ver como fazer para ir atualizando
- }
- /*----------------------------------------------------INTERFACE-------------------------------------------------------*/
- ////////////////////////////////////////////MENU////////////////////////////////////////////
- class MenuInicial extends JFrame{ //O que por no registar? (opcoes)
- private final JLabel welcome; //Texto a dar as boas vindas
- private final JButton botLogin;
- private final JButton botRegistar;
- private final JButton botSair;
- public MenuInicial(){
- this.setPreferredSize (new Dimension(450,300));
- this.setTitle("Menu Inicial");
- this.setLocation (600,300); //Mais ou menos centrado
- this.setVisible(true);
- this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- this.setLayout (new GridLayout (4,2,20,20));
- welcome = new JLabel (" Bem vindo ao Mega Convivio do DEI");
- this.add (welcome);
- botLogin = new JButton("Login");
- this.add(botLogin);
- botLogin.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Login Interface = new Login();
- dispose();
- }
- });
- botRegistar = new JButton("Registar");
- this.add(botRegistar);
- botRegistar.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Registar r = new Registar();
- dispose();
- }
- });
- botSair = new JButton("Sair");
- this.add(botSair);
- botSair.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- this.pack();
- }
- }
- ///////////////////////////////////////////REGISTAR////////////////////////////////////
- class Registar extends JFrame{
- private final JTextField nome;
- private final JTextField username;
- private final JPasswordField password;
- private final JPasswordField confirmapass;
- private final JComboBox showpass;
- private final JLabel label0;
- private final JLabel label1;
- private final JLabel label2;
- private final JLabel label3;
- private final JButton botEntrar; //Para fazer login
- private final JButton botLimpa; //Decidir se limpa tudo ou apenas um dos campos
- private final JButton botSai;
- private final JButton botRetroceder;
- public Registar() {
- this.setPreferredSize (new Dimension(600,450));
- this.setTitle("Registar");
- this.setLocation (600,300); //Mais ou menos centrado
- this.setVisible(true);
- this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- //this.setLayout(new FlowLayout());
- //final JPanel grid = new JPanel(new FlowLayout());
- this.setLayout (new GridLayout (7,2,15,15));
- //this.add(grid);
- label0 = new JLabel ("Nome");
- this.add (label0);
- nome = new JTextField();
- this.add(nome);
- nome.setPreferredSize (new Dimension(100,20));
- nome.addActionListener (new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }); //Mudar o método abstrato
- label1 = new JLabel ("Escolher Username");
- this.add (label1);
- username = new JTextField();
- this.add(username);
- username.setPreferredSize (new Dimension(100,20));
- username.addActionListener (new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }); //Mudar o método abstrato
- label2 = new JLabel ("Escolher Password");
- this.add (label2);
- password = new JPasswordField(); //cria novo objeto
- this.add(password); //adiciona ao container
- password.setPreferredSize (new Dimension(100,20));
- password.addActionListener (new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- password.setToolTipText("A password tem que ter pelo menos 5 carateres");
- }
- }); //Mudar o método abstrato
- label3 = new JLabel ("Confirmar Password");
- this.add (label3);
- confirmapass = new JPasswordField();
- this.add(confirmapass);
- confirmapass.setPreferredSize (new Dimension(100,20));
- confirmapass.addActionListener (new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if ((password.getPassword()).equals(confirmapass)){
- }
- else{
- System.out.println("As passwords tem que ser iguais");
- }
- }
- }); //Mudar o método abstrato
- //SHOW PASSWORDDDDDDDDDDDDDDDD
- //char[] pass = password.getPassword();
- //String pass = new String(password.getPassword());
- char[] correctpass = {'b','a','t','a','t','a'};
- //String correctpass = "batata";
- botEntrar = new JButton("Registar");
- this.add(botEntrar);
- botEntrar.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- username.getText();
- password.getPassword();
- confirmapass.getPassword();
- if (Arrays.equals(password.getPassword(),confirmapass.getPassword())) {
- System.out.println("Registo bem sucedido");
- Login escolhe = new Login();
- dispose();
- } else {
- JOptionPane.showMessageDialog(rootPane, "As passwords têm que ser iguais", "Erro", JOptionPane.ERROR_MESSAGE);
- }
- }
- });
- //Dentro do metodo abstrato, verificar se o username esta nos registados
- //e se a password corresponde a password desse username
- //Se sim, login successful Se nao, erro
- botLimpa = new JButton("Limpa");
- this.add(botLimpa);
- botLimpa.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- username.setText("");
- password.setText("");
- }
- });
- botSai = new JButton ("Sai");
- this.add(botSai);
- botSai.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- botRetroceder = new JButton ("Retroceder");
- this.add(botRetroceder);
- botRetroceder.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- MenuInicial m = new MenuInicial();
- dispose();
- }
- });
- JFrame self = this;
- String [] opcao = new String []{"Mostrar Password","Esconder Password"};
- // JComboBox<String> oplist = new JComboBox<>(opcao);
- showpass = new JComboBox (opcao);
- showpass.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent event) {
- JComboBox <String> oplist = (JComboBox<String>)event.getSource();
- String opcao = (String) oplist.getSelectedItem();
- if (opcao.equals("Mostrar Password")){
- password.setEchoChar((char)0);
- confirmapass.setEchoChar((char)0);
- }
- else{
- password.setEchoChar('•');
- confirmapass.setEchoChar('•');
- }
- }
- });
- this.add(showpass);
- this.pack();
- }
- }
- ///////////////////////////////////////LOGIN//////////////////////////////////////////////
- class Login extends JFrame{
- private final JTextField username;
- //private final JTextField password;
- private final JPasswordField password;
- private final JComboBox showpass;
- private final JLabel label1;
- private final JLabel label2;
- private final JButton botEntrar; //Para fazer login
- private final JButton botLimpa; //Decidir se limpa tudo ou apenas um dos campos
- private final JButton botSai;
- private final JButton botRetroceder;
- public Login() {
- this.setPreferredSize (new Dimension(600,450));
- this.setTitle("Login");
- this.setLocation (600,300); //Mais ou menos centrado
- this.setVisible(true);
- this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- //this.setLayout(new FlowLayout());
- //final JPanel grid = new JPanel(new FlowLayout());
- this.setLayout (new GridLayout (7,2,15,15));
- //this.add(grid);
- label1 = new JLabel ("Username");
- this.add (label1);
- username = new JTextField();
- this.add(username);
- username.setPreferredSize (new Dimension(100,20));
- username.addActionListener (new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }); //Mudar o método abstrato
- label2 = new JLabel ("Password");
- this.add (label2);
- password = new JPasswordField(); //cria novo objeto
- //password.setEchoChar((char)0);
- //password = new JTextField();
- this.add(password); //adiciona ao container
- password.setPreferredSize (new Dimension(100,20));
- password.addActionListener (new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- password.setToolTipText("A password tem que ter pelo menos 5 carateres");
- }
- }); //Mudar o método abstrato
- //char[] pass = password.getPassword();
- //String pass = new String(password.getPassword());
- char[] correctpass = {'b','a','t','a','t','a'};
- //String correctpass = "batata";
- botEntrar = new JButton("Entrar");
- this.add(botEntrar);
- botEntrar.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- username.getText();
- password.getPassword();
- if(Arrays.equals(password.getPassword(), correctpass)){
- System.out.println("Password correta");
- dispose();
- MenuEscolha escolhe = new MenuEscolha();
- } else {
- System.out.println ("Password incorreta");
- }
- }
- });
- //Dentro do metodo abstrato, verificar se o username esta nos registados
- //e se a password corresponde a password desse username
- //Se sim, login successful Se nao, erro
- botLimpa = new JButton("Limpa");
- this.add(botLimpa);
- botLimpa.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- username.setText("");
- password.setText("");
- }
- });
- botSai = new JButton ("Sai");
- this.add(botSai);
- botSai.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- botRetroceder = new JButton ("Retroceder");
- this.add(botRetroceder);
- botRetroceder.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- MenuInicial m = new MenuInicial();
- //this.dispose(); //Remove JFrame 1
- //JFrame2.setVisible(true) //Show other frame
- dispose();
- }
- });
- JFrame self = this;
- String [] opcao = new String []{"Mostrar Password","Esconder Password"};
- // JComboBox<String> oplist = new JComboBox<>(opcao);
- showpass = new JComboBox (opcao);
- showpass.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent event) {
- JComboBox <String> oplist = (JComboBox<String>)event.getSource();
- String opcao = (String) oplist.getSelectedItem();
- if (opcao.equals("Mostrar Password")){
- password.setEchoChar((char)0);
- }
- else{
- password.setEchoChar('•');
- }
- }
- });
- this.add(showpass);
- this.pack();
- }
- }
- //////////////////////////////////MOSTRAR LOCAIS///////////////////////////////////////////
- class MenuEscolha extends JFrame{
- private final JButton botEscolheLocal;
- private final JButton botShowGuest;
- private final JButton botReceitaMin;
- private final JButton botRetroceder;
- public MenuEscolha(){
- this.setPreferredSize (new Dimension(500,400));
- this.setTitle("Menu Escolha");
- this.setLocation (600,300); //Mais ou menos centrado
- this.setVisible(true);
- this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- this.setLayout(new GridLayout (5,1,30,30));
- botEscolheLocal = new JButton ("Mostra locais");
- this.add(botEscolheLocal);
- botShowGuest = new JButton ("Mostra Guest Lists");
- this.add(botShowGuest);
- botReceitaMin = new JButton ("Receita Minima Prevista");
- this.add(botReceitaMin);
- botRetroceder = new JButton ("Retroceder");
- this.add(botRetroceder);
- botRetroceder.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Login Interface = new Login();
- dispose();
- }
- });
- JFrame self = this;
- this.pack();
- }
- }
- ///////////////////////////////////////MOSTRA LOCAIS//////////////////////////////////////////////
- class MostraLocais{
- private final JLabel label1;
- public MostraLocais(){
- List<Locais> locais = new ArrayList<>();
- try(ObjectInputStream oin = new ObjectInputStream(new FileInputStream("Locais_obj.txt"))){
- locais = (ArrayList) oin.readObject();
- }catch(IOException | ClassNotFoundException e){
- System.out.println(e.getMessage());
- }
- }
- this.pack();
- }
- /*--------------------------------------MAIN----------------------------------------------*/
- public class projeto_rascunho {
- public static void main(String[] args) {
- /*INICIALIZAR*/
- Convivio c = new Convivio("Mega Convivio");
- Ficheiros_Elementos_dei a = new Ficheiros_Elementos_dei();
- a.ler_ficheiro_Elementos_dei();
- Ficheiros_Locais l = new Ficheiros_Locais();
- l.ler_ficheiro_locais();
- /*INTERFACE*/
- MenuInicial m = new MenuInicial();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement