Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.03 KB | None | 0 0
  1. public class Client implements Serializable {
  2.     private int id;
  3.     private String forname;
  4.     private String surname;
  5.     private String pesel;
  6.     private String adress;
  7.     private double founds;
  8.     static Scanner scanner;
  9.  
  10.     public Client(int id) {
  11.         scanner = new Scanner(System.in);
  12.         this.id = id;
  13.         Random rand = new Random();
  14.         System.out.println("Podaj imie");
  15.         this.forname = scanner.nextLine();
  16.         System.out.println("Podaj nazwisko");
  17.         this.surname = scanner.nextLine();
  18.         System.out.println("Podaj numer PESEL");
  19.         this.pesel = scanner.nextLine();
  20.         System.out.println("Podaj adres zamieszkania");
  21.         this.adress = scanner.nextLine();
  22.         this.founds = 0.0;
  23.     }
  24.  
  25.     public String getAdress() {
  26.         return this.adress;
  27.     }
  28.  
  29.     public String getSurname() {
  30.         return this.surname;
  31.     }
  32.  
  33.     public String getForname() {
  34.         return this.forname;
  35.     }
  36.  
  37.     public int getId() {
  38.         return this.id;
  39.     }
  40.  
  41.     public String getPesel() {
  42.         return this.pesel;
  43.     }
  44.  
  45.     void deposit(double a) {
  46.         this.founds += a;
  47.     }
  48.  
  49.     boolean enoughFounds(double a) {
  50.         if (this.founds >= a) {
  51.             return true;
  52.         }
  53.         return false;
  54.     }
  55.  
  56.     void withdraw(double a) {
  57.         if (enoughFounds(a)) {
  58.             founds -= a;
  59.         } else {
  60.             System.out
  61.                     .println("Za duza kwota do wyplaty, mozesz wyplacic tylko: "
  62.                             + founds);
  63.             System.exit(0);
  64.         }
  65.     }
  66.  
  67.     @Override
  68.     public String toString() {
  69.         String output = String
  70.                 .format("\n1.ID: %d\n2.Imie: %s\n3.Nazwisko: %s\n4.PESEL: %s\n5.Adres: %s\n6.srodki na koncie: %f\n.",
  71.                         id, forname, surname, pesel, adress, founds);
  72.         return output;
  73.     }
  74. }
  75.  
  76. public class Bank {
  77.  
  78.     private final String FILENAME = "clients.ser";
  79.     static String decide = "";
  80.     static Scanner scanner = new Scanner(System.in);
  81.     private List<Client> lista;
  82.     static int counter;
  83.     static int secondCounter;
  84.     static int numberOfClients = 0;
  85.  
  86.     public Bank() {
  87.         lista = new ArrayList<Client>();
  88.         readNumberOfClientsAndAccounts();
  89.  
  90.         welcome();
  91.     }
  92.  
  93.     private void readNumberOfClientsAndAccounts() {
  94.         try {
  95.             File f = new File(FILENAME);
  96.             if (!f.exists()) {
  97.                 f.createNewFile();
  98.             } else {
  99.                 InputStream file = new FileInputStream(FILENAME);
  100.                 InputStream buffer = new BufferedInputStream(file);
  101.  
  102.                 ObjectInput input = new ObjectInputStream(buffer);
  103.                 numberOfClients = input.readInt();
  104.                 System.out.println("test" + numberOfClients);
  105.                 lista = (List<Client>) input.readObject();
  106.                 input.close();
  107.             }
  108.         } catch (Exception e) {
  109.             System.out.println(e);
  110.         }
  111.     }
  112.  
  113.     public static boolean ifContinue() {
  114.         do {
  115.             System.out.println("Chcesz kontynuowac? Podaj TAK lub NIE");
  116.             decide = scanner.nextLine();
  117.         } while (!decide.equals("TAK") && !decide.equals("NIE"));
  118.         if (decide.equals("TAK")) {
  119.             return true;
  120.         }
  121.         return false;
  122.     }
  123.  
  124.     private void welcome() {
  125.         try {
  126.             System.out
  127.                     .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");
  128.             int a = scanner.nextInt();
  129.             switch (a) {
  130.             case 1:
  131.                 newClient();
  132.             case 2:
  133.                 depositFounds();
  134.             case 3:
  135.                 withdrawFounds();
  136.             case 4:
  137.                 remit();
  138.             case 5:
  139.                 printDatabase();
  140.             case 6:
  141.                 chooseCriterion();
  142.             case 7:
  143.                 removeClient();
  144.             case 8:
  145.                 exit();
  146.             }
  147.         } catch (Exception c) {
  148.             System.out.println("Paramter nie jest liczba");
  149.         }
  150.     }
  151.  
  152.     public String insertPesel() {
  153.         System.out.println("Podaj numer pesel\n");
  154.         scanner.nextLine();
  155.         String pesel1 = scanner.nextLine();
  156.         return pesel1;
  157.     }
  158.  
  159.     public Double insertMoney() {
  160.         System.out.println("Podaj kwote: \n");
  161.         Double money = scanner.nextDouble();
  162.         return money;
  163.     }
  164.  
  165.     public void newClient() {
  166.         numberOfClients++;
  167.         Client klient = new Client(numberOfClients);
  168.         lista.add(klient);
  169.         write(FILENAME, lista);
  170.     }
  171.  
  172.     public boolean ifExist(List<Client> lista, String pesel) {
  173.         for (int i = 0; i < lista.size(); i++) {
  174.             if (lista.get(i).getPesel().equals(pesel)) {
  175.                 counter = i;
  176.                 return true;
  177.             }
  178.         }
  179.         return false;
  180.     }
  181.  
  182.     public boolean ifExistSecond(List<Client> lista, String pesel) {
  183.         for (int i = 0; i < lista.size(); i++) {
  184.             if (lista.get(i).getPesel().equals(pesel)) {
  185.                 secondCounter = i;
  186.                 return true;
  187.             }
  188.         }
  189.         return false;
  190.     }
  191.  
  192.     public void depositFounds() {
  193.         try {
  194.             if (ifExist(lista, insertPesel())) {
  195.                 double money = insertMoney();
  196.                 if (ifContinue()) {
  197.                     int i = counter;
  198.                     lista.get(i).deposit(money);
  199.                     write(FILENAME, lista);
  200.                     welcome();
  201.                 } else {
  202.                     welcome();
  203.                 }
  204.             } else {
  205.                 System.out.println("Nie masz konta w naszym banku");
  206.                 welcome();
  207.             }
  208.         } catch (Exception c) {
  209.             System.out.println("Paramter nie jest liczba");
  210.             welcome();
  211.         }
  212.     }
  213.  
  214.     public void withdrawFounds() {
  215.         try {
  216.             if (ifExist(lista, insertPesel())) {
  217.                 double money = insertMoney();
  218.                 if (ifContinue()) {
  219.                     int i = counter;
  220.                     lista.get(i).withdraw(money);
  221.                     write(FILENAME, lista);
  222.                     welcome();
  223.                 } else {
  224.                     welcome();
  225.                 }
  226.             } else {
  227.                 System.out.println("Nie masz konta w naszym banku");
  228.                 welcome();
  229.             }
  230.         } catch (Exception c) {
  231.             System.out.println("Paramter nie jest liczba");
  232.         }
  233.     }
  234.  
  235.     public void remit() {
  236.         String pesel1 = insertPesel();
  237.         String pesel2 = insertPesel();
  238.         boolean a = ifExist(lista, pesel1);
  239.         boolean b = ifExistSecond(lista, pesel2);
  240.         System.out.println("" + counter + " " + secondCounter);
  241.         if (a) {
  242.             double money = insertMoney();
  243.             if (ifContinue()) {
  244.                 lista.get(counter).withdraw(money);
  245.                 lista.get(secondCounter).deposit(money);
  246.                 write(FILENAME, lista);
  247.                 welcome();
  248.             }
  249.         }
  250.         // welcome();
  251.     }
  252.  
  253.     public void read(String name) {
  254.         try {
  255.             FileInputStream fin = new FileInputStream(name);
  256.             ObjectInputStream ois = new ObjectInputStream(fin);
  257.             lista = (List<Client>) ois.readObject();
  258.             ois.close();
  259.         } catch (ClassNotFoundException ex) {
  260.             System.out.println("Koniec pliku");
  261.         } catch (IOException ioe) {
  262.             System.out.println("Error!");
  263.             welcome();
  264.         }
  265.     }
  266.  
  267.     public void chooseCriterion() {
  268.         switch (insertCriterion()) {
  269.         case 1:
  270.             readCriterionID();
  271.         case 2:
  272.             readCriterionForname();
  273.         case 3:
  274.             readCriterionSurname();
  275.         case 4:
  276.             readCriterionPesel();
  277.         case 5:
  278.             readCriterionAdress();
  279.         }
  280.  
  281.     }
  282.  
  283.     public int insertCriterion() {
  284.         int a;
  285.         do {
  286.             System.out
  287.                     .println("Ktore kryterium wybierasz: 1. ID\n2. Imie\n3. Nazwisko\n4. PESEL\n5. Adres zamieszkania\n");
  288.             a = scanner.nextInt();
  289.         } while (a < 0 || a > 5);
  290.         return a;
  291.     }
  292.  
  293.     public void readCriterionID() {
  294.         System.out.println("Podaj ID klienta\n");
  295.         int id = scanner.nextInt();
  296.         for (int i = 0; i < lista.size(); i++)
  297.             if (lista.get(i).getId() == id) {
  298.                 System.out.println(lista.get(i));
  299.             }
  300.         welcome();
  301.     }
  302.  
  303.     public void readCriterionForname() {
  304.         System.out.println("Podaj imie klienta\n");
  305.         scanner.nextLine();
  306.         String temp = scanner.nextLine();
  307.         for (int i = 0; i < lista.size(); i++) {
  308.             if (lista.get(i).getForname().equals(temp)) {
  309.                 System.out.println(lista.get(i));
  310.             }
  311.         }
  312.         welcome();
  313.     }
  314.  
  315.     public void readCriterionSurname() {
  316.         System.out.println("Podaj nazwisko klienta\n");
  317.         scanner.nextLine();
  318.         String temp = scanner.nextLine();
  319.         for (int i = 0; i < lista.size(); i++) {
  320.             if (lista.get(i).getSurname().equals(temp)) {
  321.                 System.out.println(lista.get(i));
  322.             }
  323.         }
  324.         welcome();
  325.     }
  326.  
  327.     public void readCriterionPesel() {
  328.         System.out.println("Podaj numer PESEL klienta\n");
  329.         scanner.nextLine();
  330.         String temp = scanner.nextLine();
  331.         for (int i = 0; i < lista.size(); i++) {
  332.             if (lista.get(i).getPesel().equals(temp)) {
  333.                 System.out.println(lista.get(i));
  334.  
  335.             }
  336.         }
  337.         welcome();
  338.     }
  339.  
  340.     public void readCriterionAdress() {
  341.         System.out.println("Podaj adres klienta\n");
  342.         scanner.nextLine();
  343.         String temp = scanner.nextLine();
  344.         for (int i = 0; i < lista.size(); i++) {
  345.             if (lista.get(i).getAdress().equals(temp)) {
  346.                 System.out.println(lista.get(i));
  347.             }
  348.         }
  349.         welcome();
  350.     }
  351.  
  352.     public void removeClient() {
  353.         if (ifExist(lista, insertPesel())) {
  354.             int i = counter;
  355.             if (ifContinue()) {
  356.                 lista.remove(i);
  357.                 write(FILENAME, lista);
  358.             } else {
  359.                 welcome();
  360.             }
  361.         }
  362.     }
  363.  
  364.     public void exit() {
  365.         if (ifContinue()) {
  366.             System.exit(0);
  367.         } else {
  368.             welcome();
  369.         }
  370.     }
  371.  
  372.     public void write(String name, List<Client> lista) {
  373.         try {
  374.             FileOutputStream fos = new FileOutputStream(name);
  375.             BufferedOutputStream bos = new BufferedOutputStream(fos);
  376.             ObjectOutputStream oos = new ObjectOutputStream(bos);
  377.             oos.writeInt(numberOfClients);
  378.             oos.writeObject(lista);
  379.             oos.close();
  380.         } catch (IOException ioe) {
  381.             System.out.println(ioe);
  382.             System.out.println("Error zapisu!");
  383.             welcome();
  384.         }
  385.     }
  386.  
  387.     public void printDatabase() {
  388.         for (Client c : lista) {
  389.             System.out.println(c);
  390.         }
  391.         welcome();
  392.     }
  393. }
  394.  
  395. public class Main {
  396.     public static void main(String[] args) {
  397.         Bank bank = new Bank();
  398.     }
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement