Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. import java .awt.* ;
  2. import java.awt.event.* ;
  3. import javax.swing.*;
  4. import java.io.*;
  5. import java.util.ArrayList;
  6.  
  7. class DebetException extends Exception {}
  8.  
  9. class Konto implements Serializable{
  10.     private int stan;
  11.     private ArrayList<Integer> operacje = new ArrayList<>();
  12.  
  13.     Konto() {
  14.         stan = 0;
  15.     }
  16.  
  17.     public void operacja(int ile) throws DebetException {
  18.         if (stan + ile >= 0 )
  19.             stan += ile;
  20.         else
  21.             throw new DebetException();
  22.     }
  23.  
  24.     public int dajStan() {
  25.         return stan ;
  26.     }
  27.  
  28.     public ArrayList<Integer> getOperacje() {
  29.         return operacje;
  30.     }
  31.  
  32.     @Override
  33.     public String toString() {
  34.         return "Konto{" +
  35.                 "stan=" + stan +
  36.                 ", operacje=" + operacje +
  37.                 '}';
  38.     }
  39. }
  40.  
  41.  
  42. class Interface extends JFrame {
  43.     Konto acc = new Konto() ;
  44.  
  45.  
  46.     JTextField
  47.             stanTF  = new JTextField(20),
  48.             operacjaTF = new JTextField(20),
  49.             rezultatTF = new JTextField(20);
  50.  
  51.     JButton
  52.             operacja = new JButton("wpłata/wypłata") ,
  53.             cofnij = new JButton("cofnij") ,
  54.             odblokuj = new JButton("odblokuj"),
  55.             zapisz = new JButton("zapisz"),
  56.             wczytaj = new JButton("wczytaj");
  57.  
  58.     Interface (){
  59.         setTitle("Konto Bankowe");
  60.         Container cp = getContentPane();
  61.         cp.setLayout(new GridLayout(5,2,10,10)) ;
  62.         cp.add(new JLabel("Stan:")) ;
  63.         cp.add(stanTF) ;
  64.         stanTF.setText(Integer.toString(acc.dajStan()));
  65.         stanTF.setEnabled(false);
  66.         operacja.addActionListener(new operacjaL());
  67.         odblokuj.addActionListener(new odblokujL());
  68.         cp.add(operacja) ;
  69.         cp.add(operacjaTF);
  70.         cp.add(rezultatTF);
  71.         cp.add(odblokuj);
  72.         odblokuj.setEnabled(false);
  73.         cofnij.addActionListener(new cofnijL());
  74.         cp.add(cofnij);
  75.         cp.add(new JLabel(""));
  76.         zapisz.addActionListener(new zapisz());
  77.         wczytaj.addActionListener(new wczytaj());
  78.         cp.add(zapisz);
  79.         cp.add(wczytaj);
  80.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  81.         setVisible(true) ;
  82.     }
  83.  
  84.     int dajLiczbe(JTextField tf) throws NumberFormatException {
  85.         return Integer.parseInt(tf.getText());
  86.     }
  87.  
  88.     class operacjaL implements ActionListener{
  89.         public void actionPerformed(ActionEvent e) {
  90.             try {
  91.                 int n = dajLiczbe(operacjaTF);
  92.                 acc.operacja(n);
  93.                 acc.getOperacje().add(n);
  94.                 stanTF.setText(Integer.toString(acc.dajStan()));
  95.                 rezultatTF.setText("OK");
  96.                 operacja.setEnabled(false);
  97.                 operacjaTF.setEnabled(false);
  98.                 odblokuj.setEnabled(true);
  99.             } catch (DebetException exc) {
  100.                 rezultatTF.setText(Integer.toString((acc.dajStan() + dajLiczbe(operacjaTF))*-1) + " za mało!");
  101.                 //rezultatTF.setText("BRAK ŚRODKÓW");
  102.                 operacja.setEnabled(false);
  103.                 operacjaTF.setEnabled(false);
  104.                 odblokuj.setEnabled(true);
  105.             } catch (NumberFormatException exc){
  106.                 rezultatTF.setText("Kwota musi byc liczba");
  107.             }
  108.  
  109.         }
  110.     }
  111.  
  112.     class odblokujL implements ActionListener{
  113.         public void actionPerformed(ActionEvent e) {
  114.             operacjaTF.setText("0");
  115.             operacja.setEnabled(true);
  116.             operacjaTF.setEnabled(true);
  117.             odblokuj.setEnabled(false);
  118.         }
  119.     }
  120.  
  121.     class cofnijL implements ActionListener {
  122.         public void actionPerformed(ActionEvent e) {
  123.             try {
  124.                 acc.operacja(-(acc.getOperacje().get(acc.getOperacje().size()-1)));
  125.                 acc.getOperacje().remove(acc.getOperacje().size() - 1);
  126.                 stanTF.setText(Integer.toString(acc.dajStan()));
  127.                 rezultatTF.setText("Cofnieto");
  128.                 operacja.setEnabled(false);
  129.                 operacjaTF.setEnabled(false);
  130.                 odblokuj.setEnabled(true);
  131.             } catch (DebetException e1) {
  132.                 e1.printStackTrace();
  133.             }
  134.  
  135.         }
  136.     }
  137.  
  138.     class zapisz implements ActionListener{
  139.         public void actionPerformed(ActionEvent actionEvent) {
  140.             try{
  141.                 FileOutputStream f = new FileOutputStream("konto.txt");
  142.                 ObjectOutputStream os = new ObjectOutputStream(f);
  143.                 os.writeObject(acc);
  144.                 f.close();
  145.                 rezultatTF.setText("Zapisano!");
  146.             } catch (IOException e){}
  147.         }
  148.     }
  149.  
  150.     class wczytaj implements ActionListener{
  151.         public void actionPerformed(ActionEvent actionEvent) {
  152.             try{
  153.                 ObjectInputStream is = new ObjectInputStream(
  154.                         new FileInputStream("konto.txt"));
  155.                 acc = (Konto) is.readObject();
  156.                 is.close();
  157.                 rezultatTF.setText("Wczytano");
  158.                 stanTF.setText(Integer.toString(acc.dajStan()));
  159.             } catch (IOException e){System.out.println("--wyjatek!");}
  160.             catch (ClassNotFoundException e){}
  161.         }
  162.     }
  163.  
  164.     public static void main(String[] arg){
  165.         JFrame gi = new Interface() ;
  166.         gi.setSize(300,200) ;
  167.     }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement