Seguinte, estou tentando implementar uma agenda que adiciona contatos e, pra cada, contato, podem ser adicionados numeros de telefones (string) e IDs (string).
Classe fone:
package agenda2;
public class Fone {
public String id;
public String number;
public Fone(String id, String number) {
this.id = id;
this.number = number;
}
public Fone(String serial) {
this.id = serial.replaceAll("[^a-z]", "");
this.number = serial.replaceAll("[0-9][(][)][.]", "");
}
public String getId() {
return this.id;
}
public String getNumber() {
return this.number;
}
public void setId(String id) {
this.id = id;
}
public void setNumber(String number) {
this.number = number;
}
// checando se o numero é válido
public static boolean validate(String number) {
return number.matches("[(][0-9]{2}[)][0-9]{5}[.][0-9]{4,}");
}
}
Classe contato:
package agenda2;
import java.util.ArrayList;
public class Contato{
private String name;
private ArrayList<Fone> fones;
public Contato(String name) {
this.name = name;
fones = new ArrayList<Fone>();
}
public Contato() {
this.name = "";
fones = new ArrayList<Fone>();
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Fone> getFones(){
return this.fones;
}
// adicionando um numero de telefone ao contato
public boolean addFone(String serial) {
boolean isFoneValid = true;
String idid = serial.replaceAll("[^a-z]", "");
String num = serial.replaceAll("[^0-9.()]", "");
if(Fone.validate(num)) {
Fone fon = new Fone(idid, num);
getFones().add(fon);
isFoneValid = true;
}else {
System.out.println("fail: number doesnt correspond to pattern");
isFoneValid = false;
}
return isFoneValid;
}
}
Classe agenda:
package agenda2;
import java.util.ArrayList;
import java.util.List;
public class Agenda {
private ArrayList<Contato> contatos;
public Agenda() {
contatos = new ArrayList<Contato>();
}
public ArrayList<Contato> getContatos(){
return contatos;
}
// adding contacts
public void addContato(String name, ArrayList<Fone> fone) {
boolean isNameThere = false;
if(getContatos().size() == 0) {
Contato c = new Contato(name);
String serial = String.valueOf(fone.get(0));
c.addFone(serial);
}else {
for(int i = 0; i < getContatos().size(); i++) {
if(getContatos().get(i).getName() == name) {
String serial = String.valueOf(fone);
getContatos().get(i).addFone(serial);
}
}
}
if(isNameThere == false) {
Contato c = new Contato(name);
String serial = String.valueOf(fone);
c.addFone(serial);
}
}
public String toString() {
String str = " - ";
for(int i = 0; i < getContatos().size(); i++) {
str += getContatos().get(i).getName();
for(int j = 0; j < getContatos().get(i).getFones().size(); j++) {
str += "[" + j + ":" + getContatos().get(i).getFones().get(j).getId() + ":" +
getContatos().get(i).getFones().get(j).getNumber() + "]";
}
}
return str;
}
}
Main:
package agenda2;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Agenda a = new Agenda();
Scanner sc = new Scanner(System.in);
boolean end = false;
while(!end) {
System.out.println("Cmd: ");
String line = sc.nextLine();
String[] cmd = line.split(" ");
String comando = cmd[0];
switch(comando) {
case "add":
String name = cmd[1];
String serial = cmd[2];
ArrayList<Fone> fon = new ArrayList<>();
Fone f = new Fone(serial);
fon.add(f);
a.addContato(name, fon);
break;
case "show":
System.out.println(a);
break;
}
}
}
}
Executando o código:
Cmd:
show
-
Cmd:
add roberto oi:(98)99223.4444
fail: number doesnt correspond to pattern
fail: number doesnt correspond to pattern
Cmd:
show
-
Cmd:
Era pra adicionar o contato roberto na lista de contatos, e o ID (oi) e numero ((98)99223.4444 ao contato roberto, e me mostrar isso:
- roberto [oi:(98)99223.4444]
Não sei o que estou fazendo de errado, alguém pode dar um help?