Question

In: Computer Science

The Calendar Program The purpose of this lab is to give you a chance to use...

The Calendar Program

The purpose of this lab is to give you a chance to use some of the data stream tools we have been discussing in a simple application. The assignment is to write a calendar application which allows the user to select a date, and either retrieve a previously stored calendar entry, or save a calendar entry.

Your program should present a GUI interface which allows the user to specify the month, day, and year of the calendar entry. The GUI should also have a text area for displaying and editing a particular entry. It will also need two buttons, one for saving an entry, and the other for retrieving an entry.

Required program elements:

Your user interface must allow the user to enter the month, day, and year. You can do this using text fields for input, or you can use ComboBoxes if you feel adventurous.

The only GUI components which create events that your program needs to handle are the save and retrieve buttons.

Don

Solutions

Expert Solution

Save Operation

File content after save

Retrieve operation

===================================================

CalenderInterface.java

====================================================

import java.awt.Button;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CalendarInterface extends Frame implements ActionListener {
  
   Label l1 = new Label("Month :");
   Label l2 = new Label("Day: ");
   Label l3 = new Label("Year: ");

   Label l4 = new Label("");
  
   TextField t1 = new TextField();
   TextField t2 = new TextField();
   TextField t3 = new TextField();
  
   Button b = new Button("Save");
   Button b1 = new Button("Retrieve");

   public CalendarInterface() {
      
       add(l1);
       add(t1);

       add(l2);
       add(t2);
      
       add(l3);
       add(t3);
      
       add(b);
       add(b1);
       add(l4);
      
       l1.setBounds(20, 45, 70, 20);
       t1.setBounds(180, 45, 200, 20);
      
       l2.setBounds(20, 95, 70, 20);
       t2.setBounds(180, 95, 200, 20);
      
       l3.setBounds(20, 135, 70, 20);
       t3.setBounds(180, 135, 200, 20);
      
       b.setBounds(180, 175, 70, 20);
       b.addActionListener(this);
      
       b1.setBounds(310, 175, 70, 20);
       b1.addActionListener(this);
      
       addWindowListener(new gws());
   }

   public void actionPerformed(ActionEvent e) {
       if(e.getActionCommand().equals("Save")) {
          
           String month = t1.getText();
           String day = t2.getText();
           String year = t3.getText();
          
           if(!CalendarManager.validateMonth(month)) {
               l4.setText("Invalid Month entered");
           }
          
           if(!CalendarManager.validateDay(day)) {
               l4.setText("Invalid Day entered");
           }
          
           CalendarManager.save(month, day, year);
           l4.setText("Data written successfully to file with name "+ month+" "+year+".txt");
          
       } else if(e.getActionCommand().equals("Retrieve")){

          
           String month = t1.getText();
           String day = t2.getText();
           String year = t3.getText();
          
           if(!CalendarManager.validateMonth(month)) {
               l4.setText("Invalid Month entered");
           }
          
           if(!CalendarManager.validateDay(day)) {
               l4.setText("Invalid Day entered");
           }
          
           String result = CalendarManager.retrieve(month, day, year);
           l4.setText(result);
          
      
          
       }
   }

  
}

class gws extends WindowAdapter {
   public gws() {
   }

   public void windowClosing(WindowEvent e) {
       System.exit(0);
   }
}

===================================================

CalenderManager.java

====================================================

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CalendarManager {

   public static boolean save(String month, String day, String year) {
      
       try {

           File file = new File(month+" "+year);

           // if file doesnt exists, then create it
           if (!file.exists()) {
               file.createNewFile();
           }

           // true = append file
           FileWriter fileWritter = new FileWriter(file.getName(), true);
           BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
           bufferWritter.write(month+"-"+day+"-"+year+"\n");
           bufferWritter.close();

           System.out.println("Done");

       } catch (IOException e) {
           e.printStackTrace();
       }

       return true;
   }
  
   public static boolean validateMonth(String month) {
      

       if(month.equalsIgnoreCase("January")) {
           return true;
       }
       else if(month.equalsIgnoreCase("February")) {
           return true;
       }
       else if(month.equalsIgnoreCase("March")) {
           return true;
       }
       else if(month.equalsIgnoreCase("April")) {
           return true;
       }
       else if(month.equalsIgnoreCase("May")) {
           return true;
       }
       else if(month.equalsIgnoreCase("June")) {
           return true;
       }
       else if(month.equalsIgnoreCase("July")) {
           return true;
       }
       else if(month.equalsIgnoreCase("August")) {
           return true;
       }
       else if(month.equalsIgnoreCase("September")) {
           return true;
       }
       else if(month.equalsIgnoreCase("October")) {
           return true;
       }
       else if(month.equalsIgnoreCase("November")) {
           return true;
       }
       else if(month.equalsIgnoreCase("December")) {
           return true;
       }
       else {
           return false;
       }

   }
  
   public static boolean validateDay(String day) {

  
       if(Integer.parseInt(day)>=1&&Integer.parseInt(day)<=31){
           return true;
       } else {
           return false;
       }
   }
  
   public static String retrieve(String month, String day, String year) {

       BufferedReader br = null;
       boolean found=false;
       try {

           String sCurrentLine;

           br = new BufferedReader(new FileReader(month+" "+year));

           while ((sCurrentLine = br.readLine()) != null) {
               if(sCurrentLine.equals(month+"-"+day+"-"+year)) {
                   found=true;
                   break;
               }
           }

       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (br != null)br.close();
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }
      
       if(found) {
           return "Entry found in file : "+month+" "+year+".txt";
       }
       return "Entry not found";
      
   }
}

===================================================

TestCalender.java

====================================================

import java.awt.Dimension;

public class TestCalender {

   public static void main(String[] args) {

       CalendarInterface l = new CalendarInterface();
       l.setSize(new Dimension(600, 600));
       l.setTitle("Calander App");
       l.setVisible(true);

   }
}


Related Solutions

Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Purpose: This lab will give you experience modifying an existing ADT. Lab Main Task 1: Modify...
Purpose: This lab will give you experience modifying an existing ADT. Lab Main Task 1: Modify the ListInterface Java interface source code given below. Change the name of ListInterface to ComparableListInterface, and have ComparableListInterface inherit from the built-in Java interface Comparable. Note that Comparable has a method compareTo(). compareTo() must be used in programming logic you will write in a method called isInAscendingOrder() (more about isInAscendingOrder() is mentioned further down in the lab description). You can find a brief summary...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers. Instructions You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
The purpose of this lab:  Wireshark Intro Lab is to get students familiar with the use of...
The purpose of this lab:  Wireshark Intro Lab is to get students familiar with the use of their VMs and running wireshark on their VMs. We also examine Ethernet, IPv4, and TCP addressing at the Network Access, Network, and Transport layers of the TCP/IP stack. Reflection: In two paragraphs reflect the experience of using Wireshark capture (in the lab) on the following questions: What was the most valuable feature of the lab? How did you prepare for this lab? What changes...
In this lab you will get a chance to explore names w/ Vectors. You will need...
In this lab you will get a chance to explore names w/ Vectors. You will need to create two vectors of strings, one for first names, and one for last names. You will then read in first and last names and place these into vectors. See example below: while (true){ getline(cin, name); if (name.empty()){ break; } // Add to vector of First Names getline(cin, name); // Add to vector of Last Names // Print out First Name and Last Name...
This project is assigned to give you the chance to apply the knowledge that you have...
This project is assigned to give you the chance to apply the knowledge that you have acquired in statistics to our Global Society. The following data has been collected for you and you are going to look at the possible relationships and make some decisions that might impact your life based on the outcomes. Use the following data in this project. The data represents the Total Number of Alternative-Fueled Vehicles in use in the United States (source:  US Department of Energy:  http://tonto.eia.doe.gov/aer/)...
This project is assigned to give you the chance to apply the knowledge that you have...
This project is assigned to give you the chance to apply the knowledge that you have acquired in statistics to our Global Society. The following data has been collected for you and you are going to look at the possible relationships and make some decisions that might impact your life based on the outcomes. Use the following data in this project. The data represents the Total Number of Alternative-Fueled Vehicles in use in the United States (source:  US Department of Energy:  http://tonto.eia.doe.gov/aer/)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT