diff --git a/bank.py b/bank.py index ab74f6f..1d4b72a 100644 --- a/bank.py +++ b/bank.py @@ -1,184 +1,268 @@ -class Bank: - - def __init__(self, name): - # Manages accounts, transactions, and loans. Generates unique account numbers. - self.name = name - self.accounts = [] - self.transactions = [] - self.loans = [] - - - def create_account(self, account_holder, initial_balance): - # Creates a new account with a unique number and initial balance, add it to accounts array and return account number - pass - - - def get_account(self, account_number): - # Retrieves an account by its number, returns None if not found. - pass - - - def list_accounts(self): - # Lists all accounts with their details like account number, holder, balance - pass - - - def close_account(self, account_number): - # Closes an account by removing it from the list. - pass - - - def transfer_funds(self, from_account, to_account, amount): - # Transfers funds between accounts if sufficient balance is available, return boolean values depending on - # transfer success - pass - - - def generate_account_statement(self, account_number): - # Generates a transaction history for a given account number, return None if no account number found - pass - - - - def calculate_total_assets(self): - # Calculates the total assets by summing all account balances. - pass - - - def process_loan_payments(self): - # Processes loan payments by deducting the installment amount from the balance. - pass - - -class Account: - # Represents a bank account with balance and transactions. Default balance is 0. - def __init__(self, account_number, account_holder, balance=0.0): - self.account_number = account_number - self.account_holder = account_holder - self.balance = balance - self.transactions = [] - - - def deposit(self, amount): - # Deposits a positive amount to the account and records the transaction. When adding transaction to array specift "Deposit" and amount - pass - - - def withdraw(self, amount): - # Withdraws an amount if sufficient balance is available. When adding into transactions array, specify Withdraw and amount - pass - - - def get_balance(self): - # Returns the current balance of the account. - pass - - - def get_transaction_history(self): - # Retrieves the transaction history of the account. - pass - - - def apply_interest(self, rate): - # Applies interest based on a given rate. - pass - - - def overdraft_protection(self, amount): - # Checks if a withdrawal amount is within available balance. - pass - - - def update_contact_information(self, new_contact_info): - # Updates the contact information of the account holder. - pass - - -class Transaction: - # Represents a transaction with an ID, type, amount, and timestamp. - def __init__(self, transaction_id, account_number, amount, transaction_type, timestamp): - self.transaction_id = transaction_id - self.account_number = account_number - self.amount = amount - self.transaction_type = transaction_type - self.timestamp = timestamp - - - def process_transaction(self): - # Processes a transaction if the amount is positive. - return self.amount > 0 - - - def validate_transaction(self): - # Validates the transaction before processing. - return self.amount > 0 - - - def reverse_transaction(self): - # Reverses a transaction by negating the amount. - pass - - -class BankEmployee: - # Represents a bank employee with an ID, name, and role. - def __init__(self, employee_id, name, role): - self.employee_id = employee_id - self.name = name - self.role = role - - - def approve_loan(self, account_number, amount): - # Approves a loan request if the amount is positive. - return amount > 0 - - - def view_account_details(self, account_number): - # Views account details given an account number by returning account number - pass - - - def suspend_account(self, account_number): - # Return suspension message as string - pass - - - def generate_financial_report(self): - # Return string "Financial Report Generated" - return "Financial Report Generated" - - -class Loan: - # Represents a loan with an ID, amount, interest rate, and tenure. - def __init__(self, loan_id, account_number, amount, interest_rate, tenure): - self.loan_id = loan_id - self.account_number = account_number - self.amount = amount - self.interest_rate = interest_rate - self.tenure = tenure - self.remaining_balance = amount - - - def make_payment(self, amount): - # Makes a loan payment if the amount is within the remaining balance. - pass - - - def get_remaining_balance(self): - # Returns the remaining balance of the loan. - pass - - - def calculate_interest(self): - # Calculates interest on the remaining balance. - pass - - - def check_loan_eligibility(self, account_number, income, credit_score): - # Checks if an applicant is eligible for a loan based on income(>50k) and credit score(>650). Return boolean - pass - - # Allows restructuring of the loan with new terms. - def restructure_loan(self, new_terms): - self.tenure = new_terms.get("tenure", self.tenure) - self.interest_rate = new_terms.get("interest_rate", self.interest_rate) - return True +import time + +class Bank: + + def __init__(self, name): + # Manages accounts, transactions, and loans. Generates unique account numbers. + self.name = name + self.accounts = [] + self.transactions = [] + self.loans = [] + self._next_account_number = 1000 + + + def create_account(self, account_holder, initial_balance): + # Creates a new account with a unique number and initial balance, add it to accounts array and return account number + account_number = self._next_account_number + self._next_account_number += 1 + account = Account(account_number, account_holder, initial_balance) + self.accounts.append(account) + return account_number + + + def get_account(self, account_number): + # Retrieves an account by its number, returns None if not found. + for account in self.accounts: + if account.account_number == account_number: + return account + return None + + + def list_accounts(self): + # Lists all accounts with their details like account number, holder, balance + result = [] + for account in self.accounts: + result.append({ + "account_number": account.account_number, + "holder": account.account_holder, + "balance": account.balance + }) + return result + + + def close_account(self, account_number): + # Closes an account by removing it from the list. + account = self.get_account(account_number) + if account: + self.accounts.remove(account) + return True + return False + + + def transfer_funds(self, from_account, to_account, amount): + # Transfers funds between accounts if sufficient balance is available, return boolean values depending on + # transfer success + from_acc = self.get_account(from_account) + to_acc = self.get_account(to_account) + + if from_acc is None or to_acc is None: + return False + + if from_acc.balance < amount: + return False + + from_acc.withdraw(amount) + to_acc.deposit(amount) + + self.transactions.append({ + "type": "transfer", + "from": from_account, + "to": to_account, + "amount": amount + }) + + return True + + + def generate_account_statement(self, account_number): + # Generates a transaction history for a given account number, return None if no account number found + account = self.get_account(account_number) + if account is None: + return None + + return { + "account_number": account.account_number, + "holder": account.account_holder, + "balance": account.balance, + "transactions": account.transactions + } + + + + def calculate_total_assets(self): + # Calculates the total assets by summing all account balances. + total = 0.0 + for account in self.accounts: + total += account.balance + return total + + + def process_loan_payments(self): + # Processes loan payments by deducting the installment amount from the balance. + for loan in self.loans: + account = self.get_account(loan.account_number) + if account and loan.remaining_balance > 0: + installment = loan.amount / loan.tenure + interest = loan.calculate_interest() + total_payment = installment + interest + if account.balance >= total_payment: + account.balance -= total_payment + loan.remaining_balance -= installment + if loan.remaining_balance < 0: + loan.remaining_balance = 0 + account.transactions.append({ + "type": "loan_payment", + "amount": total_payment, + "loan_id": loan.loan_id + }) + + +class Account: + # Represents a bank account with balance and transactions. Default balance is 0. + def __init__(self, account_number, account_holder, balance=0.0): + self.account_number = account_number + self.account_holder = account_holder + self.balance = balance + self.transactions = [] + self.contact_info = None + + + def deposit(self, amount): + # Deposits a positive amount to the account and records the transaction. When adding transaction to array specify "Deposit" and amount + if amount > 0: + self.balance += amount + self.transactions.append({"type": "Deposit", "amount": amount}) + return True + return False + + + def withdraw(self, amount): + # Withdraws an amount if sufficient balance is available. When adding into transactions array, specify Withdraw and amount + if amount > 0 and self.balance >= amount: + self.balance -= amount + self.transactions.append({"type": "Withdraw", "amount": amount}) + return True + return False + + + def get_balance(self): + # Returns the current balance of the account. + return self.balance + + + def get_transaction_history(self): + # Retrieves the transaction history of the account. + return self.transactions + + + def apply_interest(self, rate): + # Applies interest based on a given rate. + interest = self.balance * rate + self.balance += interest + self.transactions.append({"type": "Interest", "amount": interest}) + return interest + + + def overdraft_protection(self, amount): + # Checks if a withdrawal amount is within available balance. + return amount <= self.balance + + + def update_contact_information(self, new_contact_info): + # Updates the contact information of the account holder. + self.contact_info = new_contact_info + return True + + +class Transaction: + # Represents a transaction with an ID, type, amount, and timestamp. + def __init__(self, transaction_id, account_number, amount, transaction_type, timestamp): + self.transaction_id = transaction_id + self.account_number = account_number + self.amount = amount + self.transaction_type = transaction_type + self.timestamp = timestamp + + + def process_transaction(self): + # Processes a transaction if the amount is positive. + return self.amount > 0 + + + def validate_transaction(self): + # Validates the transaction before processing. + return self.amount > 0 + + + def reverse_transaction(self): + # Reverses a transaction by negating the amount. + self.amount = -self.amount + return True + + +class BankEmployee: + # Represents a bank employee with an ID, name, and role. + def __init__(self, employee_id, name, role): + self.employee_id = employee_id + self.name = name + self.role = role + + + def approve_loan(self, account_number, amount): + # Approves a loan request if the amount is positive. + return amount > 0 + + + def view_account_details(self, account_number): + # Views account details given an account number by returning account number + return account_number + + + def suspend_account(self, account_number): + # Return suspension message as string + return f"Account {account_number} has been suspended." + + + def generate_financial_report(self): + # Return string "Financial Report Generated" + return "Financial Report Generated" + + +class Loan: + # Represents a loan with an ID, amount, interest rate, and tenure. + def __init__(self, loan_id, account_number, amount, interest_rate, tenure): + self.loan_id = loan_id + self.account_number = account_number + self.amount = amount + self.interest_rate = interest_rate + self.tenure = tenure + self.remaining_balance = amount + + + def make_payment(self, amount): + # Makes a loan payment if the amount is within the remaining balance. + if amount > 0 and amount <= self.remaining_balance: + self.remaining_balance -= amount + return True + return False + + + def get_remaining_balance(self): + # Returns the remaining balance of the loan. + return self.remaining_balance + + + def calculate_interest(self): + # Calculates interest on the remaining balance. + return self.remaining_balance * self.interest_rate + + + def check_loan_eligibility(self, account_number, income, credit_score): + # Checks if an applicant is eligible for a loan based on income(>50k) and credit score(>650). Return boolean + return income > 50000 and credit_score > 650 + + # Allows restructuring of the loan with new terms. + def restructure_loan(self, new_terms): + self.tenure = new_terms.get("tenure", self.tenure) + self.interest_rate = new_terms.get("interest_rate", self.interest_rate) + return True diff --git a/game.py b/game.py index b13902f..58973dc 100644 --- a/game.py +++ b/game.py @@ -1,147 +1,210 @@ -import random - -class Player: - def __init__(self): - # Initial values: Health = 100, Fuel = 50, Gold = 0, Oxygen = 100, XP = 0, Level = 1 - self.health = 100 - self.fuel = 50 - self.gold = 0 - self.items = [] - self.oxygen = 100 - self.xp = 0 - self.level = 1 - - def check_status(self): - # Displays current player status, show None if no items - pass - - def level_up(self): - # XP required to level up: 100; resets XP to 0 after leveling up and print appropriate message - pass - - def use_oxygen(self): - # Oxygen limit: 100; cannot use oxygen if supply is below 10 and print appropriate message - pass - - def collect_gold(self, amount): - # Collects gold and increment amount with appropriate message - pass - -class Alien: - def __init__(self, name, health, attack): - # Alien health typically ranges from 30 to 100; attack power ranges from 5 to 15 - self.name = name - self.health = health - self.attack = attack - - def attack_player(self, player): - # Alien attack damage varies between 3 and its max attack power, reduce player health and show appropriate message - pass -class SpaceGame: - def __init__(self): - # Initializes game with a player character - self.player = Player() - - def start_game(self): - # Displays game introduction and starts the main loop - print("Welcome to the Space Exploration Adventure!") - print("Travel across planets, meet aliens, and gather resources.") - self.game_loop() - - def game_loop(self): - # Runs the game loop until player health or fuel reaches 0 - while self.player.health > 0 and self.player.fuel > 0: - action = self.main_menu() - if action == "1": - self.explore_planet() - elif action == "2": - self.player.check_status() - elif action == "3": - self.player.use_oxygen() - elif action == "4": - print("You decide to return to your home planet. Game Over.") - break - - def main_menu(self): - # Presents the main menu options to the player - print("\nWhat would you like to do?") - print("1. Explore a new planet") - print("2. Check status") - print("3. Use oxygen supply") - print("4. Exit game") - return input("Enter your choice: ") - - def explore_planet(self): - # Randomly selects an event when exploring a new planet - print("\nYou approach a new planet...") - event = random.choice([self.alien_encounter, self.find_resources, self.empty_planet, - self.fuel_station, self.asteroid_field, self.space_pirates, - self.ancient_ruins, self.lost_colony, self.abandoned_ship, self.black_hole]) - event() - - def alien_encounter(self): - # Creates a random alien encounter for combat, call create_alien() and combat() and display appropriate message - pass - - def create_alien(self): - # Defines possible alien enemies with varying stats - aliens = [ - Alien("Martian", 30, 5), - Alien("Venusian", 50, 8), - Alien("Zorgon", 100, 15), - Alien("Cybertronian", 40, 6), - Alien("Void Entity", 70, 10) - ] - return random.choice(aliens) - - def combat(self, alien): - # Combat loop: Player and alien take turns attacking until one is defeated, loot alien if you win and - # print appropriate messages for each outcome - pass - - def attack_alien(self, alien): - # Player deals random damage between 5 and 15 to the alien; print appropriate message as well - pass - - def loot_alien(self, alien): - # Player loots between 10 and 50 gold from a defeated alien by calling collect_gold() - pass - - def find_resources(self): - # Player finds resources, adding between 5 and 50 gold by calling collect_gold() - pass - - def fuel_station(self): - # Fuel station restores between 5 and 20 fuel and print appropriate message - pass - - def empty_planet(self): - # display "This planet is barren. Nothing to see here." - pass - - def asteroid_field(self): - # display "You navigate through an asteroid field, avoiding collisions!" - pass - - def space_pirates(self): - # display "Pirates attack your ship! Will you fight or escape?" - pass - - def ancient_ruins(self): - # display "You discovered ancient alien ruins with hidden technology!" - pass - - def lost_colony(self): - # display "You find a lost human colony struggling to survive." - pass - - def abandoned_ship(self): - print("An abandoned spaceship drifts in the void. Do you explore it?") - - def black_hole(self): - # display "A black hole is nearby! Will you attempt to study it or flee?" - pass - -if __name__ == "__main__": - game = SpaceGame() - game.start_game() +import random + +class Player: + def __init__(self): + # Initial values: Health = 100, Fuel = 50, Gold = 0, Oxygen = 100, XP = 0, Level = 1 + self.health = 100 + self.fuel = 50 + self.gold = 0 + self.items = [] + self.oxygen = 100 + self.xp = 0 + self.level = 1 + + def check_status(self): + # Displays current player status, show None if no items + print(f"\n--- Player Status ---") + print(f"Health: {self.health}") + print(f"Fuel: {self.fuel}") + print(f"Gold: {self.gold}") + print(f"Oxygen: {self.oxygen}") + print(f"XP: {self.xp}/100") + print(f"Level: {self.level}") + print(f"Items: {', '.join(self.items) if self.items else 'None'}") + + def level_up(self): + # XP required to level up: 100; resets XP to 0 after leveling up and print appropriate message + if self.xp >= 100: + self.level += 1 + self.xp = 0 + print(f"Congratulations! You leveled up to Level {self.level}!") + else: + print(f"Not enough XP to level up. ({self.xp}/100)") + + def use_oxygen(self): + # Oxygen limit: 100; cannot use oxygen if supply is below 10 and print appropriate message + if self.oxygen < 10: + print("Warning: Oxygen supply too low to use! Find a refill station.") + return + self.oxygen -= 10 + if self.oxygen < 0: + self.oxygen = 0 + print(f"Used oxygen. Remaining: {self.oxygen}") + + def collect_gold(self, amount): + # Collects gold and increment amount with appropriate message + self.gold += amount + print(f"Collected {amount} gold! Total gold: {self.gold}") + +class Alien: + def __init__(self, name, health, attack): + # Alien health typically ranges from 30 to 100; attack power ranges from 5 to 15 + self.name = name + self.health = health + self.attack = attack + + def attack_player(self, player): + # Alien attack damage varies between 3 and its max attack power, reduce player health and show appropriate message + damage = random.randint(3, self.attack) + player.health -= damage + if player.health < 0: + player.health = 0 + print(f"{self.name} attacks you for {damage} damage! Your health: {player.health}") + +class SpaceGame: + def __init__(self): + # Initializes game with a player character + self.player = Player() + + def start_game(self): + # Displays game introduction and starts the main loop + print("Welcome to the Space Exploration Adventure!") + print("Travel across planets, meet aliens, and gather resources.") + self.game_loop() + + def game_loop(self): + # Runs the game loop until player health or fuel reaches 0 + while self.player.health > 0 and self.player.fuel > 0: + action = self.main_menu() + if action == "1": + self.explore_planet() + elif action == "2": + self.player.check_status() + elif action == "3": + self.player.use_oxygen() + elif action == "4": + print("You decide to return to your home planet. Game Over.") + break + + if self.player.health <= 0: + print("Your ship was destroyed. Game Over.") + elif self.player.fuel <= 0: + print("You ran out of fuel and are stranded. Game Over.") + + def main_menu(self): + # Presents the main menu options to the player + print("\nWhat would you like to do?") + print("1. Explore a new planet") + print("2. Check status") + print("3. Use oxygen supply") + print("4. Exit game") + return input("Enter your choice: ") + + def explore_planet(self): + # Randomly selects an event when exploring a new planet + print("\nYou approach a new planet...") + event = random.choice([self.alien_encounter, self.find_resources, self.empty_planet, + self.fuel_station, self.asteroid_field, self.space_pirates, + self.ancient_ruins, self.lost_colony, self.abandoned_ship, self.black_hole]) + event() + + def alien_encounter(self): + # Creates a random alien encounter for combat, call create_alien() and combat() and display appropriate message + print("An alien ship approaches!") + alien = self.create_alien() + print(f"A {alien.name} appears! (HP: {alien.health}, ATK: {alien.attack})") + self.combat(alien) + + def create_alien(self): + # Defines possible alien enemies with varying stats + aliens = [ + Alien("Martian", 30, 5), + Alien("Venusian", 50, 8), + Alien("Zorgon", 100, 15), + Alien("Cybertronian", 40, 6), + Alien("Void Entity", 70, 10) + ] + return random.choice(aliens) + + def combat(self, alien): + # Combat loop: Player and alien take turns attacking until one is defeated, loot alien if you win and + # print appropriate messages for each outcome + while self.player.health > 0 and alien.health > 0: + self.attack_alien(alien) + if alien.health <= 0: + print(f"You defeated the {alien.name}!") + self.loot_alien(alien) + self.player.xp += 25 + self.player.level_up() + return + alien.attack_player(self.player) + + if self.player.health <= 0: + print(f"You were defeated by the {alien.name}...") + + def attack_alien(self, alien): + # Player deals random damage between 5 and 15 to the alien; print appropriate message as well + damage = random.randint(5, 15) + alien.health -= damage + if alien.health < 0: + alien.health = 0 + print(f"You attack {alien.name} for {damage} damage! Alien HP: {alien.health}") + + def loot_alien(self, alien): + # Player loots between 10 and 50 gold from a defeated alien by calling collect_gold() + loot = random.randint(10, 50) + print(f"You search the {alien.name}'s wreckage...") + self.player.collect_gold(loot) + + def find_resources(self): + # Player finds resources, adding between 5 and 50 gold by calling collect_gold() + print("You discover a resource-rich area on the planet!") + amount = random.randint(5, 50) + self.player.collect_gold(amount) + + def fuel_station(self): + # Fuel station restores between 5 and 20 fuel and print appropriate message + fuel_amount = random.randint(5, 20) + self.player.fuel += fuel_amount + print(f"You found a fuel station! Refueled {fuel_amount} units. Total fuel: {self.player.fuel}") + + def empty_planet(self): + # display "This planet is barren. Nothing to see here." + print("This planet is barren. Nothing to see here.") + + def asteroid_field(self): + # display "You navigate through an asteroid field, avoiding collisions!" + print("You navigate through an asteroid field, avoiding collisions!") + + def space_pirates(self): + # display "Pirates attack your ship! Will you fight or escape?" + print("Pirates attack your ship! Will you fight or escape?") + + def ancient_ruins(self): + # display "You discovered ancient alien ruins with hidden technology!" + print("You discovered ancient alien ruins with hidden technology!") + self.player.xp += 15 + self.player.level_up() + + def lost_colony(self): + # display "You find a lost human colony struggling to survive." + print("You find a lost human colony struggling to survive.") + + def abandoned_ship(self): + print("An abandoned spaceship drifts in the void. Do you explore it?") + loot = random.randint(20, 80) + self.player.collect_gold(loot) + self.player.items.append("Salvaged Parts") + print("You salvaged parts from the abandoned ship!") + + def black_hole(self): + # display "A black hole is nearby! Will you attempt to study it or flee?" + print("A black hole is nearby! Will you attempt to study it or flee?") + self.player.fuel -= random.randint(5, 15) + if self.player.fuel < 0: + self.player.fuel = 0 + print(f"Escaping the gravitational pull used some fuel. Fuel: {self.player.fuel}") + +if __name__ == "__main__": + game = SpaceGame() + game.start_game()