Writing to a file and a TextArea



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]
.