hi i have been working on my project to create a store and i have to add items and sell items, and once i add items i store them into a textfile separated by commas, and i made the add item button work perfectly but i am having trouble with the sell button.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class text_feild implements ActionListener {
JPanel titlePanel, buttonPanel;
JButton button, Sell_button;
InputStreamReader input_stream = new InputStreamReader(System.in);
BufferedReader read_Buffer;
FileOutputStream file_out_put;
PrintStream print_stream;
FileReader read_from_file;
JTextField field_id = new JTextField(20);
JTextField field_name = new JTextField(20);
JTextField field_price = new JTextField(20);
JTextField field_sq = new JTextField(20);
public JButton Add_Product() {
JFrame frame = new JFrame("Adding Items");
JPanel panel = new JPanel();
button = new JButton("Save Items");
JLabel lab_id = new JLabel("ID #:");
JLabel lab_name = new JLabel("Name:");
JLabel lab_price = new JLabel("Price:");
JLabel lab_sq = new JLabel("Quantity: ");
button.addActionListener(this);
lab_id.setLocation(0, 0);
lab_id.setSize(50, 50);
frame.add(lab_id);
field_id.setLocation(100, 15);
field_id.setSize(80, 20);
frame.add(field_id);
lab_name.setLocation(0, 25);
lab_name.setSize(50, 50);
frame.add(lab_name);
field_name.setLocation(100, 40);
field_name.setSize(130, 20);
frame.add(field_name);
lab_price.setLocation(0, 50);
lab_price.setSize(50, 50);
frame.add(lab_price);
field_price.setLocation(100, 65);
field_price.setSize(130, 20);
frame.add(field_price);
lab_sq.setLocation(0, 70);
lab_sq.setSize(75, 50);
frame.add(lab_sq);
field_sq.setLocation(100, 90);
field_sq.setSize(130, 20);
frame.add(field_sq);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.setSize(350, 300);
frame.setVisible(true);
return button;
}
public JButton Sell_Product() {
JFrame frame = new JFrame("Sell Items");
JPanel panel = new JPanel();
Sell_button = new JButton("Buy Items");
JLabel lab_id = new JLabel("ID #:");
JLabel lab_name = new JLabel("Name:");
JLabel lab_price = new JLabel("Price:");
JLabel lab_sq = new JLabel("Quantity: ");
Sell_button.addActionListener(this);
lab_id.setLocation(0, 0);
lab_id.setSize(50, 50);
frame.add(lab_id);
field_id.setLocation(100, 15);
field_id.setSize(80, 20);
frame.add(field_id);
lab_name.setLocation(0, 25);
lab_name.setSize(50, 50);
frame.add(lab_name);
field_name.setLocation(100, 40);
field_name.setSize(130, 20);
frame.add(field_name);
lab_price.setLocation(0, 50);
lab_price.setSize(50, 50);
frame.add(lab_price);
field_price.setLocation(100, 65);
field_price.setSize(130, 20);
frame.add(field_price);
lab_sq.setLocation(0, 70);
lab_sq.setSize(75, 50);
frame.add(lab_sq);
field_sq.setLocation(100, 90);
field_sq.setSize(130, 20);
frame.add(field_sq);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.SOUTH, Sell_button);
frame.setSize(350, 300);
frame.setVisible(true);
return Sell_button;
}
public void actionPerformed(ActionEvent b) {
Transcation tn = new Transcation ();
JFrame f = new JFrame();
String id = field_id.getText().trim();
String name = field_name.getText().trim();
String price = field_price.getText().trim();
String quantity = field_sq.getText().trim();
Integer[] value=new Integer[5];
if (b.getSource() == button) {
if (id == null || id.equals("") || name == null || name.equals("")
|| price == null || price.equals("") || quantity == null
|| quantity.equals(""))
{
JOptionPane.showMessageDialog(f, "Information was left out");
}
else {
try {
file_out_put = new FileOutputStream("Item.txt", true);
print_stream = new PrintStream(file_out_put);
print_stream.println(id + "," + name + "," + price + ","
+ quantity);
file_out_put.close();
} catch (Exception ex) {
System.out.println("There was a problem with the system");
}
}
}
else if (b.getSource() == Sell_button) {
try
{
int ID = Integer.parseInt(id);
int PRICE = Integer.parseInt(price);
int QUY = Integer.parseInt(quantity);
tn.makeTransaction();
file_out_put=new FileOutputStream("Transaction.txt",true);
print_stream=new PrintStream(file_out_put);
print_stream.println(ID+","+name+","+PRICE+","+QUY);
file_out_put.close();
System.out.println("Transaction has been successful");
}
catch(Exception ex)
{
System.out.println("There was a problem with the SELL");
}
JOptionPane.showMessageDialog(f, "Congrats on your item");
}
}
private String findProduct(String pID) throws IOException
{
int counter=0,i=0,flag=0;
String s;
String cline[]=new String[255];
String cID[]=new String[5];
String value=" ";
read_from_file=new FileReader("Item.txt");
read_Buffer=new BufferedReader(read_from_file);
try
{
while((s=read_Buffer.readLine()) != null)
{
counter=counter+1;
cline[counter]=s;
cID=cline[counter].split(",");
if(cID[0].equals(pID))
{
for(;i<cID.length;i++)
{
value=value+","+cID[i];
}
flag=1;
break;
}
}
switch(flag)
{
case 0:
value="Product was not in system";
break;
case 1:
value=value.substring(2);
break;
}
}
catch(Exception ex)
{
System.out.println("There was a problem with the system");
}
return value;
}
//function for updating the value of current current_stock whenever a specific item is sold out
private int updatecurrent_stock(int iD,int qUY) throws IOException
{
int counter=0,i=0,pos=0;
String s;
String cline[]=new String[255];
String cID[]=new String[5];
String row[]=new String[5];
String item_descripton,price;
int current_stock=0,status=0;
try
{
read_from_file=new FileReader("Item.txt");
read_Buffer=new BufferedReader(read_from_file);
while((s=read_Buffer.readLine()) != null)
{
counter=counter+1;
cline[counter]=s;
cID=s.split(",");
if(cID[0].equals(iD))
{
pos=counter;
row=s.split(",");
}
}
file_out_put=new FileOutputStream("Item.txt",false);
print_stream=new PrintStream(file_out_put);
for(i=0;i<cline.length;i++)
{
if(i==pos)
{
item_descripton=row[1];
price=row[2];
current_stock=Integer.parseInt(row[3])-qUY;
print_stream.println(iD+","+item_descripton+","+price+","+current_stock);
}
else
{
if(cline[i]!=null)
{
print_stream.println(cline[i]);
}
}
}
file_out_put.close();
status=0;
}
catch(Exception ex)
{
status=1;
}
return status;
}
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Store");
//Create and set up the content pane.
ButtonDemo_Extended demo = new ButtonDemo_Extended();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
public static void main(String args[]) throws IOException {
createAndShowGUI();
}
this is my code so far, i have many classes and this class is the class that deals with the add and sell function if any one could plese help it would be greatly appreatied thanks