Advertisement
Guest User

OrderCalculatorGUI

a guest
Jul 20th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.text.DecimalFormat;
  5.  
  6. /**
  7. The OrderCalculatorGUI class creates the GUI for the
  8. Brandi's Bagel House application.
  9. */
  10.  
  11. public class OrderCalculatorGUI extends JFrame
  12. {
  13. private BagelPanel bagels; // Bagel panel
  14. private ToppingPanel toppings; // Topping panel
  15. private CoffeePanel coffee; // Coffee panel
  16. private GreetingPanel banner; // To display a greeting
  17. private JPanel buttonPanel; // To hold the buttons
  18. private JButton calcButton; // To calculate the cost
  19. private JButton exitButton; // To exit the application
  20. private final double TAX_RATE = 0.06; // Sales tax rate
  21.  
  22. /**
  23. Constructor
  24. */
  25.  
  26. public OrderCalculatorGUI()
  27. {
  28. // Display a title.
  29. setTitle("Order Calculator");
  30.  
  31. // Specify an action for the close button.
  32. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.  
  34. // Create a BorderLayout manager.
  35. setLayout(new BorderLayout());
  36.  
  37. // Create the custom panels.
  38. banner = new GreetingPanel();
  39. bagels = new BagelPanel();
  40. toppings = new ToppingPanel();
  41. coffee = new CoffeePanel();
  42.  
  43. // Create the button panel.
  44. buildButtonPanel();
  45.  
  46. // Add the components to the content pane.
  47. add(banner, BorderLayout.NORTH);
  48. add(bagels, BorderLayout.WEST);
  49. add(toppings, BorderLayout.CENTER);
  50. add(coffee, BorderLayout.EAST);
  51. add(buttonPanel, BorderLayout.SOUTH);
  52.  
  53. // Pack the contents of the window and display it.
  54. pack();
  55. setVisible(true);
  56. }
  57.  
  58. /**
  59. The buildButtonPanel method builds the button panel.
  60. */
  61.  
  62. private void buildButtonPanel()
  63. {
  64. // Create a panel for the buttons.
  65. buttonPanel = new JPanel();
  66.  
  67. // Create the buttons.
  68. calcButton = new JButton("Calculate");
  69. exitButton = new JButton("Exit");
  70.  
  71. // Register the action listeners.
  72. calcButton.addActionListener(new CalcButtonListener());
  73. exitButton.addActionListener(new ExitButtonListener());
  74.  
  75. // Add the buttons to the button panel.
  76. buttonPanel.add(calcButton);
  77. buttonPanel.add(exitButton);
  78. }
  79.  
  80. /**
  81. Private inner class that handles the event when
  82. the user clicks the Calculate button.
  83. */
  84.  
  85. private class CalcButtonListener implements ActionListener
  86. {
  87. public void actionPerformed(ActionEvent e)
  88. {
  89. // Variables to hold the subtotal, tax, and total
  90. double subtotal, tax, total;
  91.  
  92. // Calculate the subtotal.
  93. subtotal = bagels.getBagelCost() +
  94. toppings.getToppingCost() +
  95. coffee.getCoffeeCost();
  96.  
  97. // Calculate the sales tax.
  98. tax = subtotal * TAX_RATE;
  99.  
  100. // Calculate the total.
  101. total = subtotal + tax;
  102.  
  103. // Create a DecimalFormat object to format output.
  104. DecimalFormat dollar = new DecimalFormat("0.00");
  105.  
  106. // Display the charges.
  107. JOptionPane.showMessageDialog(null, "Subtotal: $" +
  108. dollar.format(subtotal) + "\n" +
  109. "Tax: $" + dollar.format(tax) + "\n" +
  110. "Total: $" + dollar.format(total));
  111. }
  112. }
  113.  
  114. /**
  115. Private inner class that handles the event when
  116. the user clicks the Exit button.
  117. */
  118.  
  119. private class ExitButtonListener implements ActionListener
  120. {
  121. public void actionPerformed(ActionEvent e)
  122. {
  123. System.exit(0);
  124. }
  125. }
  126.  
  127. /**
  128. main method
  129. */
  130.  
  131. public static void main(String[] args)
  132. {
  133.  
  134.  
  135. new OrderCalculatorGUI();
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement