Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package il.ac.hit
- import javax.swing.SwingUtilities
- /**
- * Designed by:
- * Yossi Yadgar
- * Lior Yamin
- */
- */
- object Application {
- def main ( args : Array[String] ) : Unit = {
- SwingUtilities.invokeLater(new Runnable {
- override def run(): Unit = {
- val ui = new View()
- ui.start()
- }
- })
- }
- }
- //----------------------View.java class -----------------------
- package il.ac.hit;
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- import org.w3c.dom.NodeList;
- public class View {
- private JPanel mainP; // that panel will have all the currencies operations
- private JPanel dateP; // that panel will have the details about the current date
- private JFrame frame; // the frame
- private JTable currencyT; // the currency table
- // all the titles\labels :
- private JLabel l_title;
- private JLabel l_date;
- private JLabel l_convert;
- private JLabel l_from;
- private JLabel l_to;
- private JButton b_calc; // the calculate button
- private JTextField tf_amount; // in this textField the user enter the amount to calculate
- private JTextField tf_result; // in this textField the user will get the answer
- private JComboBox<?> cb_from; // this comboBox will include all the currencies that the xml have
- private JComboBox<?> cb_to; // this comboBox will include all the currencies that the xml have
- private DateFormat currentDateUpdated; // the current date
- private Date date; // the current date
- private ActionListener al; // will listen to any operation that the user do
- private String[] curreciesNames; // will include all the currenciesNames that the xml have
- private double[] curreciesRate; // will include all the currenciesRates that the xml have
- private Timer timer;
- public View()
- {
- al = new ButtonListener(); // initialize the listener to be ButtonListener
- timer = new Timer();
- }
- // in this function the 2 arrays of currencies data will be initialize
- public void initializeCurrenciesArrays(){
- int numOfCurrencies = XMLreader.connectToUrl();
- curreciesNames = new String[numOfCurrencies];
- curreciesRate = new double[numOfCurrencies];
- currentDateUpdated = new SimpleDateFormat("yyyy-MM-dd");// define format for date
- date = XMLreader.updateData(curreciesNames, curreciesRate);
- }
- // find specific element in the CurrencyNames array
- public int findCurrencyNameInArray(String toFind){
- int ans = -1; // -1 means doesn't found
- for(int i = 0 ; i < curreciesNames.length ; i++){ // check any cell to find
- if ( curreciesNames[i].equals(toFind) ) // the "toFind" in currency
- ans = i; // names array
- }
- return ans;
- }
- // this class role is to handle buttons press
- public class ButtonListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent arg0)
- {
- Object pressed = arg0.getSource(); // to know what button was clicked
- Object cb_toSelected = cb_to.getSelectedItem(); // to know which element was
- Object cb_fromSelected = cb_from.getSelectedItem(); // chosen from the comboBoxes
- if ( pressed == b_calc ){ // if calculate button was pressed
- String temp = tf_amount.getText(); // get the amount
- if (temp.equals("") || temp.equals("insert amount")) // if the amount textFiled is empty
- {
- JOptionPane.showMessageDialog(null,"Please Enter Amount To Convert From");
- }
- else { // if the amount textFiled isn't empty
- int toIndex = findCurrencyNameInArray(cb_toSelected.toString()); // find that specific currency index
- int fromIndex = findCurrencyNameInArray(cb_fromSelected.toString());// from the currencyName array
- tf_result.setText( Double.toString // Calculate currency
- (Double.parseDouble(tf_amount.getText()) * curreciesRate[fromIndex] / curreciesRate[toIndex]) );
- } // end of else
- } // end if
- } // end of actionPerformed
- } // end of ButtonListener
- public void fillTabel(){
- // fill names
- for ( int i = 0 ; i < curreciesRate.length ; i++){
- currencyT.setValueAt(curreciesNames[i], i+1, 0);
- currencyT.setValueAt(curreciesNames[i], 0, i+1);
- }
- // fill rates
- for ( int i = 0 ; i < curreciesRate.length ; i++){
- for ( int j = 0 ; j < curreciesRate.length ; j++){
- currencyT.setValueAt(curreciesRate[i] / curreciesRate[j], i+1, j+1);
- }
- }
- }
- public void start()
- {
- initializeCurrenciesArrays();
- mainP = new JPanel();
- dateP = new JPanel();
- l_title = new JLabel("Currency Table");
- l_title.setFont(new Font("Serif", Font.BOLD, 15));
- l_date = new JLabel(currentDateUpdated.format(date));
- l_date.setFont(new Font("Serif", Font.BOLD, 15));
- l_convert = new JLabel("Convert");
- l_from = new JLabel("From");
- l_to = new JLabel("To");
- b_calc = new JButton("Calculate");
- b_calc.addActionListener(al);
- cb_from = new JComboBox<String>(curreciesNames);
- cb_to = new JComboBox<String>(curreciesNames);
- cb_from.addActionListener(al);
- cb_to.addActionListener(al);
- tf_amount = new JTextField(10);
- tf_result = new JTextField(10);
- frame = new JFrame("Currency converter");
- tf_amount.addFocusListener(new FocusListener(){ // this lines purpose
- // is to set the words
- @Override // insert currency in
- public void focusGained(FocusEvent arg0) { // the amount text field
- tf_amount.setText(""); // when there is no text
- } // there at all.
- @Override
- public void focusLost(FocusEvent arg0) {
- if (tf_amount.getText().equals(""))
- tf_amount.setText("insert amount");
- }
- });
- currencyT = new JTable(curreciesNames.length+1, curreciesNames.length+1);
- currencyT.setEnabled(false);
- fillTabel();
- frame.setLayout(new BorderLayout());
- dateP.setLayout(new FlowLayout());
- frame.add(dateP, BorderLayout.NORTH);
- dateP.add(l_title);
- dateP.add(l_date);
- frame.add(currencyT, BorderLayout.SOUTH);
- mainP.setLayout(new GridLayout(6, 1));
- tf_amount.setFont(new Font("Serif", Font.BOLD, 15));
- mainP.add(l_convert);
- mainP.add(tf_amount);
- mainP.add(l_from);
- mainP.add(cb_from);
- mainP.add(l_to);
- mainP.add(cb_to);
- mainP.add(b_calc);
- mainP.add(tf_result);
- frame.pack();
- frame.add(mainP, BorderLayout.CENTER);
- frame.setResizable(false);
- frame.setVisible(true); // set the frame to be visible
- frame.setSize(600, 400); // set the frame default size
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- timer.schedule(new TimerTask(){
- @Override
- public void run() {
- date = XMLreader.updateData(curreciesNames, curreciesRate);
- }
- }, 0, 10000000);
- }
- }
- //--------------------XMLReader.scala Object---------------------------
- package il.ac.hit
- import java.io._
- import java.net.URL
- import java.text.SimpleDateFormat
- import java.util.StringTokenizer
- import scala.io.Source
- import scala.xml.XML
- import java.util.Date
- object XMLreader extends XMLReaderInterface{
- /////////////////////////////////////////////////////////////////////////////////////////////////
- /*
- def main ( args : Array[String] ) : Unit ={
- val numOfNodes = XMLreader.connectToUrl()
- var currenciesNamesArray = new Array[String](numOfNodes)
- var currenciesRatesArray = new Array[Double](numOfNodes)
- updateData(currenciesNamesArray, currenciesRatesArray)
- }
- */
- /////////////////////////////////////////////////////////////////////////////////////////////////
- def connectToUrl (): Int = {
- val url = new URL("http://www.abelski.com/currencies.xml")
- //val url = new URL("http://www.boi.org.il/currency.xml")
- val connection = url.openConnection()
- val document = XML.load(connection.getInputStream)
- var countCurrencies : Int = 0
- val writer = new PrintWriter(new File("CurrenicesData.txt"))
- try{
- // write the date in the last line in the text
- //println(((document\\"CURRENCIES")\\"LAST_UPDATE").text)
- writer.write( ((document\\"CURRENCIES")\\"LAST_UPDATE").text )
- writer.write(System.lineSeparator())
- for ( currentData <- (document\\"CURRENCY") ){
- countCurrencies = countCurrencies + 1
- writer.write( (currentData\\"CURRENCYCODE").text + " " + (currentData\\"RATE").text )
- writer.write(System.lineSeparator())
- }
- }
- finally writer.close()
- countCurrencies
- }
- def updateData(currenciesNamesArray: Array[String], currenciesRatesArray: Array[Double]): java.util.Date ={
- connectToUrl()
- var ob = Source.fromFile("CurrenicesData.txt");
- var i_local = 0
- for( line <- ob.getLines.drop(1) ) // to not contain the first line of the file
- {
- var st = new StringTokenizer(line," ", false)
- var currencyName = st.nextToken()
- var currencyRate = st.nextToken().toDouble
- currenciesNamesArray(i_local) = currencyName
- currenciesRatesArray(i_local) = currencyRate
- i_local = i_local + 1
- }
- ob = Source.fromFile("CurrenicesData.txt");
- var iterator = ob.getLines()
- var stringDate = iterator.next()
- var simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- var date = simpleDateFormat.parse(stringDate);
- date
- }
- }
- //--------------------XMLReaderInterface----------------
- package il.ac.hit;
- import java.util.Date;
- /**
- * Created by Yossi on 04/05/2015.
- */
- public interface XMLReaderInterface {
- // the method connect to the Israeli Bank Currency xml file
- // and return the number of currencies names that he have
- public int connectToUrl() ;
- public Date updateData(String[] currenciesNamesArray, double[] currenciesRatesArray);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement