Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class AgendaTelefone {
- private ArrayList<Contacto> agenda;
- public AgendaTelefone(){
- agenda = new ArrayList<Contacto>();
- }
- public void inserirContacto(Contacto x) {
- boolean find = false;
- for(int i = 0; i < agenda.size(); i++){
- if(agenda.get(i).equals(x)){
- find = true;
- break;
- }
- }
- if(find){
- throw new ContactoJaInseridoException(x.getNome() + "Contacto já existente");
- }
- else{
- agenda.add(x);
- }
- }
- public void list(){
- for(Contacto c: agenda){
- System.out.println();
- }
- }
- public String toString(){
- return agenda.toString();
- }
- public void removerContacto(Contacto x){
- agenda.remove(x);
- }
- public static void main(String[] args){
- Contacto c1 = new Contacto("Nuno Ribeiro", 961345111);
- Contacto c2 = new Contacto("João Ferreira", 961111111);
- AgendaTelefone a1 = new AgendaTelefone();
- a1.inserirContacto(c1);
- a1.inserirContacto(c2);
- System.out.println(a1);
- a1.list();
- Contacto c3 = new Contacto("João Ferreira", 961101111);
- a1.inserirContacto(c3);
- System.out.println(a1);
- a1.list();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement