Calculate values and update in File Each Time
import java.util.StringTokenizer;
import javax.swing.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Window implements ActionListener
{
JFrame frame;
JLabel totallbl, rejectlbl, approvelbl, resultlbl, head, Approvedlbl, Rejectedlbl, TotalTipslbl, copyrightlbl;
JTextField totalTF, rejectTF;
JButton calculateBtn, exit, hold;
Window()
{
frame = new JFrame(“Calculate Q.C.”);
frame.setSize(350,400);
frame.setVisible(true);
frame.setLayout(null);
frame.add(head = new JLabel(“Q.C. Of The Day”));
head.setFont(new Font(“TimesRoman”, Font.PLAIN, 25));
head.setBounds(80,30,300,30);
frame.add(totallbl = new JLabel(“Total”));
totallbl.setBounds(50,100,100,20);
frame.add(totalTF = new JTextField());
totalTF.setBounds(200,100,100,20);
frame.add(rejectlbl = new JLabel(“Rejected”));
rejectlbl.setBounds(50,140,100,20);
frame.add(rejectTF = new JTextField());
rejectTF.setBounds(200,140,100,20);
frame.add(calculateBtn = new JButton(“Calculate”));
calculateBtn.setBounds(50,180,100,25);
frame.add(exit = new JButton(“Exit”));
exit.setBounds(240,180,60,25);
frame.add(hold = new JButton(“Hold”));
hold.setBounds(160,180,70,25);
frame.add(approvelbl = new JLabel(“Total Approved : “));
approvelbl.setBounds(50,220,150,20);
frame.add(resultlbl = new JLabel(“”));
resultlbl.setBounds(200,220,150,20);
frame.add(Approvedlbl = new JLabel(“Approved Tips : “));
Approvedlbl.setBounds(100,260,150,20);
frame.add(Rejectedlbl = new JLabel(“Rejected Tips : “));
Rejectedlbl.setBounds(100,290,150,20);
frame.add(TotalTipslbl = new JLabel(“Total Tips : “));
TotalTipslbl.setBounds(100,320,150,20);
frame.add(copyrightlbl = new JLabel(“Copyright All Rights Reserved 2013”));
copyrightlbl.setFont(new Font(“TimesRoman”, Font.PLAIN, 10));
copyrightlbl.setBounds(80,350,350,20);
readwrite(0,0,0);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
calculateBtn.addActionListener(this);
exit.addActionListener(this);
hold.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Object actionButton = ae.getSource();
if(actionButton == calculateBtn)
{
int total=0;
int reject=0;
int approve=0;
if(totalTF.getText().trim().length() == 0 || rejectTF.getText().trim().length() == 0)
{
JOptionPane.showMessageDialog(null, “All Fields are required.”, “Q. C. Rom”, 1);
}
else
{
total = Integer.parseInt(totalTF.getText());
reject = Integer.parseInt(rejectTF.getText());
approve = total – reject;
resultlbl.setText(String.valueOf(approve));
totalTF.setText(“”);
rejectTF.setText(“”);
readwrite(total,reject,approve);
}
}
if(actionButton == exit)
{
int gtotal=0;
int rejected=0;
int approved=0;
write(gtotal,rejected,approved);
frame.dispose();
System.exit (0);
}
if(actionButton == hold)
{
frame.dispose();
System.exit (0);
}
}
public void readwrite(int gtotal,int rejected, int approved)
{
try{
File file = new File(“data.txt”);
int row = 0;
int col = 0;
int [][] data = new int [1][4];
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String Stringline = null;
String line = null;
int array[]=new int[4];
int i =0;
int a,r,g;
while((line = bufRdr.readLine()) != null && row < data.length)
{
StringTokenizer st = new StringTokenizer(line,”,”);
while (st.hasMoreTokens())
{
try
{
data[row][col] = Integer.parseInt(st.nextToken());
array[i] = data[row][col];
} catch (NumberFormatException e) { }
i++;
col++;
}
a = approved+array[2];
r = rejected+array[1];
g = gtotal+array[0];
Approvedlbl.setText(“Approved Tips : “+String.valueOf(a));
Rejectedlbl.setText(“Rejected Tips : “+String.valueOf(r));
TotalTipslbl.setText(“Total Tips : “+String.valueOf(g));
//System.out.println(gtotal);
write(g,r,a);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void write(int gtotal,int rejected,int approved)
{
try
{
FileWriter fstream = new FileWriter(“data.txt”,false);
BufferedWriter out = new BufferedWriter(fstream);
out.write(gtotal+”,”+rejected+”,”+approved);
out.close();
}
catch(Exception ae)
{}
}
}
class QCProgram
{
public static void main(String[] args) throws IOException
{
new Window();
}
}