def main(): """ The main function """ # Create two manufacturers and add models of bike pinarello = Manufacturer("Pinarello", 0.3) pinarello.add_model("TimeAttack", CarbonFrame, TimeTrialWheel) pinarello.add_model("RoadWarrior", AluminiumFrame, RoadWheel) pinarello.add_model("SanRemo", CarbonFrame, RoadWheel) trek = Manufacturer("Trek", 0.3) trek.add_model("MountainHawg", SteelFrame, MountainBikeWheel) trek.add_model("Rodeo", SteelFrame, RoadWheel) trek.add_model("Everest", CarbonFrame, MountainBikeWheel) # Create a shop which stocks products from both manufacturers bobs_cycles = Shop("Bob's Cycles", 0.2) bobs_cycles.add_manufacturer(pinarello) bobs_cycles.add_manufacturer(trek) # Buy 5 of each bike as stock for i in xrange(5): bobs_cycles.buy("Pinarello", "TimeAttack") bobs_cycles.buy("Pinarello", "RoadWarrior") bobs_cycles.buy("Pinarello", "SanRemo") bobs_cycles.buy("Trek", "MountainHawg") bobs_cycles.buy("Trek", "Rodeo") bobs_cycles.buy("Trek", "Everest") # Create three customers with different budgets alice = Customer("Alice", 200) dan = Customer("Dan", 500) carol = Customer("Carol", 1000) customers = (alice, dan, carol) # Print the weight of the bikes sold by Bob's Cycles print "Bicycle weights:" print "" for bike_specification in bobs_cycles.product_range(): print "{:40} {}kg".format(bike_specification.full_name(), bike_specification.weight) print "" # Print the bikes which each customer can afford print "Customers:" print "" for customer in customers: print "{} can afford the following bikes:".format(customer.name) print "" for bike_specification in bobs_cycles.product_range(): price = bobs_cycles.price(bike_specification.manufacturer_name, bike_specification.name) if price > customer.money: continue print bike_specification.full_name() print "" # Print the current stock levels at Bob's Cycles print "Inventory of Bob's Cycles: " print "" for bike_specification in bobs_cycles.product_range(): stock = bobs_cycles.stock(bike_specification.manufacturer_name, bike_specification.name) print "{:40} {} in stock".format(bike_specification.full_name(), stock) # Buy a bike for each customer alice.buy(bobs_cycles, "Trek", "MountainHawg") dan.buy(bobs_cycles, "Trek", "MountainHawg") carol.buy(bobs_cycles, "Pinarello", "SanRemo") print "" # Print the customer's purchases, and remaining money for customer in customers: print "{} now owns a {}".format(customer.name, customer.bike.full_name()) print "{} has ${:.2f} remaining".format(customer.name, customer.money) print "" # Print the updates stock levels print "Updated inventory of Bob's Cycles: " print "" for bike_specification in bobs_cycles.product_range(): stock = bobs_cycles.stock(bike_specification.manufacturer_name, bike_specification.name) print "{:40} {} in stock".format(bike_specification.full_name(), stock) print "" # Print the amount of profit Bob has made print "Profit for Bob's Cycles: ${}".format(bobs_cycles.profit)
class DAPP: def __init__(self, root): with open('users_pwd.pkl','rb') as f: self.pwd = pickle.load(f) self.p = Shop() self.l1 = tk.Label(root, text="Account Number") self.l1.grid(row=0, column=0) self.l2 = tk.Label(root, text="Password") self.l2.grid(row=1, column=0) self.v1 = tk.StringVar() self.v2 = tk.StringVar() self.e1 = tk.Entry(root, textvariable=self.v1) self.e1.grid(row=0, column=1, padx=10, pady=5) self.e2 = tk.Entry(root, textvariable=self.v2, show="*") self.e2.grid(row=1, column=1, padx=10, pady=5) self.b1 = tk.Button(root, text="Login", width=10, command=self.home_page) self.b1.grid(row=2, column=0, sticky=tk.W, padx=10, pady=5) self.b2 = tk.Button(root, text="Sign up", width=10, command=self.sign_up) self.b2.grid(row=2, column=1, sticky=tk.E, padx=10, pady=5) def sign_up(self): sign = tk.Toplevel() sign.title('Sign Up') tk.Label(sign, text='Account Number').grid(row=0, column=0) tk.Label(sign, text='Password').grid(row=1, column=0) v_account = tk.StringVar() v_password = tk.StringVar() self.e_account = tk.Entry(sign, textvariable=v_account) self.e_account.grid(row=0, column=1, padx=10, pady=5) self.e_password = tk.Entry(sign, textvariable=v_password) self.e_password.grid(row=1, column=1, padx=10, pady=5) tk.Button(sign, text="Submit", width=10, command=self.sign_up_verify)\ .grid(row=2, columnspan=3, sticky=tk.W, padx=80, pady=5) def sign_up_verify(self): if self.e_account.get() != '' and self.e_password.get() != '': self.p.register(self.e_account.get()) self.pwd[self.e_account.get()] = self.e_password.get() with open('users_pwd.pkl', 'wb') as f: pickle.dump(self.pwd, f) sign_up_success = tk.Toplevel() tk.Label(sign_up_success, text='Sign up Successful! You can login now')\ .grid(row=0, column=0) else: fail = tk.Toplevel() tk.Label(fail, text = 'Account number and password are requested!').grid(row=0, column=0) def show_my_pets(self): self.p.show_my_pet() def show_shelf(self): self.p.show_shelf() def home_page(self): if self.e1.get() == '' or self.e2.get() == '': empty = tk.Toplevel() tk.Label(empty, text = 'Please input your Account Number and password!')\ .grid(row=0, column=0) elif self.p.userList.get(self.e1.get(), 0): if self.e2.get() == self.pwd[self.e1.get()]: self.p.login(self.e1.get()) home = tk.Toplevel() home.title('Home') tk.Label(home, text="Hello " + self.e1.get() + "! Welcome to Pets shop!").grid(row=0, column=0, sticky=tk.W, padx=10, pady=5) tk.Button(home, text="My pets", width=10, command=self.show_my_pets).grid(row=1, column=0, sticky=tk.W, padx=10, pady=5) tk.Button(home, text="Shop's shelf", width=10, command=self.show_shelf).grid(row=1, column=2, sticky=tk.E, padx=10, pady=5) tk.Label(home, text="Input the index to buy a pet:").grid(row=2, column=0, sticky=tk.W, padx=10, pady=5) tk.Label(home, text="Input the index to sell a pet:").grid(row=3, column=0, sticky=tk.W, padx=10, pady=5) tk.Button(home, text="Request", command=self.buy_pets).grid(row=2, column=2, sticky=tk.E, padx=10, pady=5) tk.Button(home, text="Request", command=self.selling).grid(row=3, column=2, sticky=tk.E, padx=10, pady=5) index_buy = tk.StringVar() index_sell = tk.StringVar() self.e_buy = tk.Entry(home, textvariable=index_buy) self.e_sell = tk.Entry(home, textvariable=index_sell) self.e_buy.grid(row=2, column=1, sticky=tk.W, padx=0, pady=5) self.e_sell.grid(row=3, column=1, sticky=tk.W, padx=0, pady=5) else: wrong_pwd = tk.Toplevel() tk.Label(wrong_pwd, text='The password is wrong!').grid(row=0, column=0) else: not_user = tk.Toplevel() tk.Label(not_user, text = 'Account Number and password cannot match!').grid(row=0, column=0) def selling(self): if self.e_sell.get() not in self.p.currentUser['pets'].keys(): no_such_pet = tk.Toplevel() no_such_pet.title('There is no such pet.') tk.Label(no_such_pet, text='There is no such pet.').grid(row=0, column=1) else: selling = tk.Toplevel() selling.title('Pets on sale!') tk.Label(selling, text='Your pet is on sale now.').grid(row=0, column=0) self.p.sell(self.e_sell.get()) def buy_pets(self): if self.e_buy.get() not in self.p.shelf_list.keys(): no_such_pet = tk.Toplevel() no_such_pet.title('There is no such pet!') tk.Label(no_such_pet, text='There is no such pet, you can buy another one.')\ .grid(row=0, column=0) elif self.p.shelf_list[self.e_buy.get()]['status'] == 'Sold': sold = tk.Toplevel() sold.title('This pet is sold.') tk.Label(sold, text='This pet has been sold, you can buy another one.')\ .grid(row=0, column=0) else: trans = tk.Toplevel() trans.title('Transaction page') tk.Label(trans, text='The price is $' + self.p.shelf_list[self.e_buy.get()]['price'] + '.')\ .grid(row=0, column=0) tk.Label(trans, text="Recipient's public key").grid(row=1, column=0) tk.Label(trans, text="Your public key:").grid(row=2, column=0) tk.Label(trans, text="Your pravite key:").grid(row=3, column=0) pra_key = tk.StringVar() self.e_add = tk.Entry(trans)#, textvariable=address) self.e_pub_key = tk.Entry(trans)#, textvariable=pub_key) self.e_pra_key = tk.Entry(trans, textvariable=pra_key, show="*") self.e_add.grid(row=1, column=1, padx=10, pady=5) self.e_add.delete(0, tk.END) self.e_add.insert(0,self.p.shelf_list[self.e_buy.get()]['publicKey']) self.e_pub_key.grid(row=2, column=1, padx=10, pady=5) self.e_pub_key.delete(0, tk.END) self.e_pub_key.insert(0, self.p.currentUser['publicKey']) self.e_pra_key.grid(row=3, column=1, padx=10, pady=5) tk.Button(trans, text="Submit", width=10, command=self.bought)\ .grid(row=4, columnspan=3, sticky=tk.W, padx=100, pady=5) def bought(self): verify = self.p.buy(self.e_buy.get(), self.e_pra_key.get()) if verify: success = tk.Toplevel() tk.Label(success, text='Transaction is successful!').grid(row=0, column=0) else: error = tk.Toplevel() tk.Label(error, text='Sorry! Transaction Failed!').grid(row=0, column=0)
class SpaceshipCommand(cmd.Cmd): intro = 'Welcome to Spaceship Build' prompt = '> ' def __init__(self): self.bus = Bus('root') self.inventory = Inventory() self.shop = Shop() super().__init__() def do_exit(self, _): '''Exits the game''' print(random.choice([ 'So long!', 'Toodles!', 'Sayonara!', 'Bye', 'Farewell', 'Take care', 'Goodbye', 'Till next time', 'Later', 'Best of luck!', 'Peace Out!', 'Adios', 'Ciao!', 'Au revoir', "Don't leave!"])) return True def do_EOF(self, arg): '''Handles Ctrl+D''' return self.do_exit(arg) #----Spaceship Commands def do_broadcast(self, arg): '''Broadcast message from terminal to attached Buses: > broadcast guns.all fire! > broadcast system.report 'Everything nominal' ''' topic, message, *_ = shlex.split(arg) self.bus.broadcast(topic, message) def do_buy(self, type_name): '''This purchases an item from the shop and puts it into the player's inventory > buy raygun Raygun-001 purchased and added to inventory. ''' item = self.shop.buy(type_name) if item is None: print("Can't buy {!r} there's nothing like that." .format(type_name)) else: self.inventory.store(item) print(item, "purchased and added to inventory.") def do_subscribe(self, topic_key=''): '''Subscribe to message using the topic key, they will be visible in the console. If no argument given, all topics are subscribed to. > subscribe damage.report > subscribe guns > subscribe ''' self.bus.subscribe(topic_key, spaceship.basic_subscriber) def do_inv(self, verbose): ''' Prints full inventory''' if verbose == '-v': print(self.inventory.contents()) else: print(self.inventory.summary_contents())