// 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, 100));
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // We used a grid layout so we could have 3 rows and 1 column
      window.setLayout(new GridLayout(3, 1));
        
      // We placed the label and textfield in a separate panel so that 
      // we could align them with a flow layout. This means that they will appear
      // next to each other horizontally and at their preferred sizes. This also
      // allowed us to put both of them in the first row of the GridLayout
      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);
      
      // We created another subpanel with FlowLayout for the buttons
      // for the same reason
      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);
      window.add(middleRow);
      
      MyActionListener listener = new MyActionListener();
      button1.addActionListener(listener);
      button2.addActionListener(listener);
      button3.addActionListener(listener);
      
      // We just added the result label directly because we didn't mind
      // if it stretched to fit the whole row of the grid and we didn't 
      // need to put multiple things in this row.
      total = new JLabel("");
      window.add(total);
        
      window.setVisible(true);
    
   }
   
   // 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.
   public 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);
       }
   }
}