article· jaw
Java Swing example
java
Here is an example of how to create a Java swing application. It's a little dated, I'm not sure I would still do it this way but the basic Java is valid and even though I wrote this 14 years ago it still compiles and runs on the latest version of Java. That's pretty cool.
I'm going to post the code in the forum as well. The code <> tags are a lot better than here.
------------------------start cut--------------------------------
/*
By: JAW
PROGRAM DESCRIPTION
This program allows the user to input the salesperson's name and the
items and quantity that they have sold then calculates and displays their
commission
*/
import javax.swing.JOptionPane; //include java packages
import java.text.DecimalFormat;
public class ass413
{
public static void main (String args [])
{
//Variable Declarations
String input_item, input_quant, input_name, name, message;
double total_sales, commission, salary;
String error = "Invalid Item Number";
String error2 = "No sales entered";
int itemnumber, quantity, counter, cont, valid, get_quant;
//Variable initialization
get_quant = 0;
valid = 1;
cont = 1;
commission = 0;
counter = 0;
salary = 0;
itemnumber = 0;
input_name = "no name";
quantity = 0;
total_sales = 0;
input_item = "0";
input_quant = "0";
while ( itemnumber != -1 ) {// start main loop
//Get salesperson name
input_name = JOptionPane.showInputDialog (
"Enter Salesperson's name");
//Move to new variable
name = input_name;
itemnumber = 0;
total_sales = 0;
cont = 1;
get_quant = 0;
while (cont == 1){ //start continue loop
while ( valid == 1 ) // start validation loop
{
//Get item number from user
input_item = JOptionPane.showInputDialog(
"Enter Item number 1-4 (-1 to exit\n or 5 to display commission)");
counter++;
//convert string to integer
itemnumber = Integer.parseInt ( input_item );
//Validate for commission display action and display commission
if (itemnumber == 5 & counter > 1 )
{
commission = (total_sales * .09) + 200;
DecimalFormat twoDigits = new DecimalFormat("$.00");
message = name + "'s commission is " + twoDigits.format( commission);
JOptionPane.showMessageDialog ( null, message, "output window", JOptionPane.INFORMATION_MESSAGE );
cont = 0;
get_quant = 1;
valid = 1;
break;
}
// Test for exit action
if (itemnumber == -1 )
{
valid = 0;
cont = 0;
}
//Display error message if commission display code invalid
if (itemnumber == 5 & counter == 1 )
{
JOptionPane.showMessageDialog(null, error2,
"Wrong Wrong Wrong Wrong....",
JOptionPane.INFORMATION_MESSAGE );
valid = 1;
get_quant = 1;
cont = 1;
continue;
}
//Test for exit code
if (itemnumber != -1)
{ //Validate item number
if (itemnumber < 1 || itemnumber > 5)
{
valid = 1;
JOptionPane.showMessageDialog(null, error,
"Wrong Wrong Wrong Wrong....",
JOptionPane.INFORMATION_MESSAGE );
}
}
//Set flag to valid if item number is valid
if (itemnumber > 0 & itemnumber < 6 || itemnumber == -1)
{
valid = 0;
break;
}
} //end validation loop
//test item number for exit code
if (itemnumber != -1)
{
if (get_quant == 0)//test get quantity flag if true get quantity
{
// Get quantity from user
input_quant = JOptionPane.showInputDialog(
"Enter Quantity sold");
//convert string to integer
quantity = Integer.parseInt ( input_quant );
valid = 1;
get_quant = 0;
}
//Test item number to determine cost & calculate total sales
if (itemnumber == 1 )
{
total_sales = total_sales + (239.99 * quantity);
counter++;
}
else if (itemnumber == 2)
{
total_sales = total_sales + (quantity * 129.75);
counter++;
}
else if (itemnumber == 3)
{
total_sales = total_sales + (quantity * 99.95);
counter++;
}
else if (itemnumber == 4)
{
total_sales = total_sales + (quantity * 350.89);
counter++;
}
}//end continue loop
}//end quantity condition check
} //end main loop
System.exit( 0 ); //exit program & return 0 to O\S
} //end main function
}//end public class statment
-----------------------------------------end cut----------------------------------------------------------------
Tags:
Restored from the original jawtech.com (archived copy). Links and downloads from that era may no longer work.