Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. package wyplataInterfejs;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import java.io.*;
  7.  
  8. @SuppressWarnings("serial")
  9.  
  10. class DebetException extends Exception {
  11. int i;
  12.  
  13. DebetException() {
  14. super();
  15. }
  16.  
  17. DebetException(String msg) {
  18. super(msg);
  19. }
  20.  
  21. DebetException(int i) {
  22. this.i = -i;
  23. }
  24. }
  25.  
  26. class Przyciski extends JFrame {
  27. JTextField text = new JTextField(10);
  28. JButton b1 = new JButton("wpłata/wypłata");
  29. JTextField text2 = new JTextField(10);
  30. JTextField result = new JTextField(10);
  31. JButton unblock = new JButton("odblokuj");
  32. JButton undo = new JButton("cofnij");
  33. JButton save = new JButton("zapisz");
  34. JButton read = new JButton("wczytaj");
  35. Konto konto = new Konto();
  36.  
  37. Przyciski() {
  38. setTitle("Twoje konto");
  39. Container cp = getContentPane();
  40. cp.setLayout(new GridLayout(5, 2, 10, 10));
  41.  
  42. cp.add(new JLabel("Stan konta: "));
  43. cp.add(text);
  44. cp.add(b1);
  45. cp.add(text2);
  46. cp.add(result);
  47. cp.add(unblock);
  48. cp.add(save);
  49. cp.add(read);
  50. cp.add(undo);
  51. text.setText(Integer.toString(konto.dajStan()));
  52.  
  53. setDefaultCloseOperation(EXIT_ON_CLOSE);
  54. setSize(450, 800);
  55. setVisible(true);
  56.  
  57. unblock.addActionListener(new Unblock());
  58. b1.addActionListener(new Wyplata());
  59. undo.addActionListener(new Undo());
  60. save.addActionListener(new Save());
  61. read.addActionListener(new Read());
  62. }
  63.  
  64. int dajLiczbe(JTextField tf) {
  65. try {
  66. return Integer.parseInt(tf.getText());
  67. } catch (NumberFormatException e) {
  68. return 0;
  69. }
  70. }
  71.  
  72. class Unblock implements ActionListener {
  73. public void actionPerformed(ActionEvent e) {
  74. b1.setEnabled(true);
  75. text2.setEnabled(true);
  76. }
  77. }
  78. int r=50,g=40,b=30;
  79. Color Color = new Color(r%250,g%250,b%250);
  80. class Save implements ActionListener {
  81. public void actionPerformed(ActionEvent ev) {
  82. r+=20;
  83. g-=30;
  84. b+=25;
  85. read.setBackground(Color);
  86. read.setEnabled(true);
  87. save.setEnabled(false);
  88. try {
  89. FileOutputStream f = new FileOutputStream("zapis");
  90. ObjectOutputStream os = new ObjectOutputStream(f);
  91. os.writeObject(konto);
  92. os.writeObject(previous);
  93. f.close();
  94. } catch (IOException e) {
  95. }
  96. Color = new Color(r%250,g%250,b%250);
  97. }
  98. }
  99.  
  100. class Read implements ActionListener {
  101. public void actionPerformed(ActionEvent ev) {
  102. r+=40;
  103. g+=20;
  104. b-=10;
  105. save.setBackground(Color);
  106. read.setEnabled(false);
  107. save.setEnabled(true);
  108. Konto konto1 = new Konto();
  109. int previous1;
  110. //try {
  111. // ObjectInputStream is = new ObjectInputStream(new FileInputStream("obiektA"));
  112. // konto1 = (Konto) is.readObject();
  113. // previous1 = (Integer) is.readObject();
  114. // is.close();
  115. //} catch (IOException e) {
  116. // System.out.println("--wyjatek!");
  117. //} catch (ClassNotFoundException e) {
  118. //}
  119. konto = konto1;
  120. Color = new Color(r%250,g%250,b%250);
  121. }
  122. }
  123.  
  124. int previous;
  125.  
  126. class Undo implements ActionListener {
  127. public void actionPerformed(ActionEvent e) {
  128. konto.setStan(previous);
  129. undo.setEnabled(false);
  130. text.setText(Integer.toString(konto.dajStan()));// odświeżenie wypisania stanu konta
  131. }
  132. }
  133.  
  134. class Wyplata implements ActionListener {
  135. public void actionPerformed(ActionEvent e) {
  136. int r = konto.dajStan() - dajLiczbe(text2);
  137. if (r >= 0) {
  138. previous = konto.dajStan();
  139. result.setText("OK.");
  140. konto.setStan(r);
  141. text.setText(Integer.toString(konto.dajStan()));
  142. b1.setEnabled(false);
  143. text2.setEnabled(false);
  144. undo.setEnabled(true);
  145. } else {
  146. result.setText("Brak środków.");
  147. try {
  148. konto.e1(r);
  149.  
  150. } catch (DebetException exc) {
  151. result.setText(result.getText() + " Za dużo o " + Integer.toString(exc.i));
  152. }
  153. }
  154. }
  155. }
  156. }
  157.  
  158. class Konto {
  159. private int stan;
  160.  
  161. public void setStan(int stan) {
  162. this.stan = stan;
  163. }
  164.  
  165. Konto() {
  166. stan = 0;
  167. }
  168.  
  169. public void operacja(int ile) throws DebetException {
  170. if (stan + ile >= 0)
  171. stan += ile;
  172. else
  173. throw new DebetException();
  174. }
  175.  
  176. public int dajStan() {
  177. return stan;
  178. }
  179.  
  180. void e1(int n) throws DebetException {
  181. if (n < 0)
  182. throw new DebetException(n);
  183.  
  184. }
  185.  
  186. public static void main(String[] args) {
  187. JFrame f = new Przyciski();
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement