Advertisement
Guest User

ConverterV1

a guest
May 4th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 11.20 KB | None | 0 0
  1. package il.ac.hit
  2.  
  3. import javax.swing.SwingUtilities
  4.  
  5. /**
  6.  *  Designed by:
  7.  *              Yossi Yadgar
  8.  *              Lior Yamin
  9.  */
  10.  */
  11. object Application {
  12.   def main ( args : Array[String] ) : Unit = {
  13.     SwingUtilities.invokeLater(new Runnable {
  14.       override def run(): Unit = {
  15.         val ui = new View()
  16.         ui.start()
  17.       }
  18.     })
  19.   }
  20. }
  21.  
  22.  
  23. //----------------------View.java class -----------------------
  24.  
  25. package il.ac.hit;
  26.  
  27. import java.awt.BorderLayout;
  28. import java.awt.FlowLayout;
  29. import java.awt.Font;
  30. import java.awt.GridLayout;
  31. import java.awt.event.ActionEvent;
  32. import java.awt.event.ActionListener;
  33. import java.awt.event.FocusEvent;
  34. import java.awt.event.FocusListener;
  35. import java.text.DateFormat;
  36. import java.text.SimpleDateFormat;
  37. import java.util.Date;
  38. import java.util.Timer;
  39. import java.util.TimerTask;
  40. import javax.swing.JButton;
  41. import javax.swing.JComboBox;
  42. import javax.swing.JFrame;
  43. import javax.swing.JLabel;
  44. import javax.swing.JOptionPane;
  45. import javax.swing.JPanel;
  46. import javax.swing.JTable;
  47. import javax.swing.JTextField;
  48.  
  49. import org.w3c.dom.NodeList;
  50.  
  51. public class View {
  52.  
  53.     private JPanel mainP;       // that panel will have all the currencies operations
  54.     private JPanel dateP;       // that panel will have the details about the current date
  55.     private JFrame frame;       // the frame
  56.  
  57.     private JTable currencyT;   // the currency table
  58.     // all the titles\labels :
  59.     private JLabel l_title;
  60.     private JLabel l_date;
  61.     private JLabel l_convert;
  62.     private JLabel l_from;
  63.     private JLabel l_to;
  64.  
  65.     private JButton b_calc;                 // the calculate button
  66.  
  67.     private JTextField tf_amount;           // in this textField the user enter the amount to calculate
  68.     private JTextField tf_result;           // in this textField the user will get the answer
  69.  
  70.     private JComboBox<?> cb_from;           // this comboBox will include all the currencies that the xml have
  71.     private JComboBox<?> cb_to;             // this comboBox will include all the currencies that the xml have
  72.     private DateFormat currentDateUpdated;  // the current date
  73.     private Date date;                      // the current date
  74.     private ActionListener al;              // will listen to any operation that the user do
  75.     private String[] curreciesNames;        // will include all the currenciesNames that the xml have
  76.     private double[] curreciesRate;         // will include all the currenciesRates that the xml have
  77.     private Timer timer;
  78.  
  79.  
  80.     public View()
  81.     {
  82.         al = new ButtonListener();      // initialize the listener to be ButtonListener
  83.         timer = new Timer();
  84.     }
  85.  
  86.  
  87.     // in this function the 2 arrays of currencies data will be initialize
  88.     public void initializeCurrenciesArrays(){
  89.         int numOfCurrencies = XMLreader.connectToUrl();
  90.         curreciesNames = new String[numOfCurrencies];
  91.         curreciesRate = new double[numOfCurrencies];
  92.  
  93.             currentDateUpdated = new SimpleDateFormat("yyyy-MM-dd");// define format for date
  94.             date =  XMLreader.updateData(curreciesNames, curreciesRate);
  95.  
  96.     }
  97.  
  98.  
  99.     // find specific element in the CurrencyNames array
  100.     public int findCurrencyNameInArray(String toFind){
  101.  
  102.         int ans = -1;   // -1 means doesn't found
  103.         for(int i = 0 ; i < curreciesNames.length ; i++){   // check any cell to find
  104.             if ( curreciesNames[i].equals(toFind) )         // the "toFind" in currency
  105.                 ans = i;                                    // names array
  106.         }
  107.         return ans;
  108.     }
  109.  
  110.     // this class role is to handle buttons press
  111.     public class ButtonListener implements ActionListener{
  112.         @Override
  113.         public void actionPerformed(ActionEvent arg0)
  114.         {
  115.             Object pressed = arg0.getSource();                  // to know what button was clicked
  116.             Object cb_toSelected = cb_to.getSelectedItem();     // to know which element was
  117.             Object cb_fromSelected = cb_from.getSelectedItem(); // chosen from the comboBoxes
  118.  
  119.             if ( pressed == b_calc ){   // if calculate button was pressed
  120.                 String temp = tf_amount.getText();  // get the amount
  121.                 if (temp.equals("") || temp.equals("insert amount"))    // if the amount textFiled is empty
  122.                 {
  123.                     JOptionPane.showMessageDialog(null,"Please Enter Amount To Convert From");
  124.                 }
  125.                 else {  // if the amount textFiled isn't empty
  126.  
  127.                     int toIndex = findCurrencyNameInArray(cb_toSelected.toString());    // find that specific currency index
  128.                     int fromIndex = findCurrencyNameInArray(cb_fromSelected.toString());// from the currencyName array
  129.                     tf_result.setText( Double.toString  // Calculate currency
  130.                             (Double.parseDouble(tf_amount.getText()) * curreciesRate[fromIndex] / curreciesRate[toIndex])  );
  131.                 }   // end of else
  132.             }   // end if
  133.  
  134.         }   // end of actionPerformed
  135.     }   // end of ButtonListener
  136.  
  137.  
  138.     public void fillTabel(){
  139.  
  140.         // fill names
  141.         for ( int i = 0 ; i < curreciesRate.length ; i++){
  142.             currencyT.setValueAt(curreciesNames[i], i+1, 0);
  143.             currencyT.setValueAt(curreciesNames[i], 0, i+1);
  144.         }
  145.  
  146.         // fill rates
  147.         for ( int i = 0 ; i < curreciesRate.length ; i++){
  148.             for ( int j = 0 ; j < curreciesRate.length ; j++){
  149.                 currencyT.setValueAt(curreciesRate[i] / curreciesRate[j], i+1, j+1);
  150.             }
  151.         }
  152.     }
  153.  
  154.  
  155.     public void start()
  156.     {
  157.         initializeCurrenciesArrays();
  158.  
  159.         mainP = new JPanel();
  160.         dateP = new JPanel();
  161.         l_title = new JLabel("Currency Table");
  162.         l_title.setFont(new Font("Serif", Font.BOLD, 15));
  163.         l_date = new JLabel(currentDateUpdated.format(date));
  164.         l_date.setFont(new Font("Serif", Font.BOLD, 15));
  165.         l_convert = new JLabel("Convert");
  166.         l_from = new JLabel("From");
  167.         l_to = new JLabel("To");
  168.  
  169.         b_calc = new JButton("Calculate");
  170.         b_calc.addActionListener(al);
  171.         cb_from = new JComboBox<String>(curreciesNames);
  172.         cb_to = new JComboBox<String>(curreciesNames);
  173.         cb_from.addActionListener(al);
  174.         cb_to.addActionListener(al);
  175.         tf_amount = new JTextField(10);
  176.  
  177.         tf_result = new JTextField(10);
  178.         frame = new JFrame("Currency converter");
  179.  
  180.         tf_amount.addFocusListener(new FocusListener(){     // this lines purpose
  181.             // is to set the words
  182.             @Override                                       // insert currency in
  183.             public void focusGained(FocusEvent arg0) {      // the amount text field
  184.                 tf_amount.setText("");                      // when there is no text
  185.             }                                               // there at all.
  186.  
  187.             @Override
  188.             public void focusLost(FocusEvent arg0) {
  189.                 if (tf_amount.getText().equals(""))
  190.                     tf_amount.setText("insert amount");
  191.             }
  192.  
  193.         });
  194.  
  195.  
  196.         currencyT = new JTable(curreciesNames.length+1, curreciesNames.length+1);
  197.         currencyT.setEnabled(false);
  198.  
  199.         fillTabel();
  200.  
  201.         frame.setLayout(new BorderLayout());
  202.         dateP.setLayout(new FlowLayout());
  203.  
  204.         frame.add(dateP, BorderLayout.NORTH);
  205.         dateP.add(l_title);
  206.         dateP.add(l_date);
  207.  
  208.         frame.add(currencyT, BorderLayout.SOUTH);
  209.  
  210.         mainP.setLayout(new GridLayout(6, 1));
  211.         tf_amount.setFont(new Font("Serif", Font.BOLD, 15));
  212.         mainP.add(l_convert);
  213.         mainP.add(tf_amount);
  214.         mainP.add(l_from);
  215.         mainP.add(cb_from);
  216.         mainP.add(l_to);
  217.         mainP.add(cb_to);
  218.         mainP.add(b_calc);
  219.  
  220.         mainP.add(tf_result);
  221.  
  222.         frame.pack();
  223.  
  224.         frame.add(mainP, BorderLayout.CENTER);
  225.         frame.setResizable(false);
  226.         frame.setVisible(true);     // set the frame to be visible
  227.         frame.setSize(600, 400);    // set the frame default size
  228.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  229.  
  230.         timer.schedule(new TimerTask(){
  231.  
  232.             @Override
  233.             public void run() {
  234.                 date =  XMLreader.updateData(curreciesNames, curreciesRate);
  235.             }
  236.  
  237.         }, 0, 10000000);
  238.  
  239.  
  240.     }
  241.  
  242. }
  243.  
  244.  
  245. //--------------------XMLReader.scala  Object---------------------------
  246. package il.ac.hit
  247.  
  248. import java.io._
  249. import java.net.URL
  250. import java.text.SimpleDateFormat
  251. import java.util.StringTokenizer
  252. import scala.io.Source
  253. import scala.xml.XML
  254. import java.util.Date
  255.  
  256.  
  257. object XMLreader extends XMLReaderInterface{
  258.  
  259. /////////////////////////////////////////////////////////////////////////////////////////////////
  260.   /*
  261.   def main ( args : Array[String] ) : Unit ={
  262.     val numOfNodes = XMLreader.connectToUrl()
  263.     var currenciesNamesArray = new Array[String](numOfNodes)
  264.     var currenciesRatesArray = new Array[Double](numOfNodes)
  265.     updateData(currenciesNamesArray, currenciesRatesArray)
  266.   }
  267. */
  268.   /////////////////////////////////////////////////////////////////////////////////////////////////
  269.  
  270.   def connectToUrl (): Int = {
  271.  
  272.     val url = new URL("http://www.abelski.com/currencies.xml")
  273.     //val url = new URL("http://www.boi.org.il/currency.xml")
  274.     val connection = url.openConnection()
  275.     val document = XML.load(connection.getInputStream)
  276.     var countCurrencies : Int = 0
  277.     val writer = new PrintWriter(new File("CurrenicesData.txt"))
  278.  
  279.       try{
  280.  
  281.         // write the date in the last line in the text
  282.         //println(((document\\"CURRENCIES")\\"LAST_UPDATE").text)
  283.         writer.write( ((document\\"CURRENCIES")\\"LAST_UPDATE").text )
  284.         writer.write(System.lineSeparator())
  285.  
  286.         for ( currentData <- (document\\"CURRENCY") ){
  287.           countCurrencies = countCurrencies + 1
  288.           writer.write( (currentData\\"CURRENCYCODE").text + " " +  (currentData\\"RATE").text )
  289.           writer.write(System.lineSeparator())
  290.         }
  291.  
  292.       }
  293.       finally writer.close()
  294.       countCurrencies
  295.   }
  296.  
  297.  
  298.   def updateData(currenciesNamesArray: Array[String], currenciesRatesArray: Array[Double]): java.util.Date ={
  299.     connectToUrl()
  300.  
  301.     var ob = Source.fromFile("CurrenicesData.txt");
  302.     var i_local = 0
  303.  
  304.  
  305.       for( line <- ob.getLines.drop(1) )   // to not contain the first line of the file
  306.       {
  307.           var st = new StringTokenizer(line," ", false)
  308.           var currencyName = st.nextToken()
  309.           var currencyRate = st.nextToken().toDouble
  310.  
  311.             currenciesNamesArray(i_local) = currencyName
  312.             currenciesRatesArray(i_local) = currencyRate
  313.             i_local = i_local + 1
  314.       }
  315.  
  316.  
  317.       ob = Source.fromFile("CurrenicesData.txt");
  318.       var iterator = ob.getLines()
  319.       var stringDate = iterator.next()
  320.       var  simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  321.       var date = simpleDateFormat.parse(stringDate);
  322.  
  323.       date
  324.   }
  325. }
  326.  
  327.  
  328. //--------------------XMLReaderInterface----------------
  329.  
  330. package il.ac.hit;
  331.  
  332. import java.util.Date;
  333. /**
  334.  * Created by Yossi on 04/05/2015.
  335.  */
  336. public interface XMLReaderInterface {
  337.     // the method connect to the Israeli Bank Currency xml file
  338.     // and return the number of currencies names that he have
  339.     public int connectToUrl() ;
  340.  
  341.  
  342.     public Date updateData(String[] currenciesNamesArray, double[] currenciesRatesArray);
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement