Skip to content

Instantly share code, notes, and snippets.

@Sevitte
Created December 17, 2017 22:51
Show Gist options
  • Select an option

  • Save Sevitte/94082cd8ffdde7aeb0cfcf6531afd351 to your computer and use it in GitHub Desktop.

Select an option

Save Sevitte/94082cd8ffdde7aeb0cfcf6531afd351 to your computer and use it in GitHub Desktop.
import java .awt.* ;
import java.awt.event.* ;
import javax.swing.*;
import static java.lang.Math.abs;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import javax.swing.event.ChangeEvent;
class Calculations//obliczenia
{
public int calculate(int n,int m) throws DebetException
{
if((n+m)>=0)
{
return n+m;
}
else throw new DebetException(n,m);
}
}
class DebetException extends Exception //wyjatek
{
int balance;
int operation;
DebetException(int b, int o)
{
balance = b;
operation = o;
}
String GetString()
{
return "o " + String.valueOf(abs(balance+operation)+ " za dużo");
}
}
public class Account extends JFrame
{
JTextField
data = new JTextField(20),
write = new JTextField(20),
result = new JTextField(20);
Calculations cal = new Calculations();
JButton
payment = new JButton("Wpłata/Wypłata") ,
unlock = new JButton("ODBLOKUJ");
Account()
{
setTitle("Konto Bankowe");
Container cp = getContentPane();
cp.setLayout(new GridLayout(5,2,10,10)) ;
cp.add(new JLabel("Stan:")) ;
cp.add(data) ;
data.setEditable(false);
cp.add(new JLabel(""));//odstep
cp.add(new JLabel(""));
payment.addActionListener(new Payments());
cp.add(payment) ;
cp.add(write);
data.setText("0");// inicjal danych
cp.add(new JLabel("")) ; // odstep
cp.add(new JLabel("")) ;
cp.add(result) ;
unlock.addActionListener(new Unlock());
cp.add(unlock) ;
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true) ;
}
int GetNumber(JTextField tf)
{
return Integer.parseInt(tf.getText()) ;
}
class Payments implements ActionListener//wplata/wyplata/lapanie wyjatku
{
public void actionPerformed(ActionEvent e)
{
unlock.setEnabled(true);
result.setEnabled(false);
result.setText("OK");
try
{
data.setText(Integer.toString(cal.calculate(GetNumber(write),(GetNumber(data))))) ;
}
catch(DebetException e1)
{
result.setText(e1.GetString());
}
catch(NumberFormatException exc)
{
result.setText("ZLY FORMAT");
}
payment.setEnabled(false);
}
}
class Unlock implements ActionListener //przycisk odblokuj
{
public void actionPerformed(ActionEvent e)
{
payment.setEnabled(true);
}
}
public static void main(String[] arg)
{
JFrame gi = new Account() ;
gi.setSize(400,200) ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment