JavaFx assignment

profilesravanh
p1.zip

MonthlyReport.java

MonthlyReport.java

import  java . io . * ;
import  java . awt . * ;
import  javax . swing . * ;
import  java . util . * ;

public   class   MonthlyReport   extends   JPanel   implements   Observer   {
     private   Report  report ;
     private   SalesData  data ;

     public   MonthlyReport ( Report  r ,   SalesData  d )   throws   Exception   {
        report  =  r ;
        data = d ;
        report . register ( this );  
     }

     public   void  refreshData ( Observable  subject )   {
        repaint ();
     }


     public   void  paintComponent ( Graphics  g )   {
         super . paintComponent ( g );
         // get subject's state
         String  department  =  report . getDepartment ();

        g . drawString ( "Current Month Transactions - "   +  department ,   50 ,   20 );
         Calendar  cal  =   Calendar . getInstance ();
        cal . setTime ( new   Date ());
         int  month  = cal . get ( Calendar . MONTH )   +   1 ;
         ArrayList < SaleItem >  trnList  =  data . getCurrentMonthTransactions ( month , department );
         int  x  =   50 ,  y  =   40 ;
         int  width  =   80 ,  height  =   20 ;
         for   ( int  i  =   0 ;  i  <  trnList . size ();  i ++ )   {
             SaleItem  st = trnList . get ( i );
            x = 50 ;
            g . drawRect ( x ,  y ,  width ,  height );
            g . drawString ( " "   +   ( +   1 ),  x ,  y  +   15 );
            x  =  x  +  width ;
            g . drawRect ( x ,  y ,  width ,  height );
            g . drawString ( " " + st . getMonth ()   +   "/"   +  st . getDay (),  x ,  y  +   15 );
            x  =  x  +  width ;
            g . drawRect ( x ,  y ,  width ,  height );
            g . drawString ( " " + st . getQuantity ()   +   " Items" ,  x ,  y  +   15 );
            x  =  x  +  width ;
            g . drawRect ( x ,  y ,  width ,  height );
            g . drawString ( " $" + st . getAmount (),  x ,  y  +   15 );
            y = y + height ;
         }
     }
} // end of class

Observable.java

Observable.java

public   interface   Observable   {
   public   void  notifyObservers ();
   public   void  register ( Observer  obs );
   public   void  unRegister ( Observer  obs );
}

Observer.java

Observer.java

public   interface   Observer   {
   public   void  refreshData ( Observable  subject );
}

Report.java

Report.java

import  javax . swing . JFrame ;
import  javax . swing . JPanel ;
import  java . awt . BorderLayout ;
import  javax . swing . JLabel ;
import  java . awt . FlowLayout ;
import  javax . swing . JComboBox ;
import  javax . swing . DefaultComboBoxModel ;
import  javax . swing . JButton ;
import  java . awt . event . ActionListener ;
import  java . awt . event . ActionEvent ;
import  java . util . ArrayList ;


public   class   Report   extends   JPanel   implements   Observable {
     public   Report ()   {
        observersList = new   ArrayList ();
        setLayout ( new   FlowLayout ());
         JLabel  lblNewLabel  =   new   JLabel ( "Please select a department" );
        add ( lblNewLabel );
        
         final   JComboBox  comboBox  =   new   JComboBox ();
        comboBox . setModel ( new   DefaultComboBoxModel ( new   String []   { "Furniture" ,   "HardWare" ,   "Electronics" }));
        add ( comboBox );
        
         JButton  btnNewButton  =   new   JButton ( "Ok" );
        btnNewButton . addActionListener ( new   ActionListener ()   {
             public   void  actionPerformed ( ActionEvent  arg0 )   {              
                setDepartment (( String ) comboBox . getSelectedItem ());
                notifyObservers ();
             }
         });
        add ( btnNewButton );
        
         JButton  btnNewButton_1  =   new   JButton ( "Exit" );
        btnNewButton_1 . addActionListener ( new   ActionListener ()   {
             public   void  actionPerformed ( ActionEvent  e )   {
                 System . exit ( 0 );
             }
         });
        add ( btnNewButton_1 );
        
        department = "Furniture" ;
     }

    @ Override
     public   void  notifyObservers ()   {
         for   ( int  i  =   0 ;  i  <  observersList . size ();  i ++ )   {
               Observer  observer  =
                 ( Observer )  observersList . get ( i );
              observer . refreshData ( this );
             }
        
     }

    @ Override
     public   void  register ( Observer  obs )   {
        observersList . add ( obs );      
     }

    @ Override
     public   void  unRegister ( Observer  obs )   {
         // TODO Auto-generated method stub
        
     }
     
     private   ArrayList  observersList ;
     public   String  getDepartment ()   {
         return  department ;
     }

     public   void  setDepartment ( String  department )   {
         this . department  =  department ;
     }

     private   String  department ;

}

SaleItem.java

SaleItem.java



public   class   SaleItem   {
     private   String  dept ;
     private   int  month ;
     private   int  day ;
     private   int  quantity ;
     private   int  amount ;
    
