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
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 ONE_TIME = "One Time"; public static final String DAILY = "Daily"; public static final String MONTHLY = "Monthly"; ArrayList
// 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
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 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 = new 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 oneTime = new 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 one1 = new 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 projectStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started