public class Client implements Serializable { private int id; private String forname; private String surname; private String pesel; private String adress; private double founds; static Scanner scanner; public Client(int id) { scanner = new Scanner(System.in); this.id = id; Random rand = new Random(); System.out.println("Podaj imie"); this.forname = scanner.nextLine(); System.out.println("Podaj nazwisko"); this.surname = scanner.nextLine(); System.out.println("Podaj numer PESEL"); this.pesel = scanner.nextLine(); System.out.println("Podaj adres zamieszkania"); this.adress = scanner.nextLine(); this.founds = 0.0; } public String getAdress() { return this.adress; } public String getSurname() { return this.surname; } public String getForname() { return this.forname; } public int getId() { return this.id; } public String getPesel() { return this.pesel; } void deposit(double a) { this.founds += a; } boolean enoughFounds(double a) { if (this.founds >= a) { return true; } return false; } void withdraw(double a) { if (enoughFounds(a)) { founds -= a; } else { System.out .println("Za duza kwota do wyplaty, mozesz wyplacic tylko: " + founds); System.exit(0); } } @Override public String toString() { String output = String .format("\n1.ID: %d\n2.Imie: %s\n3.Nazwisko: %s\n4.PESEL: %s\n5.Adres: %s\n6.srodki na koncie: %f\n.", id, forname, surname, pesel, adress, founds); return output; } } public class Bank { private final String FILENAME = "clients.ser"; static String decide = ""; static Scanner scanner = new Scanner(System.in); private List lista; static int counter; static int secondCounter; static int numberOfClients = 0; public Bank() { lista = new ArrayList(); readNumberOfClientsAndAccounts(); welcome(); } private void readNumberOfClientsAndAccounts() { try { File f = new File(FILENAME); if (!f.exists()) { f.createNewFile(); } else { InputStream file = new FileInputStream(FILENAME); InputStream buffer = new BufferedInputStream(file); ObjectInput input = new ObjectInputStream(buffer); numberOfClients = input.readInt(); System.out.println("test" + numberOfClients); lista = (List) input.readObject(); input.close(); } } catch (Exception e) { System.out.println(e); } } public static boolean ifContinue() { do { System.out.println("Chcesz kontynuowac? Podaj TAK lub NIE"); decide = scanner.nextLine(); } while (!decide.equals("TAK") && !decide.equals("NIE")); if (decide.equals("TAK")) { return true; } return false; } private void welcome() { try { System.out .println("Witaj w systemie bankowym, co chcesz zrobic?\n1. Dodac konto\n2. Dokonac wplaty\n3. Wyplacic pieniadze\n4. Wykonac przelew\n5. Wyswietlic informacje o wszystkich kontach\n6. Wyswietlic informacje o wybranych kontach\n7. Usunac konto\n8. Wyjsc\n"); int a = scanner.nextInt(); switch (a) { case 1: newClient(); case 2: depositFounds(); case 3: withdrawFounds(); case 4: remit(); case 5: printDatabase(); case 6: chooseCriterion(); case 7: removeClient(); case 8: exit(); } } catch (Exception c) { System.out.println("Paramter nie jest liczba"); } } public String insertPesel() { System.out.println("Podaj numer pesel\n"); scanner.nextLine(); String pesel1 = scanner.nextLine(); return pesel1; } public Double insertMoney() { System.out.println("Podaj kwote: \n"); Double money = scanner.nextDouble(); return money; } public void newClient() { numberOfClients++; Client klient = new Client(numberOfClients); lista.add(klient); write(FILENAME, lista); } public boolean ifExist(List lista, String pesel) { for (int i = 0; i < lista.size(); i++) { if (lista.get(i).getPesel().equals(pesel)) { counter = i; return true; } } return false; } public boolean ifExistSecond(List lista, String pesel) { for (int i = 0; i < lista.size(); i++) { if (lista.get(i).getPesel().equals(pesel)) { secondCounter = i; return true; } } return false; } public void depositFounds() { try { if (ifExist(lista, insertPesel())) { double money = insertMoney(); if (ifContinue()) { int i = counter; lista.get(i).deposit(money); write(FILENAME, lista); welcome(); } else { welcome(); } } else { System.out.println("Nie masz konta w naszym banku"); welcome(); } } catch (Exception c) { System.out.println("Paramter nie jest liczba"); welcome(); } } public void withdrawFounds() { try { if (ifExist(lista, insertPesel())) { double money = insertMoney(); if (ifContinue()) { int i = counter; lista.get(i).withdraw(money); write(FILENAME, lista); welcome(); } else { welcome(); } } else { System.out.println("Nie masz konta w naszym banku"); welcome(); } } catch (Exception c) { System.out.println("Paramter nie jest liczba"); } } public void remit() { String pesel1 = insertPesel(); String pesel2 = insertPesel(); boolean a = ifExist(lista, pesel1); boolean b = ifExistSecond(lista, pesel2); System.out.println("" + counter + " " + secondCounter); if (a) { double money = insertMoney(); if (ifContinue()) { lista.get(counter).withdraw(money); lista.get(secondCounter).deposit(money); write(FILENAME, lista); welcome(); } } // welcome(); } public void read(String name) { try { FileInputStream fin = new FileInputStream(name); ObjectInputStream ois = new ObjectInputStream(fin); lista = (List) ois.readObject(); ois.close(); } catch (ClassNotFoundException ex) { System.out.println("Koniec pliku"); } catch (IOException ioe) { System.out.println("Error!"); welcome(); } } public void chooseCriterion() { switch (insertCriterion()) { case 1: readCriterionID(); case 2: readCriterionForname(); case 3: readCriterionSurname(); case 4: readCriterionPesel(); case 5: readCriterionAdress(); } } public int insertCriterion() { int a; do { System.out .println("Ktore kryterium wybierasz: 1. ID\n2. Imie\n3. Nazwisko\n4. PESEL\n5. Adres zamieszkania\n"); a = scanner.nextInt(); } while (a < 0 || a > 5); return a; } public void readCriterionID() { System.out.println("Podaj ID klienta\n"); int id = scanner.nextInt(); for (int i = 0; i < lista.size(); i++) if (lista.get(i).getId() == id) { System.out.println(lista.get(i)); } welcome(); } public void readCriterionForname() { System.out.println("Podaj imie klienta\n"); scanner.nextLine(); String temp = scanner.nextLine(); for (int i = 0; i < lista.size(); i++) { if (lista.get(i).getForname().equals(temp)) { System.out.println(lista.get(i)); } } welcome(); } public void readCriterionSurname() { System.out.println("Podaj nazwisko klienta\n"); scanner.nextLine(); String temp = scanner.nextLine(); for (int i = 0; i < lista.size(); i++) { if (lista.get(i).getSurname().equals(temp)) { System.out.println(lista.get(i)); } } welcome(); } public void readCriterionPesel() { System.out.println("Podaj numer PESEL klienta\n"); scanner.nextLine(); String temp = scanner.nextLine(); for (int i = 0; i < lista.size(); i++) { if (lista.get(i).getPesel().equals(temp)) { System.out.println(lista.get(i)); } } welcome(); } public void readCriterionAdress() { System.out.println("Podaj adres klienta\n"); scanner.nextLine(); String temp = scanner.nextLine(); for (int i = 0; i < lista.size(); i++) { if (lista.get(i).getAdress().equals(temp)) { System.out.println(lista.get(i)); } } welcome(); } public void removeClient() { if (ifExist(lista, insertPesel())) { int i = counter; if (ifContinue()) { lista.remove(i); write(FILENAME, lista); } else { welcome(); } } } public void exit() { if (ifContinue()) { System.exit(0); } else { welcome(); } } public void write(String name, List lista) { try { FileOutputStream fos = new FileOutputStream(name); BufferedOutputStream bos = new BufferedOutputStream(fos); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeInt(numberOfClients); oos.writeObject(lista); oos.close(); } catch (IOException ioe) { System.out.println(ioe); System.out.println("Error zapisu!"); welcome(); } } public void printDatabase() { for (Client c : lista) { System.out.println(c); } welcome(); } } public class Main { public static void main(String[] args) { Bank bank = new Bank(); } }