def buy_ticket(usr, tickets): title = input("Event title: ") numPurchased = int(input("Tickets required: ")) seller = input("Enter seller name: ") key = getKey(title, seller) if numPurchased > 4: raise ValueError("Only allowed 4 tickets per transaction.") if key not in tickets: raise ValueError("No such event.") event = tickets[key] if event['num'] < numPurchased: raise ValueError("Not enough tickets for sale.") totalCost = numPurchased * event['price'] confirm = input("Total cost: [${}]. Continue [Y][N]: ".format(totalCost)) if confirm == 'Y': # update all records usr.chargeCredit(totalCost) event['num'] -= numPurchased tickets[key] = event trans = formatTransaction(BUY, event) return res(trans, "Purchased tickets.") else: print("Purchase cancelled.")
def refund_credit(usr, accs): fromUser = input("Refund from [username]: ") if fromUser not in accs: raise ValueError("No such user.") toUser = input("Refund to [username]: ") if toUser not in accs: raise ValueError("No such user.") amount = Decimal(input("Amount to refund: ")) # instanciate users seller = accs[fromUser] buyer = accs[toUser] # charge/credit accounts seller.chargeCredit(amount) buyer.addCredit(amount) refundJSON = { 'from': seller.username, 'to': buyer.username, 'credit': amount } trans = formatTransaction(REFUND, refundJSON) return res(trans, "Refunded credit to user.")
def session_new(usr, accs): if usr != None: raise ValueError("User already logged in.") username = input("Enter username: "******"User not found.") user = accs[username] return res(user, 'Logged in.')
def delete_user(usr, accs): username = input("User to delete: ") if usr.username == username: raise ValueError("Cannot delete yourself.") if username not in accs: raise ValueError("User doesn't exist.") deletedUser = accs[username] del accs[username] trans = formatTransaction(DELETE, deletedUser.asJSON()) return res(trans, "Deleted user.")
def new_user(usr, accs): username = input("New username: "******"Username must be 4 to 15 characters.") if username in accs: raise ValueError("Username already exists.") userType = input("Type [AA, FS, SS, BS]: ") if userType not in accountTypes: raise ValueError("Invalid account type.") newUser = User(username, userType) accs[username] = newUser trans = formatTransaction(CREATE, newUser.asJSON()) return res(trans, "Created new user.")
def add_credit(usr, accs): trans = '' credit = Decimal(input("Enter credit amount: ")) if credit > 1000: raise ValueError("Can only add $1000 per transaction.") if usr.type == AA: username = input("Enter target username: "******"User does not exist.") toUser = accs[username] toUser.addCredit(credit) trans = formatTransaction(ADD_CREDIT, toUser.asJSON()) else: usr.addCredit(credit) trans = formatTransaction(ADD_CREDIT, usr.asJSON()) return res(trans, "Added credit to user.")
def sell_ticket(usr, tickets): title = input("Event title: ") if len(title) < 4 or len(title) > 25: raise ValueError("Title must be 4 to 25 characters.") numAvailable = int(input("Tickets available: ")) if numAvailable > 100: raise ValueError("Max number of tickets is 100.") price = Decimal(input("Event price (00.00): ")) if price < 0 or price > 999.99: raise ValueError("Price must be 0.00 to 999.99.") newTicket = getTicketJSON(title, usr.username, numAvailable, price) key = getKey(title, usr.username) tickets[key] = newTicket trans = formatTransaction(SELL, newTicket) return res(trans, "Added new tickets for sale.")
def session_end(usr, accs, trans, tickets): # write accs file f = open(filenames.ACCOUNT_FILE, 'w') for key, value in accs.items(): f.write(getUserFromJSON(value.asJSON()) + '\n') f.write('END') f.close() logout = formatTransaction(LOGOUT, usr.asJSON()) trans.append(logout) # write transactions file f = open(filenames.TRANSACTION_FILE, 'w') for value in trans: f.write(value + '\n') f.close() # write tickets file f = open(filenames.TICKET_FILE, 'w') for key, value in tickets.items(): f.write(getTicketFromJSON(value) + '\n') f.write('END') f.close() return res(None, 'Session finished.')