In: Computer Science
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
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);
}
}