Question
Need help with JAVA File Handling . I've already done action listeners and hard coded conversion values to the program but it was too long
Need help with JAVA File Handling . I've already done action listeners and hard coded conversion values to the program but it was too long to post so here's a code model below without the validation.
import java.awt.event.*;
/** @SuppressWarnings("serial") public class MainPanel extends JPanel {
private final static String[] list = { "inches/cm" }; private JTextField textField; private JLabel label; private JComboBox
MainPanel() {
ActionListener listener = new ConvertListener();
combo = new JComboBox
JLabel inputLabel = new JLabel("Enter value:");
JButton convertButton = new JButton("Convert"); convertButton.addActionListener(listener); // convert values when pressed
label = new JLabel("---"); textField = new JTextField(5); add(combo); add(inputLabel); add(textField); add(convertButton); add(label);
setPreferredSize(new Dimension(800, 80)); setBackground(Color.LIGHT_GRAY); }
private class ConvertListener implements ActionListener {
@Override public void actionPerformed(ActionEvent event) {
String text = textField.getText().trim();
if (text.isEmpty() == false) { double value = Double.parseDouble(text);
// the factor applied during the conversion double factor = 0;
// the offset applied during the conversion. double offset = 0;
// Setup the correct factor/offset values depending on required conversion switch (combo.getSelectedIndex()) {
case 0: // inches/cm factor = 2.54; break; }
double result = factor * value + offset;
label.setText(Double.toString(result)); } } }
}
----------
import javax.swing.JFrame;
/** * The main driver program for the GUI based conversion program. * * @author mdixon */ public class Converter { public static void main(String[] args) { JFrame frame = new JFrame("Converter"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MainPanel panel = new MainPanel(); frame.setJMenuBar(panel.setupMenu()); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }
For this requirement the program should be improved so that it no longer relies on hard coded conversion values for the various currencies. In reality the conversion rates constantly change. In order to better support this the available currencies, conversion factors, and currency symbol should be stored in a text file. When the application first starts the contents of this text file (called currency.txt) should be accessed and imported into the program, thus changing the list of supported currencies and the current conversion rates. You will be provided with sample files to use during the development. Typical file content is shown below. Your program should assume the file is always in this format. Euro (EUR), 1.06, South African Rand (ZAR), 17.96, R New Zealand Dollar (NZD), 1.8, NZ$ Note: the file will be UTF-8 encoded (to handle the special non-ASCII characters), so to read the file use something like - BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8")); In some cases, the file contents may be corrupt, so your program must be able to identify invalid entries and discard them. Any invalid entries found should be reported via a dialogue box when the application accesses the file contents. For example, a file with invalid entries may look like the following. Canadian Dollars (CAD), 4a, C$ United Arab Emirates Dirham (AED) 4.2, 1.a , 45.2: % Thai Baht (THB), 44.1, B In the above example the first three entries are invalid, but the final one is correct. Hence only the final currency information should be made available by the program. The other three should be reported as invalid when the program first startsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started