// Allison Obourn
// CS& 141, Winter 2021
// Lecture 17

// An object template class that creates a GUI when constructed. This GUI
// allows the user to type a number representing a bill subtotal in a textbox 
// and then when the user clicks a button calculates the total bill if the user
// were to leave a tip of the percentage amount on the button. The options are 
// 15%, 20% and 25%. The textbox and label are displayed on one line, the three
// buttons on the next line and the caculated total on the last.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TipCalculatorGUI {
   private JTextField input;
   private JButton button1;
   private JButton button2;
   private JLabel total;
   
   public TipCalculatorGUI() {
      JFrame window = new JFrame();
      window.setSize(new Dimension(300, 150));
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setLayout(new GridLayout(3, 1));
        
      createTopPanel(window);
            
      MyActionListener listener = new MyActionListener();
      createMiddlePanel(window, listener);
      
      total = new JLabel("");
      window.add(total);
        
      window.setVisible(true);
    
   }
   
   // Creates a panel and adds three buttons to it that will appear
   // side by side and adds this panel to the passed in window
   private void createMiddlePanel(JFrame window, MyActionListener listener) {
      JPanel middleRow = new JPanel(new FlowLayout());
      button1 = new JButton("15%");
      middleRow.add(button1);
      button2 = new JButton("20%");
      middleRow.add(button2);
      JButton button3 = new JButton("25%");
      middleRow.add(button3);
      
      button1.addActionListener(listener);
      button2.addActionListener(listener);
      button3.addActionListener(listener);
   
      window.add(middleRow);
   }
   
   // Creates a panel with a label and the text box next to each other
   // and adds this panel to the passed in window
   private void createTopPanel(JFrame window) {
      JPanel topRow = new JPanel(new FlowLayout());
      JLabel label = new JLabel("Bill Subtotal:");
      topRow.add(label);
      input = new JTextField(10);
      topRow.add(input);
      window.add(topRow);

   }
   
   // Calculates the total bill (subtotal and tip) when a button
   // with a tip amount is clicked. Uses the tip percentage written on
   // the pressed button.
   private class MyActionListener implements ActionListener {
       public void actionPerformed(ActionEvent event){
            // check which button was pressed and use it to choose the 
            // right percent for the tip
            double percent = .25;
            if(button1 == event.getSource()) {
               percent = .15;
            } else if(button2 == event.getSource()) {
               percent = .20;
            } 
            // Convert the number the user typed into the textbox into a number
            int subtotal = Integer.parseInt(input.getText());
            double costTotal = subtotal + subtotal * percent;
            total.setText("= $" + costTotal);
       }
   }
}