Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to code buttons for an applet?

 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have to do:

Create a Swing based Applet for a computer company that sells computers with associated peripherals.   The user will select the peripherals that they want, choose a method of shipping, and click a button to obtain a table of costs.

GUI:

Checkboxes for at least 6 computer peripherals (printer, speakers, etc).
Three option buttons for three types of shipping (ground, air, fedex)
A button to calculate total with shipping
A button to calculate total without shipping
A large textPane to display your results.
Output:

When the user presses one of the two calculate buttons, your program should display a table for the cost of 1 unit, 100 units, 200 units, up to 1000 units.  It should have one column for the quantity, and one column for the cost.  The cost should may or may not including shipping costs, depending on which button was clicked.

calculateCost:

Create two methods called calculateCost (to demonstrate overloading).  Both should return a double.

One should take no parameters and simply calculate the cost for ONE unit (base cost plus any selected peripherals).
The other should take two parameters (quantity and a boolean that indicates whether shipping should be included or not) and it should return the cost for the specified quantity, factoring in the quantity discount and shipping costs (if applicable).  It should use the other calculateCost method to get the cost for one unit.
Obviously, you will need to determine what a base computer costs and what the peripherals cost.

If shipping is to be included, costs are:

Ground is 5% of total order if less than 100 units (otherwise, free)
Air is 10% of total order
FedEx is 20% of total order
getDiscountPercentage:

Create a helper method to determine the discount percentage based on quantity.   The discount should be 1% for 100-199, 2% for 200-299, etc.:

public double getDiscountPercentage(int quantity)

Event Handlers:

Create event handlers for the two calculate buttons.  The event handlers will loop, calling calculateCost for quantities 1, 100, 200, up to 1000 and displaying the results as a table in the GUI.  The event handlers will be virtually identical, with the exception of the boolean value passed to calculateCost, so you may find it useful to write another method to eliminate duplicate code.  I'll leave that one up to you to figure out.

You will use the TextPane for the output.  You will be building a string for the output.  Use characters "\t" for tab and "\n" for a line feed.  Each row should have the quantity and the cost separated by a tab.  You will probably find this line of code rather useful!

            outputTextPane.setText(outputTextPane.getText() + quantity + "\t" + cost  + "\n" );

The above line of code gets the previous contents of the output text pane and appends the quantity and the cost, separated by a tab.  The end of the line is a line feed so that each line of output ends up on a separate line in the output text pane.

Comments

In your comments at the top of the program, be sure to include the following information:

Cost of base computer
Cost of each peripheral
Comments are a very important part of your code so remember to include them in all your methods.

And so far this is my code:


My question is how do I code the two buttons for more than one unit? I am confused by what the assignment is asking me to do as well as how to do it.
 
Knute Snortum
Bartender
Pie
Posts: 2904
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instructions wrote:A button to calculate total with shipping
A button to calculate total without shipping
A large textPane to display your results.
Output:

When the user presses one of the two calculate buttons, your program should display a table for the cost of 1 unit, 100 units, 200 units, up to 1000 units.  It should have one column for the quantity, and one column for the cost.  The cost should may or may not including shipping costs, depending on which button was clicked. 

So it sounds like all the output should be displayed in the testPane.
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I knew that part. Was just unsure of how do I code the two buttons for more than one unit?
 
Campbell Ritchie
Sheriff
Posts: 51447
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using applets, which nobody else uses?
I presume that you can add a listener to each button, and you can do that easily with a λ (Java8 only).
myButton.addActionListener(evt -> someMethodOrOther());
Now create that method, with void return type and (if in the same class) private access. If you need to update two components, put both of them into the method.
At least that is how you would do it in Swing.

You still have the problem about what you are returning in that method which calculates the prices.
 
Knute Snortum
Bartender
Pie
Posts: 2904
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:Yes, I knew that part. Was just unsure of how do I code the two buttons for more than one unit?

