# Running total with checkboxes problem

Page 1 of 1

## 2 Replies - 1672 Views - Last Post: 11 December 2012 - 06:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303649&amp;s=80eb8bc86e8d7b7560ad5a0477138475&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Autumn13

Reputation: 0
• Posts: 2
• Joined: 11-December 12

# Running total with checkboxes problem

Posted 11 December 2012 - 04:59 PM

My Java final is Thursday. I thought I had everything done, but missed something. Please note that I am new to Java, and while I am eager to learn though this is not my major, I might easily get lost.

I believe I have a good start to my code, though. My Professor was not available to help. This is our first time working with checkboxes, which actually we did not cover the chapter - our final is a challenge (don't ask!).

Problem: I have the user entering the number of weeks they would like to "rent a cottage" for. After they press enter, a window opens with their choices of cottages and rentals and their base prices. Basically I need to multiply the total by the number of weeks. So simple, yet I am not understanding how to do this.

Any, any help is much appreciated! (also, I've been using this forum this whole semester, so a big thanks to everyone here!)

```import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;

public class FINAL extends
JFrame implements ItemListener
{
//		final int BASE_PRICE = 300;
final int ONE_BR = 600;
final int TWO_BR = 850;
final int ROWBOAT_RENTAL = 60;
int totalPrice;
int userEntry;

JCheckBox oneBox = new JCheckBox
("1 bedroom Cottage \$" + ONE_BR, false);
JCheckBox twoBox = new
JCheckBox("2 bedroom Cottage \$" + TWO_BR, false);
JCheckBox rentalBox = new JCheckBox
("Rowboat Rental \$" + ROWBOAT_RENTAL, false);
JLabel eventHandlersLabel = new JLabel
("Cottages");
JLabel ePrice = new JLabel("The price for your event is");
JTextField totPrice = new JTextField(10);
JLabel optionExplainLabel = new JLabel
+ totalPrice + ".");

JLabel optionExplainLabel2 = new JLabel
("Check the options you want.");

public FINAL()
{

super("Event Price Estimator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
totPrice.setText("\$" + totalPrice);
}

public void itemStateChanged(ItemEvent event)

{
Object source = event.getSource();
int select = event.getStateChange();

if(source == oneBox)
if(select == ItemEvent.SELECTED)
totalPrice += ONE_BR;
else
totalPrice -= ONE_BR;

else if(source == twoBox)
{
if(select == ItemEvent.SELECTED)
totalPrice += TWO_BR;
else
totalPrice -= TWO_BR;
}

else if(source == rentalBox)

if(select == ItemEvent.SELECTED)
totalPrice += ROWBOAT_RENTAL;
else
totalPrice -= ROWBOAT_RENTAL;
totPrice.setText("\$" + totalPrice);
}

public static void main(String[] args)
{
int userEntry;
Scanner input = new Scanner(System.in);
System.out.print("How many weeks do you want to get away for? ");
userEntry = input.nextInt();

FINAL aFrame = new FINAL();

final int WIDTH = 300;
final int HEIGHT = 250;

aFrame.setSize(WIDTH, HEIGHT);
aFrame.setVisible(true);
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Running total with checkboxes problem

### #2 ihatesegfault

Reputation: 17
• Posts: 47
• Joined: 24-September 12

## Re: Running total with checkboxes problem

Posted 11 December 2012 - 06:13 PM

You only need to define userEntry once. You've defined it in the class as well as in the main method. The local variable userEntry is hiding the global one in the main method. Just get rid of line 84 and add in the multiplication lines.

Hint: don't actually modify the totalPrice variable when adding the multiplier. Do it as part of the setText line (line 79). Other wise the multiplier will be added every time an option changes.

Also rename userEntry to weeks or some other more descriptive name.

### #3 Autumn13

Reputation: 0
• Posts: 2
• Joined: 11-December 12

## Re: Running total with checkboxes problem

Posted 11 December 2012 - 06:33 PM

Thank you so much for your help will report back when i can