def createPreferences(self): global preferences #brings the global list in to the local scope personFlag = False #flag to recognise if the person has been identified drinkFlag = False #flag to recognise if the drink has been identified allPeople = db.returnPeople( ) #calls method to get attributes of all people, assigns to var 'allPeople' allDrinks = db.returnDrinks( ) #calls method to get attributes of all drinks, assigns to var 'allDrinks' personInput = input("What is your name?: ") for peopleRow in allPeople: #iterates through all saved people if peopleRow[ 0] == personInput: #checks if the given name is found in the database personFlag = True #since name has been found, change flag to True drinkInput = input("What is your preffered drink?: ") for drinkRow in allDrinks: #iterates through all saved drinks if drinkRow[ 0] == drinkInput: #checks if the given drink is in the database drinkFlag = True newPreference = Preferences( personInput, drinkInput ) #creates preference object with given name and drink preferences.append( newPreference) #adds the object to the global list print("New preference added.") if personFlag == False: #checks if person was found print("You were not found on our system. Please register. ") if drinkFlag == False and personFlag == True: #checks if drink was found print("Drink is not in stock, sorry!") clearScreen()
def insert_db_drinks(self): connection = psycopg2.connect( host= "localhost", #connects to the local database with correct credentials user="******", password="******", database="analysis", port="5432") newDrink = DrinkObject.createNewDrink( ) #calls function which creates new 'Drink' object, and assigns to 'newDrink' value = newDrink.returnDrinkProperties( ) #calls function which breaks down the object in to readable strings try: with connection.cursor() as cursor: sql = ( "INSERT INTO drinks VALUES ('%s','%s','%s','%s','%s');" % (value[0], value[1], value[2], value[3], value[4])) cursor.execute(sql) except Exception as e: print("Error: %s" % (e)) connection.commit() connection.close() #print("closed") print("Drink data created & uploaded") clearScreen()
def insert_db_person(self): connection = psycopg2.connect( host= "localhost", #connects to the local database with correct credentials user="******", #superuser password="******", database="analysis", port="5432") #database is listening on this port newPerson = PersonObject.createNewPerson( ) #calls function which creates new 'Person' object, and assigns to 'newPerson' value = newPerson.returnPersonProperties( ) #calls function which breaks down the object in to readable strings try: #will attempt to run indented code with connection.cursor( ) as cursor: #cursor object is used to commuicate with the database sql = ( "INSERT INTO people VALUES ('%s','%s','%s','%s','%s','%s');" % (value[0], value[1], value[2], value[3], value[4], value[5]) ) #sql query, inserts vals from readable string cursor.execute(sql) #executes the query on the database except Exception as e: #error handling - returns error to the user print("Error: %s" % (e)) connection.commit() #pushes the changes made to the database connection.close() #closes the connection to the database print("Person data created & uploaded") clearScreen() #calls clear screen function - QoL function
def printPreferences(self): global preferences for entry in preferences: #grabs names and drinks name = entry.name #from each entry in drink = entry.drink #the preferences list print("%s's preffered drink is %s. \n" % (name, drink)) clearScreen()
def isRoundActive(self): global roundFlag if roundFlag == True: print("Rounds are available!") else: print("Rounds are not available!") clearScreen()
def createNewPerson(self): nameInput = input("What is the person's name?: ") ageInput = int(input("What is the person's age?: ")) genderInput = input("What is the person's gender?: ") eyeInput = input("What is the person's eye colour?: ") hairInput = input("What is the person's hair colour?: ") occInput = input("What is the person's occupation?: ") newPerson = Person(nameInput, ageInput, genderInput, eyeInput, hairInput, occInput) #creates new person clearScreen() return newPerson
def print_db_drinks(self): os.system("cls") page_count = 1 #var used to count how many of the drinks have been printed connection = psycopg2.connect( host="localhost", #connects to database with user="******", #correct credentials password="******", database="analysis", # port="5432") # try: with connection.cursor() as cursor: sqlQuery = "SELECT * FROM drinks;" cursor.execute(sqlQuery) drinkRecords = cursor.fetchall() for row in drinkRecords: if row[1] == True: #changes values from a boolean r1 = "Yes" #into a more user-friendly and else: #readable 'Yes' or 'No' r1 = "No" # if row[2] == True: # r2 = "Yes" # else: # r2 = "No" # print("================================") print("Name: %s" % (row[0])) print("Fizzy?: %s" % (r1)) print("Alcoholic?: %s" % (r2)) print("Colour: %s" % (row[3])) print("Measurement: %s" % (row[4])) print("================================") print("(Drink #%d out of %d)" % (page_count, len(drinkRecords))) print("\n") page_count += 1 clearScreen() except Exception as e: print("Error occured: %s" % (e)) finally: connection.close() print("All drinks printed.") print("Connection Closed.") clearScreen()
def print_db_person(self): os.system("cls") page_count = 1 connection = psycopg2.connect(host="localhost", user="******", password="******", database="analysis", port="5432") try: with connection.cursor() as cursor: sqlQuery = "SELECT * FROM people;" #sql query that returns all data from 'people' table cursor.execute(sqlQuery) peopleRecords = cursor.fetchall( ) #grabs all the results returned from the query for row in peopleRecords: #iterates through all results, with row being a record print("================================") print("Name: %s" % (row[0])) print("Age: %s" % (row[1])) print("Gender: %s" % (row[2])) print("Eye Colour: %s" % (row[3])) print("Hair Colour: %s" % (row[4])) print("Occupation: %s" % (row[5])) print("================================") print("(Person #%d out of %d)" % (page_count, len(peopleRecords))) print("\n") page_count += 1 clearScreen() except Exception as e: print("Error occured: %s" % (e)) finally: #the last step which ALWAYS happens even if error occurs connection.close() #closes connection to database print("All users printed.") print("Connection Closed.") clearScreen()
def createNewDrink(self): nameInput = input("What is the drink called?: ") fizzyInput = input( "Is the drink fizzy? Y/N?: ") #changes output from "Y" if fizzyInput == "Y": #or "N" to Boolean fizzyInput = True # else: #True or False is more fizzyInput = False #readable to user alcoholInput = input( "Is the drink alcholic? Y/N: ") #changes output from "Y" if alcoholInput == "Y": #or "N" to Boolean alcoholInput = True # else: #True or False is more alcoholInput = False #readable to user colourInput = input("What is the drinks's colour?: ") measureInput = input("What is the measure of the drink?: ") newDrink = Drink(nameInput, fizzyInput, alcoholInput, colourInput, measureInput) clearScreen() return newDrink
def createRound(self): global roundFlag if roundFlag == False: #checks to see if rounds are being acceptd print("We are currently not excepting rounds, sorry!") clearScreen() else: localFlag = True #flag to check if user is done adding to the round allPeople = db.returnPeople()#grabs all people info from database allDrinks = db.returnDrinks()#grabs all drink info from database roundNameList =[]#creates empty lists to be used later roundDrinkList =[]# while localFlag == True: personFlag = False#flags to confirm if name provided matches drinkFlag = False #data from database roundName = input("What is your name?: ") for peopleRow in allPeople:#iterates through all people data if peopleRow[0] == roundName:#checks if person name matches input personFlag = True#flag changes as name matched a data entry roundNameList.append(roundName) roundDrink = input("What drink do you want?: ") for drinkRow in allDrinks:#checks if drink name matches input if drinkRow[0] == roundDrink: drinkFlag = True#flag changes as name matched a data entry roundDrinkList.append(roundDrink) userInput = input("\nDo you wish to enter more? Y/N: ") if userInput == "Y": pass #method begins again from the start else: localFlag = False#method doesnt reiterate as flag is now false clearScreen() print("\nThis is your round: \n") for i in range(0,len(roundNameList)): print("%s has ordered a %s." %(roundNameList[i], roundDrinkList[i])) if personFlag == False or drinkFlag == False: localFlag = False if personFlag == False:#person was not found print("You are not on our system. Please sign up.") localFlag = False#makes sure method doesnt reiterate and ends if drinkFlag == False and personFlag == True:#person was found, drink was not found print("That drink is not stocked, sorry!") localFlag = False#makes sure method doesnt reiterate and ends print("") clearScreen()