Created
October 27, 2019 07:10
-
-
Save anasAlsalol/e1bc5016dbc5a350ee551ecdcca4261c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # check Reservation - recive id - read from file - if ok change other reject | |
| # 1- init the empty file | |
| # 2- diplay the menu | |
| # 3- while not 5 list . | |
| # python tcp client A | |
| import socket | |
| import pickle | |
| import numpy | |
| import psycopg2 | |
| import json | |
| TicPrice = 1000 | |
| heading = ["A", "B", "C", "D", "E"] | |
| SeatAvail = 40 | |
| Booked = numpy.zeros((8, 5)) | |
| Booked_Matrix = numpy.zeros((8, 5)) | |
| def DisplaySeats(): | |
| print("Bus reservation services server .......") | |
| print("No of Seats Available are ", SeatAvail) | |
| " ", | |
| for i in range(0, 5): | |
| heading[i], " ", | |
| for i in range(0, 8): | |
| i + 1, " ", | |
| for j in range(0, 5): | |
| if (Booked_Matrix[i][j] == 0): | |
| "A" + " ", | |
| elif (Booked_Matrix[i][j] == 1): | |
| "R" + " ", | |
| def BookTicket(): | |
| global SeatAvail | |
| global Booked_Matrix | |
| global Booked | |
| SeatAvail = 40 | |
| Booked = numpy.zeros((8, 5)) | |
| Booked_Matrix = numpy.zeros((8, 5)) | |
| SeatAvail = tcpClientA.recv(BUFFER_SIZE) | |
| SeatAvail = int(SeatAvail) | |
| Booked = tcpClientA.recv(BUFFER_SIZE) | |
| Booked_Matrix = pickle.loads(Booked) | |
| DisplaySeats() | |
| noSeats = input("Enter the number of seats you want to book:") | |
| tcpClientA.send(noSeats) | |
| noSeats = int(noSeats) | |
| status = tcpClientA.recv(BUFFER_SIZE) | |
| "status is ", status | |
| if (status == "1"): | |
| "The no of seats you want are available :" | |
| User_Data(noSeats) | |
| else: | |
| "The number of seats you require are not available , only ", SeatAvail, "seats are available." | |
| "Please try again with only the available seats" | |
| def User_Data(s): | |
| global SeatAvail | |
| global Booked_Matrix | |
| global Booked | |
| for i in range(1, s + 1): | |
| s_no = input("Enter the seat number you want to book (Eg: A1, E4) :") | |
| tcpClientA.send(s_no) | |
| name = input("Enter passengers name :") | |
| tcpClientA.send(name) | |
| email = input("Enter your Email ID :") | |
| tcpClientA.send(email) | |
| age = input("Enter passengers age :") | |
| tcpClientA.send(age) | |
| spq = input("Enter if You fall under Special Quota : t for yes , f otherwise :") | |
| tcpClientA.send(spq) | |
| if (spq == "t"): | |
| price = int((float(TicPrice) * (0.95))) | |
| elif (int(age) >= 60): | |
| price = int((float(TicPrice) * (0.90))) | |
| else: | |
| price = TicPrice | |
| c_no = ord(s_no[0]) - 65 | |
| r_no = int(s_no[1]) | |
| r_no = r_no - 1 | |
| Booked_Matrix[r_no][c_no] = 1 | |
| SeatAvail = SeatAvail - 1; | |
| ticketDisplay(name, age, s_no, price) | |
| "Data Updated" | |
| def CancellTicket(): | |
| DisplaySeats() | |
| name = input("Enter your Email ID :") | |
| tcpClientA.send(name) | |
| SNum = input("Enter your seat number :") | |
| tcpClientA.send(SNum) | |
| statP = tcpClientA.recv(BUFFER_SIZE) | |
| # ststP = int(statP) | |
| statP = int((float(statP) * (0.80))) | |
| "The amount of Rupees ", statP, " will be refunded" | |
| "Cancellation Succesfull" | |
| "*****************************************" | |
| "*****************************************" | |
| def history(): | |
| email = input("Enter your Email ID:") | |
| # tcpClientA.send(email) | |
| "*****************************************" | |
| "*****************************************" | |
| dbCon = None | |
| try: | |
| dbCon = psycopg2.connect(host="localhost", database="postgres", user="postgres", password="cnp") | |
| # print dbCon | |
| cur = dbCon.cursor() | |
| # print "Database Connection Open" | |
| cur.execute('SELECT * from thebus where emailId = %(some_n)s', {'some_n': email}) | |
| details = cur.fetchall() | |
| "::::::Your Booking History::::::" | |
| # print details | |
| cur.close() | |
| dbCon.commit() | |
| except (Exception, psycopg2.DatabaseError) as error: | |
| error | |
| "Invalid data , Please try again" | |
| finally: | |
| if dbCon is not None: | |
| dbCon.close() | |
| # dbCon.commit() | |
| " Name Seat Number Ticket Price" | |
| for row in details: | |
| " ", row[0], " ", row[3], " ", row[5] | |
| # print "Database connection closed." | |
| "*****************************************" | |
| "*****************************************" | |
| def ticketDisplay(name, age, s_no, price): | |
| "***********Cool Bus travels**************" | |
| "*****************************************" | |
| "*** Name: ", name | |
| "*** Age: ", age | |
| "*** SeatNumber: ", s_no | |
| "*** Price to be paid is: ", price | |
| "*****************************************" | |
| "" | |
| host = socket.gethostname() | |
| port = 2004 | |
| BUFFER_SIZE = 2000 | |
| tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| tcpClientA.connect((host, port)) | |
| flag = True | |
| while (flag): | |
| # DisplaySeats(); | |
| "Hello Welcome to Bus sevices:" | |
| "1:BOOKING" | |
| "2:CANCELLATION " | |
| "3: VIEW BOOKING HISTORY " | |
| "4:EXIT" | |
| choice = input("Enter your choice:") | |
| choice | |
| if (choice == "1"): | |
| tcpClientA.send(choice) | |
| BookTicket() | |
| elif (choice == "2"): | |
| tcpClientA.send(choice) | |
| CancellTicket() | |
| elif (choice == "3"): | |
| tcpClientA.send(choice) | |
| history() | |
| elif (choice == "4"): | |
| "THANK YOU , COME AGAIN" | |
| tcpClientA.send(choice) | |
| flag = False | |
| tcpClientA.close() | |
| # File | |
| def create(): | |
| f = open("seatfile.txt",) | |
| for i in range(len(booked_Matrix)): | |
| split = i + 1 | |
| if arra[i] == 0: | |
| f.write(0) | |
| else: | |
| f.write(1) | |
| if split % 4 == 0: | |
| f.write("\n") | |
| f.close() | |
| def read(): | |
| seat_file = open("seatfile.txt") | |
| for line in seat_file: | |
| for item in line.split(' '): | |
| print() | |
| seat_file.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment