Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Help With My DormAndMealPlanCalculator Assignment Please?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Help With My DormAndMealPlanCalculator Assignment Please?

    I've been working on my program for a while and I can't seem to figure it out, since I still consider myself new to the language. Anyways, my professor is looking for the GUI Application to calculate charges when selecting dorms and meals, and when you click the "Calculate" button, you're suppose to get the total charges. Only in my program, when I select either a dorm or meal it calculates the total charges without clicking the button. Can anyone help me out? I'd very much appreciate it. I will also attach a pic on what the expected output my professor is looking for. Thanks.

    import java.awt.*;			// Import common GUI elements.
    import java.awt.event.*;	// Import common GUI event listeners.
    import javax.swing.*;		// Import more common GUI elements.
     
    /** DormAndMealPlanCalculator class. */
    public class DormAndMealPlanCalculator extends JFrame
    {
    	private static final long serialVersionUID = 1L;
    	private JLabel label;					// Display a message.
    	private JPanel dormPanel;				// To hold dorm panel components.
    	private JPanel selectedDormPanel;		// To hold selected dorm panel components.
    	private JComboBox dormBox;				// List of dorms.
    	private JTextField selectedDorm;		// Selected dorm.
    	private JPanel mealPanel;				// To hold meal panel components.
    	private JPanel selectedMealPanel;		// To hold selected meal panel components.
    	private JComboBox mealBox;				// List of meal plans.
    	private JTextField selectedMeal;		// Selected meal plan.
    	private JButton calcButton;				// To hold total panel component.
    	private JTextField total;				// Total plan.
     
    	/** Array to hold the values of the dormitory
    	    combo box. */
    	private String[] dorm =
    		{
    			"Allen Hall $" + 1500, "Pike Hall $" + 1600, 
    			"Farthing Hall $" + 1200, "University Suites $" + 1800
    		};
     
    	/** Array to hold the dormitory rate. */
    	double[] dRate = {1500, 1600, 1200, 1800};
     
    	/** Array to hold the values of the meal
    	    combo box. */
    	private String[] meal =
    		{
    			"7 Meals Per Week $" + 560,
    			"14 Meals Per Week $" + 1095,
    			"Unlimited Meals $" + 1500
    		};
     
    	/** Array to hold the meal plan rate. */
    	double[] mRate = {560, 1095, 1500};
     
    	/** Constructor. */
    	public DormAndMealPlanCalculator()
    	{
    		setTitle("Dorm and Meal Plan Calculator.");			// Set the title.
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		// Specify action for close button.
    		setLayout(new GridLayout(3,2));						// Create a border layout manager.
     
    		// Build panels.
    		buildDormPanel();
    		buildMealPanel();
    		buildTotalPanel();
     
    		// Add panels to content pane.
    		add(dormPanel);
    		add(mealPanel);
    		add(calcButton);
     
    		// Pack and display.
    		pack();
    		setVisible(true);
    	}
     
    	/** buildDormPanel function that adds a combo box
    	    with the dormitories to the panel. */
    	private void buildDormPanel()
    	{
    		dormPanel = new JPanel();		// Create panel to hold combo box.
    		dormBox = new JComboBox(dorm);	// Create the combo box.
     
    		// Register an action listener. 
    		dormBox.addActionListener(new ComboBoxListener());
    		dormPanel.add(dormBox);
    	}
     
     
    	/** buildMealPanel function that adds a combo box
    	    with the types of meals to the panel. */
    	private void buildMealPanel()
    	{
    		mealPanel = new JPanel();		// Create panel to hold combo box.
    		mealBox = new JComboBox(meal);	// Create the combo box.
     
    		// Register an action listener.
    		mealBox.addActionListener(new ComboBoxListener());
    		mealPanel.add(mealBox);
    	}
     
    	/** buildSelectedDormPanel function adds a
    	    read-only text field to the panel. */
    	public void buildSelectedDormPanel()
    	{
    		selectedDormPanel = new JPanel();			// Create panel to hold components.
    		label = new JLabel("Your Dormitory Is: ");	// Create the label.
     
    		// Create the uneditable text field.
    		selectedDorm = new JTextField(20);
    		selectedDorm.setEditable(false);
     
    		// Add the label and text field
    		// to the panel.
    		selectedDormPanel.add(label);
    		selectedDormPanel.add(selectedDorm);
    	}
     
    	/** buildSelectedMealPanel function adds a
    	    read-only text field to the panel. */
    	public void buildSelectedMealPanel()
    	{
    		selectedMealPanel = new JPanel();			// Create panel to hold components.
    		label = new JLabel("Your Meal Plan Is: ");	// Create the label.
     
    		// Create the uneditable text field.
    		selectedMeal = new JTextField(20);
    		selectedMeal.setEditable(false);
     
    		// Add the label and text field
    		// to the panel.
    		selectedMealPanel.add(label);
    		selectedMealPanel.add(selectedMeal);
    	}
     
    	/** buildTotalPanel function adds a
    	  read-only text field to the panel. */
    	private void buildTotalPanel()
    	{
    		calcButton = new JButton("Calculate");
     
    		// Create the uneditable text field.
    		total = new JTextField(15);
    		total.setEditable(false);
    	}
     
    	/** Private inner class that handles the event when the
    	    user selects an item from the combo box. */
    	private class ComboBoxListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			// Declare variables.
    			int dorm;
    			int meal;
    			double total1;		// String total.
     
    			// Get the selected dormitory.
    			dormBox.getSelectedItem();
    			dorm = dormBox.getSelectedIndex();
     
    			// Get the selected meal plan.
    			mealBox.getSelectedItem();
    			meal = mealBox.getSelectedIndex();
     
    			// Add the selections.
    			total1 = dRate[dorm] + mRate[meal];
    			total.setText("$" + total1);
     
    			JOptionPane.showMessageDialog(null, "Total Charges Per Semester: " + total1);
    		}
    	}
     
    	/** Main function. */
    	public static void main(String[] args) 
    	{
    		new DormAndMealPlanCalculator();
    	}
     
    }
    Attached Images Attached Images


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help With My DormAndMealPlanCalculator Assignment Please?

    Did you write this? If you had written it, it seems you'd know that the ComboBoxListener class computes and displays the total when a change is detected in a combo box. Knowing that (as you should), you might be able to modify what you'd written to change the action to a listener on the calculate button.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Caffeine (July 10th, 2014)

  4. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help With My DormAndMealPlanCalculator Assignment Please?

    Quote Originally Posted by GregBrannon View Post
    Did you write this? If you had written it, it seems you'd know that the ComboBoxListener class computes and displays the total when a change is detected in a combo box. Knowing that (as you should), you might be able to modify what you'd written to change the action to a listener on the calculate button.
    Of course I've written it, I've been working on it for the past week or two. Not to mention I have the book, but understanding this button stuff is difficult to grasp for me.

  5. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Help With My DormAndMealPlanCalculator Assignment Please?

    Have you debugged it step by step? Try setting a breakpoint where you think
    (or want) the program to terminate. Also, set the debugger so it shows you
    how values change with each executable step. If you place a breakpoint in the wrong
    place, you can either continue debugging past that point or step out of the code.

    It looks like a logical error - and the debugger is a good tool to aid you in this.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  6. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    Caffeine (July 10th, 2014)

  7. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help With My DormAndMealPlanCalculator Assignment Please?

    Quote Originally Posted by Ada Lovelace View Post
    Have you debugged it step by step? Try setting a breakpoint where you think
    (or want) the program to terminate. Also, set the debugger so it shows you
    how values change with each executable step. If you place a breakpoint in the wrong
    place, you can either continue debugging past that point or step out of the code.

    It looks like a logical error - and the debugger is a good tool to aid you in this.

    Wishes Ada xx
    No I haven't done that yet to be honest, thanks for the reminder of that.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With My DormAndMealPlanCalculator Assignment Please?

    If converting the code is giving you problems, perhaps another approach would help.
    Write a small simple program with a frame, a button and a listener for that button. Put a call to JOptionPane in the button's listener so you know its been called. If you have problems with it, copy and paste it here and describe your problems.
    When you get it to work, you should see how to use a button with a listener.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Caffeine (July 10th, 2014)

  10. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help With My DormAndMealPlanCalculator Assignment Please?

    Quote Originally Posted by Norm View Post
    If converting the code is giving you problems, perhaps another approach would help.
    Write a small simple program with a frame, a button and a listener for that button. Put a call to JOptionPane in the button's listener so you know its been called. If you have problems with it, copy and paste it here and describe your problems.
    When you get it to work, you should see how to use a button with a listener.
    I'll try that, thanks.

  11. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help With My DormAndMealPlanCalculator Assignment Please?

    Of course I've written it, I've been working on it for the past week or two.
    You have the code you need to make your program do what you you've described, but you have it in the wrong place. To make the necessary changes, here are 3 simple steps:

    1. Comment out the statement that adds the ComboBoxListener to the dormBox,
    2. Comment out the statement that adds the ComboBoxListener to the mealBox, and
    3. Add a statements that adds the ComboBoxListener to the computeButton.

    Then it will work as you've described. You may want to then do some cleanup, change the name of the ActionListener class, delete lines that you've commented out, etc., but that's purely up to you.

  12. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help With My DormAndMealPlanCalculator Assignment Please?

    Okay I have it working correctly, but I feel it could use a touch up. I'm trying to add the width and height of the buttons but after putting it in different areas of the code, it doesn't seem to work. I have the variables declared and I tried using
    setSize(width, height);
    within the button coding but no luck. What am I missing? It's gotta be something simple.

    import java.awt.*;						// Needed for BorderLayout.
    import javax.swing.*;					// Import more common GUI elements.
    import java.awt.event.ActionEvent; 		// Needed for an action on a component.
    import java.awt.event.ActionListener;	// ActionListener interface can receive ActionEvent objects.
    import java.text.DecimalFormat;			// Needed for a custom output.
     
    /** DormAndMealPlanCalculator class. */
    public class DormAndMealPlanCalculator extends JFrame
    {
    	private static final long serialVersionUID = 1L;
    	private JLabel label;					// Display a message.
    	private JPanel dormPanel;				// To hold dorm panel components.
    	private JPanel selectedDormPanel;		// To hold selected dorm panel components.
    	private JComboBox dormBox;				// List of dorms.
    	private JTextField selectedDorm;		// Selected dorm.
    	private JPanel mealPanel;				// To hold meal panel components.
    	private JPanel selectedMealPanel;		// To hold selected meal panel components.
    	private JComboBox mealBox;				// List of meal plans.
    	private JTextField selectedMeal;		// Selected meal plan.
    	private JButton calcButton;				// To calculate the total charges.
    	private JButton exitButton;				// To exit and close the program.
    	private JTextField total;				// Total text.
    	private int width = 400;
    	private int height = 200;
     
    	/** Allocate memory to hold the values 
    	    of the dormitory combo box. */
    	private String[] dorm =
    		{
    			"Farthing Hall $" + 1200, "Allen Hall $" + 1500, 
    			"Pike Hall $" + 1600, "University Suites $" + 1800
    		};
     
    	/** Allocate memory to hold the dormitory rate. */
    	double[] dRate = {1200, 1500, 1600, 1800};
     
    	/** Allocate memory to hold the values 
    	    of the meal combo box. */
    	private String[] meal =
    		{
    			"7 Meals Per Week $" + 560,
    			"14 Meals Per Week $" + 1095,
    			"Unlimited Meals $" + 1500
    		};
     
    	/** Allocate memory to hold the meal plan rate. */
    	double[] mRate = {560, 1095, 1500};
     
    	/** Constructor. */
    	public DormAndMealPlanCalculator()
    	{
    		setTitle("Dorm and Meal Plan Calculator.");			// Set the title.
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		// Specify action for close button.
    		setLayout(new GridLayout(3,2));						// Create a border layout manager.
     
    		// Build panels.
    		buildDormPanel();
    		buildMealPanel();
    		buildTotalPanel();
     
    		// Add panels to content pane.
    		add(dormPanel);
    		add(mealPanel);
    		add(calcButton);
    		add(exitButton);
     
    		// Pack and display.
    		pack();
    		setVisible(true);
    	}
     
    	/** buildDormPanel function that adds a combo box
    	    with the dormitories to the panel. */
    	private void buildDormPanel()
    	{
    		dormPanel = new JPanel();		// Create panel to hold combo box.
    		dormBox = new JComboBox(dorm);	// Create the combo box.
     
    		// Register an action listener. 
    		dormBox.addActionListener(new ComboBoxListener());
    		dormPanel.add(dormBox);
    	}
     
     
    	/** buildMealPanel function that adds a combo box
    	    with the types of meals to the panel. */
    	private void buildMealPanel()
    	{
    		mealPanel = new JPanel();		// Create panel to hold combo box.
    		mealBox = new JComboBox(meal);	// Create the combo box.
     
    		// Register an action listener.
    		mealBox.addActionListener(new ComboBoxListener());
    		mealPanel.add(mealBox);
    	}
     
    	/** buildSelectedDormPanel function adds a
    	    read-only text field to the panel. */
    	public void buildSelectedDormPanel()
    	{
    		selectedDormPanel = new JPanel();			// Create panel to hold components.
    		label = new JLabel();						// Create the label.
     
    		// Create the uneditable text field.
    		selectedDorm = new JTextField(20);
    		selectedDorm.setEditable(false);
     
    		// Add the label and text field
    		// to the panel.
    		selectedDormPanel.add(label);
    		selectedDormPanel.add(selectedDorm);
    	}
     
    	/** buildSelectedMealPanel function adds a
    	    read-only text field to the panel. */
    	public void buildSelectedMealPanel()
    	{
    		selectedMealPanel = new JPanel();			// Create panel to hold components.
    		label = new JLabel();						// Create the label.
     
    		// Create the uneditable text field.
    		selectedMeal = new JTextField(20);
    		selectedMeal.setEditable(false);
     
    		// Add the label and text field
    		// to the panel.
    		selectedMealPanel.add(label);
    		selectedMealPanel.add(selectedMeal);
    	}
     
    	/** buildTotalPanel function adds a
    	  read-only text field to the panel. */
    	private void buildTotalPanel()
    	{
    		calcButton = new JButton("Calculate Charges");
    		calcButton.addActionListener(new calcButtonListener());
     
    		exitButton = new JButton("Exit");
    		exitButton.addActionListener(new exitButtonListener());
     
    		// Create the uneditable text field.
    		total = new JTextField(15);
    		total.setEditable(false);
    	}
     
    	/** Private inner class that handles the event when the
    	    user selects an item from the combo box. */
    	private class ComboBoxListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			// Declare variables.
    			int dorm;
    			int meal;
    			double total1;		// String total.
     
    			// Get the selected dormitory.
    			dormBox.getSelectedItem();
    			dorm = dormBox.getSelectedIndex();
     
    			// Get the selected meal plan.
    			mealBox.getSelectedItem();
    			meal = mealBox.getSelectedIndex();
     
    			// Add the selections.
    			total1 = dRate[dorm] + mRate[meal];
    			total.setText("$" + total1);
    		}
    	}
     
    	/** Private inner class that handles the total event when the
    	    user selects the calculate button. */
    	private class calcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{		
    			// Declare variables.
    			int dorm;
    			int meal;
    			double total2;		// String total.
     
    			// Create a dollar object.
    			DecimalFormat dollar = new DecimalFormat("0.00");
     
    			// Get the selected dormitory.
    			dormBox.getSelectedItem();
    			dorm = dormBox.getSelectedIndex();
     
    			// Get the selected meal plan.
    			mealBox.getSelectedItem();
    			meal = mealBox.getSelectedIndex();
     
    			// Add the selections.
    			total2 = dRate[dorm] + mRate[meal];
    			total.setText("$" + total2);
     
    			// Display the total charges in a separate window.
    			JOptionPane.showMessageDialog(null, "Total Chargers Per Semester: $" + dollar.format(total2));
    		}
    	}
     
    	/** Private inner class that handles the exit event when the
    	    user selects the exit button. */
    	private class exitButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			// Close the box.
    			System.exit(0);
    		}
    	}
     
    	/** Main function. */
    	public static void main(String[] args) 
    	{
    		new DormAndMealPlanCalculator();
    	}
     
    }

  13. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Help With My DormAndMealPlanCalculator Assignment Please?

    You dont set the size of components. You never do that.
    The sizes of components, as well as their positions, are determined by the layout manager used in their parent.
    Pick the right layout manager for the parent and use the preferredSize of the component if need be.

  14. #11
    Junior Member
    Join Date
    Jun 2014
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help With My DormAndMealPlanCalculator Assignment Please?

    Quote Originally Posted by Cornix View Post
    You dont set the size of components. You never do that.
    The sizes of components, as well as their positions, are determined by the layout manager used in their parent.
    Pick the right layout manager for the parent and use the preferredSize of the component if need be.
    Gotcha. Thanks for the tip.

Similar Threads

  1. Behaviour of shortcut assignment and normal assignment for float
    By rakeshkr2 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 20th, 2014, 02:54 AM
  2. Assignment
    By bada_bum in forum Member Introductions
    Replies: 2
    Last Post: October 14th, 2013, 02:56 AM
  3. assignment help
    By suddenark in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 11:11 PM
  4. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM