I have to write two programs, one writes to file based on what the user selects. The other reads the file, and outputs what the user selects. And I'm really not sue if I'm doing this right.
Here is what I have for writting to the file:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/*
* class LogOrder
* Allows user to pick from five pieces of clothing to order
* user enters name and picks clothing and presses order.
* Stores users order to "order.txt"
* @author Kimberlie Davis
* @version 11/20/08
*/
public class LogOrder
{
/*
* main method
* declares and sets frame as visible
* @param args not used
*/
public static void main(String args[])
{
EventQueue.invokeLater(
new Runnable()
{
public void run()
{
LogOrderFrame mainFrame = new LogOrderFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
}
});
}
}
/*
* LogOrderFrame method
* sets up frame
* Declares and adds panel
*/
class LogOrderFrame extends JFrame
{
public LogOrderFrame()
{
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
LogOrderPanel panel = new LogOrderPanel();
this.add(panel);
this.setTitle("Log order");
}
private static int DEFAULT_WIDTH = 500;
private static int DEFAULT_HEIGHT = 500;
}
/*
* LogOrderPanel method
* Layouts and panels
* Sets up panel with components
* adds actionListener to button
*/
class LogOrderPanel extends JPanel
{
public LogOrderPanel()
{
//set up Buffered writer
try
{
writer = new BufferedWriter(new FileWriter("order.txt"));
}
catch(IOException e)
{
}
//set up layouts
this.setLayout(new BorderLayout());
//north
JPanel north = new JPanel(new BorderLayout());
this.add(north, BorderLayout.NORTH);
//north center
JPanel northCenter = new JPanel();
north.add(northCenter, BorderLayout.CENTER);
//center
JPanel center = new JPanel();
this.add(center, BorderLayout.CENTER);
Box boxLayout = Box.createVerticalBox();
center.add(boxLayout);
//set up components
JLabel companyName = new JLabel("Clothing Inc.");
JLabel askName = new JLabel("Please enter your name:");
enterName = new JTextField(5);
//Array of 5 checkboxes
for(int i = 0; i < 5; i++)
{
check[i] = new JCheckBox();
}
check[0].setText("Jacket");
check[1].setText("Shirt");
check[2].setText("Pants");
check[3].setText("Socks");
check[4].setText("Shoes");
JButton order = new JButton("Order");
//add components
northCenter.add(askName);
northCenter.add(enterName);
boxLayout.add(check[0]);
boxLayout.add(check[1]);
boxLayout.add(check[2]);
boxLayout.add(check[3]);
boxLayout.add(check[4]);
north.add(companyName, BorderLayout.NORTH);
this.add(order, BorderLayout.SOUTH);
ClothingChooser chooseAction = new ClothingChooser();
order.addActionListener(chooseAction);
}
/*
* ClothingChooser class
* implements ActionListener
* sets up actions for button
*/
private class ClothingChooser implements ActionListener
{
/*
* actionPerformed method
* checks what checkboxes have been selected
* stores users order to file "order.txt"
* @param ActionEvent
*/
public void actionPerformed(ActionEvent e)
{
try
{
try
{
writer.write("Order:" + '\n');
writer.write("For: " + enterName.getText() + '\n');
if(check[0].isSelected())
writer.write('\t' + "Jacket" + '\n');
if(check[1].isSelected())
writer.write('\t' + "Shirt" + '\n');
if(check[2].isSelected())
writer.write('\t' + "Pants" + '\n');
if(check[3].isSelected())
writer.write('\t' + "Socks" + '\n');
if(check[4].isSelected())
writer.write('\t' + "Shoes" + '\n');
writer.write('\n');
}
finally
{
JOptionPane.showMessageDialog(null, "Conratulations " + enterName.getText() + " your order was successful!");
}
}
catch(IOException exception)
{
JOptionPane.showMessageDialog(null, "Error: Order not receive\n Please call Clothing Inc. to order");
}
}
}
//datafields
JCheckBox[] check = new JCheckBox[5];
BufferedWriter writer;
JTextField enterName;
}
Did I do this correctly?