Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. /*
  2. author: kkosak
  3. program: PaintStore
  4. date: december.2016
  5. */
  6.  
  7. import java.awt.*;
  8. import javax.swing.*;
  9. import java.awt.event.*;
  10. import java.text.*;
  11. import java.text.DecimalFormat.*;
  12.  
  13. public class PaintStore extends JFrame
  14. {
  15. //declarations
  16.  
  17. Color black = new Color(0, 0, 0);
  18. Color white = new Color(255, 255, 255);
  19.  
  20. JRadioButton deluxePaintJRadioButton;
  21. JRadioButton premiumPaintJRadioButton;
  22.  
  23. JLabel amountOfGallonsJLabel;
  24. JTextField amountOfGallonsJTextField;
  25.  
  26. JLabel amountOfSalesTaxJLabel;
  27. JTextField amountOfSalesTaxJTextField;
  28.  
  29. JLabel totalAmountJLabel;
  30. JTextField totalAmountJTextField;
  31.  
  32. JButton enterJButton;
  33. JButton clearJButton;
  34. JButton closeJButton;
  35.  
  36. final float DELUXE_PAINT = 24.95f;
  37. final float PREMIUM_PAINT = 34.95f;
  38. final float TAX_RATE = .07f;
  39.  
  40. float amountOfGallons;
  41. float paintPrice;
  42. float salesTotal;
  43. float totalAmount;
  44.  
  45. ButtonGroup displayButtonGroup;
  46. DecimalFormat decimalFormat;
  47. PaintCalculator paintCalculator;
  48.  
  49.  
  50. public PaintStore()
  51. {
  52. createUserInterface();
  53. }
  54.  
  55. public void createUserInterface()
  56. {
  57. Container contentPane = getContentPane();
  58. contentPane.setBackground(white);
  59. contentPane.setLayout(null);
  60.  
  61. displayButtonGroup = new ButtonGroup();
  62.  
  63. deluxePaintJRadioButton = new JRadioButton();
  64. deluxePaintJRadioButton.setBounds(150, 20, 100, 20);
  65. deluxePaintJRadioButton.setText("Deluxe Paint $24.95");
  66. deluxePaintJRadioButton.setSelected(true);
  67. deluxePaintJRadioButton.setForeground(black);
  68. deluxePaintJRadioButton.setBackground(white);
  69. displayButtonGroup.add(deluxePaintJRadioButton);
  70. contentPane.add(deluxePaintJRadioButton);
  71.  
  72. premiumPaintJRadioButton = new JRadioButton();
  73. premiumPaintJRadioButton.setBounds(250, 20, 100, 20);
  74. premiumPaintJRadioButton.setText("Premium Paint $34.95");
  75. premiumPaintJRadioButton.setSelected(false);
  76. premiumPaintJRadioButton.setForeground(black);
  77. premiumPaintJRadioButton.setBackground(white);
  78. displayButtonGroup.add(premiumPaintJRadioButton);
  79. contentPane.add(deluxePaintJRadioButton);
  80.  
  81. amountOfGallonsJLabel = new JLabel();
  82. amountOfGallonsJLabel.setBounds(100, 50, 30, 20);
  83. amountOfGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
  84. amountOfGallonsJLabel.setText("Amount of Gallons");
  85. amountOfGallonsJLabel.setForeground(black);
  86. amountOfGallonsJLabel.setHorizontalAlignment(JLabel.LEFT);
  87. contentPane.add(amountOfGallonsJLabel);
  88.  
  89. amountOfGallonsJTextField = new JTextField();
  90. amountOfGallonsJTextField.setBounds(170, 50, 50, 20);
  91. amountOfGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
  92. amountOfGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
  93. amountOfGallonsJTextField.setForeground(black);
  94. amountOfGallonsJTextField.setBackground(white);
  95. amountOfGallonsJTextField.setEditable(true);
  96. contentPane.add(amountOfGallonsJTextField);
  97.  
  98. amountOfSalesTaxJLabel = new JLabel();
  99. amountOfSalesTaxJLabel.setBounds(100, 100, 30, 20);
  100. amountOfSalesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
  101. amountOfSalesTaxJLabel.setText("Amount of Sales Tax");
  102. amountOfSalesTaxJLabel.setForeground(black);
  103. amountOfSalesTaxJLabel.setHorizontalAlignment(JLabel.LEFT);
  104. contentPane.add(amountOfSalesTaxJLabel);
  105.  
  106. amountOfSalesTaxJTextField = new JTextField();
  107. amountOfSalesTaxJTextField.setBounds(170, 100, 50, 20);
  108. amountOfSalesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
  109. amountOfSalesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
  110. amountOfSalesTaxJTextField.setForeground(black);
  111. amountOfSalesTaxJTextField.setBackground(white);
  112. amountOfSalesTaxJTextField.setEditable(false);
  113. contentPane.add(amountOfSalesTaxJTextField);
  114.  
  115. totalAmountJLabel = new JLabel();
  116. totalAmountJLabel.setBounds(100, 150, 30, 20);
  117. totalAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
  118. totalAmountJLabel.setText("Total Amount");
  119. totalAmountJLabel.setForeground(black);
  120. totalAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
  121. contentPane.add(totalAmountJLabel);
  122.  
  123. totalAmountJTextField = new JTextField();
  124. totalAmountJTextField.setBounds(170, 150, 50, 20);
  125. totalAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
  126. totalAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
  127. totalAmountJTextField.setForeground(black);
  128. totalAmountJTextField.setBackground(white);
  129. totalAmountJTextField.setEditable(false);
  130. contentPane.add(totalAmountJTextField);
  131.  
  132. enterJButton = new JButton();
  133. enterJButton.setBounds(30, 300, 100, 20);
  134. enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
  135. enterJButton.setText("Enter");
  136. enterJButton.setForeground(black);
  137. enterJButton.setBackground(white);
  138. contentPane.add(enterJButton);
  139. enterJButton.addActionListener(
  140.  
  141. new ActionListener()
  142. {
  143. public void actionPerformed(ActionEvent event)
  144. {
  145. enterJButtonActionPerformed(event);
  146. }
  147. }
  148. );
  149.  
  150. clearJButton = new JButton();
  151. clearJButton.setBounds(150, 300, 100, 20);
  152. clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
  153. clearJButton.setText("Clear");
  154. clearJButton.setForeground(black);
  155. clearJButton.setBackground(white);
  156. contentPane.add(clearJButton);
  157. clearJButton.addActionListener(
  158.  
  159. new ActionListener()
  160. {
  161. public void actionPerformed(ActionEvent event)
  162. {
  163. clearJButtonActionPerformed(event);
  164. }
  165. }
  166. );
  167.  
  168. closeJButton = new JButton();
  169. closeJButton.setBounds(270, 300, 100, 20);
  170. closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
  171. closeJButton.setText("Close");
  172. closeJButton.setForeground(black);
  173. closeJButton.setBackground(white);
  174. contentPane.add(clearJButton);
  175. clearJButton.addActionListener(
  176.  
  177. new ActionListener()
  178. {
  179. public void actionPerformed(ActionEvent event)
  180. {
  181. closeJButtonActionPerformed(event);
  182. }
  183. }
  184. );
  185.  
  186. setTitle("PaintStore");
  187. setSize(400, 400);
  188. setVisible(true);
  189. }
  190.  
  191. public static void main (String[] args)
  192. {
  193. PaintStore application = new PaintStore();
  194. application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  195. }
  196.  
  197. public void enterJButtonActionPerformed(ActionEvent event)
  198. {
  199. getAmountOfGallons();
  200. }
  201.  
  202. public void getAmountOfGallons()
  203. {
  204. try
  205. {
  206. amountOfGallons = Float.parseFloat(amountOfGallonsJTextField.getText());
  207. numberOfGallonsNotZero();
  208. }
  209.  
  210. catch(NumberFormatException exception)
  211. {
  212. JOptionPane.showMessageDialog(this,
  213. "Please enter a number greater than 0!",
  214. "Number Format Error", JOptionPane.ERROR_MESSAGE);
  215. amountOfGallonsJTextField.setText("");
  216. amountOfGallonsJTextField.requestFocusInWindow();
  217. }
  218. }
  219.  
  220. public void numberOfGallonsNotZero()
  221. {
  222. if(amountOfGallons == 0)
  223. {
  224. JOptionPane.showMessageDialog(this,
  225. "Please enter a number greater than 0!",
  226. "Number Format Error", JOptionPane.ERROR_MESSAGE);
  227. amountOfGallonsJTextField.setText("");
  228. amountOfGallonsJTextField.requestFocusInWindow();
  229. }
  230.  
  231. else
  232. {
  233. getTotalAmount();
  234. }
  235. }
  236.  
  237. public void getTotalAmount()
  238. {
  239. if(deluxePaintJRadioButton.isSelected())
  240. {
  241. paintPrice = DELUXE_PAINT;
  242. }
  243.  
  244. if(premiumPaintJRadioButton.isSelected())
  245. {
  246. paintPrice = PREMIUM_PAINT;
  247. }
  248.  
  249. calculateTotalAmount();
  250. }
  251.  
  252. public void calculateTotalAmount()
  253. {
  254. salesTotal = (amountOfGallons * paintPrice) * TAX_RATE;
  255. paintCalculator = new PaintCalculator(amountOfGallons, paintPrice);
  256. totalAmount = paintCalculator.getResults();
  257. displaySales();
  258. }
  259.  
  260. public void displaySales()
  261. {
  262. decimalFormat = new DecimalFormat("$0.00");
  263. totalAmountJTextField.setText("" + decimalFormat.format(totalAmount));
  264. }
  265.  
  266. public void clearJButtonActionPerformed()
  267. {
  268. amountOfGallonsJTextField.setText("");
  269. amountOfSalesTaxJTextField.setText("");
  270. totalAmountJTextField.setText("");
  271. amountOfGallonsJTextField.requestFocusInWindow();
  272. deluxePaintJRadioButton.setSelected(true);
  273. }
  274.  
  275. public void closeJButtonActionPerformed()
  276. {
  277. PaintStore.this.dispose();
  278. }
  279.  
  280. }
  281.  
  282. class PaintCalculator
  283. {
  284. float numberOne;
  285. float numberTwo;
  286.  
  287. public PaintCalculator(float firstValue, float secondValue)
  288. {
  289. numberOne = firstValue;
  290. numberTwo = secondValue;
  291. }
  292.  
  293. public float getResults()
  294. {
  295. return numberOne * numberTwo;
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement