Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import json
- import argparse
- games = []
- class Game:
- def __init__(self, name, console, played=True):
- self.name = name
- self.console = console
- self.played = played
- def __str__(self):
- return f"{self.name} - {self.console} - {'Played' if self.played else 'Not Played'}"
- class GameNotFoundError(Exception):
- pass
- class InvalidConsoleError(Exception):
- pass
- class InvalidPlayedError(Exception):
- pass
- def save_data(file_path):
- with open(file_path, 'w') as file:
- json.dump([vars(g) for g in games], file)
- def load_data(file_path):
- if os.path.exists(file_path):
- with open(file_path, 'r') as file:
- data = json.load(file)
- games.extend([Game(g['name'], g['console'], g['played']) for g in data])
- def input_console():
- while True:
- console = input("Console (1 PC/2 PS4)? ")
- if console == "1":
- return "PC"
- elif console == "2":
- return "PS4"
- else:
- raise InvalidConsoleError("Invalid console option. Please enter 1 or 2.")
- def input_played():
- while True:
- played = input("Played (True/False)? ").capitalize()
- if played == "True":
- return True
- elif played == "False":
- return False
- else:
- raise InvalidPlayedError("Invalid played option. Please enter True or False.")
- def game_add(file_path):
- print()
- name = input("Name? ")
- console = input_console()
- game = Game(name, console)
- games.append(game)
- print(f"Added: {game}")
- save_data(file_path)
- input("Press ANY key to continue")
- def game_remove(file_path):
- game_name = input("Which game should be removed? ")
- for game in games[:]:
- if game.name == game_name:
- games.remove(game)
- print(f"Removed: {game}")
- save_data(file_path)
- break
- else:
- raise GameNotFoundError(f"{game_name} not found in the library.")
- input("Press ANY key to continue")
- def game_modify(file_path):
- game_name = input("Enter the name of the game to modify: ")
- for game in games:
- if game.name == game_name:
- game.name = input("New name? ")
- game.console = input_console()
- game.played = input_played()
- print(f"Modified: {game}")
- save_data(file_path)
- break
- else:
- raise GameNotFoundError(f"{game_name} not found in the library.")
- input("Press ANY key to continue")
- def games_list():
- for i, game in enumerate(games):
- print(f"{i+1}) {game}")
- input("Press ANY key to continue")
- if __name__ == "__main__":
- parser = argparse.ArgumentParser(description='Game Collection Library Management System')
- parser.add_argument('-f', '--file', default='games.json', help='JSON file to load and save games')
- args = parser.parse_args()
- response = 0
- load_data(args.file)
- while response != 5:
- print("__________________________________________")
- print()
- print("Game Collection Library Management System")
- print("*****************************************")
- print(f"Games in library: {len(games)}")
- print()
- print("1) Add a new game")
- print("2) Remove a game")
- print("3) Modify a game")
- print("4) List all games")
- print("5) Exit")
- print()
- new_response = input("~> ")
- try:
- response = int(new_response)
- if response not in range(1, 6):
- raise ValueError("Invalid choice. Please enter a number between 1 and 5.")
- except ValueError as e:
- print(e)
- response = 0
- if response == 1:
- game_add(args.file)
- elif response == 2:
- game_remove(args.file)
- elif response == 3:
- game_modify(args.file)
- elif response == 4:
- games_list()
- elif response == 5:
- save_data(args.file)
- print("Goodbye!")
- else:
- print("Invalid choice. Try again.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement