0% found this document useful (0 votes)
412 views13 pages

Birthday Reminder

This document contains code for a Java program that allows users to add friends' birthdays to a CSV file and check for upcoming birthdays. The main class contains a main menu that allows the user to add friends, check reminders, view the file contents, or exit. The addFriend() method prompts the user to enter a friend's details and writes it to the CSV file. The checkReminders() method reads the CSV file, gets the current date, and checks for birthdays within the next week.

Uploaded by

Areeba Shakil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
412 views13 pages

Birthday Reminder

This document contains code for a Java program that allows users to add friends' birthdays to a CSV file and check for upcoming birthdays. The main class contains a main menu that allows the user to add friends, check reminders, view the file contents, or exit. The addFriend() method prompts the user to enter a friend's details and writes it to the CSV file. The checkReminders() method reads the CSV file, gets the current date, and checks for birthdays within the next week.

Uploaded by

Areeba Shakil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

BirthDayREMINDER (MAIN CLASS)

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.InputMismatchException;
import java.util.Scanner;

public class BirthdayReminder {


private static Scanner input = new Scanner(System.in);
private static BRWriter br;
private static BRReader brReader;

public static void mainMenu(){


int option = 0;
System.out.println("******* BIRTHDAY REMINDER
*******");
System.out.println("Please select any option from
below menu: ");
System.out.println("1. Add Friend");
System.out.println("2. Check Reminders");
System.out.println("3. View File Contents");
System.out.println("4. Exit");

try{
option = input.nextInt();
}catch(InputMismatchException e){
System.out.println("Invalid option selected,
Please try again\n");
mainMenu();
}

if(option == 1){
addFriend();
mainMenu();
}else if(option == 2){
checkReminders();
mainMenu();
}else if(option ==3){
brReader = new BRReader("BirthdayReminders");
brReader.readFile();
mainMenu();
}else if(option ==4){
input.close();
System.exit(0);
}else{
System.out.println("Invalid option, Please select
again");
mainMenu();
}

public static void addFriend(){


String name = null,age = null,gender = null;
String sDay = null,sMonth = null,sYear = null;
int iDay,iMonth,iYear;

System.out.println("Please enter a Friend's name: ");


try{
name = input.next();
}catch(InputMismatchException e){
System.out.println("Invalid input value, Please
try again\n");
mainMenu();
}
System.out.println("Please enter the 4 digit year
he/she was born in: ");
try{
sYear = input.next();
}catch(InputMismatchException e){
System.out.println("Invalid input value, Please
try again\n");
mainMenu();
}
iYear = Integer.parseInt(sYear);
System.out.println("Please enter the numerical month
he/she was born: ");
try{
sMonth = input.next();
}catch(InputMismatchException e){
System.out.println("Invalid input value, Please
try again\n");
mainMenu();
}
iMonth = Integer.parseInt(sMonth);
System.out.println("Please enter the day he was born:
");
try{
sDay = input.next();
}catch(InputMismatchException e){
System.out.println("Invalid input value, Please
try again\n");
mainMenu();
}
iDay = Integer.parseInt(sDay);
System.out.println("Please enter his/her age: ");
try{
age = input.next();
}catch(InputMismatchException e){
System.out.println("Invalid input value, Please
try again\n");
mainMenu();
}
System.out.println("Please enter his/her gender: ");
try{
gender = input.next();
}catch(InputMismatchException e){
System.out.println("Invalid input value, Please
try again\n");
mainMenu();
}

GregorianCalendar calendarBday = new


GregorianCalendar(iYear, iMonth-1, iDay);
Date dateBday = calendarBday.getTime();
String strBday = new
SimpleDateFormat("MM/dd/yyyy").format(dateBday);

br = new BRWriter();

br.pushData(name,strBday,age,gender);

br.writeCsvFile("BirthdayReminders");

public static void checkReminders(){


brReader = new BRReader("BirthdayReminders");
//saving name dob date in these array
int[] dobs = brReader.returnDaysOfTheYear();
String[] nameList = brReader.returnNames();
String[] dateList = brReader.returnDates();
String sDay = null,sMonth = null,sYear = null;
int iDay,iMonth,iYear;
//int location = 0;
System.out.println("Please enter current year: ");
try{
sYear = input.next();
}catch(InputMismatchException e){
System.out.println("Invalid input value, Please
try again\n");
mainMenu();
}
iYear = Integer.parseInt(sYear);
System.out.println("Please enter current month: ");
try{
sMonth = input.next();
}catch(InputMismatchException e){
System.out.println("Invalid input value, Please
try again\n");
mainMenu();
}
iMonth = Integer.parseInt(sMonth);
System.out.println("Please enter current day: ");
try{
sDay = input.next();
}catch(InputMismatchException e){
System.out.println("Invalid input value, Please
try again\n");
mainMenu();
}
iDay = Integer.parseInt(sDay);

//current calender

GregorianCalendar calendarTday = new


GregorianCalendar(iYear, iMonth-1, iDay);
Calendar calTday = Calendar.getInstance();
calTday.setTime(calendarTday.getTime());

//
int today = calTday.get(Calendar.DAY_OF_YEAR);
int i;

//DOB jitni length hogi utna chalega


for(i=0; i<dobs.length; i++){

//this loop defines how many days it will check


if((dobs[i] - today) <= 2 && (dobs[i] - today) <=
6 && (dobs[i] - today) >= 0){
System.out.println("Upcomming Birthday: " +
nameList[i] + " on " + dateList[i]);
}
}
if(i == dobs.length){
System.out.println("There are no more upcomming
Birthdays");
}
/*else{
System.out.println("Upcomming Birthday: " +
nameList[location] + " on " + dateList[location]);
}*/
}

public static void main(String[] args){


mainMenu();
}

BRWriter Class:

//this class defines file structure

/*A Comma-Separated Values (CSV) file is just a normal plain-text file,


store data in column by column, and split it by a separator
(e.g normally it is a comma “,”).*/

//BRWriter writes the data into the file when you add a new friends birthday.

import java.io.FileWriter;
import java.io.IOException;

public class BRWriter {


/* q k name, dob, age, gender
ki values jab aengi to unko separate karky write karna hy file mn
to bar bar comma daalnay k bajae usko ek constant declare kardia
aur har data end hoga to ek new line add karni paraygi
warna sara data ek hi line mn hga to separate karna mushkil hojaega */

private static final String COMMA_DELIMITER = ",";


private static final String NEW_LINE_SEPARATOR = "\n";

private String name;


private String dob;
private String age;
private String gender;
private FileWriter fw = null;

/* class variables ko hum koshish kartay hain k private rakhen


and unki value assign karnay k liay pushData ka method hy
isliay pvt hain aur taa k object k instance se access na hosaken isliay private hain*/

//setter function class mai bhejni hai value to ye setter use karenge
public void pushData(String _name,String _dob, String _age, String _gender){
name = _name;
dob = _dob;
age = _age;
gender = _gender;
}

//paramerter use to write data in the define name in BirthdayReminder class


public void writeCsvFile(String _fileName) {
try { //Java try block is used to enclose the code that might throw an
exception.

fw = new FileWriter(_fileName,true);// true se ye hoga ke new file


write nahi kia usi file mai append kardega
fw.append(name);//append func write in file
fw.append(COMMA_DELIMITER);
fw.append(dob);
fw.append(COMMA_DELIMITER);
fw.append(age);
fw.append(COMMA_DELIMITER);
fw.append(gender);
fw.append(NEW_LINE_SEPARATOR);
System.out.println("CSV file was written successfully !!!");

//Java catch block is used to handle the Exception. It must be used


after the try block only.
} catch (Exception e) {
System.out.println("Error in CsvFileWriter !!!");
e.printStackTrace(); //throw or display error

//Finally block in java can be used to put "cleanup" code such as


closing a file, closing connection etc.

//flush() writes the content of the buffer to the destination and makes the buffer empty for
further data to store but it does not closes the stream permanently.
//That means you can still write some more data to the stream.
//But close() closes the stream permanently. If you want to write some data further, then you
have to reopen the stream again and append the data with the existing ones.

} finally {
try {
fw.flush();
fw.close();
} catch (IOException e) {
System.out.println("Error while flushing/closing fileWriter
!!!");
e.printStackTrace();
}
}
}
}

BRReader Class:
//reader class
//read line while kee loop mai lagaya hai ta ke next line mai jai peche nahi
jata
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class BRReader {

//Delimiter used in CSV file


private static final String COMMA_DELIMITER = ",";

private static final int NAME_IDX = 0;


private static final int DOB_IDX = 1;
private static final int AGE = 2;
private static final int GENDER = 3;
private String fileName;

//set method
public BRReader(String _fileName){
fileName = _fileName;
}

//tell the total lines in the file


public int countLines() throws IOException { //if this func use in
other func to wo throw se pata chalega ta ke pata chale masla
LineNumberReader reader = null;
try {
reader = new LineNumberReader(new FileReader(fileName));
while ((reader.readLine()) != null);
return reader.getLineNumber();
} catch (Exception ex) {
return -1; //agar garbage value aye is liye -1

} finally {
if(reader != null)
reader.close();
}
}

//ye return karega sare list of names in file


public String[] returnNames()
{
BufferedReader fileReader = null;
int lineNumber = 0;
String[] names = null; //jitni no of lines name honge utna size
hoga array ka
try{
names = new String[countLines()];//array size define hoga
}catch(IOException e){
e.printStackTrace();
}

//while loop jab tk chalta rahe ga jab line ki value null nahi hojata
try {
String line = "";
fileReader = new BufferedReader(new FileReader(fileName));

while ((line = fileReader.readLine()) != null) {


String[] tokens = line.split(COMMA_DELIMITER); //line split
mai jo para dalte hain usko split karta hai and then string ke array mai
bethaiga
if (tokens.length > 0) {
names[lineNumber] = tokens[NAME_IDX];
/*name chahye jo file reader hai jo read line ka func wo puri line

read kar raha hai par line chahye jo pheli line read tokens mai
divide kia to 0 index mai

naam betha hoga kisi ka bhi q ke line pe phela index name hai or
ye token divide com ma del se ho raha hai to

tokens mai array mai 0 pos mai naam hai then dob then gender to
mujhe srif naam chhaye token ke 0
index se naam uthaaya or name ke array mai rukh dia and then line
number increment */
lineNumber++;
}
}
}

catch (Exception e) {
System.out.println("Error in CsvFileReader !!!");
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
System.out.println("Error while closing fileReader !!!");
e.printStackTrace();
}
}
return names;
}

//return DOB in strings form


public String[] returnDates(){
BufferedReader fileReader = null;
int lineNumber = 0;
String[] dates = null;
try{
dates = new String[countLines()];
}catch(IOException e){
e.printStackTrace();
}

try {
String line = "";
fileReader = new BufferedReader(new FileReader(fileName));

while ((line = fileReader.readLine()) != null) {


String[] tokens = line.split(COMMA_DELIMITER);
if (tokens.length > 0) {
dates[lineNumber] = tokens[DOB_IDX];
lineNumber++;
}
}
}

catch (Exception e) {
System.out.println("Error in CsvFileReader !!!");
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
System.out.println("Error while closing fileReader !!!");
e.printStackTrace();
}
}
return dates;
}

//
public int[] returnDaysOfTheYear(){
BufferedReader fileReader = null;
int lineNumber = 0;
int[] daysOfTheYear = null;
try{
daysOfTheYear = new int[countLines()];
}catch(IOException e){
e.printStackTrace();
}

try {

String line = "";


fileReader = new BufferedReader(new FileReader(fileName));

while ((line = fileReader.readLine()) != null) {


String[] tokens = line.split(COMMA_DELIMITER);
if (tokens.length > 0) { //tokens mai jub kuch index mai zero
hogi to kaam karne ka faida kia hai
Calendar cal = Calendar.getInstance();
String strDate = tokens[DOB_IDX];
DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); //df
mai formate hai
try {
cal.setTime(df.parse(strDate)); //strdate jo nikali
hai wo string ki formate mai hai lekin jo wo date ki formate mai

//chahye to jo cal obj string read nahi karta to usko parse use kia jo
uske cal ke formate mai convert kardega

daysOfTheYear[lineNumber] =
cal.get(Calendar.DAY_OF_YEAR); //cal mai se jo string bethai hain mm/dd/yyyy
jo /dd value return kardega
lineNumber++;
} catch (ParseException e) {
e.printStackTrace();
}
}
}

catch (Exception e) {
System.out.println("Error in CsvFileReader !!!");
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
System.out.println("Error while closing fileReader !!!");
e.printStackTrace();
}
}
return daysOfTheYear;
}

public void readFile() {


BufferedReader fileReader = null;
try {
String line = "";

fileReader = new BufferedReader(new FileReader(fileName));

while ((line = fileReader.readLine()) != null) {


String[] tokens = line.split(COMMA_DELIMITER);
if (tokens.length > 0) {
Friend friend = new Friend(tokens[NAME_IDX],
tokens[DOB_IDX], tokens[AGE], tokens[GENDER]);
System.out.println(friend.toString());
}
}
}
catch (Exception e) {
System.out.println("Error in CsvFileReader !!!");
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
System.out.println("Error while closing fileReader !!!");
e.printStackTrace();
}
}

FRIEND Classes:
//use this class for readfile method
public class Friend {

private String name;


private String dob;
private String age;
private String gender;

public Friend(String _name, String _dob, String _age, String _gender) {


this.name = _name;
this.dob = _dob;
this.gender = _gender;
this.age = _age;
}

public String getName() {


return name;
}

public void setName(String _name) {


this.name = _name;
}

public String getDOB() {


return dob;
}

public void setDOB(String _dob) {


this.dob = _dob;
}

public String getGender() {


return gender;
}

public void setGender(String _gender) {


this.gender = _gender;
}

public String getAge() {


return age;
}

public void setAge(String age) {


this.age = age;
}

//toStind method use q ke puri line dede ga string , mai


//get all friend in formation
public String toString() {
return "Name= " + name + ", DOB= " + dob
+ ", Gender=" + gender + ", Age="
+ age ;
}
}

You might also like