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 6 of 6

Thread: Dorm and Meal Plan Calculator

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

    Default Dorm and Meal Plan Calculator

    I'm a bit confused about my program. I have it all working great, but my professor is expecting a different output. He wants a box where you select your dormitory and meal plan, with "Calculate charges" and "Exit" buttons. When you click "Calculate charges," a second box comes up with a message with your total charges. I've tried other stuff like:

    		JOptionPane.showMessageDialog(total,
    			    "Total Charges For the Semester: ");

    But with no luck. Any tips or suggestions on how to rewrite my code to satisfy my professor will be greatly appreciated. Thanks

    Here's my code:

    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 JPanel totalPanel;			// 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(totalPanel);
     
    		// 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()
    	{
    		totalPanel = new JPanel();						// Create the panel.
    		label = new JLabel("Your Total Charges For the Semester Is: $");	// Create the label.
     
    		// Create the uneditable text field.
    		total = new JTextField(15);
    		total.setEditable(false);
     
    		// Add the label and text field
    		// to the panel.
    		totalPanel.add(label);
    		totalPanel.add(total);
    	}
     
    	/** 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);
    		}
    	}
     
    	/** Main function. */
    	public static void main(String[] args) 
    	{
    		new DormAndMealPlanCalculator();
    	}
     
    }


  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: Dorm and Meal Plan Calculator

    What do you mean, "But with no luck." Do you mean it didn't meet your professor's requirements, or is it not working?

    A JDialog is similar to a JFrame, but it provides the option of choosing its modality. It's an excellent choice for a child dialog to a main JFrame. Perhaps that's what your professor is looking for if s/he's not thrilled with the JOptionPane.

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

    Default Re: Dorm and Meal Plan Calculator

    When I tried using the
    JOptionPane.showMessageDialog(total,
    			    "Total Charges For the Semester: ");
    ,

    that box pops up but without the total charges and then I get errors in the console like:

    Exception in thread "main" java.lang.NullPointerException
    	at java.awt.Container.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at chapter12.DormAndMealPlanCalculator.buildTotalPanel(DormAndMealPlanCalculator.java:149)
    	at chapter12.DormAndMealPlanCalculator.<init>(DormAndMealPlanCalculator.java:62)
    	at chapter12.DormAndMealPlanCalculator.main(DormAndMealPlanCalculator.java:181)

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

    Default Re: Dorm and Meal Plan Calculator

    Oh, and it's not meeting his requirements. See if I run my code now, everything displays in one box/pane with the total charges for the semester. He is not looking for that, he is looking for this:
    Attached Images Attached Images

  5. #5
    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: Dorm and Meal Plan Calculator

    You have most of the needed parts, not all in the right places, and you're missing the two buttons.

    Create and add two JButtons to the JFrame, perhaps in another JPanel replacing the total JPanel, add the appropriate action listeners to the JButtons, and then use a JDialog to display the existing total panel in response to selection of the calculate button.

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

    Default Re: Dorm and Meal Plan Calculator

    Okay, I'll try that, thanks for the tips.

Similar Threads

  1. help me please this is a calculator by the way
    By ryry163 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 8th, 2014, 05:02 PM
  2. My Life plan(if in wrong section pelase move)
    By loui345 in forum Totally Off Topic
    Replies: 2
    Last Post: October 2nd, 2012, 12:47 PM
  3. Game Devlopment Plan Help
    By Chillers in forum Java Theory & Questions
    Replies: 2
    Last Post: March 18th, 2012, 08:54 AM