     public   String  getDepartment ()
     {
         return  dept ;
     }
     public   int  getMonth ()   {
         return  month ;
     }
     public   int  getDay ()   {
         return  day ;
     }
     public   String  getDept ()   {
         return  dept ;
     }
     public   int  getQuantity ()   {
         return  quantity ;
     }
     public   int  getAmount ()   {
         return  amount ;
     }
     public   SaleItem ( String  d , int  month ,   int  day ,   int  quantity ,   int  amount )   {
        dept = d ;
         this . month  =  month ;
         this . day  =  day ;
         this . quantity  =  quantity ;
         this . amount  =  amount ;
     }
}

SalesData.java

SalesData.java


import  java . io . File ;
import  java . io . IOException ;
import  java . util . ArrayList ;
import  java . util . Calendar ;
import  java . util . Date ;
import  java . util . Scanner ;
import  java . util . StringTokenizer ;

public   class   SalesData   {
     ArrayList < SaleItem >  allRows ;
     String  department = "Furniture" ;

     public   SalesData ( String  fileName )
     {
        allRows  =   new   ArrayList < SaleItem > ();
         Scanner  input  =   null ;
         try   {
            input  =   new   Scanner ( new   File ( fileName ));
         }   catch   ( IOException  e )   {
             System . err . println ( "File not found!" );
         }
         while   ( input . hasNext ())   {
             String  line = input . nextLine ();
             StringTokenizer  st  =   new   StringTokenizer ( line ,   "," );
             String  s = st . nextToken ();
             int  m = Integer . parseInt ( st . nextToken ());
             int  d = Integer . parseInt ( st . nextToken ());
             int  q = Integer . parseInt ( st . nextToken ());
             int  a = Integer . parseInt ( st . nextToken ());
    
             SaleItem  item = new   SaleItem ( s , m , d , q , a );
            allRows . add ( item );
         }
     }

     public   String  getDepartment (){ return  department ;      }

     public   void  setDepartment ( String  d ){ department = d ;}

     public   int []  getYTDTotals ( String  department )   {
         int []  totals  =   {   0 ,   0 ,   0 ,   0 ,   0 ,   0 ,   0 ,   0 ,   0 ,   0 ,   0 ,   0   };
         for   ( int  i  =   0 ;  i  <   12 ;  i ++ )   {
            totals [ i ]   =  getMonthlyTotal ( +   1 ,  department );
         }
         return  totals ;
     }

     public   int  getMonthlyTotal ( int  month ,   String  department )   {
         int  total  =   0 ;

         ArrayList < SaleItem >  result = getCurrentMonthTransactions ( month , department );

         for   ( SaleItem  s : result )   {
            total  =  total  +  s . getAmount ();
         }
         return  total ;
     }

     public   ArrayList  getCurrentMonthTransactions ( int  month , String  department )   {
         ArrayList < SaleItem >  result  =   new   ArrayList < SaleItem > ();

         for   ( SaleItem  s : allRows )   {
             if   ( s . getDepartment (). equals ( department )   &&  s . getMonth () == month )   {
                result . add ( s );
             }
         }
         return  result ;
     }


}

SupervisorView.java

SupervisorView.java

import  java . awt . BorderLayout ;
import  java . awt . GridLayout ;

import  javax . swing . JFrame ;
import  javax . swing . JPanel ;

public   class   SupervisorView   {

   public   static   void  main ( String []  args )   throws   Exception   {

     JFrame  mainFrame = new   JFrame ();
    mainFrame . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
       //Create the Subject
     Report  objSubject  =   new   Report ();
    mainFrame . add ( objSubject , BorderLayout . NORTH );
     //Create Observers
     JPanel  display = new   JPanel ();
    display . setLayout ( new   GridLayout ( 1 , 2 ));
     SalesData  data = new   SalesData ( "Transactions.dat" );
    display . add ( new   MonthlyReport ( objSubject , data ));
    display . add ( new   YTDChart ( objSubject ,  data ));
     
    mainFrame . add ( display , BorderLayout . CENTER );

    mainFrame . setSize ( 1500 , 600 );
    mainFrame . setVisible ( true );
   }
} // end of class

Transactions.dat

