Commit 4d0a627a authored by Jasmin Jutila's avatar Jasmin Jutila
Browse files

Merge branch 'k' into 'master'

FINAL COMMIT 7.5.2020

See merge request !3
parents 5bec5e3d d5ec266b
import Customer
from Customer import *
import ReadingCustomers
import WritingCustomers
import random
import re
#Asking user for all needed data to create a new customer
def new_customer_datainput():
#firstname
print("Type first name: ")
firstname = input()
if firstname.isalpha(): #checking that name input is all letters
i = True
else:
i = False
while i == False:
print("Name has to be a string. Try again.")
print("Type first name: ")
firstname = input()
if firstname.isalpha():
i = True
else:
i = False
#lastname
print ("Type last name: ")
lastname = input()
if lastname.isalpha(): #checking that name input is all letters
i = True
else:
i = False
while i == False:
print("Name has to be a string. Try again.")
print("Type last name: ")
lastname = input()
if lastname.isalpha():
i = True
else:
i = False
#customerid
id = random.randint(100,999) #giving random customer id
while check_customer_id(id) == False: #check its not already in use
id = random.randint(100,999)
check_customer_id(id)
print("Your customer id is: "+str(id)+". Please write it down as you will need it to make reservations.")
#gender
print("Do you identify as Man, Women or Other?")
gender = input()
while gender not in {"Man", "Women", "Other"}:
print("Please insert Man, Women or Other:")
gender = input()
#email address
print("Type email address. It will be used to communicate with you. :")
email = input()
while check_email(email) == False: #check email address is valid
print("Invalid email. Please insert a valid email address:")
email = input()
check_email(email)
new_customer = Customer(firstname, lastname, id, gender, email) #creating a new customer
return new_customer
#Checking if customer id is already in customer database
def check_customer_id(id):
data = ReadingCustomers.reading_customers()
if len(data) == 0:
return True
for i in range(len(data)):
if data[i].id == id:
return False
else:
return True
#save customer, so write it in a csv file
def save_customer(Customer):
WritingCustomers.write_new_customer(str(Customer.firstname),str(Customer.lastname),str(Customer.id),str(Customer.gender),str(Customer.email))
#check email address is valid
def check_email(email):
regex = '^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$'
if (re.search(regex,email)):
return True
else:
return False
from Reservation import *
from ReadingReservations import *
from ReadingCustomers import *
from WritingCustomers import *
from CreateCustomer import *
import Reservation
import ReadingReservations
import ReadingCustomers
import WritingReservations
import CreateCustomer
import datetime
import sys
import re
#Check customer input that is needed for a new reservation
def new_reservation_datainput():
#date of reservation
print("Which date would you like to reserve for? Enter the date in format 'ddmmyyyy' :")
date = input()
day = date[0:2]
month = date[2:4]
year = date[6:8]
year_2 = date[4:8]
try: #check input valid
datetime.datetime(int(year),int(month),int(day)) #check input is a date overall
i = True
date_reservation = datetime.datetime(int(year_2),int(month),int(day))
date_today = datetime.datetime.today()
if date_reservation > date_today: #check input date is in the future
j = True
else:
j = False
except ValueError:
i = False
j = False
while i == False or j == False:
if j == False and i == False:
print("Input date is not valid. Try again.")
elif j == False and i == True:
print("Reservation date has to be in the future.")
else:
print("Input date is not valid. Try again.")
print("Enter the date in format 'ddmmyyyy' :")
date = input()
day = date[0:2]
month = date[2:4]
year = date[6:8]
year_2 = date[4:8]
try:
datetime.datetime(int(year),int(month),int(day))
i = True
date_reservation = datetime.datetime(int(year_2),int(month),int(day))
date_today = datetime.datetime.today()
if date_reservation > date_today:
j = True
else:
j = False
except ValueError:
i = False
j = False
#reservation time
print ("Enter reservation time; format hhmm (24h clock). Length of reservation is always 60 minutes. :")
time = input()
#not the smartest way to check input, but clear and simple
while time not in {'0700','0800','0900','1000','1100','1200','1300','1400','1500','1600','1700','1800','1900','2000','2100'}:
print("Invalid time. Reservation must start at even hours. Try again.")
print ("Type time; format hhmm (24h clock). Length of reservation is always 60 minutes.")
time = input()
# reservation length is predetermined, and is told to the customer in previous function
length = 60
#customer ID
print("Type your Customer id. If you don't have a customer id type 'no id' :")
id = input()
if id.isalpha() == True: #checking that id input is not a string
i = True
while i == True:
print("ID has to be a number. Try again. If you don't have a customer id type 'no id' :")
id = input()
if id.isalpha():
i = True
else:
i = False
if id == 'no id': #If customer doesn't yet have one
print("Do you want to add a new customer?")
vastaus_2 = input()
while vastaus_2 not in {"Yes", "No" }:
print("Please answer Yes or No.")
vastaus_2 = input()
if vastaus_2 == ("Yes"): #Creating a new customer account
new_customer = CreateCustomer.new_customer_datainput()
CreateCustomer.save_customer(new_customer)
id = new_customer.id
elif vastaus_2 == ("No"): #Rservation cannot be added without valid ID
print("Reservation cannot be added without Customer ID. Quitting reservation system. Please contact customer service")
sys.exit()
else:
if check_customer_id(id) == False:
j = True
else:
j = False
while j == False: #If customer id cannot be found from our customer database
print("The customer id is not in our database. Did you mean to write: "+id+"? ")
confirmation = input()
while confirmation not in {"Yes", "No"}:
print("Please answer Yes or No:")
confirmation = input()
if confirmation == ("Yes") : #Adding new customer
print("The customer id is not in our database. Do you want to add a new customer")
vastaus_2 = input()
while vastaus_2 not in {"Yes", "No"}:
print("Please answer Yes or No.")
vastaus_2 = input()
if vastaus_2 == ("Yes"):
new_customer = CreateCustomer.new_customer_datainput()
CreateCustomer.save_customer(new_customer)
id = new_customer.id
break
else:
print("Reservation cannot be added without Customer ID. Quitting reservation system. Please contact customer service")
sys.exit()
else:
print("Please rewrite your customer id, remember customer id is a number between 100-999:")
id = input()
if check_customer_id(id) == False:
j = True
break
else:
print("The customer id "+id+" is still not in our database. Please contact customer service or do you want to add a new customer?")
vastaus_2 = input()
while vastaus_2 not in {"Yes", "No"}:
print("Please answer Yes or No.")
vastaus_2 = input()
if vastaus_2 == ("Yes"):
new_customer = CreateCustomer.new_customer_datainput()
CreateCustomer.save_customer(new_customer)
id = new_customer.id
break
else:
print("Reservation cannot be added without Customer ID. Quitting reservation system. Please contact customer service")
sys.exit()
#Field
field = input("Which field would you like to reserve? (give field number):\n")
while field not in {'1','2','3','4','5'}:
print("Error. Insert field number between 1-5:")
field = input()
#saving all the data gathered
new_reservation = Reservation.Reservation(date,time,length,id,field)
return new_reservation
#Check availability of reservation, so are there previous reservations at the dame date, time and field
def check_availability(Reservation):
data = ReadingReservations.reading_reservations()
if len(data) == 0:
return True
for i in range(len(data)):
if int(data[i].date) == int(Reservation.date):
if int(data[i].time) == int(Reservation.time) and int(data[i].field) == int(Reservation.field):
return False
else:
pass
else:
return True
#save reservation to an external csv file for safekeeping
def save_reservation(Reservation):
if check_availability(Reservation) is True:
WritingReservations.write_new_reservation(str(Reservation.date),str(Reservation.time),str(Reservation.length),str(Reservation.id),str(Reservation.field))
#Checking if customer id is in our database
def check_customer_id(id): #Palauttaa True jos ei löydy, False jos löytyy
data = ReadingCustomers.reading_customers()
if len(data) == 0:
return True
try:
for i in range(len(data)):
if int(id) == int(data[i].id):
return False
else:
return True
except ValueError:
return True
#Creating the customer class
class Customer():
def __init__(self,firstname,lastname,id,gender,email):
self.firstname = firstname
self.lastname = lastname
self.id = id
self.gender = gender
self.email = email
self.history = []
def get_firstname(self):
return self.firstname
def get_lastname(self):
return self.lastname
def get_id(self):
return self.id
def get_gender(self):
return self.gender
def get_email(self):
return self.email
#def add_to_history(self):
#ADD reservation to history
\ No newline at end of file
class Field():
def __init__(self,number,price,players):
self.number = number
self.price = price
self.size = players
self.players = players
\ No newline at end of file
from Customer import *
import csv
def reading_customers():
try:
lahtotiedosto = open('customers.csv', "r")
rivilista = lahtotiedosto.readlines()
lahtotiedosto.close()
a = 1
customers = []
for rivi in rivilista:
rivi = rivi.rstrip()
osat = rivi.split(',')
a = Customer(osat[0],osat[1],osat[2],osat[3],osat[4])
customers.append(a)
a =+ 1
except OSError:
print("Virhe tiedoston lukemisessa")
return customers
\ No newline at end of file
from Field import *
import csv
def reading_fields():
try:
lahtotiedosto = open('fields.csv', "r")
rivilista = lahtotiedosto.readlines()
lahtotiedosto.close()
a = 1
fields = []
for rivi in rivilista:
rivi = rivi.rstrip()
osat = rivi.split(',')
a = Field(osat[0],osat[1],osat[2])
fields.append(a)
a =+ 1
except OSError:
print("Virhe tiedoston lukemisessa")
return fields
from Reservation import *
import csv
def reading_reservations():
try:
lahtotiedosto = open('reservations.csv', "r")
rivilista = lahtotiedosto.readlines()
lahtotiedosto.close()
a = 1
reservations = []
for rivi in rivilista:
rivi = rivi.rstrip()
osat = rivi.split(',')
a = Reservation(osat[0],osat[1],osat[2],osat[3],osat[4])
reservations.append(a)
a =+ 1
except OSError:
print("Virhe tiedoston lukemisessa")
return reservations
\ No newline at end of file
import Customer
class Reservation():
def __init__(self,date,time,length,id,field):
self.date = date
self.time = time
self.length = length
self.id = id
self.field = field
def get_date(self):
return self.date
def get_time(self):
return self.time
def get_length(self):
return self.length
\ No newline at end of file
import csv
import os
def write_new_customer(firstname,lastname,id,gender,email):
with open('customers.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow([firstname,lastname,id,gender,email])
file.flush()
os.fsync(file.fileno())
file.close()
\ No newline at end of file
import csv
with open('fields.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow([1, 60, 10])
writer.writerow([2, 100, 20])
writer.writerow([3, 20, 5])
\ No newline at end of file
import csv
import os
def write_new_reservation(date,time,length,id,field):
with open('reservations.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow([date, time, length, id, field])
file.flush()
os.fsync(file.fileno())
file.close()
\ No newline at end of file
import ReadingFields
import CreateReservation
import csv
try:
file = open('reservations.csv')
file.close()
except FileNotFoundError:
with open("reservations.csv", "a") as f:
read_file = csv.writer(f)
try:
file_2 = open('customers.csv')
file_2.close()
except FileNotFoundError:
with open("customers.csv", "a") as f:
read_file = csv.writer(f)
response = input("Do you want to add new reservation?\n")
while response not in {"Yes", "No"}:
response = input("Please answer Yes or No: \n")
while response == "Yes":
new_reservation = CreateReservation.new_reservation_datainput()
if CreateReservation.check_availability(new_reservation) is True:
CreateReservation.save_reservation(new_reservation)
print("Reservation SUCCESFULL.")
response = input("Do you want to add another new reservation?\n")
while response not in {"Yes", "No"}:
response = input("Please enter Yes or No: ")
else:
print("No availability. Reservation failed.")
response = input("Do you want to try a different date or time?\n")
while response not in {"Yes", "No"}:
response = input("Please enter yes or no: \n")
else:
print("Ok. Reservation system will close")
quit()
import unittest
import csv
import ReadingReservations
import ReadingCustomers
from CreateReservation import check_availability
class TestStringMethods(unittest.TestCase):
def test_writing_reservations(self):
with open('reservations.csv',"w") as file:
data = ReadingReservations.reading_reservations()
row_count_1 = len(data)
writer= csv.writer(file)
writer.writerow(['testi', 'testi'])
reader = csv.reader(file,delimiter = ",")
data = ReadingReservations.reading_reservations()
row_count_2 = len(data)
self.assertIs(row_count_1,row_count_2)
def test_writing_customers(self):
with open('customers.csv',"w") as file:
data = ReadingCustomers.reading_customers()
row_count_1 = len(data)
writer= csv.writer(file)
writer.writerow(['J', 'J', '130','Other', 'j.j@gmail.com'])
reader = csv.reader(file,delimiter = ",")
data = ReadingCustomers.reading_customers()
row_count_2 = len(data)
self.assertIs(row_count_1,row_count_2)
def test_check_availability(self):
with open('reservations.csv',"w") as file:
writer= csv.writer(file)
date_1 = ['20052020', '1200','60','440','1']
writer.writerow(date_1)
date_2 = ['20052021', '1200','60','440','1']
self.assertEqual(check_availability(date_2), True)
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment