##Year 2, Term 2 - Programming challenge##
##To be completed##


def get_data(): 
##Student to add comments to explain what this function does.##
    fileToUse = 'SampleMembershipData.txt'
    rawData = open(fileToUse, "r")
    inputData = rawData.readlines()
    rawData.close()

    #???
    loyaltyScheme = []
    index = 0
    for line in inputData:
        loyaltyScheme.append(inputData[index].split(","))
        index += 1

        #???
        for record in loyaltyScheme: 
                record[4] = int(record[4])
                record[5] = int(record[5])

    return loyaltyScheme
##endfunction##


def menu():
##Displays the menu and allows users to make a selection.##
    again = True
    while again == True:
        print("\n____________Cygnet Hotel Group Loyalty Scheme___________\n\n")
        print("MENU\n")
        
        print("Option 1\tAdd a new member\n") 
        print("Option 2\tDelete a member\n") 
        print("Option 3\tDisplay a member's status\n")
        print("Option 4\tMakea booking\n")
        print("Option 5\tDRedeem points\n")
        print("Option 6\tDisplay scheme status\n")
        print('Press Q to quit the menu\n')

        optionsChoice = input('Please enter an option (1, 2, 3, 4, 5, 6 or Q): ')
        ##Student to write selection statement to handle each of the seven options.##

##endfunction##


def return_to_menu():
##Prompts to return to menu after one of the options has been selected.##
    backToMenu = False
    print("")
    while backToMenu == False:
        choice = input("Enter Y to return to menu. ")
        if choice.upper() == "Y":
            backToMenu = True          
##endfunction##


def display_record(memberRecord):
##Student to explain what this function does.##
        print("Member ID:", memberRecord[0])
        print("Surname:", memberRecord[1])
        print("Year joined:", memberRecord[2])
        print("Current membership status:", memberRecord[3])
        print("Number of nights booked:", memberRecord[4])
        print("Current points balance:", memberRecord[5])
##endfunction##


def get_member_details(member):
##Student to explain what this function does and how it works.##
    memberRecord = []

    found = False
    index = 0
    while found == False and index < len(loyaltyScheme): 
            if loyaltyScheme[index][0] == member:
                    found = True
            else:
                    index += 1
    if not found:
        print("The member ID you entered does not exist.")
    else:
        memberRecord = loyaltyScheme[index]
        display_record(memberRecord)
        
    return(memberRecord)
##endfunction##

           
def add_new_member():
##Adds a new member to the loyalty scheme.##
    print("\n____________Cygnet Hotel Group Loyalty Scheme___________\n")
    print("\n1: Add a new member to the scheme")
    print("_" * 34,'\n')
    surname = input("Enter surname: ")
    
    now = datetime.datetime.now() ##Studentsto explain these three line of code. ##
    yearJoined = str(now.year)
    year = yearJoined[2:]
    
    membershipStatus = 'Silver'
    nightsBooked = 0
    pointsBalance = 0

    #Constructs memberID
    if len(surname) == 1: ##Student to explain what this section of code does.##
        first3Letters = surname + "ZZ"
    elif len(surname) == 2:
        first3Letters = surname + "Z"
    else:
        first3Letters = surname[:3]
        
    global nextNumber ##Student to supply explanation##
    memberID = first3Letters + str(nextNumber) + year
    nextNumber = nextNumber + 1
    
    newMember = [memberID, surname, yearJoined, membershipStatus, nightsBooked, pointsBalance]
    #Creates the new record
    display_record(newMember) ##Student to explain what the display_record function is used for.##

   ##Student to write the missing code to append the new record to loyaltyScheme.##

    return_to_menu()
##endfunction##


def delete_a_member():
##Student to write the  code for this function.##

    return_to_menu()
##endfunction##


def display_member_status():
##Displays a member's record.##
    print("\n____________Cygnet Hotel Group Loyalty Scheme___________\n")
    print("\n4. Show member status")
    print('_' * 33,'\n')
    whichMember = input("Enter the member's ID number: ")
    memberRecord = get_member_details(whichMember)

    return_to_menu()
##endfunction##


def make_a_booking():
##Student to write the  code for this function.##

    return_to_menu()
##endfunction##


def redeem_points(): 
    ##Student to write the  code for this function.##

    return_to_menu()
##endfunction##
                      

def display_scheme_status(): 
    ##Student to write the  code for this function.##

    return_to_menu()
##endfunction##


def quit_program():
##Save file and exits the program.##
    updatedFile = open(latestFile, "w")
    #Converts each record to CSV string and saves to file
    for record in loyaltyScheme:
            str_data = ','.join(str(item) for item in record)
            str_data = str_data + "\n"
            updatedFile.write(str_data)
    updatedFile.close() 
    print("Goodbye")
##endfunction##                      

###MAIN PROGRAM###
import datetime
nextNumber = 800
loyaltyScheme = get_data()
menu()


