Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

19. Open the solution file contained in the VbReloaded2015lChapo5Bonus Solution

ID: 3810452 • Letter: 1

Question

19. Open the solution file contained in the VbReloaded2015lChapo5Bonus Solution folder. (4, 6, 8, 10) a. The user will enter the sales amount as an integer in the sales TextBox, which should accept only numbers and the Backspace key. Code the appropriate event procedure. b. The calcButton Click procedure should display the salespersons bonus. A salesperson with sales from $0 through $3,500 receives a 1% bonus. A salesperson with sales from $3,501 through $10,000 receives a 5% bonus. A salesperson whose sales are more than $10,000 receives a 10% bonus. The procedure should display the bonus, formatted with a dollar sign and two decimal places, in the bonusLabel. The procedure should not make any calculations when the sales TextBox is empty rather, it should display an appropriate message in a message box. Code the procedure.

Explanation / Answer

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import javax.swing.text.NumberFormatter;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;

import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;

public class BonusTime {

   private JFrame frame;
   private JTextField textSales;
   private JTextField textField_1;

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

   /**
   * Create the application.
   */
   public BonusTime() {
       initialize();
   }

   /**
   * Initialize the contents of the frame.
   */
   private void initialize() {
       frame = new JFrame();
       frame.setBounds(100, 100, 450, 300);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.getContentPane().setLayout(null);
      
       JLabel lblBonusTime = new JLabel("Bonus Time");
       lblBonusTime.setFont(new Font("Stencil", Font.BOLD, 25));
       lblBonusTime.setForeground(Color.BLUE);
       lblBonusTime.setHorizontalAlignment(SwingConstants.CENTER);
       lblBonusTime.setBounds(54, 27, 297, 48);
       frame.getContentPane().add(lblBonusTime);
      
       JLabel lblSales = new JLabel("Sales :");
       lblSales.setBounds(64, 86, 46, 14);
       frame.getContentPane().add(lblSales);
      
       NumberFormat format = NumberFormat.getInstance();
   NumberFormatter formatter = new NumberFormatter(format);
   formatter.setValueClass(Integer.class);
   formatter.setMinimum(0);
   formatter.setMaximum(Integer.MAX_VALUE);
   formatter.setAllowsInvalid(false);
   // If you want the value to be committed on each keystroke instead of focus lost
   formatter.setCommitsOnValidEdit(true);
   JFormattedTextField field = new JFormattedTextField(formatter);
   field.setBounds(112, 83, 94, 20);
//       textSales = new JTextField();
//       textSales.setBounds(112, 83, 94, 20);
       frame.getContentPane().add(field);
       field.setColumns(10);
      
       JButton btnCalculate = new JButton("Calculate");
       btnCalculate.setBounds(244, 82, 89, 23);
       btnCalculate.addActionListener(new ActionListener() {
          
           @Override
           public void actionPerformed(ActionEvent e) {
               int val = (int) field.getValue();
               if(val<=3500){
                   textField_1.setText("$"+(.01*val));
               }
               else if(val>3500 && val<=10000){
                   textField_1.setText("$"+(.05*val));
               }
               else if(val>10000){
                   textField_1.setText("$"+(.1*val));
               }else{
                   textField_1.setText("Invalid");
               }
           }
       });
       frame.getContentPane().add(btnCalculate);
      
       JLabel lblBonus = new JLabel("Bonus :");
       lblBonus.setBounds(64, 130, 46, 14);
       frame.getContentPane().add(lblBonus);
      
       textField_1 = new JTextField();
       textField_1.setBounds(112, 127, 94, 20);
       frame.getContentPane().add(textField_1);
       textField_1.setColumns(10);
      
       JButton btnNewButton = new JButton("Exit");
       btnNewButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               frame.setVisible(false);
           }
       });
       btnNewButton.setBounds(244, 126, 89, 23);
       frame.getContentPane().add(btnNewButton);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote