|
| 1 | +import os |
| 2 | + |
| 3 | +""" |
| 4 | + * EJERCICIO: |
| 5 | + * Desarrolla un programa capaz de crear un archivo que se llame como |
| 6 | + * tu usuario de GitHub y tenga la extensión .txt. |
| 7 | + * Añade varias líneas en ese fichero: |
| 8 | + * - Tu nombre. |
| 9 | + * - Edad. |
| 10 | + * - Lenguaje de programación favorito. |
| 11 | + * Imprime el contenido. |
| 12 | + * Borra el fichero. |
| 13 | +""" |
| 14 | + |
| 15 | +file_name = "Dkp-Dev.txt" |
| 16 | + |
| 17 | +with open(file_name, "w") as file: |
| 18 | + file.write("Dkp Dev\n") |
| 19 | + file.write("29\n") |
| 20 | + file.write("Python") |
| 21 | + |
| 22 | +with open(file_name, "r") as file: |
| 23 | + print(file.read()) |
| 24 | + |
| 25 | +os.remove(file_name) |
| 26 | + |
| 27 | +""" |
| 28 | +* DIFICULTAD EXTRA (opcional): |
| 29 | + * Desarrolla un programa de gestión de ventas que almacena sus datos en un |
| 30 | + * archivo .txt. |
| 31 | + * - Cada producto se guarda en una línea del archivo de la siguiente manera: |
| 32 | + * [nombre_producto], [cantidad_vendida], [precio]. |
| 33 | + * - Siguiendo ese formato, y mediante terminal, debe permitir añadir, consultar, |
| 34 | + * actualizar, eliminar productos y salir. |
| 35 | + * - También debe poseer opciones para calcular la venta total y por producto. |
| 36 | + * - La opción salir borra el .txt. |
| 37 | + */ |
| 38 | +""" |
| 39 | + |
| 40 | +file_name = "Dkp-Dev Shop.txt" |
| 41 | + |
| 42 | +open(file_name, "a") |
| 43 | + |
| 44 | +while True: |
| 45 | + print("1. Añadir producto") |
| 46 | + print("2. Consultar producto") |
| 47 | + print("3. Actualizar producto") |
| 48 | + print("4. Borrar producto") |
| 49 | + print("5. Mostrar productos") |
| 50 | + print("6. Calcular venta total") |
| 51 | + print("7. Calcular venta por producto") |
| 52 | + print("8. Salir") |
| 53 | + |
| 54 | + option = input("Selecciona una opción: ") |
| 55 | + |
| 56 | + if option == "1": |
| 57 | + name = input("Nombre: ") |
| 58 | + quantity = input("Cantidad: ") |
| 59 | + price = input("Precio: ") |
| 60 | + with open(file_name, "a") as file: |
| 61 | + file.write(f"{name}, {quantity}, {price}\n") |
| 62 | + elif option == "2": |
| 63 | + name = input("Nombre: ") |
| 64 | + with open(file_name, "r") as file: |
| 65 | + for line in file.readlines(): |
| 66 | + if line.split(", ")[0] == name: |
| 67 | + print(line) |
| 68 | + break |
| 69 | + elif option == "3": |
| 70 | + name = input("Nombre: ") |
| 71 | + quantity = input("Cantidad: ") |
| 72 | + price = input("Precio: ") |
| 73 | + with open(file_name, "r") as file: |
| 74 | + lines = file.readlines() |
| 75 | + with open(file_name, "w") as file: |
| 76 | + for line in lines: |
| 77 | + if line.split(", ")[0] == name: |
| 78 | + file.write(f"{name}, {quantity}, {price}\n") |
| 79 | + else: |
| 80 | + file.write(line) |
| 81 | + elif option == "4": |
| 82 | + name = input("Nombre: ") |
| 83 | + with open(file_name, "r") as file: |
| 84 | + lines = file.readlines() |
| 85 | + with open(file_name, "w") as file: |
| 86 | + for line in lines: |
| 87 | + if line.split(", ")[0] != name: |
| 88 | + file.write(line) |
| 89 | + elif option == "5": |
| 90 | + with open(file_name, "r") as file: |
| 91 | + print(file.read()) |
| 92 | + elif option == "6": |
| 93 | + total = 0 |
| 94 | + with open(file_name, "r") as file: |
| 95 | + for line in file.readlines(): |
| 96 | + components = line.split(", ") |
| 97 | + quantity = int(components[1]) |
| 98 | + price = float(components[2]) |
| 99 | + total += quantity * price |
| 100 | + print(total) |
| 101 | + elif option == "7": |
| 102 | + name = input("Nombre: ") |
| 103 | + total = 0 |
| 104 | + with open(file_name, "r") as file: |
| 105 | + for line in file.readlines(): |
| 106 | + components = line.split(", ") |
| 107 | + if components[0] == name: |
| 108 | + quantity = int(components[1]) |
| 109 | + price = float(components[2]) |
| 110 | + total += quantity * price |
| 111 | + break |
| 112 | + print(total) |
| 113 | + elif option == "8": |
| 114 | + os.remove(file_name) |
| 115 | + break |
| 116 | + else: |
| 117 | + print("Selecciona una de las opciones disponibles.") |
0 commit comments