I'm still not sure what your question is.  I would assume that you would build a string with the necessary text and send it to the JTextPane.
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you using applets, which nobody else uses?
I presume that you can add a listener to each button, and you can do that easily with a λ (Java8 only).
myButton.addActionListener(evt -> someMethodOrOther());
Now create that method, with void return type and (if in the same class) private access. If you need to update two components, put both of them into the method.
At least that is how you would do it in Swing.

You still have the problem about what you are returning in that method which calculates the prices.


I am well aware no one uses applets anymore, this assignment is for a class I am currently taking.
 
Campbell Ritchie
Sheriff
Posts: 51447
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't get me started about things nobody uses any more. Did you have any success with the listeners? Have you sorted out the method that calculates prices?
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Did you have any success with the listeners? Have you sorted out the method that calculates prices?


Yes, this is my updated code:



My newest problem is How do I code the "with shipping" and "without shipping" buttons so that they calculate the total cost with the shipping or without the shipping and displays a table for the cost of 1 unit, 100 units, 200 units, up to 1000 units. The question asks for two columns and based on my display in my GUI I do not understand how I would do that as well.

 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

From the fact that you never showed us the GUI setup code, and your code has a "do not modify" section, I am going to speculate that this question isn't a GUI question, as your instructor already did all the GUI the work.

I am going to speculate that the question is how to implement the button listener in your code? Is it the implementation of the button listener whose outline has already been created, but is just empty?

Is that what you are asking?
Henry
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
From the fact that you never showed us the GUI setup code, and your code has a "do not modify" section, I am going to speculate that this question isn't a GUI question, as your instructor already did all the GUI the work.

I am going to speculate that the question is how to implement the button listener in your code? Is it the implementation of the button listener whose outline has already been created, but is just empty?

Is that what you are asking?
Henry


Yes, that would be correct. I set up the GUI in NetBeans and it automatically writes the code for the GUI.
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... then it should be straightforward. The button callbacks just need to set some sort of state (probably via a variable) that "with" or "without" has been pressed.

The tedious part is the rest of the applet. It has to be modified to behave correctly based on this saved state. 

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:... then it should be straightforward. The button callbacks just need to set some sort of state (probably via a variable) that "with" or "without" has been pressed.

The tedious part is the rest of the applet. It has to be modified to behave correctly based on this saved state. 

Henry


What do you mean via a variable? Do I have to add a new variable to my method? How would I do that?
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
What do you mean via a variable? Do I have to add a new variable to my method? How would I do that?


First, of course, I don't mean adding a new variable to the method. You need a scope that will be visible by other parts of your applet.


BTW, you do know that this is the third (or is it fourth? ) time that we have come down this path? Have you read the tutorial on variables yet?

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I have read it multiple times and I can not for the life of me get what it means.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, can I suggest taking a step back here?
How much Java have you written before this?
 
Campbell Ritchie
Sheriff
Posts: 51447
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:. . . Yes, this is my updated code. . . .
But the no‑arguments calculateCost method still looks incorrect to me. How much will you charge for a computer with an extra keyboard (do you really charge $30?) and a mouse?
The calculateCost method with arguments uses == true. That is not only bad style, but also very error‑prone. Every now and again we see somebody write = instead of ==.
Not if (b == true) ...
but
if (b) ...
Not if (b == false) ...
but
if (!b) ...
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But the no‑arguments calculateCost method still looks incorrect to me. How much will you charge for a computer with an extra keyboard (do you really charge $30?) and a mouse?
The calculateCost method with arguments uses == true. That is not only bad style, but also very error‑prone. Every now and again we see somebody write = instead of ==.
Not if (b == true) ...
but
if (b) ...
Not if (b == false) ...
but
if (!b) ...


What do you mean by this? Should I be using == instead of +=?
 
Campbell Ritchie
Sheriff
Posts: 51447
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:. . . Should I be using == instead of +=?
No. I have told you a circumstance where you shouldn't use == at all.
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is all too confusing.
 
Campbell Ritchie
Sheriff
Posts: 51447
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your last code block change line 43 to read:-
if (shipping) {
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic