Aggiunto ContoCorrente

Correzione verifica di recupero
This commit is contained in:
Giacomo R. 2023-06-06 22:21:44 +02:00
parent 84b40cc8e9
commit 9ed76d07ce
5 changed files with 160 additions and 0 deletions

View File

@ -0,0 +1,52 @@
public class ContoCorrente implements Comparable<ContoCorrente> {
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);
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,36 @@
import java.util.ArrayList;
import java.util.Collections;
public class Filiale {
private ArrayList<ContoCorrente> 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;
}
}

View File

@ -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);
}
}