package edu.school;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Информатика, 8 клас, издателство Просвета, 2017
 * Автори: Светла Бойчева, Николина Николова, Елиза Стефанова, Антон Денев
 * 3.1.2. Целочислени типове данни
 * Урок: Целочислени типове данни – упражнение
 * Задача: Да се реализира приложение PiggyBank, което
 * да дава възможност да се въведат брой монети от 
 * всеки вид в касичка и да пресмята общата сума в лева и стотинки.
 * 
 * Вариант 1.
 */

public class PiggyBankFrame extends JFrame {

	private JPanel contentPane;
	private JTextField txtLeva2;
	private JTextField txtLev1;
	private JTextField txtStotinki50;
	private JTextField txtStotinki20;
	private JTextField txtStotinki10;
	private JTextField txtStotinki5;
	private JTextField txtStotinki2;
	private JTextField txtStotinka1;
	private JLabel lblLev1;
	private JLabel lblStotinki50;
	private JLabel lblStotinki20;
	private JLabel lblStotink10;
	private JLabel lblStotinki5;
	private JLabel lblStotinki2;
	private JLabel lblStotinka1;
	private JLabel lblResultMessage;
	private JLabel lblResultLeva;
	private JLabel lblResultStotinki;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PiggyBankFrame frame = new PiggyBankFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PiggyBankFrame() {
		setFont(new Font("AngsanaUPC", Font.PLAIN, 18));
		setForeground(Color.BLACK);
		setTitle("\u041A\u0430\u0441\u0438\u0447\u043A\u0430");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 346, 379);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(255, 175, 175));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblLeva2 = new JLabel("2 \u043B\u0432.");
		lblLeva2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblLeva2.setBounds(30, 27, 46, 14);
		contentPane.add(lblLeva2);

		txtLeva2 = new JTextField();
		txtLeva2.setText("0");
		txtLeva2.setBounds(86, 24, 86, 20);
		contentPane.add(txtLeva2);
		txtLeva2.setColumns(10);

		txtLev1 = new JTextField();
		txtLev1.setText("0");
		txtLev1.setBounds(86, 59, 86, 20);
		contentPane.add(txtLev1);
		txtLev1.setColumns(10);

		txtStotinki50 = new JTextField();
		txtStotinki50.setText("0");
		txtStotinki50.setColumns(10);
		txtStotinki50.setBounds(86, 101, 86, 20);
		contentPane.add(txtStotinki50);

		txtStotinki20 = new JTextField();
		txtStotinki20.setText("0");
		txtStotinki20.setColumns(10);
		txtStotinki20.setBounds(86, 136, 86, 20);
		contentPane.add(txtStotinki20);

		txtStotinki10 = new JTextField();
		txtStotinki10.setText("0");
		txtStotinki10.setColumns(10);
		txtStotinki10.setBounds(86, 177, 86, 20);
		contentPane.add(txtStotinki10);

		txtStotinki5 = new JTextField();
		txtStotinki5.setText("0");
		txtStotinki5.setColumns(10);
		txtStotinki5.setBounds(86, 212, 86, 20);
		contentPane.add(txtStotinki5);

		txtStotinki2 = new JTextField();
		txtStotinki2.setText("0");
		txtStotinki2.setColumns(10);
		txtStotinki2.setBounds(86, 253, 86, 20);
		contentPane.add(txtStotinki2);

		txtStotinka1 = new JTextField();
		txtStotinka1.setText("0");
		txtStotinka1.setColumns(10);
		txtStotinka1.setBounds(86, 288, 86, 20);
		contentPane.add(txtStotinka1);

		JButton btnCalc = new JButton("\u041F\u0420\u0415\u0421\u041C\u0415\u0422\u041D\u0418");
		btnCalc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				int leva2 = Integer.parseInt(txtLeva2.getText());
				int lev1 = Integer.parseInt(txtLev1.getText());
				int stotinki50 = Integer.parseInt(txtStotinki50.getText());
				int stotinki20 = Integer.parseInt(txtStotinki20.getText());
				int stotinki10 = Integer.parseInt(txtStotinki10.getText());
				int stotinki5 = Integer.parseInt(txtStotinki5.getText());
				int stotinki2 = Integer.parseInt(txtStotinki2.getText());
				int stotinka1 = Integer.parseInt(txtStotinka1.getText());

				int totalStotinki = 200 * leva2 + 100 * lev1 + 50 * stotinki50 + 20 * stotinki20 + 10 * stotinki10
						+ 5 * stotinki5 + 2 * stotinki2 + stotinka1;

				int resultLeva = totalStotinki / 100;
				int resultStotinki = totalStotinki % 100;

				String resultLevaMessage = Integer.toString(resultLeva) + " лв. и ";
				String resultStotinkiMessage = Integer.toString(resultStotinki) + " ст.";

				lblResultLeva.setText(resultLevaMessage);
				lblResultStotinki.setText(resultStotinkiMessage);
			}
		});
		btnCalc.setBounds(200, 23, 109, 53);
		contentPane.add(btnCalc);

		JButton btnClear = new JButton("\u0418\u0417\u0427\u0418\u0421\u0422\u0418");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				txtLeva2.setText("0");
				txtLev1.setText("0");
				txtStotinki50.setText("0");
				txtStotinki20.setText("0");
				txtStotinki10.setText("0");
				txtStotinki5.setText("0");
				txtStotinki2.setText("0");
				txtStotinka1.setText("0");
				lblResultLeva.setText("0 лв. и");
				lblResultStotinki.setText("0 ст.");

			}
		});
		btnClear.setBounds(200, 100, 109, 53);
		contentPane.add(btnClear);

		JButton btnExit = new JButton("\u0418\u0417\u0425\u041E\u0414");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				System.exit(0);

			}
		});
		btnExit.setBounds(200, 176, 109, 53);
		contentPane.add(btnExit);

		lblLev1 = new JLabel("1 \u043B\u0432.");
		lblLev1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblLev1.setBounds(30, 62, 46, 14);
		contentPane.add(lblLev1);

		lblStotinki50 = new JLabel("50 \u0441\u0442.");
		lblStotinki50.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblStotinki50.setBounds(30, 104, 46, 14);
		contentPane.add(lblStotinki50);

		lblStotinki20 = new JLabel("20 \u0441\u0442.");
		lblStotinki20.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblStotinki20.setBounds(30, 139, 46, 14);
		contentPane.add(lblStotinki20);

		lblStotink10 = new JLabel("10 \u0441\u0442.");
		lblStotink10.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblStotink10.setBounds(30, 180, 46, 14);
		contentPane.add(lblStotink10);

		lblStotinki5 = new JLabel("5 \u0441\u0442.");
		lblStotinki5.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblStotinki5.setBounds(30, 215, 46, 14);
		contentPane.add(lblStotinki5);

		lblStotinki2 = new JLabel("2 \u0441\u0442.");
		lblStotinki2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblStotinki2.setBounds(30, 256, 46, 14);
		contentPane.add(lblStotinki2);

		lblStotinka1 = new JLabel("1 \u0441\u0442.");
		lblStotinka1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblStotinka1.setBounds(30, 291, 46, 14);
		contentPane.add(lblStotinka1);

		lblResultMessage = new JLabel("\u041E\u0431\u0449\u043E:");
		lblResultMessage.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblResultMessage.setBounds(215, 253, 74, 17);
		contentPane.add(lblResultMessage);

		lblResultLeva = new JLabel("0 \u043B\u0432. \u0438 ");
		lblResultLeva.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblResultLeva.setBounds(215, 270, 74, 17);
		contentPane.add(lblResultLeva);

		lblResultStotinki = new JLabel("0 \u0441\u0442.");
		lblResultStotinki.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblResultStotinki.setBounds(215, 288, 74, 17);
		contentPane.add(lblResultStotinki);
	}
}
