Hello there! The application I am building will calculate the total sales for
any number of salesmen. The application will allow the salesman to enter
their name, the product (numbered 1-5 in a JComboBox) they sold as well as
the amount of products they sold. They would then hit the Salesman Entry Done
button to tell the application that the salesman is done entering their
information. Once all of the salesmen have entered their information they can
hit Grand Total and the grand total of those sales will display in the text
area.
So it would look something like this:
TOTAL SALES
Name XXXXXX
Name XXXXXX
Total XXXXXX
I put together a GUI in flow layout (which I may change, but I wanted to move
on to the technical things first). Everything in the application seems to be
working ok except 1) it is not printing to the output textarea and 2) the
calculations do not seem to be working properly. The data is being sent to
the .txt file and it looks great, except that the sales and total are all $0.
00, nothing is being calculated. I hoping maybe someone could help me see
what I have wrong. It compile s just fine, but it is not calculating
anything. Would this have anything to do with "myList or outp"?
Hopefully the above makes sense, but if not just let me know and I will try
to explain a little better. Thanks so much!! That is just a brief run down
of what I am trying to do. Any little tips just letting me know I am on the
right track, or if you see something I should change or add, or whatever
would be appreciated!! Thanks so much everyone!!
[code]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.io.*;
import java.util.*;
import java.text.NumberFormat;
public class Sales extends JFrame
{
static JLabel salesmanlabel;
static JTextField name;
static JLabel amtsold;
static JTextField amount;
static JLabel productname;
static JComboBox product;
static JButton salesmandone;
static JButton calctotal;
static JTextArea output;
static FileWriter file;
static BufferedWriter buff;
static String outp;
static Handler handler = new Handler();
static ArrayList<Salesman> myList = new ArrayList<Salesman>();
static NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
public Sales()
{
super("Monthly Sales");
Container c = getContentPane();
c.setLayout(new FlowLayout());
try
{
file = new FileWriter("SalesData.txt");
buff = new BufferedWriter(file);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
salesmanlabel = new JLabel("Salesman:");
name = new JTextField(20);
amtsold = new JLabel("Amount Sold:");
amount = new JTextField(20);
productname = new JLabel("Product Sold:");
product = new JComboBox();
salesmandone = new JButton("Salesman Entry Done");
calctotal = new JButton("Grand Total");
output = new JTextArea(400, 200);
product.addItem("");
product.addItem("Product 1");
product.addItem("Product 2");
product.addItem("Product 3");
product.addItem("Product 4");
product.addItem("Product 5");
c.add(salesmanlabel);
c.add(name);
c.add(amtsold);
c.add(amount);
c.add(productname);
c.add(product);
c.add(salesmandone);
c.add(calctotal);
c.add(output);
salesmandone.addActionListener(handler);
calctotal.addActionListener(handler);
}
public static void main (String args[])
{
Sales sale = new Sales();
sale.setSize(300,300);
sale.setVisible(true);
sale.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
try
{
buff.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
System.exit(0);
}});
}
public static class Handler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == salesmandone)
{
addSalesman();
}
else if(e.getSource() == calctotal)
{
calculateTotal();
}
}
}
public static void addSalesman()
{
double sales = calculateSales(product.getSelectedIndex(), Integer.parseInt
(amount.getText()));
myList.add(new Salesman(name.getText(), sales));
name.setText("");
product.setSelectedIndex(0);
amount.setText("");
name.requestFocus();
}
public static double calculateSales(int product,int amount)
{
double sales;
product++;
switch(product)
{
case 1: sales = (amount * 2.98);
break;
case 2: sales = (amount * 4.50);
break;
case 3: sales = (amount * 9.98);
break;
case 4: sales = (amount * 4.49);
break;
case 5: sales = (amount * 6.87);
break;
default: sales = (0.00);
}
return sales;
}
public static void calculateTotal()
{
double total = 0.00;
Collections.sort(myList, new Comparator<Salesman>(){
public int compare(Salesman s1, Salesman s2) {
return s1.getName().compareTo(s2.getName());}});
outp = "\tTOTAL SALES\n\n";
for (Salesman salesm : myList)
{
outp += salesm.toString() +"\n";
total += salesm.getSales();
}
outp += "\nTotal\t\t\t" + currencyFormat.format(total);
output.setText(outp);
try
{
buff.write(outp);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
[/code]
[code]
import java.text.NumberFormat;
public class Salesman
{
private String name;
private double sales;
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
public Salesman()
{
setName("");
setSales(0);
}
public Salesman(String name, double sales)
{
setName(name);
setSales(0);
}
public void setName(String name)
{
this.name = name;
}
public void setSales(double sales)
{
this.sales = sales;
}
public String getName()
{
return name;
}
public double getSales()
{
return sales;
}
public String toString()
{
return (name + "\t\t\t" + currencyFormat.format(sales));
}
}
[/code]
IchBin - 24 Feb 2006 19:29 GMT
> Hello there! The application I am building will calculate the total sales for
> any number of salesmen. The application will allow the salesman to enter
[quoted text clipped - 4 lines]
> hit Grand Total and the grand total of those sales will display in the text
> area.
[snip code]
> public Salesman(String name, double sales)
> {
> setName(name);
> setSales(0);
> }
[snip code]
Change this method to ..
public Salesman(String name, double sales)
{
setName(name);
setSales(sales);
}

Signature
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
IchBin - 24 Feb 2006 22:53 GMT
> Hello there! The application I am building will calculate the total sales for
> any number of salesmen. The application will allow the salesman to enter
[quoted text clipped - 4 lines]
> hit Grand Total and the grand total of those sales will display in the text
> area.
If you replace and or add the code I include this will get you where you
want to be. You will need to change the layout manager and component
resizing but I'm sure you will not have a problem with that.. You may
want to add your JTextArea into a scroll pane. This is normally the
standard you do in most cases.
[snip code]
> static NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
>
private static final StyledDocument document = new DefaultStyledDocument();
private static int offset = document.getLength();
`
[snip code]
> output = new JTextArea(400, 200);
output = new JTextArea(document);
output.setOpaque(true);
output.setLineWrap(true);
output.setWrapStyleWord(true);
JScrollPane jScrollPane = new JScrollPane(output);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setOpaque(true);
jScrollPane.setSize(300, 300);
[snip code]
c.add(output);
c.add(jScrollPane);
[snip code]
> Sales sale = new Sales();
> sale.setSize(300,300);
sale.pack();
> sale.setVisible(true);
[snip code]
> outp += "\nTotal\t\t\t" + currencyFormat.format(total);
[snip code]
try {
SimpleAttributeSet sas = new SimpleAttributeSet();
sas.addAttribute(StyleConstants.Foreground, Color.BLUE);
document.insertString(offset, outp+ " \n", sas);
sas.removeAttribute(StyleConstants.Foreground);
offset = document.getLength();
} catch (BadLocationException ble) {
ble.printStackTrace();
}

Signature
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
zhah99 - 25 Feb 2006 01:43 GMT
Thank you so much for your reply. Everything is running great!! I
appreciate your help very much!!
zhah99 - 25 Feb 2006 01:57 GMT
Hey there, one more question, if you have time. Let's say I am the user and
I want to enter a name "Alex" and then I sold 5 of Product 2 and I also sold
4 of Product 3. Is there a way I can enter in Alex the number and the
product amount then hit salesman entry done and then put his name in again
Alex but then a different product and amount and when it displays still only
have his name listed once with everything added together?
>Thank you so much for your reply. Everything is running great!! I
>appreciate your help very much!!
IchBin - 25 Feb 2006 03:47 GMT
> Hey there, one more question, if you have time. Let's say I am the user and
> I want to enter a name "Alex" and then I sold 5 of Product 2 and I also sold
[quoted text clipped - 5 lines]
>> Thank you so much for your reply. Everything is running great!! I
>> appreciate your help very much!!
Sure.. Just added a Number_of input field or better yet use a JSpinner
for user to select the number_of. I can pass you code if you have a hard
time with that.
I would not use a switch on product for subtotals. In your sample, I
would have an array of 2 dimensions call it 'ProductCostArray'. The
first Dim is your product code (index++), since this is what the numbers
are now. The second dim would be [0]cost_product and [1]sales_amt you
multiply. This way you can just loop thru and calculate in three lines
of code.
Good place to look for examples is at "The Java Developers Almanac 1.4":
http://javaalmanac.com Just search for the object to see examples.
Take a peek at the JTextComponent. You can do some really neat things
with this, it in respect to formating.
Take a look at JGoodies Forms and L&Feel Libs at
https://jgoodies.dev.java.net for a layout manager. I find it to have
more power than GridBagLayout but with a tenth of the headaches.
Hope this make sense. Not sure where you are going with this but you
should start thinking about persistence.
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Martin Bradley - 26 Feb 2006 20:54 GMT
> Hello there! The application I am building will calculate the total sales for
> any number of salesmen.
> switch(product)
> {
[quoted text clipped - 15 lines]
> default: sales = (0.00);
> }
Hi,
I'd read the numbers you have from a properties file so that your
not re-releasing code each time a price changes.
priceinfo = 2.98,4.50,9.98,4.49,6.87
regards,
Martin.