Creating An Appointment Book Hello! I am having trouble with this project. I hav
ID: 3844670 • Letter: C
Question
Creating An Appointment Book
Hello! I am having trouble with this project. I have the main structure set up, but I am not sure how to implement events indefinitely, such as, the daily and monthly events. Also, the loading part is something I have not seen before, so I am not sure how to go about it. I would like to see a working code to edit my own code off of the working code. At the bottom is my failed coding efforts. Would love to see it edited and made nice or if you can do it better without doing that, I would appreciate it too!
Appointment Book class:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Scanner;
// Class Description: Stores Appointments into an array list
public class AppointmentBook {
public static final String;
public static final String DAILY = "Daily";
public static final String MONTHLY = "Monthly";
ArrayList<String> sApptList = new ArrayList<String>();
// Add appt to the string array list
String sAppt = "";
public void addAppt(Appointment appt) {
sAppt = appt.toString();
sApptList.add(sAppt);
}
// Add loaded appts to new array list
String sApptLoad = "";
ArrayList<String> apptListLoad = new ArrayList<String>();
public void addApptLoad(Appointment appt) {
sApptLoad = appt.toString();
apptListLoad.add(sApptLoad);
}
// Saves the array list to a text file
PrintWriter writer;
public void save(String string) {
try {
writer = new PrintWriter(new FileWriter(string + ".txt"));
for (String str : sApptList) {
writer.write(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}
}
FileReader fileRead;
BufferedReader buffRead;
Scanner input;
String txt;
GregorianCalendar gDate;
String type;
DateFormat dateFormat;
java.util.Date date;
String sDate;
File file;
public void load(String string) {
file = new File(string + ".txt");
try {
input = new Scanner(file);
fileRead = new FileReader(file);
buffRead = new BufferedReader(fileRead);
dateFormat = new SimpleDateFormat("yyyy MM dd");
ArrayList<String> listReentry = new ArrayList<String>();
String currentLine;
buffRead = new BufferedReader(new FileReader(file));
while ((currentLine = buffRead.readLine()) != null) {
listReentry.add(currentLine);
}
if (listReentry.size() == 3) {
type = listReentry.get(0);
txt = listReentry.get(1);
sDate = listReentry.get(2);
if (type.contains(" ")) {
type = type.replace(" ", "");
}
if (txt.contains(" ")) {
txt = txt.replace(" ", "");
}
if (sDate.contains(" ")) {
sDate = sDate.replace(" ", "");
}
type = type.substring(18);
txt = txt.substring(13);
sDate = sDate.substring(15);
}
try {
date = dateFormat.parse(sDate);
} catch (ParseException e) {
e.getErrorOffset();
e.printStackTrace();
}
gDate.setTime(date);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (buffRead != null) {
buffRead.close();
}
if (fileRead != null) {
fileRead.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
if (type.equals(DAILY)) {
Daily daily = new Daily(type, txt, gDate);
addAppt(daily);
} else if (type.equals(MONTHLY)) {
Monthly monthly = new Monthly(type, txt, gDate);
addAppt(monthly);
} else if (type.equals(ONE_TIME)) {
OneTime OneTime(type, txt, gDate);
addAppt(oneTime);
} else {
System.out.println("...Oops?");
}
}
public String getsApptLoad() {
return sApptLoad;
}
}
Appointment class:
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
// Has to be of 3 subclasses
public abstract class Appointment extends AppointmentBook {
/*
* Variables:
*/
// textual description of event
private String txt;
// date of event
private GregorianCalendar date;
// Constructor:
public Appointment(String txt, GregorianCalendar date) {
this.txt = txt;
this.date = date;
}
// Methods:
// Test three different one before, after, and on given date
public void occursOn(int year, int month, int day) {
GregorianCalendar compDate = new GregorianCalendar(year, month, day);
if (date.equals(compDate)) {
System.out.println("The date is on the date given");
} else if (compDate.after(date)) {
System.out.println("The date is before the date given.");
} else {
System.out.println("The date is after the date given.");
}
}
SimpleDateFormat f = new SimpleDateFormat("yyyy MM dd");
String sDate = "";
// toString method
public String toString(){
sDate = f.format(date.getTime());
return " Description: " + txt + " Date Of Event: " + sDate;
}
// Getters and Setters:
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
public GregorianCalendar getDate() {
return date;
}
public void setDate(GregorianCalendar date) {
this.date = date;
}
}
OneTime class(Monthly and Daily look the same just with monthly and daily variables instead because I do not know how to make them repeat indefinitely):
import java.util.GregorianCalendar;
public class OneTime extends Appointment {
// Variables:
GregorianCalendar GregorianCalendar();
// Constructor:
public OneTime(String type, String txt, GregorianCalendar date){
super(txt, date);
type = ONE_TIME;
}
// toString method
String sType = "";
public String toString(){
sType = "One Time";
return " Appointment Type: " + sType + super.toString();
}
}
Tester class:
import java.util.GregorianCalendar;
public class Tester {
public static void main(String[] args) {
GregorianCalendar gDate = new GregorianCalendar();
gDate.set(2017, 3, 10);
AppointmentBook ab = new AppointmentBook();
Monthly m1 = new Monthly("Monthly", "Workout", gDate);
Daily d1 = new Daily("Daily", "Eat lunch", gDate);
OneTime OneTime("One Time", "Skydiving", gDate);
Monthly m2 = new Monthly("Monthly", "Visit grandma", gDate);
Daily d2 = new Daily("Daily", "Sleep", gDate);
ab.addAppt(m1);
ab.addAppt(d1);
ab.addAppt(one1);
ab.addAppt(m2);
ab.addAppt(d2);
ab.save("appointments1");
ab.load("appointments1");
ab.getsApptLoad();
}
}
If you have scrolled to the bottom and read this, thank you! You are the best person ever! I have no idea how to do this code :)
Project 1-75 points For this project we will be constructing an appointment book in an incremental fashion. It will be drawing on everything we have learned in the course thus far. You have some flexibility here in how you design this, so if you make any assumptions about the requirements, be sure to state them in your comments. This project is more independent than the Labs/Assignments. So, while the TAs and I are happy to help you with course concepts and requirements, we won't help you design or code for this project.Explanation / Answer
AppointmentBook.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Scanner;
// Class Description: Stores Appointments into an array list
public class AppointmentBook {
public static final String;
public static final String DAILY = "Daily";
public static final String MONTHLY = "Monthly";
ArrayList<String> sApptList = new ArrayList<String>();
// Add appt to the string array list
String sAppt = "";
public void addAppt(Appointment appt) {
sAppt = appt.toString();
sApptList.add(sAppt);
}
// Add loaded appts to new array list
String sApptLoad = "";
ArrayList<String> apptListLoad = new ArrayList<String>();
public void addApptLoad(Appointment appt) {
sApptLoad = appt.toString();
apptListLoad.add(sApptLoad);
}
// Saves the array list to a text file
PrintWriter writer;
public void save(String string) {
try {
writer = new PrintWriter(new FileWriter(string + ".txt"));
for (String str : sApptList) {
writer.write(str);
writer.write(System.getProperty( "line.separator" ));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}
}
FileReader fileRead;
BufferedReader buffRead;
Scanner input;
String txt;
GregorianCalendar gDate;
String type;
DateFormat dateFormat;
java.util.Date date;
String sDate;
File file;
public void load(String string) {
file = new File(string + ".txt");
try {
gDate = new GregorianCalendar();
input = new Scanner(file);
fileRead = new FileReader(file);
buffRead = new BufferedReader(fileRead);
dateFormat = new SimpleDateFormat("yyyy MM dd");
ArrayList<String> listReentry = new ArrayList<String>();
String currentLine;
buffRead = new BufferedReader(new FileReader(file));
while ((currentLine = buffRead.readLine()) != null) {
//System.out.println(currentLine);
listReentry.add(currentLine);
}
//System.out.println(listReentry.size());
int count = listReentry.size();
while(count>=0){
type = listReentry.get(0);
txt = listReentry.get(1);
sDate = listReentry.get(2);
if (type.contains(" ")) {
type = type.replace(" ", "");
}
if (txt.contains(" ")) {
txt = txt.replace(" ", "");
}
if (sDate.contains(" ")) {
sDate = sDate.replace(" ", "");
}
type = type.substring(18);
txt = txt.substring(13);
//System.out.println("sDate:"+sDate);
sDate = sDate.substring(15);
try {
//System.out.println("sDate:"+sDate);
date = dateFormat.parse(sDate);
} catch (ParseException e) {
e.getErrorOffset();
e.printStackTrace();
}
gDate.setTime(date);
if (type.equals(DAILY)) {
Daily daily = new Daily(type, txt, gDate);
addAppt(daily);
} else if (type.equals(MONTHLY)) {
Monthly monthly = new Monthly(type, txt, gDate);
addAppt(monthly);
} else if (type.equals(ONE_TIME)) {
OneTime OneTime(type, txt, gDate);
addAppt(oneTime);
} else {
System.out.println("...Oops?");
}
count -=3;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (buffRead != null) {
buffRead.close();
}
if (fileRead != null) {
fileRead.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
//System.out.println(type);
}
public String getsApptLoad() {
return sApptLoad;
}
}
Appointment.java
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
// Has to be of 3 subclasses
public abstract class Appointment extends AppointmentBook {
/*
* Variables:
*/
// textual description of event
private String txt;
// date of event
private GregorianCalendar date;
// Constructor:
public Appointment(String txt, GregorianCalendar date) {
this.txt = txt;
this.date = date;
}
// Methods:
// Test three different one before, after, and on given date
public void occursOn(int year, int month, int day) {
GregorianCalendar compDate = new GregorianCalendar(year, month, day);
if (date.equals(compDate)) {
System.out.println("The date is on the date given");
} else if (compDate.after(date)) {
System.out.println("The date is before the date given.");
} else {
System.out.println("The date is after the date given.");
}
}
SimpleDateFormat f = new SimpleDateFormat("yyyy MM dd");
String sDate = "";
// toString method
public String toString(){
sDate = f.format(date.getTime());
return " Description: " + txt + " Date Of Event: " + sDate;
}
// Getters and Setters:
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
public GregorianCalendar getDate() {
return date;
}
public void setDate(GregorianCalendar date) {
this.date = date;
}
}
OneTime.java
//OneTime class(Monthly and Daily look the same just with monthly and daily variables instead because I do not know how to make them repeat indefinitely):
import java.util.GregorianCalendar;
public class OneTime extends Appointment {
// Variables:
GregorianCalendar GregorianCalendar();
// Constructor:
public OneTime(String type, String txt, GregorianCalendar date){
super(txt, date);
type = ONE_TIME;
}
// toString method
String sType = "";
public String toString(){
sType = "One Time";
return "Appointment Type: " + sType + super.toString();
}
}
Daily.java
import java.util.GregorianCalendar;
public class Daily extends Appointment {
// Variables:
GregorianCalendar daily = new GregorianCalendar();
// Constructor:
public Daily(String type, String txt, GregorianCalendar date){
super(txt, date);
type = DAILY;
}
// toString method
String sType = "";
public String toString(){
sType = "Daily";
return "Appointment Type: " + sType + super.toString();
}
}
Monthly.java
import java.util.GregorianCalendar;
public class Monthly extends Appointment {
// Variables:
GregorianCalendar monthly = new GregorianCalendar();
// Constructor:
public Monthly(String type, String txt, GregorianCalendar date){
super(txt, date);
type = MONTHLY;
}
// toString method
String sType = "";
public String toString(){
sType = "Monthly";
return "Appointment Type: " + sType + super.toString();
}
}
Tester.java
package appiotment;
import java.util.GregorianCalendar;
public class Tester {
public static void main(String[] args) {
GregorianCalendar gDate = new GregorianCalendar();
gDate.set(2017, 3, 10);
//System.out.println(gDate.getTime());
AppointmentBook ab = new AppointmentBook();
Monthly m1 = new Monthly("Monthly", "Workout", gDate);
Daily d1 = new Daily("Daily", "Eat lunch", gDate);
OneTime OneTime("One Time", "Skydiving", gDate);
Monthly m2 = new Monthly("Monthly", "Visit grandma", gDate);
Daily d2 = new Daily("Daily", "Sleep", gDate);
ab.addAppt(m1);
ab.addAppt(d1);
ab.addAppt(one1);
ab.addAppt(m2);
ab.addAppt(d2);
ab.save("appointments1");
ab.load("appointments1");
ab.getsApptLoad();
}
}
Output file:
Appointment Type: Monthly
Description: Workout
Date Of Event: 2017 04 10
Appointment Type: Daily
Description: Eat lunch
Date Of Event: 2017 04 10
Appointment Type: One Time
Description: Skydiving
Date Of Event: 2017 04 10
Appointment Type: Monthly
Description: Visit grandma
Date Of Event: 2017 04 10
Appointment Type: Daily
Description: Sleep
Date Of Event: 2017 04 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.