Furniture 1 1 2 200
Furniture 1 10 2 400
Furniture 1 15 2 400
Furniture 2 1 2 2000
Furniture 2 10 2 2000
Furniture 2 15 2 1000
Furniture 3 1 2 2000
Furniture 3 10 2 1000
Furniture 3 15 2 1000
Furniture 4 1 2 200
Furniture 4 10 2 400
Furniture 4 15 2 400
Furniture 5 1 2 2000
Furniture 5 1 2 2000
Furniture 5 1 2 2000
Furniture 6 10 2 2000
Furniture 6 15 2 2000
Furniture 6 1 2 1000
Furniture 6 2 2 4000
Furniture 7 10 2 200
Furniture 7 15 2 700
Furniture 7 1 2 100
Furniture 8 10 2 200
Furniture 8 15 2 200
Furniture 8 1 2 1600
Furniture 9 10 2 200
Furniture 9 15 2 200
Furniture 9 1 2 600
Furniture 10 10 2 1000
Furniture 10 15 2 2000
Furniture 10 1 2 1000
Furniture 11 10 2 200
Furniture 11 15 2 200
Furniture 11 1 2 800
Furniture 12 10 2 2000
Furniture 12 15 2 2000
Furniture 12 1 2 2000
HardWare 1 1 2 2000
HardWare 1 10 2 2000
HardWare 1 15 2 1000
HardWare 2 1 2 2000
HardWare 2 10 2 2000
HardWare 2 15 2 2000
HardWare 3 1 2 200
HardWare 3 10 2 1000
HardWare 3 15 2 800
HardWare 4 1 2 200
HardWare 4 10 2 400
HardWare 4 15 2 400
HardWare 5 1 2 200
HardWare 5 1 2 200
HardWare 5 1 2 600
HardWare 6 10 3 3000
HardWare 6 15 2 2000
HardWare 6 1 2 1000
HardWare 7 10 2 200
HardWare 7 15 2 700
HardWare 7 1 2 100
HardWare 8 10 2 200
HardWare 8 15 2 200
HardWare 8 1 2 600
HardWare 9 10 2 200
HardWare 9 15 2 200
HardWare 9 1 2 600
HardWare 10 10 2 2000
HardWare 10 15 2 2000
HardWare 10 1 2 2000
HardWare 11 10 2 1000
HardWare 11 15 2 200
HardWare 11 1 2 1800
HardWare 12 10 2 200
HardWare 12 15 2 200
HardWare 12 1 2 600
Electronics 1 1 2 200
Electronics 1 10 2 400
Electronics 1 15 2 400
Electronics 2 1 2 2000
Electronics 2 10 2 2000
Electronics 2 15 2 1000
Electronics 3 1 2 2000
Electronics 3 10 2 1000
Electronics 3 15 2 1000
Electronics 4 1 2 200
Electronics 4 10 2 400
Electronics 4 15 2 400
Electronics 5 1 2 200
Electronics 5 1 2 200
Electronics 5 1 2 600
Electronics 6 10 2 2000
Electronics 6 15 1 1000
Electronics 6 1 2 1000
Electronics 7 10 2 200
Electronics 7 15 2 700
Electronics 7 1 2 100
Electronics 8 10 2 1200
Electronics 8 15 2 1200
Electronics 8 1 2 1600
Electronics 9 10 2 200
Electronics 9 15 2 200
Electronics 9 1 2 600
Electronics 10 10 2 1000
Electronics 10 15 2 2000
Electronics 10 1 2 1000
Electronics 11 10 2 1000
Electronics 11 15 2 200
Electronics 11 1 2 1800
Electronics 12 10 2 200
Electronics 12 15 2 200
Electronics 12 1 2 600

YTDChart.java

YTDChart.java

import  java . io . * ;
import  java . awt . * ;
import  java . awt . event . * ;
import  javax . swing . * ;
import  com . sun . java . swing . plaf . windows . * ;
import  java . util . * ;

public   class   YTDChart   extends   JPanel   implements   Observer   {

     private   Report  report ;
     private   String  department  =   "Furniture" ;
     private   SalesData  data ;

     public   YTDChart ( Report  r ,   SalesData  d )   throws   Exception   {
         this . report  =  r ;
        data = d ;
        report . register ( this );
     }

     public   void  refreshData ( Observable  subject )   {
         if   ( subject  ==  report )   {
            department  =  report . getDepartment (). trim ();
            repaint ();
         }
     }

     public   void  paintComponent ( Graphics  g )   {
         super . paintComponent ( g );
        plotMonths ( g );

         int  x  =   100 ,  y  =   50 ;
         int  w  =   50 ;
         int  h  =   20 ;

         int []  totals  =  data . getYTDTotals ( department );
         // current month
         Calendar  cal  =   Calendar . getInstance ();
        cal . setTime ( new   Date ());
         int  month  = cal . get ( Calendar . MONTH )   +   1 ;

         for   ( int  i  =   0 ;  i  <  month ;  i ++ )   {
            g . setColor ( Color . blue );
             if   ( totals [ i ]   >   0 )   {
                w  =   ( int )   ( totals [ i ]   /   50 );
                g . fillRect ( x ,  y ,  w ,  h );
                g . drawString ( "$"   +  totals [ i ],  x  +  w  +   5 ,  y  +   15 );
             }
            y  =  y  +   30 ;
         }
     }

     private   void  plotMonths ( Graphics  g )   {
        g . drawString ( department  +   " YTD Report" ,   150 ,   20 );

         String []  months  =   {   "Jan" ,   "Feb" ,   "Mar" ,   "Apr" ,   "May" ,   "Jun" ,   "Jul" ,
                 "Aug" ,   "Sep" ,   "Oct" ,   "Nov" ,   "Dec"   };

         int  x  =   50 ,  y  =   65 ;
         for   ( int  j  =   0 ;  j  <  months . length ;  j ++ )   {
            g . setColor ( Color . blue );
            g . drawString ( months [ j ],  x ,  y );
            y  =  y  +   30 ;
         }

     }
} // end of class YTDChart