Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

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 combo;

MainPanel() {

ActionListener listener = new ConvertListener();

combo = new JComboBox(list); combo.addActionListener(listener); //convert values when option changed

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-ASCll characters), so to read the file use something like - BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

More Books

Students also viewed these Databases questions

Question

Distinguish between formal and informal reports.

Answered: 1 week ago