Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package konto;
- //import gidoobl.Obl2L.Odblokuj;
- import java .awt.* ;
- import java.awt.event.* ;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- import java.util.ArrayList;
- import javax.swing.*;
- class Obliczenia{
- int i;
- int[] tablica = new int[999999];
- boolean wartosc = true;
- public int stanKonta;
- public int wplata(int kwota, int stan) {
- if(kwota+stan>=0)
- return stan+kwota ;
- else
- return stan;
- }
- public String operacja(int kwota, int stan) {
- int wynik=kwota+stan;
- if(wynik>=0){
- wartosc = true;
- stanKonta = wynik;
- return "OK";
- }
- else{
- wartosc = false;
- wynik = wynik * (-1);
- return "Czcesz wypłacić o " + wynik + " za dużo";}
- }
- public void ustaw(int kwota){
- tablica[i] = kwota;
- i++;
- }
- public int cofnij( int stan){
- i--;
- if(i==0)
- wartosc=false;
- stan=stan-tablica[i];
- return stan;
- }
- public String mozliwosc(){
- if(i<1 && wartosc==false)
- return "Nie można cofnąć;";
- else
- return "Można cofnąć";
- }
- public int zerowanie(){return 0;}
- }
- public class Konto extends JFrame implements Serializable{
- int money;
- private int rozmiar = 0;
- ArrayList<Integer> operacje = new ArrayList<>();
- JTextField
- stan = new JTextField(20),
- kwota = new JTextField(20),
- rezultat = new JTextField(20),
- mozliwosc = new JTextField(20);
- Obliczenia ob = new Obliczenia() ;
- JButton
- obl1 = new JButton("Wpłata/wypłata") ,
- obl2 = new JButton("Odblokuj") ,
- cofnij = new JButton("Cofnij"),
- zapisz = new JButton("Zapisz"),
- wczytaj = new JButton("Wczytaj");
- Konto(){
- this.money = ob.stanKonta;
- setTitle("KONTO");
- Container cp = getContentPane();
- cp.setLayout(new GridLayout(5,5,20,20)) ;
- cp.add(new JLabel("Stan:")) ;
- cp.add(stan) ;
- stan.setEditable(false);
- //stan.setText(Integer.toString(1000));
- obl1.addActionListener(new Obl1L());
- obl2.addActionListener(new Odblokuj());
- cofnij.addActionListener(new Cofnij());
- zapisz.addActionListener(new Zapisz());
- // wczytaj.addActionListener(new Wczytaj());
- cp.add(obl1) ;
- cp.add(kwota) ;
- cp.add(rezultat);
- cp.add(obl2) ;
- cp.add(cofnij);
- cp.add(mozliwosc);
- cp.add(zapisz);
- cp.add(wczytaj);
- mozliwosc.setText("Nie można cofnąć");
- cofnij.setEnabled(false);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true) ;
- }
- int dajLiczbe(JTextField tf){
- try{
- return Integer.parseInt(tf.getText()) ;
- }
- catch (NumberFormatException e){ return 0 ; }
- }
- class Obl1L implements ActionListener{
- public void actionPerformed(ActionEvent e){
- try{
- int bufor = Integer.parseInt(kwota.getText());
- operacje.add(bufor);
- System.out.println(operacje.get(rozmiar));
- rozmiar++;
- stan.setText(Integer.toString(ob.wplata(dajLiczbe(kwota), dajLiczbe(stan)))) ;
- money = Integer.parseInt(stan.getText());
- obl1.setEnabled(false);
- kwota.setEditable(false);
- rezultat.setText((ob.operacja(dajLiczbe(kwota), dajLiczbe(stan)))) ;
- if(ob.wartosc==true)
- ob.ustaw(dajLiczbe(kwota));
- mozliwosc.setText(ob.mozliwosc());
- if(ob.i>0) cofnij.setEnabled(true);
- System.out.println("stan konta: " + dajLiczbe(stan));
- }
- catch(NumberFormatException exp5){
- kwota.setText("to nie jest liczba");
- }
- }
- }
- class Odblokuj implements ActionListener{
- public void actionPerformed(ActionEvent e){
- obl1.setEnabled(true);
- kwota.setText(Integer.toString(ob.zerowanie()));
- kwota.setEditable(true);
- if(ob.i>0)
- cofnij.setEnabled(true);
- mozliwosc.setText(ob.mozliwosc());
- ob.wartosc=true;
- }
- }
- class Cofnij implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if(ob.wartosc == true || ob.i>0){
- rozmiar--;
- System.out.println(operacje.get(rozmiar));
- stan.setText(Integer.toString(
- ob.cofnij(dajLiczbe(stan)))) ;
- if(ob.i==0)
- cofnij.setEnabled(false);
- mozliwosc.setText(ob.mozliwosc());}
- }
- }
- class Zapisz implements ActionListener{
- public void actionPerformed(ActionEvent e){
- try (FileOutputStream f = new FileOutputStream("konto")) {
- ObjectOutputStream os = new ObjectOutputStream(f);
- os.writeObject(money);
- os.writeObject(ob.tablica);
- os.writeObject(ob.wartosc);
- os.writeObject(rozmiar);
- os.writeObject(operacje);
- f.close();
- }
- catch (IOException exp1) {
- }
- }
- }
- class Wczytaj implements ActionListener{
- public void actionPerformed(ActionEvent e){
- try (FileInputStream f = new FileInputStream("konto")) {
- ObjectInputStream in = new ObjectInputStream(f);
- int NewStan = (int) in.readObject();
- money = NewStan;
- int[] NewTab = (int[]) in.readObject();
- ob.tablica = NewTab;
- boolean NewWartosc = (boolean) in.readObject();
- ob.wartosc = NewWartosc;
- int NewRozmiar = (int) in.readObject();
- rozmiar = NewRozmiar;
- ArrayList<Integer> newOperacje = (ArrayList<Integer>) in.readObject();
- operacje = newOperacje;
- stan.setText(Integer.toString(money));
- }
- catch (IOException ex) {
- } catch (ClassNotFoundException ex1) {
- }
- }
- }
- public static void main(String[] arg){
- JFrame gi = new Konto() ;
- gi.setSize(500,400) ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement