cant get historic value?
import javax.swing.*;
import java.lang.Math;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
//method implementing the ActionListener
class CurrencyConverter implements ActionListener {
// creating objects of AWT methods
JFrame frame = new JFrame();
JFrame historyFrame = new JFrame();
JButton convert = new JButton("Convert");
JButton historic = new JButton("Historic");
JButton reset = new JButton("Reset");
JTextField moneyIn = new JTextField();
JTextField moneyOut = new JTextField();
JLabel rate = new JLabel("Rate = ");
JLabel convertedRate = new JLabel(" ");
String[] optionsToChoose = { "RiyalSA",
"DollarAM", "EuroEU" };
JComboBox<String> currencyIn = new
JComboBox<>(optionsToChoose);
JComboBox<String> currencyOut = new
JComboBox<>(optionsToChoose);
DecimalFormat df = new DecimalFormat("#.##");
Path fileName =
Path.of("/Users/abood4433/Desktop/history.txt");
// counter variable to keep track of counter
variable
int counter = 0;
CurrencyConverter() {
// setting the frame title
frame.setTitle("Money
Converter");
frame.getContentPane().setLayout(null);
// setting the dimensions of
frame
frame.setBounds(100, 200, 550,
120);
frame.add(rate);
frame.add(convertedRate);
// setting the bounds of button,
label and text fields
moneyIn.setBounds(20, 20, 100,
20);
moneyOut.setBounds(320, 20, 100,
20);
// setting bounds for
textField
currencyIn.setBounds(120, 20, 100,
20);
currencyOut.setBounds(420, 20, 100,
20);
// bounds for Label
rate.setBounds(220, 20, 100, 20);
convertedRate.setBounds(270, 20, 80,
20);
// bound for buttons
convert.setBounds(100, 60, 100,
20);
historic.setBounds(200, 60, 100,
20);
reset.setBounds(300, 60, 100, 20);
// adding the button to the UI
frame.add(convert);
frame.add(historic);
frame.add(reset);
// adding text field to the UI
frame.add(moneyIn);
frame.add(moneyOut);
// adding comboBox to UI
frame.add(currencyIn);
frame.add(currencyOut);
// adding a button listener for
convert button
convert.addActionListener(actionEvent
-> {
String
selectedCurrencyIn =
currencyIn.getItemAt(currencyIn.getSelectedIndex());
String
selectedCurrencyOut =
currencyOut.getItemAt(currencyOut.getSelectedIndex());
String toConvert =
moneyIn.getText();
float result = 0,
toConvertFloat = 0;
try {
if
(toConvert.isEmpty()) {
throw new NumberFormatException();
} else if
(selectedCurrencyIn.equals(selectedCurrencyOut)) {
JOptionPane.showMessageDialog(frame,
"Please choose two different
currencies.",
"Inane warning",
JOptionPane.WARNING_MESSAGE);
} else
{
toConvertFloat = (float)
(Double.parseDouble(toConvert));
if (toConvertFloat < 0) {
JOptionPane.showMessageDialog(frame,
"Enter a positive
value.",
"Inane
warning",
JOptionPane.WARNING_MESSAGE);
}
}
} catch
(NumberFormatException e) {
JOptionPane.showMessageDialog(frame,
"Missing value \nPlease enter a value.",
"Inane error",
JOptionPane.ERROR_MESSAGE);
}
if (selectedCurrencyIn
== "RiyalSA" && selectedCurrencyOut == "EuroEU") {
result =
(float) (toConvertFloat * 0.25);
convertedRate.setText("0.25");
} else if
(selectedCurrencyIn == "RiyalSA" && selectedCurrencyOut ==
"DollarAM") {
result =
(float) (toConvertFloat * 0.266);
convertedRate.setText("0.266");
} else if
(selectedCurrencyIn == "DollarAM" && selectedCurrencyOut ==
"EuroEU") {
result =
(float) (toConvertFloat * 0.92);
convertedRate.setText("0.92");
} else if
(selectedCurrencyIn == "EuroEU" && selectedCurrencyOut ==
"RiyalSA") {
result =
(float) (toConvertFloat * 4.05);
convertedRate.setText("4.05");
} else if
(selectedCurrencyIn == "DollarAM" && selectedCurrencyOut ==
"RiyalSA") {
result =
(float) (toConvertFloat * 3.75);
convertedRate.setText("3.75");
} else if
(selectedCurrencyIn == "EuroEU" && selectedCurrencyOut ==
"DollarAM") {
result =
(float) (toConvertFloat * 1.08);
convertedRate.setText("1.08");
}
moneyOut.setText(Double.toString(result));
try (
// create a
fileWriter object
FileWriter
fWriter = new
FileWriter("/Users/abood4433/Desktop/history.txt",
true)) {
// writing
to the file
fWriter.write(toConvert + " " + selectedCurrencyIn + "= " +
Double.toString(result) + " "
+ selectedCurrencyOut + "\n");
}
// Catch block to
handle if exception occurs
catch (IOException e)
{
// Print
the exception
System.out.print(e.getMessage());
}
});
// adding a button listener for
historic button
historic.addActionListener(actionEvent
-> {
// setting properties
for historic frame
historyFrame.setTitle("Historic");
historyFrame.getContentPane().setLayout(null);
// setting the
dimensions of frame
historyFrame.setBounds(300, 400, 550, 400);
JTextArea historicData
= new JTextArea("hi");
historicData.setBounds(0,
0, 550, 400);
historyFrame.add(historicData);
historyFrame.setVisible(true);
String filePath =
"/Users/abood4433/Desktop/history.txt" ;
try {
String data
= Files.readString(Paths.get(filePath),
StandardCharsets.UTF_8);
historicData.setText(data);
}
catch (IOException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
historyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
historyFrame.setVisible(true);
});
reset.addActionListener(actionEvent
-> {
convertedRate.setText("");
moneyIn.setText("");
moneyOut.setText("");
});
// setting the default close
operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
public class Currency {
public static void main(String[] args) throws
IOException {
new CurrencyConverter();
}
}
Navigator Historic Converter 7 Riyal SA Convert Rate= 1.0 1.862 Historic Reset ох T 18 DollarAM 203 204 205 207 208 209 210 211 212 +2 214 216 217 U }); // setting the def frame.setDefaultCl frame.setVisible (t } @Override public void actionPerf // TODO Auto-gene } } public class Currency { public static void maz new CurrencyConve }
cant get historic value? import javax.swing.*; import java.lang.Math; import java.nio.charset.StandardCharsets; import j
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am