From 342b1abd4a03c1a5afa5c96ce0b37a6b7665e4a4 Mon Sep 17 00:00:00 2001 From: Giuli1297 Date: Thu, 21 Oct 2021 23:12:30 -0300 Subject: [PATCH] Twitter pt1 --- twitter/src/twitter/BaseDeDatos.java | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ twitter/src/twitter/Dashboard.java | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ twitter/src/twitter/Reaccion.java | 25 +++++++++++++++++++++++++ twitter/src/twitter/Respuesta.java | 41 +++++++++++++++++++++++++++++++++++++++++ twitter/src/twitter/Seguimiento.java | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ twitter/src/twitter/TestTwitter.java | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ twitter/src/twitter/Tweet.java | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ twitter/src/twitter/Usuario.java | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 693 insertions(+) create mode 100644 twitter/src/twitter/BaseDeDatos.java create mode 100644 twitter/src/twitter/Dashboard.java create mode 100644 twitter/src/twitter/Reaccion.java create mode 100644 twitter/src/twitter/Respuesta.java create mode 100644 twitter/src/twitter/Seguimiento.java create mode 100644 twitter/src/twitter/TestTwitter.java create mode 100644 twitter/src/twitter/Tweet.java create mode 100644 twitter/src/twitter/Usuario.java diff --git a/twitter/src/twitter/BaseDeDatos.java b/twitter/src/twitter/BaseDeDatos.java new file mode 100644 index 0000000..db6b1ac --- /dev/null +++ b/twitter/src/twitter/BaseDeDatos.java @@ -0,0 +1,50 @@ +package twitter; + +import java.util.ArrayList; + +public class BaseDeDatos { + private static final ArrayList usuarios = new ArrayList(); + private static final ArrayList tweets = new ArrayList(); + + public static void addUsuario(Usuario usuario){ + /* + * Agrega un usuario si este no existe. + * */ + for(Usuario user: usuarios){ + if(user.getNombreUsuario().equals(usuario.getNombreUsuario())){ + System.out.println("ESTE NOMBRE DE USUARIO YA ESTA REGISTRADO"); + return; + } + } + usuarios.add(usuario); + } + + public static ArrayList getUsuarios(){ + /* + * Obtiene todos los usuarios + */ + return usuarios; + } + + public static Usuario getUsuarioByUsername(String username){ + /* + Obtiene un usuario por su username, si no existe retorna null + */ + for(Usuario user: usuarios){ + if(user.getNombreUsuario().equals(username)) return user; + } + return null; + } + + public static void addTweet(Tweet tweet){ + tweets.add(tweet); + } + + public static ArrayList getTweets(){ + return tweets; + } + + public static Tweet getTweetById(int id){ + return tweets.get(id); + } +} diff --git a/twitter/src/twitter/Dashboard.java b/twitter/src/twitter/Dashboard.java new file mode 100644 index 0000000..0edd4b2 --- /dev/null +++ b/twitter/src/twitter/Dashboard.java @@ -0,0 +1,51 @@ +package twitter; + +import java.util.ArrayList; + +public class Dashboard { + private Usuario usuario; + + public Dashboard(Usuario usuario) { + this.usuario = usuario; + } + + public void verUltimosTweets(int limit){ + ArrayList tweets = BaseDeDatos.getTweets(); + ArrayList usuarios = this.usuario.seguimiento.getSeguidos(); + int count = 0; + int index=0; + for(Tweet tweet: tweets){ + if(usuarios.contains(tweet.getUsuario()) || tweet.getUsuario()==this.usuario){ + System.out.print("\n---------------------"); + System.out.print("\n\t*id: "+index+"\n\tAutor: "+tweet.getUsuario().getNombreUsuario()+ + "\n\tTexto: "+tweet.getText()); + System.out.print("\n\tLikes: "+tweet.getNumeroFav()+"\tRts: "+tweet.getNumeroRt()); + if(tweet.getImageUrl()!=null){ + System.out.print("\n\tImage: "+tweet.getImageUrl()); + } + if(tweet.getLinkUrl()!=null){ + System.out.print("\n\tLink: "+tweet.getLinkUrl()); + } + if(!tweet.getRespuestas().isEmpty()){ + System.out.print("\n\tRespuestas: "); + for(Respuesta respuesta: tweet.getRespuestas()){ + System.out.print("\n\t\t\t*Usuario: "+respuesta.getUsuario().getNombreUsuario()); + System.out.print("\n\t\t\tText: "+respuesta.getText()); + if(respuesta.getImageUrl()!=null){ + System.out.print("\n\t\t\tImage: "+respuesta.getImageUrl()); + } + if(respuesta.getLinkUrl()!=null){ + System.out.print("\n\t\t\tLink: "+respuesta.getLinkUrl()); + } + } + } + count++; + if(count>limit){ + return; + } + } + System.out.println("\n\n"); + index++; + } + } +} diff --git a/twitter/src/twitter/Reaccion.java b/twitter/src/twitter/Reaccion.java new file mode 100644 index 0000000..a106429 --- /dev/null +++ b/twitter/src/twitter/Reaccion.java @@ -0,0 +1,25 @@ +package twitter; + +public class Reaccion { + private Usuario usuario; + private Tweet tweet; + private String tipoDeReaccion; + + public Reaccion(Usuario usuario, Tweet tweet, String tipoDeReaccion) { + this.usuario = usuario; + this.tweet = tweet; + this.tipoDeReaccion = tipoDeReaccion; + } + + public Usuario getUsuario() { + return usuario; + } + + public Tweet getTweet() { + return tweet; + } + + public String getTipoDeReaccion() { + return tipoDeReaccion; + } +} diff --git a/twitter/src/twitter/Respuesta.java b/twitter/src/twitter/Respuesta.java new file mode 100644 index 0000000..6d3b2f0 --- /dev/null +++ b/twitter/src/twitter/Respuesta.java @@ -0,0 +1,41 @@ +package twitter; + +import java.util.Scanner; + +public class Respuesta extends Tweet{ + private Tweet tweet; + + public Respuesta(Usuario usuario, String text, String imageUrl, String linkUrl, Tweet tweet){ + super(usuario, text, imageUrl, linkUrl); + this.tweet = tweet; + } + + public static Respuesta tweetear(Usuario user, Tweet tweet){ + Scanner in = new Scanner(System.in); + String text; + String imageUrl=null; + String linkUrl=null; + String choice; + try{ + System.out.println("Texto"); + System.out.print("-----> "); + text = in.nextLine(); + System.out.print("\nDesea agregar una imagen a la respuesta?(Y/N): "); + choice = (in.nextLine()).toLowerCase(); + if(choice.equals("y")){ + System.out.print("\nURL De la Imagen: "); + imageUrl = in.nextLine(); + } + System.out.print("\nDesea agregar un link a la respuesta?(Y/N): "); + choice = (in.nextLine()).toLowerCase(); + if(choice.equals("y")){ + System.out.print("\nURL: "); + linkUrl = in.nextLine(); + } + return new Respuesta(user, text, imageUrl, linkUrl, tweet); + }catch (Exception e){ + System.out.println("Error"); + } + return null; + } +} diff --git a/twitter/src/twitter/Seguimiento.java b/twitter/src/twitter/Seguimiento.java new file mode 100644 index 0000000..6a16a9d --- /dev/null +++ b/twitter/src/twitter/Seguimiento.java @@ -0,0 +1,116 @@ +package twitter; + +import java.util.ArrayList; +import java.util.Scanner; + +public class Seguimiento { + private Usuario usuario; + private ArrayList seguidos; + private ArrayList seguidores; + private ArrayList bloqueados; + + public Seguimiento(Usuario usuario) { + this.usuario = usuario; + this.seguidores = new ArrayList(); + this.seguidos = new ArrayList(); + this.bloqueados = new ArrayList(); + } + + public void seguir(){ + Scanner in = new Scanner(System.in); + try{ + System.out.print("\nIngrese el usuario a seguir: "); + Usuario usuario = BaseDeDatos.getUsuarioByUsername(in.nextLine()); + if(usuario!=null){ + this.seguidos.add(usuario); + usuario.seguimiento.addSeguidores(this.usuario); + return; + } + System.out.println("Usuario No Existente"); + }catch (Exception e){ + System.out.println("error"); + } + } + + public void dejarDeSeguir(){ + Scanner in = new Scanner(System.in); + try{ + System.out.print("\nIngrese el usuario a dejar de seguir: "); + Usuario usuario = BaseDeDatos.getUsuarioByUsername(in.nextLine()); + if(usuario!=null){ + this.seguidos.remove(usuario); + usuario.seguimiento.removeSeguidores(this.usuario); + return; + } + System.out.println("Usuario No Existente"); + }catch (Exception e){ + System.out.println("error"); + } + } + + public void bloquear(){ + Scanner in = new Scanner(System.in); + try{ + System.out.print("\nIngrese el usuario a bloquear: "); + Usuario usuario = BaseDeDatos.getUsuarioByUsername(in.nextLine()); + if(usuario!=null){ + this.bloqueados.add(usuario); + return; + } + System.out.println("Usuario No Existente"); + }catch (Exception e){ + System.out.println("error"); + } + } + + public void desbloquear(){ + Scanner in = new Scanner(System.in); + try{ + System.out.print("\nIngrese el usuario a desbloquear: "); + Usuario usuario = BaseDeDatos.getUsuarioByUsername(in.nextLine()); + if(usuario!=null){ + this.bloqueados.remove(usuario); + return; + } + System.out.println("Usuario No Existente"); + }catch (Exception e){ + System.out.println("error"); + } + } + + public void addSeguidores(Usuario usuario){ + this.seguidores.add(usuario); + } + + public void removeSeguidores(Usuario usuario){ + this.seguidores.remove(usuario); + } + + public Usuario getUsuario() { + return usuario; + } + + public ArrayList getSeguidos() { + return seguidos; + } + + public void printSeguidos(){ + for(Usuario user: seguidos){ + System.out.print("\n\t--"+user.getNombreUsuario()); + } + } + + public void printSeguidores(){ + for(Usuario user: seguidores){ + System.out.print("\n\t--"+user.getNombreUsuario()); + } + } + + public ArrayList getSeguidores() { + return seguidores; + } + + public ArrayList getBloqueados() { + return bloqueados; + } +} diff --git a/twitter/src/twitter/TestTwitter.java b/twitter/src/twitter/TestTwitter.java new file mode 100644 index 0000000..a50101f --- /dev/null +++ b/twitter/src/twitter/TestTwitter.java @@ -0,0 +1,156 @@ +package twitter; + +import java.util.Date; +import java.util.Scanner; + +public class TestTwitter { + public static void main(String []args){ + testData(); + Scanner in = new Scanner(System.in); + System.out.println("WELCOME TO TWITTER"); + boolean continuar = true; + int choice = 0; + Usuario loggedUser = null; + + do{ + if(loggedUser == null){ + System.out.print("\nMenu de Inicio:\n"+ + "1 - Iniciar Sesion\n"+ + "2 - Registrarse\n"+ + "0 - Salir\n"+ + "Entrada: "); + try{ + choice = in.nextInt(); + }catch (Exception e){ + System.out.println("Entrada no validad"); + } + switch (choice){ + case 1: + System.out.println("Inciando sesion"); + loggedUser = Usuario.iniciarSesion(); + break; + case 2: + System.out.println("Registrandose"); + BaseDeDatos.addUsuario(Usuario.registarUsuario()); + break; + case 0: + System.out.println("Saliendo..."); + continuar = false; + break; + default: + System.out.println("Eleccion no validad"); + } + }else{ + System.out.print("\nMenu de Usuario:\n"+ + "1 - Ver Tweets\n"+ + "2 - Twittear\n"+ + "3 - Seguimiento\n"+ + "4 - Bloqueados\n"+ + "0 - Cerrar Sesion\n"+ + "Entrada: "); + try{ + choice = in.nextInt(); + }catch (Exception e){ + System.out.println("Entrada no validad"); + } + switch (choice){ + case 1: + System.out.print("\nDashboard: "); + loggedUser.dashboard.verUltimosTweets(10); + System.out.print("\n1 - Responder"+ + "\n2 - Dar Fav"+ + "\n3 - Retweetear"+ + "\n4 - Volver"+ + "\nEntrada: "); + int choice3 = 0; + int id; + try{ + choice3 = in.nextInt(); + }catch (Exception e){ + System.out.println("Eleccion no valida"); + } + switch (choice3){ + case 1: + System.out.print("\nID de Tweet a responder: "); + id = in.nextInt(); + Tweet.responder(loggedUser, id); + break; + case 2: + System.out.print("\nID de Tweet a dar like: "); + id = in.nextInt(); + Tweet.likear(loggedUser, id); + break; + case 3: + System.out.print("\nID de Tweet a dar RT: "); + id = in.nextInt(); + Tweet.retweetear(loggedUser, id); + break; + default: + break; + } + break; + case 2: + System.out.println("Twitteando"); + Tweet.tweetear(loggedUser); + break; + case 3: + System.out.print("\nMenu de Seguimiento: "); + System.out.print("\nSeguimiento: "); + System.out.print("\n\tSeguidores: "); + loggedUser.seguimiento.printSeguidores(); + System.out.print("\n\tSeguidos: "); + loggedUser.seguimiento.printSeguidos(); + System.out.print("\n1 - Seguir"+ + "\n2 - Deja de Seguir"+ + "\n3 - Bloquear"+ + "\n4 - Desbloquear"+ + "\n5 - Volver"+ + "\nEntrada: "); + int choice2 = 0; + try{ + choice2 = in.nextInt(); + }catch (Exception e){ + System.out.println("Eleccion no valida"); + } + switch (choice2){ + case 1: + loggedUser.seguimiento.seguir(); + break; + case 2: + loggedUser.seguimiento.dejarDeSeguir(); + break; + case 3: + loggedUser.seguimiento.bloquear(); + break; + case 4: + loggedUser.seguimiento.desbloquear(); + break; + default: + break; + } + break; + case 4: + loggedUser.seguimiento.dejarDeSeguir(); + break; + case 0: + System.out.println("Saliendo..."); + loggedUser = null; + break; + default: + System.out.println("Eleccion no validad"); + } + } + + }while(continuar); + + } + + private static void testData(){ + for(int i=0;i<10;i++){ + BaseDeDatos.addUsuario(new Usuario("user"+i, "user"+i, + "correo"+i, new Date(), new Date(), "fotoUrl"+i, + "descripcion"+i, i%2 == 0)); + } + } + +} diff --git a/twitter/src/twitter/Tweet.java b/twitter/src/twitter/Tweet.java new file mode 100644 index 0000000..4ebd87f --- /dev/null +++ b/twitter/src/twitter/Tweet.java @@ -0,0 +1,121 @@ +package twitter; + +import java.util.ArrayList; +import java.util.Scanner; + +public class Tweet { + private Usuario usuario; + private String text; + private int numeroRt; + private int numeroFav; + private String imageUrl; + private String linkUrl; + private ArrayList reacciones; + private ArrayList respuestas; + + + public Tweet(Usuario usuario, String text, String imageUrl, String linkUrl) { + this.usuario = usuario; + this.text = text; + this.imageUrl = imageUrl; + this.linkUrl = linkUrl; + this.numeroFav = 0; + this.numeroRt = 0; + this.respuestas = new ArrayList(); + this.reacciones = new ArrayList(); + } + + public static void likear(Usuario usuario, int tweetId){ + BaseDeDatos.getTweetById(tweetId).addLike(usuario); + } + + public void addLike(Usuario usuario){ + for(Reaccion reaccion: reacciones){ + if(reaccion.getUsuario()==usuario){ + System.out.println("Ya diste like a esta publicacion"); + return; + } + } + this.reacciones.add(new Reaccion(usuario, this, "LIKE")); + this.numeroFav++; + } + + public static void retweetear(Usuario usuario, int tweetId){ + BaseDeDatos.getTweetById(tweetId).addRt(usuario); + } + + public void addRt(Usuario usuario){ + for(Reaccion reaccion: reacciones){ + if(reaccion.getUsuario()==usuario){ + System.out.println("Ya diste rt a esta publicacion"); + return; + } + } + this.reacciones.add(new Reaccion(usuario, this, "RT")); + this.numeroRt++; + } + + public static void responder(Usuario usuario, int tweetId){ + BaseDeDatos.getTweetById(tweetId).addRespuesta(usuario); + } + + public void addRespuesta(Usuario usuario){ + respuestas.add(Respuesta.tweetear(usuario, this)); + } + + public static void tweetear(Usuario user){ + Scanner in = new Scanner(System.in); + String text; + String imageUrl=null; + String linkUrl=null; + String choice; + try{ + System.out.println("Texto"); + System.out.print("-----> "); + text = in.nextLine(); + System.out.print("\nDesea agregar una imagen al tweet?(Y/N): "); + choice = (in.nextLine()).toLowerCase(); + if(choice.equals("y")){ + System.out.print("\nURL De la Imagen: "); + imageUrl = in.nextLine(); + } + System.out.print("\nDesea agregar un link al tweet?(Y/N): "); + choice = (in.nextLine()).toLowerCase(); + if(choice.equals("y")){ + System.out.print("\nURL: "); + linkUrl = in.nextLine(); + } + BaseDeDatos.addTweet(new Tweet(user, text, imageUrl, linkUrl)); + }catch (Exception e){ + System.out.println("Error"); + } + } + + public Usuario getUsuario() { + return usuario; + } + + public String getText() { + return text; + } + + public int getNumeroRt() { + return numeroRt; + } + + public int getNumeroFav() { + return numeroFav; + } + + public String getImageUrl() { + return imageUrl; + } + + public String getLinkUrl() { + return linkUrl; + } + + public ArrayList getRespuestas() { + return respuestas; + } +} diff --git a/twitter/src/twitter/Usuario.java b/twitter/src/twitter/Usuario.java new file mode 100644 index 0000000..ae25bea --- /dev/null +++ b/twitter/src/twitter/Usuario.java @@ -0,0 +1,133 @@ +package twitter; + +import java.util.Date; +import java.util.Objects; +import java.util.Scanner; + +public class Usuario { + private String nombreUsuario; + private String contrasenha; + private String correo; + private Date fechaNacimiento; + private Date fechaRegistro; + private String fotoUrl; + private String descripcion; + public Seguimiento seguimiento; + public Dashboard dashboard; + // private Tweet[] tweets; +// private Seguimiento seguimiento; + + public Usuario(String nombreUsuario, String contrasenha, String correo, + Date fechaNacimiento, Date fechaRegistro, String fotoUrl, + String descripcion, boolean esVerficado) { + this.nombreUsuario = nombreUsuario; + this.contrasenha = contrasenha; + this.correo = correo; + this.fechaNacimiento = fechaNacimiento; + this.fechaRegistro = fechaRegistro; + this.fotoUrl = fotoUrl; + this.descripcion = descripcion; + this.esVerficado = esVerficado; + this.seguimiento = new Seguimiento(this); + this.dashboard = new Dashboard(this); + } + + private boolean esVerficado; + + + + public Usuario(String nombreUsuario, String contrasenha, String correo, + String descripcion) { + this.nombreUsuario = nombreUsuario; + this.contrasenha = contrasenha; + this.correo = correo; + this.descripcion = descripcion; + this.seguimiento = new Seguimiento(this); + this.dashboard = new Dashboard(this); + } + + + public static Usuario registarUsuario(){ + /* + * Funcion que crea un usuario y lo retorna para ser guardado. + * */ + Scanner in = new Scanner(System.in); + String username; + String password; + String mail; + String fotoUrl; + String descripcion; + try{ + System.out.print("\nIngrese nombre de usuario: "); + username = in.nextLine(); + System.out.print("\nIngrese contrasenha: "); + password = in.nextLine(); + System.out.print("\nIngrese mail: "); + mail = in.nextLine(); + System.out.print("\nIngrese una descripcion: "); + descripcion = in.nextLine(); + return new Usuario(username, password, mail, descripcion); + }catch (Exception e){ + System.out.println("Un error"); + } + return null; + } + + public static Usuario iniciarSesion(){ + /* + Retorna usuario si las credenciales son correctas, null en caso contrario + */ + Scanner in = new Scanner(System.in); + String username; + String password; + try{ + System.out.print("\nIngrese su Nombre de Usuario: "); + username = in.nextLine(); + System.out.print("\nIngrese su Contrasenha: "); + password = in.nextLine(); + Usuario u = Objects.requireNonNull(BaseDeDatos.getUsuarioByUsername(username)); + if(u.getContrasenha().equals(password)){ + return u; + }else{ + System.out.println("Credenciales Incorrectas!"); + return null; + } + }catch (Exception e){ + System.out.println("Credenciales Incorrectas!"); + } + return null; + } + + + public String getNombreUsuario() { + return nombreUsuario; + } + + public String getContrasenha() { + return contrasenha; + } + + public String getCorreo() { + return correo; + } + + public Date getFechaNacimiento() { + return fechaNacimiento; + } + + public Date getFechaRegistro() { + return fechaRegistro; + } + + public String getFotoUrl() { + return fotoUrl; + } + + public String getDescripcion() { + return descripcion; + } + + public boolean isEsVerficado() { + return esVerficado; + } +} -- libgit2 0.26.0