Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. // Libraries
  2. import java.text.DecimalFormat;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class DormMealCaclGUI extends JFrame {
  8.  
  9. // Create memebrs
  10. private DormPrices dorm; // To display dorm prices and selections
  11. private MealPlans meals; // To display meal plan prices and selections
  12. private JPanel buttonPanel; // Holds calculate and exit buttons
  13. private JButton calcButton; // To calculate costs
  14. private JButton exitButton; // To exit application window when done
  15.  
  16. public DormMealCaclGUI(){
  17. // Set Title
  18. setTitle("Univsersity Dorm & Meal Plan Calculator");
  19.  
  20. // Specify action for application close
  21. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.  
  23. // Create border layout manager
  24. setLayout(new BorderLayout());
  25.  
  26. // Create custom panels
  27. dorm = new DormPrices();
  28. meals = new MealPlans();
  29.  
  30. // Create button panel
  31. buildButtonPanel();
  32.  
  33. // Add components to pane
  34. add(dorm, BorderLayout.WEST);
  35. add(meals, BorderLayout.EAST);
  36. add(buttonPanel, BorderLayout.SOUTH);
  37.  
  38. // Pack contents and display
  39. pack();
  40. setVisible(true);
  41.  
  42. } // end constructor
  43.  
  44. // This construtor will build the button panel
  45. public void buildButtonPanel(){
  46. // Create panel
  47. buttonPanel = new JPanel();
  48.  
  49. // Create Buttons
  50. calcButton = new JButton("Calculate");
  51. exitButton = new JButton("Exit");
  52.  
  53. // Register action listeners
  54. calcButton.addActionListener(new CalcButtonListener());
  55. exitButton.addActionListener(new ExitButtonListener());
  56.  
  57. // Add buttons to panel
  58. buttonPanel.add(calcButton);
  59. buttonPanel.add(exitButton);
  60.  
  61. } // end build button panel constructor
  62.  
  63. // Private inner class to handle calcButton action events
  64. private class CalcButtonListener implements ActionListener{
  65. public void actionPerformed(ActionEvent e){
  66.  
  67. int total = 0; // holds overall total for dorm and meal plan selections
  68.  
  69. // Calculate total
  70. total = dorm.actionCalculate() + meals.getMealPlanCost();
  71.  
  72. // Create DecimalFormat objetc
  73. DecimalFormat cost = new DecimalFormat("##00.00");
  74.  
  75.  
  76. // Display total
  77. JOptionPane.showMessageDialog(null, "Dorm & Meal Plan Total: $" + cost.format(total));
  78.  
  79. } // end action performed method
  80.  
  81. } // end calc button listener class
  82.  
  83. // Private inner class to handle exitButton action events
  84. private class ExitButtonListener implements ActionListener{
  85. public void actionPerformed(ActionEvent e){
  86. System.exit(0);
  87. } // end method
  88. }// end exit button listener class
  89.  
  90. public static void main(String[] args) {
  91.  
  92. new DormMealCaclGUI();
  93.  
  94. } // end main method
  95.  
  96. }// end class
  97.  
  98. // Libraries
  99. import javax.swing.*;
  100. import java.awt.*;
  101. import java.awt.event.*;
  102.  
  103. public class DormPrices extends JPanel {
  104.  
  105. // Constant variables used for calculating cost
  106. public final int ALLEN_HALL = 1500;
  107. public final int PIKE_HALL = 1600;
  108. public final int FARTHING_HALL = 1200;
  109. public final int UNI_SUITES = 1800;
  110.  
  111. // Create combo box to allow user to select dorm
  112. // user only allowed to select one dorm per calculation
  113. public final String [] dormNames = {"Allen Hall", "Pike Hall", "Farthing Hall", "University Suites"};
  114. private JComboBox dorms; // Combo box for dorm options
  115. private JLabel label1; // label to display results
  116. private JTextField selectedDorm; // text field to hold selected dorm
  117.  
  118.  
  119. // Constructor to build panel
  120. public DormPrices(){
  121.  
  122. // Set new layout to GridLayout manager with 4 rows and 1 column
  123. setLayout(new GridLayout(4,1));
  124.  
  125. // Create combo box, lable and text field
  126. dorms = new JComboBox<String>(dormNames);
  127. label1 = new JLabel("Dorm Selected: ");
  128. selectedDorm = new JTextField(12);
  129. selectedDorm.setEditable(false);
  130.  
  131. // Add border to panel
  132. setBorder(BorderFactory.createTitledBorder("Dorm Hall Prices"));
  133.  
  134. // Create action listener
  135. dorms.addActionListener(new ComboBoxListener());
  136.  
  137. // Add components to panel
  138. add(dorms);
  139. add(label1);
  140. add(selectedDorm);
  141.  
  142. } // end dorm prices constructor
  143.  
  144. // this inner privtae class will handle the event of an itme being selected from the combo box
  145. private class ComboBoxListener implements ActionListener{
  146. public void actionPerformed(ActionEvent e){
  147. // Get seleted dorm name
  148. String dormSelection = (String)dorms.getSelectedItem();
  149.  
  150. // Display selection
  151. selectedDorm.setText(dormSelection);
  152.  
  153. } // end method
  154.  
  155. }// end class
  156.  
  157. // this method will pass the total of the dorm selected
  158. public int actionCalculate(){
  159. // Variable
  160. int dormTotal = 0; // this holds an initial value for dorm hall cost
  161.  
  162. if(selectedDorm.getText() == "Allen Hall")
  163. dormTotal = ALLEN_HALL;
  164.  
  165. else if(selectedDorm.getText() == "Pike Hall")
  166. dormTotal = PIKE_HALL;
  167.  
  168. else if(selectedDorm.getText() == "Farthing Hall")
  169. dormTotal = FARTHING_HALL;
  170.  
  171. else if(selectedDorm.getText() == "University Suites")
  172. dormTotal = UNI_SUITES;
  173.  
  174.  
  175. return dormTotal;
  176.  
  177. }
  178.  
  179.  
  180. } // end DormPrices clas
  181.  
  182. // Libraries
  183. import javax.swing.*;
  184. import java.awt.*;
  185. import java.awt.event.*;
  186.  
  187. public class MealPlans extends JPanel{
  188.  
  189. public final int SEVEN_MEALS_PER_WEEK = 560;
  190. public final int FRTN_MEALS_PER_WEEK = 1095;
  191. public final int UNLIMITED_MEALS = 1500;
  192.  
  193. // Create combo box to allow user to select dorm
  194. // user only allowed to select one dorm per calculation
  195. public final String [] mealPlan = {"7 Meals per week", "14 Meals per week", "Unlimited Meals"};
  196. private JComboBox meal; // Combo box for dorm options
  197. private JLabel label2; // label to display results
  198. private JTextField selectedMeal; // text field to hold selected dorm
  199.  
  200.  
  201.  
  202. public MealPlans() {
  203. // Set new layout to GridLayout manager with 3 rows and 1 column
  204. setLayout(new GridLayout(3,1));
  205.  
  206. // Create combo box, label and text field
  207. meal = new JComboBox<String>(mealPlan);
  208. label2 = new JLabel("Meal Plan Selected: ");
  209. selectedMeal = new JTextField(20);
  210. selectedMeal.setEditable(false);
  211.  
  212. // Add border to panel
  213. setBorder(BorderFactory.createTitledBorder("Meal Plan Prices"));
  214.  
  215. // Create action listener
  216. meal.addActionListener(new ComboBoxListener());
  217.  
  218. // Add components to panel
  219. add(meal);
  220. add(label2);
  221. add(selectedMeal);
  222. }
  223.  
  224. // this inner privtae class will handle the event of an item being selected from the combo box
  225. private class ComboBoxListener implements ActionListener{
  226. public void actionPerformed(ActionEvent e){
  227. // Get seleted meal plan name
  228. String mealSelection = (String)meal.getSelectedItem();
  229.  
  230. // Display selection
  231. selectedMeal.setText(mealSelection);
  232.  
  233. } // end method
  234.  
  235. }// end class
  236.  
  237.  
  238.  
  239.  
  240. public int getMealPlanCost() {
  241. // Variable
  242. int mealTotal = 0; // this holds an initial value for meal plan cost
  243.  
  244. if(selectedMeal.getText() == "7 Meals per week")
  245. mealTotal = SEVEN_MEALS_PER_WEEK;
  246.  
  247. else if(selectedMeal.getText() == "14 Meals per week")
  248. mealTotal = FRTN_MEALS_PER_WEEK;
  249.  
  250. else if(selectedMeal.getText() == "Unlimited Meals")
  251. mealTotal = UNLIMITED_MEALS;
  252.  
  253.  
  254.  
  255. return mealTotal;
  256.  
  257. } // end method
  258.  
  259. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement