Creating An Appointment Book Hello! I am having trouble with this project. I hav
ID: 3836027 • 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
I have implemented the entire code without the tester section.You can test by simple codes to the main method.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
abstract class Appointment implements Serializable {
private String eventString;
private GregorianCalendar eventDate;
public String eventFrequeny = "";
public Appointment(String eventString, GregorianCalendar eventDate) {
super();
this.eventString = eventString;
this.eventDate = eventDate;
}
public String getEventString() {
return eventString;
}
public void setEventString(String eventString) {
this.eventString = eventString;
}
public GregorianCalendar getEventDate() {
return eventDate;
}
public void setEventDate(GregorianCalendar eventDate) {
this.eventDate = eventDate;
}
@Override
public String toString() {
return "Appointment [eventString=" + eventString + ", eventDate="
+ eventDate + ", eventFrequeny=" + eventFrequeny + "]";
}
abstract public String occursOn(int year, int month, int day);
}
class OneTime extends Appointment implements Serializable {
public OneTime(String eventString, GregorianCalendar eventDate) {
super(eventString, eventDate);
super.eventFrequeny = "ONETIME";
// TODO Auto-generated constructor stub
}
@Override
public String occursOn(int year, int month, int day) {
GregorianCalendar g = new GregorianCalendar(year, month, day);
if (g.equals(super.getEventDate())) {
return "There is a event on this event!!";
} else {
return "No event exists!!";
}
// TODO Auto-generated method stub
}
}
class Daily extends Appointment implements Serializable {
public Daily(String eventString, GregorianCalendar eventDate) {
super(eventString, eventDate);
super.eventFrequeny = "DAILY";
// TODO Auto-generated constructor stub
}
@Override
public String occursOn(int year, int month, int day) {
return "Daily event will happen everyday!!";
// TODO Auto-generated method stub
}
}
class Monthly extends Appointment implements Serializable {
public Monthly(String eventString, GregorianCalendar eventDate) {
super(eventString, eventDate);
super.eventFrequeny = "MONTHLY";
// TODO Auto-generated constructor stub
}
@Override
public String occursOn(int year, int month, int day) {
GregorianCalendar g = new GregorianCalendar(year, month, day);
if (g.DAY_OF_MONTH > super.getEventDate().DAY_OF_MONTH) {
return "This Month's event has already occurred !!";
} else if (g.DAY_OF_MONTH < super.getEventDate().DAY_OF_MONTH) {
return "This Month's event will happen later !!";
} else {
return "Today is the monthly event on this month!!";
}
// TODO Auto-generated method stub
}
}
class AppointmentBook {
List<Appointment> appointmentList = new ArrayList<Appointment>();
public void addNewAppointment(String eventText,
GregorianCalendar eventDate, String eventType) {
if ("ONETIME".equals(eventType)) {
appointmentList.add(new OneTime(eventText, eventDate));
} else if ("DAILY".equals(eventType)) {
appointmentList.add(new Daily(eventText, eventDate));
} else if ("MONTHLY".equals(eventType)) {
appointmentList.add(new Monthly(eventText, eventDate));
}
}
public void saveAppointmenttoFile(String fileName) throws IOException {
FileOutputStream fs = new FileOutputStream(fileName);
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(appointmentList);
fs.close();
os.close();
}
public void loadAppointmenttoFile(String fileName) throws IOException,
ClassNotFoundException {
FileInputStream fs = new FileInputStream(fileName);
ObjectInputStream os = new ObjectInputStream(fs);
List<Appointment> fetchedList = (List<Appointment>) os.readObject();
for (Appointment x : fetchedList) {
System.out.println(x);
}
fs.close();
os.close();
}
}
public class Demo {
public static void main(String[] args) {
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.