Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package wyplataInterfejs;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.io.*;
- @SuppressWarnings("serial")
- class DebetException extends Exception {
- int i;
- DebetException() {
- super();
- }
- DebetException(String msg) {
- super(msg);
- }
- DebetException(int i) {
- this.i = -i;
- }
- }
- class Przyciski extends JFrame {
- JTextField text = new JTextField(10);
- JButton b1 = new JButton("wpłata/wypłata");
- JTextField text2 = new JTextField(10);
- JTextField result = new JTextField(10);
- JButton unblock = new JButton("odblokuj");
- JButton undo = new JButton("cofnij");
- JButton save = new JButton("zapisz");
- JButton read = new JButton("wczytaj");
- Konto konto = new Konto();
- Przyciski() {
- setTitle("Twoje konto");
- Container cp = getContentPane();
- cp.setLayout(new GridLayout(5, 2, 10, 10));
- cp.add(new JLabel("Stan konta: "));
- cp.add(text);
- cp.add(b1);
- cp.add(text2);
- cp.add(result);
- cp.add(unblock);
- cp.add(save);
- cp.add(read);
- cp.add(undo);
- text.setText(Integer.toString(konto.dajStan()));
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setSize(450, 800);
- setVisible(true);
- unblock.addActionListener(new Unblock());
- b1.addActionListener(new Wyplata());
- undo.addActionListener(new Undo());
- save.addActionListener(new Save());
- read.addActionListener(new Read());
- }
- int dajLiczbe(JTextField tf) {
- try {
- return Integer.parseInt(tf.getText());
- } catch (NumberFormatException e) {
- return 0;
- }
- }
- class Unblock implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- b1.setEnabled(true);
- text2.setEnabled(true);
- }
- }
- int r=50,g=40,b=30;
- Color Color = new Color(r%250,g%250,b%250);
- class Save implements ActionListener {
- public void actionPerformed(ActionEvent ev) {
- r+=20;
- g-=30;
- b+=25;
- read.setBackground(Color);
- read.setEnabled(true);
- save.setEnabled(false);
- try {
- FileOutputStream f = new FileOutputStream("zapis");
- ObjectOutputStream os = new ObjectOutputStream(f);
- os.writeObject(konto);
- os.writeObject(previous);
- f.close();
- } catch (IOException e) {
- }
- Color = new Color(r%250,g%250,b%250);
- }
- }
- class Read implements ActionListener {
- public void actionPerformed(ActionEvent ev) {
- r+=40;
- g+=20;
- b-=10;
- save.setBackground(Color);
- read.setEnabled(false);
- save.setEnabled(true);
- Konto konto1 = new Konto();
- int previous1;
- //try {
- // ObjectInputStream is = new ObjectInputStream(new FileInputStream("obiektA"));
- // konto1 = (Konto) is.readObject();
- // previous1 = (Integer) is.readObject();
- // is.close();
- //} catch (IOException e) {
- // System.out.println("--wyjatek!");
- //} catch (ClassNotFoundException e) {
- //}
- konto = konto1;
- Color = new Color(r%250,g%250,b%250);
- }
- }
- int previous;
- class Undo implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- konto.setStan(previous);
- undo.setEnabled(false);
- text.setText(Integer.toString(konto.dajStan()));// odświeżenie wypisania stanu konta
- }
- }
- class Wyplata implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- int r = konto.dajStan() - dajLiczbe(text2);
- if (r >= 0) {
- previous = konto.dajStan();
- result.setText("OK.");
- konto.setStan(r);
- text.setText(Integer.toString(konto.dajStan()));
- b1.setEnabled(false);
- text2.setEnabled(false);
- undo.setEnabled(true);
- } else {
- result.setText("Brak środków.");
- try {
- konto.e1(r);
- } catch (DebetException exc) {
- result.setText(result.getText() + " Za dużo o " + Integer.toString(exc.i));
- }
- }
- }
- }
- }
- class Konto {
- private int stan;
- public void setStan(int stan) {
- this.stan = stan;
- }
- 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;
- }
- void e1(int n) throws DebetException {
- if (n < 0)
- throw new DebetException(n);
- }
- public static void main(String[] args) {
- JFrame f = new Przyciski();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement