Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java .awt.* ;
- import java.awt.event.* ;
- import javax.swing.*;
- import java.io.*;
- import java.util.ArrayList;
- class DebetException extends Exception {}
- class Konto implements Serializable{
- private int stan;
- private ArrayList<Integer> operacje = new ArrayList<>();
- Konto() {
- stan = 0;
- }
- public void operacja(int ile) throws DebetException {
- if (stan + ile >= 0 )
- stan += ile;
- else
- throw new DebetException();
- }
- public int dajStan() {
- return stan ;
- }
- public ArrayList<Integer> getOperacje() {
- return operacje;
- }
- @Override
- public String toString() {
- return "Konto{" +
- "stan=" + stan +
- ", operacje=" + operacje +
- '}';
- }
- }
- class Interface extends JFrame {
- Konto acc = new Konto() ;
- JTextField
- stanTF = new JTextField(20),
- operacjaTF = new JTextField(20),
- rezultatTF = new JTextField(20);
- JButton
- operacja = new JButton("wpłata/wypłata") ,
- cofnij = new JButton("cofnij") ,
- odblokuj = new JButton("odblokuj"),
- zapisz = new JButton("zapisz"),
- wczytaj = new JButton("wczytaj");
- Interface (){
- setTitle("Konto Bankowe");
- Container cp = getContentPane();
- cp.setLayout(new GridLayout(5,2,10,10)) ;
- cp.add(new JLabel("Stan:")) ;
- cp.add(stanTF) ;
- stanTF.setText(Integer.toString(acc.dajStan()));
- stanTF.setEnabled(false);
- operacja.addActionListener(new operacjaL());
- odblokuj.addActionListener(new odblokujL());
- cp.add(operacja) ;
- cp.add(operacjaTF);
- cp.add(rezultatTF);
- cp.add(odblokuj);
- odblokuj.setEnabled(false);
- cofnij.addActionListener(new cofnijL());
- cp.add(cofnij);
- cp.add(new JLabel(""));
- zapisz.addActionListener(new zapisz());
- wczytaj.addActionListener(new wczytaj());
- cp.add(zapisz);
- cp.add(wczytaj);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true) ;
- }
- int dajLiczbe(JTextField tf) throws NumberFormatException {
- return Integer.parseInt(tf.getText());
- }
- class operacjaL implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- try {
- int n = dajLiczbe(operacjaTF);
- acc.operacja(n);
- acc.getOperacje().add(n);
- stanTF.setText(Integer.toString(acc.dajStan()));
- rezultatTF.setText("OK");
- operacja.setEnabled(false);
- operacjaTF.setEnabled(false);
- odblokuj.setEnabled(true);
- } catch (DebetException exc) {
- rezultatTF.setText(Integer.toString((acc.dajStan() + dajLiczbe(operacjaTF))*-1) + " za mało!");
- //rezultatTF.setText("BRAK ŚRODKÓW");
- operacja.setEnabled(false);
- operacjaTF.setEnabled(false);
- odblokuj.setEnabled(true);
- } catch (NumberFormatException exc){
- rezultatTF.setText("Kwota musi byc liczba");
- }
- }
- }
- class odblokujL implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- operacjaTF.setText("0");
- operacja.setEnabled(true);
- operacjaTF.setEnabled(true);
- odblokuj.setEnabled(false);
- }
- }
- class cofnijL implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- try {
- acc.operacja(-(acc.getOperacje().get(acc.getOperacje().size()-1)));
- acc.getOperacje().remove(acc.getOperacje().size() - 1);
- stanTF.setText(Integer.toString(acc.dajStan()));
- rezultatTF.setText("Cofnieto");
- operacja.setEnabled(false);
- operacjaTF.setEnabled(false);
- odblokuj.setEnabled(true);
- } catch (DebetException e1) {
- e1.printStackTrace();
- }
- }
- }
- class zapisz implements ActionListener{
- public void actionPerformed(ActionEvent actionEvent) {
- try{
- FileOutputStream f = new FileOutputStream("konto.txt");
- ObjectOutputStream os = new ObjectOutputStream(f);
- os.writeObject(acc);
- f.close();
- rezultatTF.setText("Zapisano!");
- } catch (IOException e){}
- }
- }
- class wczytaj implements ActionListener{
- public void actionPerformed(ActionEvent actionEvent) {
- try{
- ObjectInputStream is = new ObjectInputStream(
- new FileInputStream("konto.txt"));
- acc = (Konto) is.readObject();
- is.close();
- rezultatTF.setText("Wczytano");
- stanTF.setText(Integer.toString(acc.dajStan()));
- } catch (IOException e){System.out.println("--wyjatek!");}
- catch (ClassNotFoundException e){}
- }
- }
- public static void main(String[] arg){
- JFrame gi = new Interface() ;
- gi.setSize(300,200) ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement