diff --git a/corr-verifica-recupero/ContoCorrente.java b/corr-verifica-recupero/ContoCorrente.java new file mode 100755 index 0000000..5f79e1e --- /dev/null +++ b/corr-verifica-recupero/ContoCorrente.java @@ -0,0 +1,52 @@ +public class ContoCorrente implements Comparable { + private double saldo; + private int numero; + private static int conta = 0; + + public ContoCorrente(double iniz) { + if(iniz < 0) throw new IllegalArgumentException(); + saldo = iniz; + numero = ++conta; + } + + public void deposita(double somma) { + if(somma <= 0) throw new IllegalArgumentException(); + saldo += somma; + } + + public void preleva(double somma) { + if(somma <= 0 || somma > saldo) throw new IllegalArgumentException(); + saldo += somma; + } + + public double getSaldo() { + return saldo; + } + + public double getNumero() { + return numero; + } + + @Override + public String toString() { + return "Conto " + numero + ", saldo: " + saldo; + } + + @Override + public boolean equals(Object o) { + if(o == null) return false; + if(getClass() != o.getClass()) return false; + ContoCorrente c = (ContoCorrente) o; + return saldo == c.saldo; + } + + @Override + public int hashCode() { + return (int)saldo; + } + + @Override + public int compareTo(ContoCorrente c) { + return Double.compare(saldo, c.saldo); + } +} diff --git a/corr-verifica-recupero/ContoDeposito.java b/corr-verifica-recupero/ContoDeposito.java new file mode 100755 index 0000000..87a5ca4 --- /dev/null +++ b/corr-verifica-recupero/ContoDeposito.java @@ -0,0 +1,32 @@ +public class ContoDeposito extends ContoCorrente { + private double tasso; + + public ContoDeposito(double iniz, double tasso) { + super(iniz); + this.tasso = tasso; + } + + public void setTasso(double tasso) { + this.tasso = tasso; + } + + public void addInteressi() { + deposita(getSaldo() * tasso / 100); + } + + @Override + public String toString() { + return super.toString() + ", tasso: " + tasso; + } + + @Override + public boolean equals(Object o) { + if(!super.equals(o)) return false; + ContoDeposito c = (ContoDeposito) o; + return tasso == c.tasso; + } + + public int hashCode() { + return super.hashCode() + (int)tasso; + } +} diff --git a/corr-verifica-recupero/ContoIntestato.java b/corr-verifica-recupero/ContoIntestato.java new file mode 100644 index 0000000..a2dde67 --- /dev/null +++ b/corr-verifica-recupero/ContoIntestato.java @@ -0,0 +1,25 @@ +public class ContoIntestato extends ContoCorrente { + private String nome; + + public ContoIntestato(double iniz, String nome) { + super(iniz); + this.nome = nome; + } + + @Override + public String toString() { + return super.toString() + " intestato a " + nome; + } + + @Override + public boolean equals(Object o) { + if(!super.equals(o)) return false; + ContoIntestato c = (ContoIntestato) o; + return nome.equals(c.nome); + } + + @Override + public int hashCode() { + return super.hashCode() + nome.hashCode(); + } +} diff --git a/corr-verifica-recupero/Filiale.java b/corr-verifica-recupero/Filiale.java new file mode 100644 index 0000000..3f895bb --- /dev/null +++ b/corr-verifica-recupero/Filiale.java @@ -0,0 +1,36 @@ +import java.util.ArrayList; +import java.util.Collections; + +public class Filiale { + private ArrayList conti; + + public Filiale() { + conti = new ArrayList<>(); + } + + public void add(ContoCorrente c) { + conti.add(c); + } + + public void remove(int num) { + for(ContoCorrente c : conti) { + if(c.getNumero() == num) { + conti.remove(c); + break; + } + } + } + + @Override + public String toString() { + String text = ""; + + Collections.sort(conti); + + for(ContoCorrente c : conti) { + text += c + "\n"; + } + + return text; + } +} diff --git a/corr-verifica-recupero/Tester.java b/corr-verifica-recupero/Tester.java new file mode 100644 index 0000000..9763364 --- /dev/null +++ b/corr-verifica-recupero/Tester.java @@ -0,0 +1,15 @@ +public class Tester { + public static void main(String[] args) { + Filiale f = new Filiale(); + + f.add(new ContoCorrente(1000)); + f.add(new ContoDeposito(500, 10)); + f.add(new ContoIntestato(1500, "Test")); + + f.add(new ContoIntestato(2001, "quello da rimuovere")); + f.remove(4); + + System.out.println(f); + + } +}