i've just 3 days to give the full program to the doctor but i've a problem and i'm tired to find this problem could anyone help me to fix it please
this program is currency converter
the code is here:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.Icon; // interface used to manipulate images
import javax.swing.ImageIcon; // loads images
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
public class project extends JFrame implements ActionListener,ItemListener{
private JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn17;
private JLabel L1,L2,L3,l2,L4;
private JTextField T,T1,T4;
private JScrollPane jsp;
private JTextArea ta;
private static JPanel p1,p2,p3,p4,p5,p8,p7,p9;
private JComboBox b1,b2;
private int a, b;
private final static String newline = "\n";
int i=0;
String currencies;
String price;
private String s1=new String();
String[] currency={"chooes currency","MYR ","EUR","USD","GBP","JPY","CNY"};
//-------------------------------------------------------------------------------------------------------
void dbTest(String data1) {
DataInputStream dis = null;
String dbRecord = null;
try {
File f = new File("input.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
String reem;
while ( (dbRecord = dis.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(dbRecord," ");
String data= st.nextToken();
String convert = st.nextToken();
if (data.equals(data1)){
ta.append(data +" "+convert+" "+newline);
i=i+1;
}
}
}
catch (IOException e) {
// catch io errors from FileInputStream or readLine()
System.out.println("Uh oh, got an IOException error: " + e.getMessage());
} finally {
// if the file opened okay, make sure we close it
if (dis != null) {
try {
dis.close();
} catch (IOException ioe) {
}
} // end if
} // end finally
} // end dbTest
//==========================================================================
void dbTest1(){
try {
BufferedWriter out = new BufferedWriter(new FileWriter("currencies.txt",true));
String currencies=JOptionPane.showInputDialog(null, "Enter a new currency: ");
String price=JOptionPane.showInputDialog(null, "Enter price: ");
out.write(currencies+" "+price);
out.flush();
out.close();
} catch (IOException e2) {
System.err.println("Error: " + e2.getMessage());
JOptionPane.showMessageDialog(null, "added completed successfully " );
}
//=====================
/*try{
PrintWriter out=new PrintWriter(new FileWriter("currencies.txt",true));
String currencies=JOptionPane.showInputDialog(null, "Enter a new currency: ");
String price=JOptionPane.showInputDialog(null, "Enter price: ");
out.write(currencies+" "+price+"\n");
out.flush();
out.close();
}catch(IOException e){System.out.println("File Not Found ");}
JOptionPane.showMessageDialog(null, "added completed successfully " );
// dbTest11();*/
}
//---------------------------------------------------------------------------------------------------------------------------------
public void removelinefromfile(String file, String lineToRemove) {//delete currency
try { BufferedReader in = new BufferedReader(new FileReader(file));
PrintWriter out = new PrintWriter(new File("output.txt"));
String line; //a line in the file
String params[]; //holds the name:number:color parameters of each line
while ((line = in.readLine()) != null) {
params = line.split(" ", 2); //split the line into the 3 parameters seperated by :
if (!lineToRemove.equals(params[0]) ) { //find the line we want to replace
out.println(line);
}
}
in.close();
out.flush();
out.close();
File f11 = new File("currencies.txt");
f11.delete();
File f21 = new File("output.txt");
f21.renameTo(f11);
}
catch (Exception e) {
e.printStackTrace();}
JOptionPane.showMessageDialog(null, "delete completed successfully " );
//dbTest11();
}
void update1(String y1,String y2 ){// update a currency
try { BufferedReader in = new BufferedReader(new FileReader("currencies.txt"));
PrintWriter out = new PrintWriter(new File("out.txt"));
String line; //a line in the file
String params[]; //holds the name:number:color parameters of each line
while ((line = in.readLine()) != null) {
params = line.split(" ", 2); //split the line into the 3 parameters seperated by :
if (y1.equals(params[0]) ) { //find the line we want to replace
out.println(params[0] + " " +y2); //output the new line
} else {
out.println(line); //if it's not the line, just output it as-is
} }
in.close();
out.flush();
out.close();
File f1 = new File("currencies.txt");
f1.delete();
File f2 = new File("out.txt");
f2.renameTo(f1);
}
catch (Exception e) {
e.printStackTrace();}
// dbTest11();
}
//--------------------------------------------------------------------------------------------------------------------------------
void dbTest11(){ //show all currencies
BufferedReader fi;
String s;
try {
fi = new BufferedReader(new FileReader ("currencies.txt"));
while ((s=fi.readLine()) != null)
ta.append(s+" "+newline);
fi.close();
}catch (Exception e) { System.err.println("File Not Found ");}
}
//=================================
public project(){
Icon bug1 = new ImageIcon( getClass().getResource( "1.jpg" ) );
l2= new JLabel( " ", bug1,
SwingConstants.CENTER);
l2.setBackground(Color.cyan);
//----------------------
p8=new JPanel();
p8.add(l2,"Center"); p8.setBackground(Color.cyan);
p1=new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
L1=new JLabel("Convert value : ");
T=new JTextField (" ",10);
p1.add(L1);
p1.add(T);
T4=new JTextField (" ",14);
p1.add(T4);
p1.setBackground(Color.cyan);
//-------------------------------------------panel 1
p2=new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
p7=new JPanel();
p7.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
L2=new JLabel("From : "); p7.add(L2);
b1 = new JComboBox(currency ); p7.add(b1);
b1.addItemListener(this);
L3=new JLabel("To : ");p7.add(L3);
b2 = new JComboBox(currency);
p7.add(b2);
b2.addItemListener(this);
L4=new JLabel("data : ");p7.add(L4);
T1=new JTextField ( " ",6);
p7.add(T1);
p9=new JPanel();
btn1=new JButton("Calculate now"); p9.add(btn1,"Center"); btn1.addActionListener(this);btn1.setForeground(Color.red);
btn3=new JButton("clear"); p9.add(btn3); btn3.addActionListener(this);btn3.setForeground(Color.red);
btn7=new JButton("cencel"); p9.add(btn7,"Center"); btn7.addActionListener(this);btn7.setForeground(Color.red);
p2.setBackground(Color.cyan);
p7.setBackground(Color.cyan); p9.setBackground(Color.cyan);
p2.add(p7,"North");
p2.add(p1,"Center");
p2.add(p9,"South");
//-------------------------------------------panel 2
p3=new JPanel();
p3. setLayout(new GridLayout (5,1));
btn4=new JButton("show all currencies"); p3.add(btn4); btn4.addActionListener(this);
btn4.setForeground(Color.blue);
btn2=new JButton("change/set"); p3.add(btn2); btn2.addActionListener(this);
btn2.setForeground(Color.blue);
btn5=new JButton("add a new currecy"); p3.add(btn5); btn5.addActionListener(this);
btn5.setForeground(Color.blue);
btn6=new JButton("delete currency"); p3.add(btn6); btn6.addActionListener(this);
btn17=new JButton("history"); p3.add(btn17,"Center"); btn17.addActionListener(this);btn17.setForeground(Color.blue);
btn6.setForeground(Color.blue);
p3.setBackground(Color.cyan);
//-----------------------------------------------panel 3
p4=new JPanel();
p4.setLayout(new BorderLayout());
ta=new JTextArea(10,20);
jsp=new JScrollPane (ta);
p4.add(jsp);
p4.setBackground(Color.cyan);
///----------------------------------------panel 4
p5=new JPanel();
p5.setLayout(new GridLayout(1,2));
p5.add(p4,"West");
p5.add(p3,"East");
p5.setBackground(Color.cyan);
//----------------------------------------
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource()==b1) {a=b1.getSelectedIndex();}
else if (e.getSource()==b2){b=b2.getSelectedIndex();}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==btn1) {
double x=Double.parseDouble(T.getText());
if (a==1){
switch(b){
case 1: s1=x+"MYR="+(x*1)+"MYR"; break;
case 2: s1=x+"MYR="+(x*0.2515)+"EUR"; break;
case 3: s1=x+"MYR="+(x*.03049)+"USD";break;
case 4: s1=x+"MYR="+(x*0.2069)+"GBP";break;
case 5: s1=x+"MYR="+(x*27.9204)+"JPY";break;
case 6: s1=x+"MYR="+(x*2.0843)+"CNY";break;
}
T4.setText(s1);
}else if(a==2){
switch(b){
case 1: s1=x+"EUR="+(x*3.9766)+"MYR"; break;
case 2: s1=x+"EUR="+(x*1)+"EUR"; break;
case 3: s1=x+"EUR="+(x*1.2126)+"USD";break;
case 4: s1=x+"EUR="+(x*0.8227)+"GBP";break;
case 5: s1=x+"EUR="+(x*111.0283)+"JPY";break;
case 6: s1=x+"EUR="+(x*8.2885)+"CNY";break;
} T4.setText(s1);
} else if (a==3) {
switch(b){
case 1: s1=x+"USD="+(x*3.2795)+"MYR"; break;
case 2: s1=x+"USD="+(x*0.8247)+"EUR"; break;
case 3: s1=x+"USD="+(x*1)+"USD";break;
case 4: s1=x+"USD="+(x*0.6785)+"GBP";break;
case 5: s1=x+"USD="+(x*91.565)+"JPY";break;
case 6: s1=x+"USD="+(x*6.8355)+"CNY";break;
} T4.setText(s1);
} else if (a==4) {
switch(b){
case 1: s1=x+"GBP="+(x*4.8335)+"MYR"; break;
case 2: s1=x+"GBP="+(x*1.2155)+"EUR"; break;
case 3: s1=x+"GBP="+(x*1.4738)+"USD";break;
case 4: s1=x+"GBP="+(x*1)+"GBP";break;
case 5: s1=x+"GBP="+(x*134.952)+"JPY";break;
case 6: s1=x+"GBP="+(x*10.0744)+"CNY";break;
} T4.setText(s1);
} else if (a==5) {
switch(b){
case 1: s1=x+"JPY="+(x*0.0358)+"MYR"; break;
case 2: s1=x+"JPY="+(x*0.009)+"EUR"; break;
case 3: s1=x+"JPY="+(x*0.0109)+"USD";break;
case 4: s1=x+"JPY="+(x*0.0074)+"GBP";break;
case 5: s1=x+"JPY="+(x*1)+"JPY";break;
case 6: s1=x+"JPY="+(x*0.0745)+"CNY";break;
}
T4.setText(s1);
} else if (a==6) {
switch(b){
case 1: s1=x+"CNY="+(x*0.4792)+"MYR"; break;
case 2: s1=x+"CNY = "+(x*0.1205)+"EUR"; break;
case 3: s1=x+"CNY = "+(x*0.1461)+"USD";break;
case 4: s1=x+"CNY = "+(x*0.0991)+"GBP";break;
case 5: s1=x+"CNY="+(x*13.3687)+"JPY";break;
case 6: s1=x+"CNY="+(x*1)+"CNY";break;
}
T4.setText(s1);
}
else JOptionPane.showMessageDialog(null, "Cannot convert!", "Incompatible units!", JOptionPane.INFORMATION_MESSAGE);
try {
BufferedWriter out = new BufferedWriter(new FileWriter("input.txt",true));
out.write(T1.getText()+" "+s1+"\n");
out.flush();
out.close();
} catch (IOException e2) {
System.err.println("Error: " + e2.getMessage());
}
}
else if (e.getSource()==btn3) {
T.setText("");
ta.setText("");
T1.setText("");T4.setText("");
b1.setEditable(true);
b1.setSelectedItem("choose currency");
b2.setEditable(true);
b2.setSelectedItem("choose currency");
}
else if (e.getSource()==btn7) {
System.exit(0);
}
else if(e.getSource()==btn17){
String y=JOptionPane.showInputDialog(null, "Enter the data of convert: / / ");
dbTest(y);
}
else if (e.getSource()==btn5){
dbTest1();
}
else if (e.getSource()==btn4){
dbTest11();
}
else if (e.getSource()==btn6){
String y=JOptionPane.showInputDialog(null, "Enter the currency that you want to delete ");
removelinefromfile("currencies.txt" ,y);
}
else if ( e.getSource()==btn2 ){
String y1=JOptionPane.showInputDialog(null, "Enter the currency that you want to update ");
String y2=JOptionPane.showInputDialog(null, "Enter the new price of currency that you want ");
update1(y1,y2);
}
}
//======================================================(MAIN PROGRAM)
public static void main(String [] args) {
project frame = new project();
frame.add(p8,"North");
frame.add(p2,"Center");
frame.add(p5,"South");
frame.setTitle("Currencies Converter");
frame.setSize(530,400);
frame.setLocation(400,190);
frame.setVisible(true);
frame.setBackground(Color.cyan);
}
}