Wednesday, 3 July 2013

Calender Manager Java Program



 Calender Manager Java Program and GUI


Java Code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class userInterface extends JFrame implements ActionListener {

       public CalenderManager calender;
       public JTextField Month, day, year;
       public JScrollPane Content;
       public JTextArea text;
       public JButton get, save;
       public JLabel m, d, y;

       public userInterface() {
              super("Calender manager");
              setLayout(new FlowLayout());
              calender = new CalenderManager();

              m = new JLabel("MONTH");
              d = new JLabel("DAY");
              y = new JLabel("YEAR");
              Month = new JTextField(12);
              day = new JTextField(12);
              year = new JTextField(12);
              add(m);
              add(Month);
              add(d);
              add(day);
              add(y);
              add(year);

              text = new JTextArea(10, 30);
              add(new JScrollPane(text));

              // Content = new JScrollPane();
              get = new JButton("GET");
              add(get);
              get.addActionListener((ActionListener) this);
              save = new JButton("SAVE");
              add(save);
              save.addActionListener(this);
             

       }
      
       @Override
       public void actionPerformed(ActionEvent e) {
              // TODO Auto-generated method stub
              CalenderManager c=new CalenderManager();
              if(e.getSource() == get){
                     text.setText(c.getEntry(Month.getText(), day.getText(), year.getText()));
              }
              if(e.getSource() == save){
                     text.setText(c.saveEntry(Month.getText(), day.getText(), year.getText(), text.getText()));
              }
             
       }


      

}

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JFileChooser;

public class CalenderManager {

       public String[] array;

       public CalenderManager() {
              String fname="base.dir";
              File dir = new File(fname);
              if (!dir.exists())
                     dir.mkdir();
              JFileChooser filechooser = new JFileChooser();
              filechooser.setCurrentDirectory(dir);
              filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
       }

       public String saveEntry(String month, String day, String year, String entry) {
              getFile(month, year);
              try {
                     array[Integer.parseInt(day) - 1] = entry;
                     FileOutputStream f;
                     ObjectOutputStream fop;
                     File file;

                     file = new File(month + " " + year + ".dat");
                     f = new FileOutputStream(file);
                     fop = new ObjectOutputStream(f);

                     fop.writeObject(array);
                     fop.close();

                     return "Success";
              } catch (Exception e) {
                     return "Error";
              }
       }

       private void getFile(String month, String year) {
              try {

                     File file = new File(month + " " + year + ".dat");
                     if (!file.exists()) {
                            file.createNewFile();
                           array = new String[31];
                     } else {
                           FileInputStream fw = new FileInputStream(file.getAbsoluteFile());
                           ObjectInputStream bw = new ObjectInputStream(fw);
                           array = (String[]) bw.readObject();
                           bw.close();
                     }

              } catch (IOException e) {
                     array = new String[31];
                     e.printStackTrace();
              } catch (ClassNotFoundException e) {
                     // TODO Auto-generated catch block
                     array = new String[31];
                     e.printStackTrace();
              }

       }

       public String getEntry(String month, String day, String year) {
              getFile(month, year);
              String data = array[Integer.parseInt(day) - 1];
              if (data != null) {
                     return data;
              }
              return "entry does not exist";

       }

}

import javax.swing.JFrame;


public class CalendarTest {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
              userInterface uI = new userInterface();
              uI.setSize(400, 400);
              uI.setVisible(true);
              uI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       }

}

Program Snapshots:




No comments:

Post a Comment