Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. public class Decorator implements ActionListener
  2. {
  3. JFrame fDecorator; // Declare Decorator frame
  4. JLabel lSize, lType, lTotal, lPrice, lx, lEquals; // Declare Labels
  5. JTextField tLength, tWidth, tSize, tTotal; // Declare JTextFields
  6. JComboBox<String> cbFlooring; //Declare JComboBox
  7. JButton bCalculate1, bCalculate2, bHelp; // Declare JButtons
  8. static double priceTotal, floorPrice, floorSize; // Declare double variables
  9.  
  10. Decorator()
  11. {
  12. String[] type_floor = new String[] // Create an array called Type_Floor
  13. {" Select ","Wood £0.24", "Ceramic £0.37", "Stone £0.86", "Lino £1.24", "Marble £1.76",
  14. "Laminate £2.10", "Bamboo £2.34", "Carpet £3.78"}; // Add array names for JComboBox
  15.  
  16. fDecorator = new JFrame("Flooring Decorator"); // Add the Frame
  17. fDecorator.setResizable(false); // Disallow user resizing of frame
  18. fDecorator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.  
  20. lSize = new JLabel("Input Size of your room (Meters): "); // Add size of floor Label
  21. lType = new JLabel("Type of flooring: "); // Add type of flooring Label
  22. lTotal = new JLabel("Total Price = "); // Add Total price of flooring Label
  23. lx = new JLabel("x"); // Add x Label
  24. lEquals = new JLabel("="); // Add equals Label
  25. lPrice = new JLabel ("Price of floor per square Meter"); // Adds Price Label
  26.  
  27. bCalculate1 = new JButton("Calculate"); // Add calculate Button
  28. bCalculate2 = new JButton("Calculate"); // Add calculate Button
  29. bHelp = new JButton("Help"); // Add Help Button
  30.  
  31. tLength = new JTextField(5); // Add user editable Text Field
  32. tWidth = new JTextField(5); // Add second user editable Text Field
  33. tSize = new JTextField(5); // Add total size of room Text Field
  34. tTotal = new JTextField(5); // Add total price Text Field
  35.  
  36. tTotal.setEditable(false); // Disallow editing of tTotal
  37. tSize.setEditable(false); // Disallow editing of tSize
  38.  
  39. cbFlooring = new JComboBox<>(type_floor); // Add ComboBox, and type cast Array
  40.  
  41. fDecorator.setLayout(new FlowLayout()); // Set the layout of Decorator to FlowLayout
  42.  
  43. fDecorator.add(lSize); // Add Size Label
  44. fDecorator.add(tLength); // Add Entry1 TextField
  45. fDecorator.add(lx); // Add x Label
  46. fDecorator.add(tWidth); // Add Entry2 TextField
  47. fDecorator.add(lEquals); // Add Equals Label
  48. fDecorator.add(tSize); // Add Size TextField
  49. fDecorator.add(bCalculate1); // Add Calculator1 Button
  50. fDecorator.add(lPrice); // Add Price Label
  51. fDecorator.add(cbFlooring); // Add Flooring ComboBox
  52. fDecorator.add(bCalculate2); // Add Calculator2 Button
  53. fDecorator.add(lTotal); // Add Total Label
  54. fDecorator.add(tTotal); // Add Total TextField
  55. fDecorator.add(bHelp); // Add Help Button
  56.  
  57. bCalculate1.addActionListener(this); // Adds an action listener to Calculate button
  58. bCalculate2.addActionListener(this); // Adds an action listener to Calculate2 button
  59. bHelp.addActionListener(this); // Adds an action listener to Help button
  60. cbFlooring.addActionListener(this); // Adds action listener to ComboBox
  61.  
  62. Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  63. fDecorator.setLocation(dim.width/3-fDecorator.getSize().width/3, dim.height/3-fDecorator.getSize().height/3);
  64.  
  65. fDecorator.setSize(420, 130); // Set frame size too 400 by 200
  66. fDecorator.setVisible(true); // Set frame to visible
  67.  
  68. }
  69.  
  70.  
  71. @Override
  72. public void actionPerformed(ActionEvent e) // Action Listener
  73. {
  74.  
  75. try // Handle exceptions with code inside this block
  76. {
  77. JComboBox<String> selectionBox = (JComboBox<String>) e.getSource(); // Declare parameterised ComboBox selectionBox
  78. String selectedFloor = (String) selectionBox.getSelectedItem(); // Declare selectedFloor as string inside ComboBox
  79.  
  80. if(selectedFloor.equals("Wood £0.24")) // If selected Equals Wood
  81. floorPrice = 0.24; // Assign floorPrice value 0.24
  82.  
  83. else if(selectedFloor.equals("Ceramic £0.37")) // If selected Equals Ceramic
  84. floorPrice = 0.37; // Assign floorPrice value 0.37
  85.  
  86. else if(selectedFloor.equals("Stone £0.86")) // If selected Equals Stone
  87. floorPrice = 0.86; // Assign floorPrice value 0.86
  88.  
  89. else if(selectedFloor.equals("Lino £1.24")) // If selected Equals Lino
  90. floorPrice = 1.24; // Assign floorPrice value 1.24
  91.  
  92. else if(selectedFloor.equals("Marble £1.76")) // If selected Equals Marble
  93. floorPrice = 1.76; // Assign floorPrice value 1.76
  94.  
  95. else if(selectedFloor.equals("Laminate £2.10")) // If selected Equals Laminate
  96. floorPrice = 2.10; // Assign floorPrice value 2.10
  97.  
  98. else if(selectedFloor.equals("Bamboo £2.34")) // If selected Equals Bamboo
  99. floorPrice = 2.34; // Assign floorPrice value 2.34
  100.  
  101. else if(selectedFloor.equals("Carpet £3.78")) // If selected Equals Carpet
  102. floorPrice = 3.78; // Assign floorPrice value 3.78
  103. }
  104.  
  105. catch(Exception a) // Handle exception using this
  106. {
  107. // Does nothing if an exception is encountered, prevents program from crashing and handles exception
  108. }
  109.  
  110. try // Handle exception with code inside this block
  111. {
  112. char[] len1 = tLength.getText().toCharArray(); // Declares a char variable that gets the text from tEntry1
  113. char[] len2 = tWidth.getText().toCharArray(); // Declares a char variable that gets the text from tEntry2
  114.  
  115. for(int loop1 = 0; loop1 < len1.length; loop1++) // For loop that searches through the TextFields
  116. {
  117. if(len1[loop1] == '-' || len2[loop1] == '-') // If the loop finds a - sign
  118. JOptionPane.showMessageDialog(null,"No Negative numbers please!"); // Displays an error message
  119. }
  120.  
  121. if (tLength.getText() != null && tWidth.getText() != null) // if TextFields have inputs
  122. {
  123. double length = Double.parseDouble(tLength.getText()); // Gets string from TextField and converts to double
  124. double width = Double.parseDouble(tWidth.getText()); // Gets string from TextField and converts to double
  125.  
  126. if(e.getSource() == bCalculate1) // When Calculate button is clicked
  127. {
  128. tSize.setText(Double.toString(length * width)); // Times input 1 and 2 to find surface area
  129. }
  130. }
  131. }
  132.  
  133. catch(Exception ab) // Handle exception using this
  134. {
  135. JOptionPane.showMessageDialog(null,"No Text Please!");// Does nothing if an exception is encountered, prevents program from crashing
  136. }
  137.  
  138. try // Handle exception with code inside this block
  139. {
  140. if(e.getSource() == bCalculate2) // If Calculate2 button is pressed
  141. {
  142. floorSize = Double.parseDouble(tSize.getText()); // Get text value from tSize and assign value too floorSize
  143. priceTotal = floorPrice * floorSize; // Assign value to priceTotal
  144. tTotal.setText(""+priceTotal); // Set tTotal TextField
  145. }
  146. }
  147.  
  148. catch(Exception abc) // Handle exceptions using this
  149. {
  150. System.out.println("Exception occured!!!!");// If an exception is encountered, prevents program from crashing and handles exception
  151. }
  152.  
  153. if(e.getSource() == bHelp)
  154. {
  155. JFrame fHelp = new JFrame("Help Screen"); // Declare JFrame fHelp
  156. JLabel lWelcomeHelp = new JLabel("<html>Welcome to the help screen!<br> This screen will guide you though using my decorator application!<br> Firslty input the length and width into the text fields, then press calculate.<br> This will generate the size of the floor you're going to be decorating.<br>After that select which floor you would like to use,<br>the price for each type of flooring is displayed next to it.<br> After deciding on a floor press the second calculate button to price up your new floor!<html>", SwingConstants.CENTER);
  157. // Declare JLabel WelcomeHelp
  158.  
  159. fHelp.setLayout(new FlowLayout()); // Set fHelp frame layout as FlowLayout
  160.  
  161. fHelp.add(lWelcomeHelp); // Add WelcomeHelp JLabel to fHelp frame
  162.  
  163. fHelp.setSize(600, 250); // Set size of frame
  164. fHelp.setVisible(true); // Make frame visible
  165.  
  166. }
  167. }
  168. }
  169.  
  170. DecimalFormat dc = new DecimalFormat("0.00");
  171. String formattedText = dc.format(yourDouble);
  172.  
  173. DecimalFormat df = new DecimalFormat("#.00");
  174. tSize.setText(Double.toString(df.format(Length * Width)));
  175.  
  176. double length = Double.parseDouble(tLength.getText());
  177. double width = Double.parseDouble(tWidth.getText());
  178.  
  179. int numberOfDigitsAfterDecimal = 2;
  180. Double yourDoubleResult = length * width;
  181. Double resultToBeShown = new BigDecimal(yourDoubleResult).setScale(numberOfDigitsAfterDecimal, BigDecimal.ROUND_HALF_UP).doubleValue();
  182. System.out.println(resultToBeShown.toString());
  183.  
  184. if(e.getSource() == bCalculate) {
  185. tSize.setText(resultToBeShown.toString());
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement