You are on page 1of 4

class ReminderEntry:

def __init__(self, tstamp=0, cname="", desc=""):


self.timestamp = tstamp
self.coursename = cname
self.description = desc

def edit(self):
print("Editing an entry, leave blank to skip editing a part")

course = input("Enter the name of a course: ")


if course != "":
self.coursename = course

description = input("Enter the description of an assignment: ")


if description != "":
self.description = description

time = input("Do you wish to edit the time? Enter anything - yes, leave
blank - no: ")
if time != "":
time_correct = False
while not time_correct:
time = input("Enter the time of the deadline in 23:59 format: ")
try:
hours, minutes = int(time.split(':')[0]), int(time.split(':')
[1])
if (hours >= 0 and hours <= 23) and (minutes >= 0 and minutes
<= 59):
time_correct = True
except:
print("Invalid input!")

day_correct = False
while not day_correct:
try:
day = int(input("Enter the day of the deadline (1-31): "))
if day >= 1 and day <= 31:
day_correct = True
except:
print("Invalid input!")

month_correct = False
while not month_correct:

try:
month = int(input("Enter the month of the deadline (1-12): "))
if month >= 1 and month <= 12:
month_correct = True
except:
print("Invalid input!")

self.timestamp = minutes + hours * 60 + day * 24 * 60 + month * 31 * 24


* 60
# EndTimeIF

def format_print(self):
# TODO Print formatted version of an entry
minutes = self.timestamp % 60
hours = int((self.timestamp / 60) % 24)
day = int(((self.timestamp / 60) / 24) % 31)
month = int(((self.timestamp / 60) / 24) / 31)
#print(self.coursename)
print("Deadline:","{:02d}:{:02d} {:02d}/{:02d}".format(hours, minutes, day,
month))
#print(self.description)

def csv_print(self):
return str(self.timestamp) + "," + self.coursename + "," + self.description

# END OF CLASS

def add_entry():
# Handles adding new entries
course = input("Enter the name of a course: ")
description = input("Enter the description of an assignment: ")

time_correct = False
while not time_correct:
time = input("Enter the time of the deadline in 23:59 format: ")
try:
hours, minutes = int(time.split(':')[0]), int(time.split(':')[1])
if (hours >= 0 and hours <= 23) and (minutes >= 0 and minutes <= 59):
time_correct = True
except:
print("Invalid input!")

day_correct = False
while not day_correct:
try:
day = int(input("Enter the day of the deadline (1-31): "))
if day >= 1 and day <= 31:
day_correct = True
except:
print("Invalid input!")

month_correct = False
while not month_correct:

try:
month = int(input("Enter the month of the deadline (1-12): "))
if month >= 1 and month <= 12:
month_correct = True
except:
print("Invalid input!")

timestamp = minutes + hours * 60 + day * 24 * 60 + month * 31 * 24 * 60


ent = ReminderEntry(timestamp, course, description)
entries.append(ent)
entries.sort(key=lambda entry: entry.timestamp)
print("New entry added successfully!")

def search_entries(coursename):
print("Entries found:")
has_result = False
for index, entry in enumerate(entries):
if entry.coursename == coursename:
print("Index: [{}]\n".format(index + 1), end="")
print("Coursename:", entry.coursename)
print("Course description:", entry.description)
entry.format_print()
print("----------------------------------")
has_result = True
else:
return has_result
def edit_entries():
course_name = input("Enter the subject of the entry to be edited\n")
if search_entries(course_name):
try:
delindex = int(input("Enter the index of an entry you want to edit: "))
delindex -= 1
if delindex in range(0, len(entries)):
entries[delindex].edit()
entries.sort(key=lambda entry: entry.timestamp)
except:
print("Index is invalid!")
return
else:
print("No entries found")

def get_entries():
print("Your notebook entries:")
for index, entry in enumerate(entries):
print("Index: [{}]\n".format(index + 1), end="")
print("Coursename:", entry.coursename)
print("Course description:", entry.description)
entry.format_print()
print("----------------------------------")

def delete_entry():
coursename = input("Enter the subject of the entry to be edited\n")
if search_entries(coursename):
try:
delindex = int(input("Enter the index of an entry you want to delete:
"))
delindex -= 1
if delindex in range(0, len(entries)):
cmd = input("Do you want to delete this entry?\n {}\ny - confirm.
".format(entries[delindex].coursename))
if cmd == "y":
del entries[delindex]
print("Deletion complete!")
entries.sort(key=lambda entry: entry.timestamp)
except:
print("Index is invalid!")
return
else:
print("No entries found")

def print_help():
print("List of commands:")
print("n - add a new entry")
print("g - get a list of entries")
print("e - edit an entry")
print("d - delete an entry")
print("q - quit the program")
def save_file():
datafile = open("database.csv", "w")
datafile.write("timestamp,coursecode,description\n")
for entry in entries:
datafile.write(entry.csv_print() + "\n")
datafile.close()

print("Welcome to The Deadline Fighter Notebook!")


print("Loading the database...")
# Contents of the previous file, needed to be saved for editing and overwriting
oldlines = []
entries = []

try:
datafile = open("database.csv", "r")
print("Database loaded successfully!")
for l in datafile:
oldlines.append(l.rstrip('\n'))
datafile.close()
for i in oldlines[1:]:
x, y, z = i.split(',')
entries.append(ReminderEntry(int(x), y, z))
except:
print("Database file could not be accessed. Creating a new database file...")

prompt = ''

while prompt != 'q':


prompt = input("Enter a command or 'h' to list all commands\n")
if prompt == 'h':
print_help()
elif prompt == 'n':
add_entry()
elif prompt == 'g':
get_entries()
elif prompt == 'd':
delete_entry()
elif prompt == 'e':
edit_entries()
elif prompt == 'q':
pass
else:
print("Invalid input!")
else:
prompt = input("You may have unsaved changes, enter 's' to save or press Enter
to quit without saving\n")
if prompt == 's':
save_file()

print("Thank you for using The Deadline Fighter Notebook!")

You might also like