I have to create a currency converter for a project and i need help, I have to build it from using my commissionapplet with the directions from the work sheet some one please help
lion hunta 0 Newbie Poster
/*
Chapter 4: Sales Commission
Programmer: Marc Johnson
Date: March 16,2010
Filename: CommissionApplet.java
Purpose: This applet calculates sales commission using a sales amount
(input by the user) and a sales code (chosen from among the option buttons)
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class CommissionApplet extends Applet implements ItemListener
{
//declare variables and construct a color
double dollars, answer;
int empCode;
Image dollarSign;
Color darkRed = new Color(160, 50, 0);
//Create components for applet
Label promptLabel = new Label("Enter the sales amount (do not use commas or dollar signs):");
TextField salesField = new TextField(20);
Label codeLabel = new Label ("Select the appropriate commission code:");
CheckboxGroup codeGroup = new CheckboxGroup();
Checkbox telephoneBox = new Checkbox("Telephone Sales",false,codeGroup);
Checkbox inStoreBox = new Checkbox("In-Store Sales",false,codeGroup);
Checkbox outsideBox = new Checkbox("Outside Sales",false,codeGroup);
Checkbox hiddenBox = new Checkbox("",true,codeGroup);
Label outputLabel = new Label("Click and option button to calculate the sales commission.");
public void init()
{
setBackground(darkRed);
setForeground(Color.white);
add(promptLabel);
add(salesField);
salesField.requestFocus();
salesField.setForeground(Color.black);
add(codeLabel);
add(telephoneBox);
telephoneBox.addItemListener(this);
add(inStoreBox);
inStoreBox.addItemListener(this);
add(outsideBox);
outsideBox.addItemListener(this);
add(outputLabel);
}
//This method is triggered by the user clicking an option button
public void itemStateChanged(ItemEvent choice)
{
try
{
dollars = getSales();
empCode = getCode();
answer = getComm(dollars,empCode);
output(answer, dollars);
}
catch (NumberFormatException e)
{
outputLabel.setText("You must enter a dollar amount greater than zero.");
hiddenBox.setState(true);
salesField.setText("");
salesField.requestFocus();
}
}
public double getSales()
{
double sales = Double.parseDouble(salesField.getText());
if (sales <=0) throw new NumberFormatException();
return sales;
}
public int getCode()
{
int code = 0;
if (telephoneBox.getState()) code = 1;
else
if (inStoreBox.getState()) code = 2;
else
if (outsideBox.getState()) code = 3;
return code;
}
public double getComm(double sales, int code)
{
double commission = 0.0;
switch(code)
{
case 1:
commission = .10 * sales;
break;
case 2:
commission = .14 * sales;
break;
case 3:
commission = .18 * sales;
break;
}
return commission;
}
public void output(double commission, double sales)
{
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
outputLabel.setText("Your commission on sales of" + twoDigits.format(sales) + "is" + twoDigits.format(commission));
}
public void paint(Graphics g)
{
dollarSign = getImage(getDocumentBase(), "dollarSign.gif");
g.drawImage(dollarSign,12,28,this);
}
}
This attachment is potentially unsafe to open. It may be an executable that is capable of making changes to your file system, or it may require specific software to open. Use caution and only open this attachment if you are comfortable working with msword files.
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
Just create the labels, buttons and textfields like the example.
Check the API for what you need to use.
When the radio buttons are clicked get the value of the text field like the example.
Look the API of the JRadioButton and see what Listener to use. Use the getSource() method of the argument of the method the listener has to get which button was clicked.
Now that you know that just do multiplication to find the amount in euros and display it.
Like the example
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.