|
Help-would like JFRAME added to program
I have the following driver and class and would like to add JFRAME to it but have no clue how. Essentially I would like to figure out how to place all "public void Display" in GUI frames instead of the boring defualt dispaly boxes. If not what can I do to dress this program up a little bit. Plus how do you line a text string up so it appears a little neater, see my menu. thanks.
Here is the Driver:
public class Main {
/**
*
*/
public static void main (String[] args) {
/**
* Set up arrays and variables
*/
double unitCost [] = {0.00, 2.50, 100.00, 75.00, 95.00, 15.00, 8.50, 65.00, 45.00, 12.00, 72.00 };
String product [] = { "No item", "BaseBall", "Bat", "Infield Glove", "Catcher's Mit", "Batting Helmet", "Batting Glove", "Uniform with Team Logo", "Uniform without Team Logo", "Hat", "Cleats" };
String custNames [] = new String [6];
int quantityOrdered [] [] = new int [6] [11];
double totalSales [] [] = new double [6] [11];
double totalUnitCost = 0.0, totalCustSale = 0.0; //taxrate = 0.10, tax = 0.0;
int quantity = 0, itemNumber = 0, maxCust = 0, custNumber = 0, maxC = 0, maxProd = 0; //
String company = "Butch's Baseball Supplies", output, input;
maxProd = product.length;
WClass WEF = new WClass();
// Obtain maximum number of Customers to process: call getMaxCust method
do
{
maxCust = WEF.getMaxCust("Enter the number of customers:");
}
while (maxCust< 1 || maxCust >5);
/**
* Obtain the names of Customers to process: call getCustNames method
*/
WEF.getCustNames(custNames, maxCust);
/**
* Use a loop to process each customer in the Customer array
*/
for ( custNumber = 1; custNumber <=maxCust; ++custNumber)
{
do // process do-while for same customer until he enters '0' for item, which is exit
{
itemNumber = WEF.getChoice (custNumber); // Call method to get the item number
if (itemNumber !=0 )
{
quantity = WEF.getQuantity (custNumber, itemNumber, product, custNames, quantityOrdered);
WEF.itemPriceTotal (custNumber, itemNumber, quantityOrdered, unitCost, totalSales);
}
} while (itemNumber !=0);
WEF.displays (custNumber, maxCust, product, custNames, itemNumber, quantityOrdered, unitCost, totalSales);
}
WEF.eodDisplay(maxProd, product, custNames, quantityOrdered, unitCost, totalSales);
}
}
Here is mty working class:
package javaapplication24;
import javax.swing.*;
import java.text.DecimalFormat;
/**
*
* @author
*/
public class WClass {
DecimalFormat twoDigits = new DecimalFormat ("0.00"); //set decimal format for double
int maxC = 0, c = 0;
String input;
public int getMaxCust (String message)
{
input = JOptionPane.showInputDialog(message);
maxC=Integer.parseInt(input);
if (maxC < 1 || maxC > 5)
JOptionPane.showMessageDialog(null, "Invalid Number - choose between 1 and 5 customers");
return maxC;
}
public void getCustNames(String custNames[], int maxC)
{
for (int c=1; c <=maxC; ++c)
{custNames [c]=JOptionPane.showInputDialog("Enter customer #"+c+"'s name:");
}
}
public int getChoice (int c)
{
String menu,input;
int choice = 0;
menu = " WELCOME TO BUTCH'S BASEBALL SUPPLIES\n";
menu += " PLEASE SELECT FROM THE ITEMS BELOW AND PLACE THE ITEM NUMBER IN THE BOX \n\n";
menu += " 1 - BaseBall------------------- $2.50\n";
menu += " 2 - Bat------------------------ $100.00\n";
menu += " 3 - Infield Glove-------------- $75.00\n";
menu += " 4 - Catcher's Mit-------------- $95.00\n";
menu += " 5 - Batting Helmet------------- $15.00\n";
menu += " 6 - Batting Glove-------------- $8.50\n";
menu += " 7 - Uniform with Team Logo----- $65.00\n";
menu += " 8 - Uniform without Team Logo-- $45.00\n";
menu += " 9 - Hat------------------------ $12.00\n";
menu += " 10- Cleats--------------------- $72.00\n";
menu += "ENTER ZERO (0) WHEN ALL NEEDED ITEMS HAVE BEEN SELECTED.\n";
input = JOptionPane.showInputDialog(menu);
choice = Integer.parseInt(input);
return choice;
}
public int getQuantity (int custNumber, int itemNumber, String product[], String custNames[], int quantityOrdered[][])
{
int quantity = 0;
String qtyPrompt;
do
{
qtyPrompt = custNames[custNumber]+", You have ordered from Butch's Baseball Supplies\n";
qtyPrompt += custNames[custNumber]+", You have order item number "+itemNumber+" for "+product[itemNumber]+"\n";
qtyPrompt += "Enter the quantity of "+product[itemNumber]+ "s you would like to order:\n";
input = JOptionPane.showInputDialog(qtyPrompt);
quantity = Integer.parseInt(input);
} while (quantity == 0);
//End "do while loop"
quantityOrdered[custNumber][itemNumber] = quantity;
quantityOrdered[0][0] += quantity;
quantityOrdered[0][itemNumber] += quantity;
return quantity;
}
public void itemPriceTotal (int custNumber, int itemNumber, int quantityOrdered[][], double unitCost[], double totalSales[][])
{
totalSales[custNumber][itemNumber] = quantityOrdered[custNumber][itemNumber]*unitCost[itemNumber];
totalSales[0][0] += totalSales[custNumber][itemNumber];
totalSales[0][itemNumber] += totalSales[custNumber][itemNumber];
totalSales[custNumber][0] += totalSales[custNumber][itemNumber];
}
public void displays (int custNumber, int maxCust, String product[], String custNames[], int itemNumber, int quantityOrdered[][],
double unitCost[], double totalSales[][])
{
String output;
if (totalSales [custNumber][0] >0)
{output = "BUTCH'S BASEBALL SUPPLIES\n\n";
output += "CUSTOMER: "+custNames[custNumber]+"\n\n";
output += "ORDER:\n\n";
output += "ITEM\tQuantity\tUnit Price\tTotal\n\n";
for (int i=1; i <= 10; i++)
{
if (quantityOrdered[custNumber][i] > 0)
{
output+= product[i]+"\t"+quantityOrdered[custNumber][i]+ "\t$" +twoDigits.format(unitCost[i])+"\t$" +twoDigits.format(totalSales[custNumber][i])+"\n";
}
} // End of 'for' loop
output += "\nTotal:\t\t\t$"+twoDigits.format(totalSales[custNumber][0])+"\n\n";
JTextArea outputArea = new JTextArea();
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea, "Total Sales Invoice", JOptionPane.PLAIN_MESSAGE);
}// endif
}
public void eodDisplay (int maxProd, String product[], String custNames[], int quantityOrdered[][], double unitCost[], double totalSales[][])
{
String output;
output = "Item #"+"\t"+"Quantity"+"\t"+"Unit Price"+"\t"+"Total\n\n";
for ( int p=1; p <= maxProd-1; p++) //this loop processes through each item's EOD quantity sold and its total revenue
{
if (quantityOrdered [0][p] > 0) //Skip items with zero quantity sold
{
output += p+"\t"+quantityOrdered[0][p]+"\t$"+twoDigits.format(unitCost[p])+"\t$"+twoDigits.format (totalSales[0][p])+"\n";
}
//endif
}
// After the loop processes the toatals for each individual item sold, display the overall day's total quantity & sales
output += "\nTotal items sold today = "+"\t"+ quantityOrdered [0][0]+"\n";
output += "\nTotal Sales today ="+"\t$"+twoDigits.format(totalSales[0][0])+"\n\n";
JTextArea outputArea = new JTextArea ();
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea, "Butch's Baseball Supplies End of Day Report",JOptionPane.PLAIN_MESSAGE);
}
}
|