Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. package konto;
  2.  
  3. //import gidoobl.Obl2L.Odblokuj;
  4. import java .awt.* ;
  5. import java.awt.event.* ;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.Serializable;
  12. import java.util.ArrayList;
  13. import javax.swing.*;
  14.  
  15.  
  16. class Obliczenia{
  17.  
  18. int i;
  19. int[] tablica = new int[999999];
  20. boolean wartosc = true;
  21. public int stanKonta;
  22.  
  23. public int wplata(int kwota, int stan) {
  24.  
  25. if(kwota+stan>=0)
  26. return stan+kwota ;
  27. else
  28. return stan;
  29. }
  30.  
  31.  
  32. public String operacja(int kwota, int stan) {
  33.  
  34. int wynik=kwota+stan;
  35.  
  36. if(wynik>=0){
  37. wartosc = true;
  38. stanKonta = wynik;
  39. return "OK";
  40. }
  41. else{
  42.  
  43. wartosc = false;
  44. wynik = wynik * (-1);
  45. return "Czcesz wypłacić o " + wynik + " za dużo";}
  46.  
  47.  
  48. }
  49.  
  50. public void ustaw(int kwota){
  51.  
  52. tablica[i] = kwota;
  53. i++;
  54.  
  55. }
  56.  
  57. public int cofnij( int stan){
  58.  
  59. i--;
  60. if(i==0)
  61. wartosc=false;
  62. stan=stan-tablica[i];
  63. return stan;
  64.  
  65.  
  66. }
  67.  
  68. public String mozliwosc(){
  69.  
  70. if(i<1 && wartosc==false)
  71. return "Nie można cofnąć;";
  72. else
  73. return "Można cofnąć";
  74.  
  75. }
  76.  
  77. public int zerowanie(){return 0;}
  78.  
  79. }
  80.  
  81. public class Konto extends JFrame implements Serializable{
  82.  
  83. int money;
  84. private int rozmiar = 0;
  85. ArrayList<Integer> operacje = new ArrayList<>();
  86.  
  87. JTextField
  88. stan = new JTextField(20),
  89. kwota = new JTextField(20),
  90. rezultat = new JTextField(20),
  91. mozliwosc = new JTextField(20);
  92.  
  93. Obliczenia ob = new Obliczenia() ;
  94.  
  95. JButton
  96. obl1 = new JButton("Wpłata/wypłata") ,
  97. obl2 = new JButton("Odblokuj") ,
  98. cofnij = new JButton("Cofnij"),
  99. zapisz = new JButton("Zapisz"),
  100. wczytaj = new JButton("Wczytaj");
  101.  
  102. Konto(){
  103. this.money = ob.stanKonta;
  104. setTitle("KONTO");
  105. Container cp = getContentPane();
  106. cp.setLayout(new GridLayout(5,5,20,20)) ;
  107. cp.add(new JLabel("Stan:")) ;
  108. cp.add(stan) ;
  109. stan.setEditable(false);
  110. //stan.setText(Integer.toString(1000));
  111.  
  112. obl1.addActionListener(new Obl1L());
  113. obl2.addActionListener(new Odblokuj());
  114. cofnij.addActionListener(new Cofnij());
  115. zapisz.addActionListener(new Zapisz());
  116. // wczytaj.addActionListener(new Wczytaj());
  117.  
  118. cp.add(obl1) ;
  119. cp.add(kwota) ;
  120. cp.add(rezultat);
  121. cp.add(obl2) ;
  122. cp.add(cofnij);
  123. cp.add(mozliwosc);
  124. cp.add(zapisz);
  125. cp.add(wczytaj);
  126.  
  127. mozliwosc.setText("Nie można cofnąć");
  128. cofnij.setEnabled(false);
  129.  
  130. setDefaultCloseOperation(EXIT_ON_CLOSE);
  131. setVisible(true) ;
  132. }
  133.  
  134.  
  135. int dajLiczbe(JTextField tf){
  136. try{
  137. return Integer.parseInt(tf.getText()) ;
  138. }
  139. catch (NumberFormatException e){ return 0 ; }
  140. }
  141.  
  142.  
  143.  
  144. class Obl1L implements ActionListener{
  145.  
  146. public void actionPerformed(ActionEvent e){
  147.  
  148. try{
  149.  
  150. int bufor = Integer.parseInt(kwota.getText());
  151.  
  152. operacje.add(bufor);
  153.  
  154. System.out.println(operacje.get(rozmiar));
  155.  
  156. rozmiar++;
  157.  
  158. stan.setText(Integer.toString(ob.wplata(dajLiczbe(kwota), dajLiczbe(stan)))) ;
  159.  
  160. money = Integer.parseInt(stan.getText());
  161.  
  162. obl1.setEnabled(false);
  163.  
  164. kwota.setEditable(false);
  165.  
  166. rezultat.setText((ob.operacja(dajLiczbe(kwota), dajLiczbe(stan)))) ;
  167.  
  168. if(ob.wartosc==true)
  169. ob.ustaw(dajLiczbe(kwota));
  170.  
  171. mozliwosc.setText(ob.mozliwosc());
  172.  
  173. if(ob.i>0) cofnij.setEnabled(true);
  174. System.out.println("stan konta: " + dajLiczbe(stan));
  175.  
  176. }
  177.  
  178. catch(NumberFormatException exp5){
  179.  
  180. kwota.setText("to nie jest liczba");
  181.  
  182.  
  183. }
  184.  
  185. }
  186.  
  187. }
  188.  
  189. class Odblokuj implements ActionListener{
  190.  
  191. public void actionPerformed(ActionEvent e){
  192.  
  193. obl1.setEnabled(true);
  194.  
  195. kwota.setText(Integer.toString(ob.zerowanie()));
  196.  
  197. kwota.setEditable(true);
  198.  
  199. if(ob.i>0)
  200. cofnij.setEnabled(true);
  201.  
  202. mozliwosc.setText(ob.mozliwosc());
  203.  
  204. ob.wartosc=true;
  205.  
  206. }
  207.  
  208. }
  209.  
  210. class Cofnij implements ActionListener{
  211.  
  212. public void actionPerformed(ActionEvent e){
  213.  
  214.  
  215.  
  216. if(ob.wartosc == true || ob.i>0){
  217. rozmiar--;
  218. System.out.println(operacje.get(rozmiar));
  219.  
  220. stan.setText(Integer.toString(
  221. ob.cofnij(dajLiczbe(stan)))) ;
  222.  
  223. if(ob.i==0)
  224. cofnij.setEnabled(false);
  225.  
  226. mozliwosc.setText(ob.mozliwosc());}
  227.  
  228. }
  229. }
  230.  
  231. class Zapisz implements ActionListener{
  232.  
  233. public void actionPerformed(ActionEvent e){
  234.  
  235. try (FileOutputStream f = new FileOutputStream("konto")) {
  236. ObjectOutputStream os = new ObjectOutputStream(f);
  237.  
  238. os.writeObject(money);
  239.  
  240. os.writeObject(ob.tablica);
  241. os.writeObject(ob.wartosc);
  242. os.writeObject(rozmiar);
  243. os.writeObject(operacje);
  244. f.close();
  245.  
  246. }
  247. catch (IOException exp1) {
  248.  
  249. }
  250.  
  251. }
  252. }
  253.  
  254. class Wczytaj implements ActionListener{
  255.  
  256. public void actionPerformed(ActionEvent e){
  257.  
  258. try (FileInputStream f = new FileInputStream("konto")) {
  259. ObjectInputStream in = new ObjectInputStream(f);
  260.  
  261. int NewStan = (int) in.readObject();
  262. money = NewStan;
  263.  
  264.  
  265. int[] NewTab = (int[]) in.readObject();
  266. ob.tablica = NewTab;
  267.  
  268. boolean NewWartosc = (boolean) in.readObject();
  269. ob.wartosc = NewWartosc;
  270.  
  271. int NewRozmiar = (int) in.readObject();
  272. rozmiar = NewRozmiar;
  273.  
  274. ArrayList<Integer> newOperacje = (ArrayList<Integer>) in.readObject();
  275. operacje = newOperacje;
  276.  
  277. stan.setText(Integer.toString(money));
  278.  
  279.  
  280. }
  281. catch (IOException ex) {
  282.  
  283. } catch (ClassNotFoundException ex1) {
  284.  
  285. }
  286.  
  287. }
  288. }
  289.  
  290.  
  291.  
  292. public static void main(String[] arg){
  293.  
  294. JFrame gi = new Konto() ;
  295. gi.setSize(500,400) ;
  296. }
  297.  
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement