Exemplo n.º 1
0
class User():
	def __init__ (self):
		self.view = View()
		self.model = Model()
		self.stock = Stock()

	def choice_login (self):
		self.view.welcome()
		choice = self.view.choice_login()
		if choice == "1":
			return self.signup()
		if choice == "2":
			return self.login()
		if choice == "3":
			return self.login_admin()
		else:
			self.choice_login() 

	def login_admin(self):
		return_adminlst = self.view.admin_login()
		result = self.model.check_login(return_adminlst[0], return_adminlst[1])
		if result != []:
			all_accounts = self.model.get_top_accounts()
			self.view.display_all_accounts(all_accounts)
		else:
			print("You do not have admin rights")
			self.view.choice_login()


	def signup (self):
		name = self.view.get_name()
		username = self.view.get_username()
		password = self.view.get_password()
		balance = 10000.00
		"send the values to the db"
		self.model.create_user(name, username, password, balance)
		self.view.restart()
		sys.exit()

	def login (self):
		# pu.db
		self.username = self.view.login_username()
		self.password = self.view.login_password()
		self.info_list = self.model.check_login (self.username, self.password)
		if self.info_list is not None:
			print ("")
			print ("Welcome back to the stock trading game!")
			balance = self.model.get_balance(self.username, self.password)
			print ("Your current balance is: ", balance, "dollars.")
			self.choose_option()
		else:
			self.view.try_again()
			sys.exit()

	def choose_option (self):
		choice = self.view.choose_option()
		if choice == "1":
			self.stock.get_company_info()	
			self.stock.get_stock_quote()
		elif choice == "2":
			self.stock.buy_stocks()
		elif choice == "3":
			self.stock.sell_stocks()
		elif choice == "4":
			self.get_portfolio()
		elif choice == "5":
			print ("Goodbye.")
			sys.exit()
		else:
			print ("Please enter a valid choice. (1, 2, 3 or 4) ")
			self.choose_option()

	def get_portfolio (self):
		portfolio = self.model.get_portfolio(self.username, self.password)
		self.view.print_portfolio(portfolio)
		self.choose_option()
class TraderController:
    def __init__(self):
        self.view = View()
        self.db = TradingUserDB()
        self.api = Markit()

    def login(self):
        user_name = self.view.get_username()
        password = self.view.get_password()
        return user_name, password

    def get_id(self, user_name):
        user_id = self.db.user_id_check(user_name)
        return user_id[0]

    def create_account(self):
        self.view.creating_account()
        user_name, password = self.login()
        user_id = self.db.user_id_check(user_name)
        if user_id == None:
            name = self.view.get_fullname()
            new_user = Player(user_name, password, name)
            self.db.add_user(new_user)
            user_id = self.db.user_id_check(user_name)
            new_account = Account(user_id)
            self.db.add_account(new_account)
        else:
            self.view.username_exists()
            self.create_account()
        return user_name, password

    def current_balance(self, user_name):
        balance = self.db.get_account_balance(self.db.user_id_check(user_name))
        self.view.get_balance(balance[0])
        return balance

    def run(self):
        signin = self.view.signin()
        if signin == "1":
            user_name, password = self.login()
            validate = self.db.signin(user_name, password)
            while validate == None:
                self.view.wrong_username_or_password()
                user_name, password = self.login()
                validate = self.db.signin(user_name, password)
            if validate[1] == 1:
                self.view.welcome_admin()
                dict_leaders = {}
                portfolios = self.db.get_all_portfolios()
                for portfolio in portfolios:
                    key = portfolio[1]
                    if key in dict_leaders:
                        value, paid, profit = self.db.one_portfolio_value(portfolio)
                        l = dict_leaders.get(key)
                        for idx, num in enumerate(l):
                            if idx == 0:
                                value = num + value
                            elif idx == 1:
                                paid = num + paid
                            else:
                                profit = num + profit
                        dict_leaders[key] = [value, paid, profit]
                    else:
                        value, paid, profit = self.db.one_portfolio_value(portfolio)
                        dict_leaders[key] = [value, paid, profit]
                self.view.leaderboard(dict_leaders)
        elif signin == "2":
            user_name, password = self.create_account()
        else:
            self.view.invalid_choice()
        user_id = self.get_id(user_name)

        while True:
            action = self.view.user_menu()
            if action == "1":
                self.current_balance(user_name)
                portfolio = self.db.get_own_portfolio(user_id)
                self.view.list_all_portfolios(portfolio)
                value, paid, profit = self.db.current_portfolio_value(portfolio)
                self.view.print_current_portfolio_value(value, paid, profit)
            elif action == "2":
                search_type = self.view.search_type()
                if search_type == "1":
                    company = self.view.search_by_company()
                    info = self.view.company_info(self.api.company_search(company))
                    if self.api.company_search(company) == []:
                        self.view.company_not_found()
                    else:
                        self.view.search_instructions()
                elif search_type == "2":
                    symbol = self.view.search_by_symbol()
                    self.api.get_company_data(symbol)
                    self.view.company_data(self.api.get_company_data(symbol))
                    self.view.current_value(symbol, self.api.get_quote(symbol))
            elif action == "3":
                balance = self.current_balance(user_name)
                symbol = self.view.search_by_symbol()
                price = self.api.get_quote(symbol)
                if price == None:
                    self.view.company_not_found()
                else:
                    self.view.current_value(symbol, price)
                    quantity = self.view.get_quantity()
                    total_price = price * float(quantity)
                    self.view.get_total_price(total_price)
                    if total_price > balance[0]:
                        self.view.insufficient_funds()
                    else:
                        confirm = self.view.confirm_purchase()
                        if confirm.lower() == "c":
                            self.db.add_to_portfolio(user_id, symbol, price, quantity)
                            self.db.update_account(total_price*-1, user_id)
                            self.current_balance(user_name)
                        elif confirm == "q":
                            continue
            elif action == "4":
                symbol = self.view.stocks_to_be_sold()
                price = self.api.get_quote(symbol)
                stocks_with_symbol = self.db.get_from_portfolio(symbol, user_id)
                self.view.list_all_portfolios(stocks_with_symbol)
                number_of_stocks = self.db.get_number_of_stocks_available(symbol, user_id)
                if number_of_stocks[0][0] == None:
                    self.view.company_not_found()
                    continue
                self.view.show_number_of_stocks(symbol, number_of_stocks[0])
                sell = int(self.view.how_many_for_sale())
                while sell > number_of_stocks[0][0] or sell < 1:
                    self.view.invalid_number()
                    sell = int(self.view.how_many_for_sale())
                price_paid = self.db.subtract_stocks(sell, stocks_with_symbol)
                profit = sell * price
                profit_loss = profit - price_paid
                self.view.show_profit_loss(profit_loss)
                self.db.update_account(profit, user_id)
                self.db.remove_zero_stocks()
            elif action == "5":
                break
            else:
                self.view.press_number()