In: Computer Science
CandleLine is a business that sells designer candles via the Internet and has the order delivered Below ts the code fora GUI Study the code and answer the questions that follow import java awt *, import java applet *, import java awt event *, import java text DecimalFormat, public class CandleApplet extends Applet amplements ItemListener { //Create components for applet Label companyNameLabel = new Label ("CandleLine--Candles On-line"), Label priceLabel=new Label ("Please enter the total amount of your order "), TextField priceField = new TextField(35), Labei deliveryLabel=new Label ("Please choose your method of delivery "), CheckboxGroup deliveryGroup = new CheckboxGroup (), Checkbox oneDayBox = new Checkbox("Priority (Overnight)",false,deliveryGroup) , Checkbox twoDayBox = new Checkbox("Express (2 business days)", false,deliveryGroup), Checkbox moreDaysBox = new Checkbox("Standard (3 to 7 business days)", false,deliveryGroup) , Checkbox hiddenBox = new Checkbox ("",true,deliveryGroup}) , Label outputLabel = new Label("We guarantee on time delivery, or your money back "”)}, public void init () { //Add components to window and set colors setBackground (Color cyan}, add (companyNameLabel) , add (priceLabel) , add (priceField), priceField requestFocus(), add (delaveryLabel), add (oneDayBox) , oneDayBox addItemListener (this), add (twoDayBox) , twoDayBox additemListener (this), add (moreDaysBox) , MoreDaysBox addItemListener(this), add(outputLabel), public void itemStateChanged(ItemEvent choice) { try { double delivery, double price = Double parseDouble(priceField getText()), QUESTION 5. 2 ENTER YOUR CODE HERE //Display output outputLabel setForeground(Color black}, DecimalFormat twoDigits = new DecimalFormat ("RH#,### 00"), outputLabel setText ("Your total cost is " + twoDigits format (price + delivery)), catch {NumberFormatException e) { outputLabel setText ("You must enter an amount greater than zero"), outputLabel setForeground(Color red), hiddenBox setState (true), priceField setText(""), priceField requestFocus({), } } }
5.1 Draw the output that is created when the above applet ts ran ?
5.2 After the user entered an amount, the program must calculate the delivery charges If a customer wants pnority delivery (overnight), then the delivery charge is R120 00 If | the customer prefers express delivery (2 business days), then the delivery charge ts R90 00 If the customer wants standard delivery (3 to 7 business days) and the total | cost of the order is more than R75 00, then CandleLine delivers the items to the customer free, If the order is less than R75 00, then CandileLine charges R20 00 for the standard delivery .
Write the code needed to calculate the delivery cost ?
Here is the completed code for this problem. Your code was a mess. Took a lot of time to format it, I’m guessing it is transcribed from an image. In the future when you are pasting a code, please make sure that it is in proper format. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// CandleApplet.java
import java.applet.Applet;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.Checkbox;
import java.text.DecimalFormat;
public class CandleApplet extends Applet implements ItemListener {
// Create components for applet
Label companyNameLabel = new Label("CandleLine--Candles On-line");
Label priceLabel = new Label("Please enter the total amount of your order ");
TextField priceField = new TextField(35);
Label deliveryLabel = new Label("Please choose your method of delivery ");
CheckboxGroup deliveryGroup = new CheckboxGroup();
Checkbox oneDayBox = new Checkbox("Priority (Overnight)", false,
deliveryGroup);
Checkbox twoDayBox = new Checkbox("Express (2 business days)", false,
deliveryGroup);
Checkbox moreDaysBox = new Checkbox("Standard (3 to 7 business days)",
false, deliveryGroup);
Checkbox hiddenBox = new Checkbox("", true, deliveryGroup);
Label outputLabel = new Label(
"We guarantee on time delivery, or your money back ");
public void init() { // Add components to window and set colors
setBackground(Color.cyan);
add(companyNameLabel);
add(priceLabel);
add(priceField);
priceField.requestFocus();
add(deliveryLabel);
add(oneDayBox);
oneDayBox.addItemListener(this);
add(twoDayBox);
twoDayBox.addItemListener(this);
add(moreDaysBox);
moreDaysBox.addItemListener(this);
add(outputLabel);
}
public void itemStateChanged(ItemEvent choice) {
try {
double delivery = 0;
double price = Double.parseDouble(priceField.getText());
// code for QUESTION 5. 2
// checking if oneDayBox is selected
if (oneDayBox.getState()) {
// delivery charge = R120
delivery = 120;
} else if (twoDayBox.getState()) {
// delivery charge = R90 for two day delivery
delivery = 90;
} else if (moreDaysBox.getState()) {
// standard delivery
if (price >= 75) {
// free delivery for products with price>=75
delivery = 0;
} else {
// delivery charge = R20 for products with price<75
delivery = 20;
}
}
// Display output
outputLabel.setForeground(Color.black);
DecimalFormat twoDigits = new DecimalFormat("RH#,### 00");
outputLabel.setText("Your total cost is "
+ twoDigits.format(price + delivery));
} catch (NumberFormatException e) {
outputLabel.setText("You must enter an amount greater than zero");
outputLabel.setForeground(Color.red);
hiddenBox.setState(true);
priceField.setText("");
priceField.requestFocus();
}
}
}
/*OUTPUT screenshots*/