Advertisement
YaBoiSwayZ

Game Collection Library Management System

Jul 24th, 2023 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.15 KB | Source Code | 0 0
  1. import os
  2. import json
  3. import argparse
  4.  
  5. games = []
  6.  
  7. class Game:
  8.     def __init__(self, name, console, played=True):
  9.         self.name = name
  10.         self.console = console
  11.         self.played = played
  12.  
  13.     def __str__(self):
  14.         return f"{self.name} - {self.console} - {'Played' if self.played else 'Not Played'}"
  15.  
  16. class GameNotFoundError(Exception):
  17.     pass
  18.  
  19. class InvalidConsoleError(Exception):
  20.     pass
  21.  
  22. class InvalidPlayedError(Exception):
  23.     pass
  24.  
  25. def save_data(file_path):
  26.     with open(file_path, 'w') as file:
  27.         json.dump([vars(g) for g in games], file)
  28.  
  29. def load_data(file_path):
  30.     if os.path.exists(file_path):
  31.         with open(file_path, 'r') as file:
  32.             data = json.load(file)
  33.             games.extend([Game(g['name'], g['console'], g['played']) for g in data])
  34.  
  35. def input_console():
  36.     while True:
  37.         console = input("Console (1 PC/2 PS4)? ")
  38.         if console == "1":
  39.             return "PC"
  40.         elif console == "2":
  41.             return "PS4"
  42.         else:
  43.             raise InvalidConsoleError("Invalid console option. Please enter 1 or 2.")
  44.  
  45. def input_played():
  46.     while True:
  47.         played = input("Played (True/False)? ").capitalize()
  48.         if played == "True":
  49.             return True
  50.         elif played == "False":
  51.             return False
  52.         else:
  53.             raise InvalidPlayedError("Invalid played option. Please enter True or False.")
  54.  
  55. def game_add(file_path):
  56.     print()
  57.     name = input("Name? ")
  58.     console = input_console()
  59.     game = Game(name, console)
  60.     games.append(game)
  61.     print(f"Added: {game}")
  62.     save_data(file_path)
  63.     input("Press ANY key to continue")
  64.  
  65. def game_remove(file_path):
  66.     game_name = input("Which game should be removed? ")
  67.     for game in games[:]:
  68.         if game.name == game_name:
  69.             games.remove(game)
  70.             print(f"Removed: {game}")
  71.             save_data(file_path)
  72.             break
  73.     else:
  74.         raise GameNotFoundError(f"{game_name} not found in the library.")
  75.     input("Press ANY key to continue")
  76.  
  77. def game_modify(file_path):
  78.     game_name = input("Enter the name of the game to modify: ")
  79.     for game in games:
  80.         if game.name == game_name:
  81.             game.name = input("New name? ")
  82.             game.console = input_console()
  83.             game.played = input_played()
  84.             print(f"Modified: {game}")
  85.             save_data(file_path)
  86.             break
  87.     else:
  88.         raise GameNotFoundError(f"{game_name} not found in the library.")
  89.     input("Press ANY key to continue")
  90.  
  91. def games_list():
  92.     for i, game in enumerate(games):
  93.         print(f"{i+1}) {game}")
  94.     input("Press ANY key to continue")
  95.  
  96. if __name__ == "__main__":
  97.     parser = argparse.ArgumentParser(description='Game Collection Library Management System')
  98.     parser.add_argument('-f', '--file', default='games.json', help='JSON file to load and save games')
  99.     args = parser.parse_args()
  100.  
  101.     response = 0
  102.  
  103.     load_data(args.file)
  104.  
  105.     while response != 5:
  106.         print("__________________________________________")
  107.         print()
  108.         print("Game Collection Library Management System")
  109.         print("*****************************************")
  110.         print(f"Games in library: {len(games)}")
  111.         print()
  112.         print("1) Add a new game")
  113.         print("2) Remove a game")
  114.         print("3) Modify a game")
  115.         print("4) List all games")
  116.         print("5) Exit")
  117.         print()
  118.  
  119.         new_response = input("~> ")
  120.  
  121.         try:
  122.             response = int(new_response)
  123.             if response not in range(1, 6):
  124.                 raise ValueError("Invalid choice. Please enter a number between 1 and 5.")
  125.         except ValueError as e:
  126.             print(e)
  127.             response = 0
  128.  
  129.         if response == 1:
  130.             game_add(args.file)
  131.         elif response == 2:
  132.             game_remove(args.file)
  133.         elif response == 3:
  134.             game_modify(args.file)
  135.         elif response == 4:
  136.             games_list()
  137.         elif response == 5:
  138.             save_data(args.file)
  139.             print("Goodbye!")
  140.         else:
  141.             print("Invalid choice. Try again.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement