package banche;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Consumer gestione banche
*/
public class Banche {
// costanti
private static final String FINE = "9";
private ArrayList<Banca> lista = new ArrayList<Banca>();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// definizione menù
Banche main = new Banche();
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader tastiera = new BufferedReader(stream);
String scelta = "0";
do {
System.out.println("\nSelezionare azione desiderata");
System.out.println("1 Inserimento banca");
System.out.println("2 Ricerca banca");
System.out.println("3 Lettura banca");
System.out.println("4 Cancellazione banca");
System.out.println("10 Inserimento conto");
System.out.println("11 Ricerca conto");
System.out.println("12 Lettura conto");
System.out.println("13 Cancellazione conto");
System.out.println("9 Fine");
System.out.println(" ");
try {
scelta = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
scelta = "0";
}
int sv = Integer.parseInt(scelta);
switch(sv) {
case 1: main.InsBanca(); break;
case 2: main.TrovaBanca(); break;
case 3: main.DatiBanca(); break;
case 4: main.CancBanca(); break;
case 11: main.InsContoBanca(); break;
case 12: main.TrovaContoBanca(); break;
case 13: main.DatiContoBanca(); break;
case 14: main.CancContoBanca(); break;
//case 9: break;
}
} while(!scelta.equals(FINE));
System.out.println("Fine applicazione");
}
public void InsBanca() {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader tastiera = new BufferedReader(stream);
System.out.println("Inserire nome banca da creare");
String nome;
try {
nome = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
nome = "";
}
addBanca(nome);
}
public void CancBanca() {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader tastiera = new BufferedReader(stream);
System.out.println("Inserire nome banca da creare");
String nome;
try {
nome = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
nome = "";
}
int ret = delBanca(nome);
if (ret > -1) {
System.out.println("Cancellazione eseguita");
}
else {
System.out.println("Cancellazione fallita");
}
}
public void TrovaBanca() {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader tastiera = new BufferedReader(stream);
System.out.println("Inserire nome banca da creare");
String nome;
try {
nome = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
nome = "";
}
int ret = getIndexBanca(nome);
if (ret > -1) {
System.out.println("Banca presente");
}
else {
System.out.println("Banca non presente");
}
}
public void DatiBanca() {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader tastiera = new BufferedReader(stream);
System.out.println("Inserire nome banca da creare");
String nome;
try {
nome = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
nome = "";
}
Banca b = this.getBanca(nome);
if (b != null) {
System.out.println("Banca presente");
System.out.println("Nome banca = " + b.getNome());
System.out.println("Numero conti banca = " + b.getNumConti());
}
else {
System.out.println("Banca non presente");
}
}
public void InsContoBanca() {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader tastiera = new BufferedReader(stream);
System.out.println("Inserire nome banca da creare");
String nome;
try {
nome = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
nome = "";
}
System.out.println("Inserire numero conto");
String numero;
try {
numero = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
numero = "";
}
System.out.println("Inserire nome correntista");
String nomec;
try {
nomec = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
nomec = "";
}
System.out.println("Inserire cognome correntista");
String cognc;
try {
cognc = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
cognc = "";
}
Banca b = this.getBanca(nome);
if (b != null) {
System.out.println("Banca presente");
b.addConto(numero, nome, cognc);
System.out.println("Conto inserito");
}
else {
System.out.println("Banca non presente");
}
}
public void TrovaContoBanca() {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader tastiera = new BufferedReader(stream);
System.out.println("Inserire nome banca da creare");
String nome;
try {
nome = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
nome = "";
}
System.out.println("Inserire numero conto");
String numero;
try {
numero = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
numero = "";
}
Banca b = this.getBanca(nome);
if (b != null) {
System.out.println("Banca presente");
ContoCorrente ret = b.getConto(numero);
if (ret != null) {
System.out.println("Conto presente");
}
else {
System.out.println("Conto non presente");
}
}
else {
System.out.println("Banca non presente");
}
}
public void DatiContoBanca() {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader tastiera = new BufferedReader(stream);
System.out.println("Inserire nome banca da creare");
String nome;
try {
nome = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
nome = "";
}
System.out.println("Inserire numero conto");
String numero;
try {
numero = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
numero = "";
}
Banca b = this.getBanca(nome);
if (b != null) {
System.out.println("Banca presente");
ContoCorrente ret = b.getConto(numero);
if (ret != null) {
System.out.println("Conto presente");
System.out.println("intestatario = " + ret.getIntestatario().getCognome() + ", " + ret.getIntestatario().getNome());
System.out.println("numero = " + ret.getNumero());
System.out.println("saldo = " + ret.getSaldo());
}
else {
System.out.println("Conto non presente");
}
}
else {
System.out.println("Banca non presente");
}
}
public void CancContoBanca() {
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader tastiera = new BufferedReader(stream);
System.out.println("Inserire nome banca da creare");
String nome;
try {
nome = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
nome = "";
}
System.out.println("Inserire numero conto");
String numero;
try {
numero = tastiera.readLine();
} catch (IOException ex) {
Logger.getLogger(Banche.class.getName()).log(Level.SEVERE, null, ex);
numero = "";
}
Banca b = this.getBanca(nome);
if (b != null) {
System.out.println("Banca presente");
ContoCorrente ret = b.getConto(numero);
if (ret != null) {
System.out.println("Conto presente");
int ris = b.delConto(numero);
if (ris > -1) {
System.out.println("Conto eliminato");
}
else {
System.out.println("Conto non eliminato");
}
}
else {
System.out.println("Conto non presente");
}
}
else {
System.out.println("Banca non presente");
}
}
public void addBanca(String nome) {
if (nome.length() > 0) {
Banca b = new Banca(nome);
lista.add(b);
}
}
public Banca getBanca(String nome) {
Banca b = null;
if (nome.length() > 0) {
// ricerca banca per nome
boolean trovato = false;
int i = 0;
while ((i < lista.size()) && !trovato) {
if (lista.get(i).getNome().equals(nome)) {
trovato = true;
b = lista.get(i);
}
else {
i++;
}
}
}
return b;
}
public int getIndexBanca(String nome) {
int ret = -1;
if (nome.length() > 0) {
// ricerca banca per nome
boolean trovato = false;
int i = 0;
while ((i < lista.size()) && !trovato) {
if (lista.get(i).getNome().equals(nome)) {
trovato = true;
ret = i;
}
else {
i++;
}
}
}
return ret;
}
public int delBanca(String nome) {
int ret = -1;
if (nome.length() > 0) {
ret = getIndexBanca(nome);
if (ret > -1) {
lista.remove(ret);
ret = 0;
}
}
return ret;
}
}
<<