-
Notifications
You must be signed in to change notification settings - Fork 1
/
TuiterLite.java
142 lines (117 loc) · 4.95 KB
/
TuiterLite.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.util.ArrayList;
import java.util.List;
/**
* Esta classe implementa um sistema de mensagens curtas estilo Twitter.
* É preciso cadastrar um usuário, identificado pelo seu e-mail, para que tuítes possam ser feitos.
* Usuários começam como iniciantes, depois são promovidos a senior e a ninja, em função do número de tuítes.
* Existe um tamanho máximo permitido por mensagem (constante TAMANHO_MAXIMO_TUITES).
* As mensagens podem conter hashtags (palavras iniciadas por #), que são detectadas automaticamente.
* Os tuítes podem conter, além da mensagem de texto, um anexo qualquer.
* Há um método para retornar, a qualquer momento, qual a hashtag mais usada em toda a história do sistema.
*/
public class TuiterLite {
public static int TAMANHO_MAXIMO_TUITES = 120;
/**
* Cadastra um usuário, retornando o novo objeto Usuario criado.
* @param nome O nome do usuário.
* @param email O e-mail do usuário (precisa ser único no sistema).
* @return O Usuario criado.
*/
public Usuario cadastrarUsuario(String nome, String email) { // throws UsuarioJaExisteException {
// ToDo IMPLEMENT ME!!!
return null;
}
/**
*
* @param usuario O autor do tuíte
* @param texto O texto desejado
* @return Um "tuíte", que será devidamente publicado no sistema
*
* PS.: Se o texto exceder o limite pré-definido, ou o usuário não estiver cadastrado, return null
*/
public Tuite tuitarAlgo(Usuario usuario, String texto) {
// ToDo IMPLEMENT ME!!!
return null;
}
/**
* Retorna a hashtag mais comum dentre todas as que já apareceram.
* A cada tuíte criado, hashtags devem ser detectadas automaticamente para que este método possa funcionar.
* @return A hashtag mais comum, ou null se nunca uma hashtag houver sido tuitada.
*/
public String getHashtagMaisComum() {
// ToDo IMPLEMENT ME!!!
return null;
}
public static void main(String[] args) {
String tuite = "#####test#lab estou resolvendo. O exercicio ###lab7. Então, , , , , ,vamos ver!####";
List<String> hashtags = new ArrayList<>();
//
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < tuite.length(); i++) {
// final char c = tuite.charAt(i);
// if (c == '#') {
// if (sb.length() > 0) {
// if (sb.charAt(sb.length() - 1) == '#') {
// continue; // ignorando a # corrente, já que o anterior tb era #
// }
// String hashtag = sb.toString();
// hashtags.add(hashtag);
// sb.setLength(1); // vai manter apenas o primeiro caracter, que já é uma hashtag
// } else {
// sb.append("#");
// }
// } else if (isAlphanumeric(c)) {
// if (sb.length() > 0) {
// sb.append(c);
// }
//
// } else { // não é alfanumérico e nem o símbolo #
// if (sb.length() > 0) {
// String hashtag = sb.toString();
// hashtags.add(hashtag);
// sb.setLength(0);
// }
// }
// }
String[] palavrasDaFrase = tuite.split("([^(a-z|A-Z|0-9|ã-ü|#)])+"); // "escapando" a contrabarra para que minha string seja "CONTRABARRA S"
System.out.println(tuite);
for (String palavra : palavrasDaFrase) {
if (palavra.charAt(0) == '#') {
palavra = palavra.replaceAll("#+", "#");
String[] tags = palavra.split("#");
for (String tag : tags) {
if (tag.length() > 0) {
hashtags.add("#" + tag);
}
}
}
}
for (String hashtag : hashtags) {
System.out.println(hashtag);
}
//
// String numerosSeparadosPorVirgula = "13+54++++7000+4,78,,,,98989+8";
// String[] numeros = numerosSeparadosPorVirgula.split("(\\++)|,+"); // "escapando" a contrabarra para que minha string seja "CONTRABARRA S"
// System.out.println(numerosSeparadosPorVirgula);
// for (String numeroComoString : numeros) {
//// int x = Integer.parseInt(numeroComoString);
// System.out.println(numeroComoString);
// }
//
// String regex1 = ".*[A-Z]+.*";
// String regex2 = ".*[a-z]+.*";
// String regex3 = ".*[0-9]+.*";
// String palavra = "VyuyH87aa";
//
//
// if (palavra.matches(regex1) && palavra.matches(regex2) && palavra.matches(regex3)) {
// System.out.println("Ok! Casou com a regexp");
// } else {
// System.out.println("Não casou com a regexp!!!");
// }
//
}
private static boolean isAlphanumeric(char c) {
return Character.isAlphabetic(c) || Character.isDigit(c);
}
}