-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsistema_inventario.py
More file actions
107 lines (85 loc) · 3.61 KB
/
Copy pathsistema_inventario.py
File metadata and controls
107 lines (85 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
class Producto:
def __init__(self, nombre, precio, cantidad):
if nombre == "":
raise ValueError("El nombre del producto no puede estar vacío.")
if precio < 0:
raise ValueError("El precio del producto no puede ser negativo.")
if cantidad < 0:
raise ValueError("La cantidad del producto no puede ser negativa.")
self.nombre = nombre
self.precio = precio
self.cantidad = cantidad
def actualizar_precio(self, nuevo_precio):
if nuevo_precio < 0:
raise ValueError("El precio del producto no puede ser negativo.")
self.precio = nuevo_precio
def actualizar_cantidad(self, nueva_cantidad):
if nueva_cantidad < 0:
raise ValueError("La cantidad del producto no puede ser negativa.")
self.cantidad = nueva_cantidad
def calcular_valor_total(self):
return self.precio * self.cantidad
def __str__(self):
return f"Producto: {self.nombre}, Precio: {self.precio}, Cantidad: {self.cantidad}"
class Inventario:
def __init__(self):
self.productos = {}
def agregar_producto(self, producto):
if producto.nombre.lower() in self.productos:
raise ValueError("El producto ya existe en el inventario.")
self.productos[producto.nombre.lower()] = producto
def buscar_producto(self, nombre_producto):
return self.productos.get(nombre_producto.lower(), None)
def calcular_valor_inventario(self):
valor_total = 0
for producto in self.productos.values():
valor_total += producto.calcular_valor_total()
return valor_total
def listar_productos(self):
return [str(producto) for producto in self.productos.values()]
def menu_principal():
inventario = Inventario()
while True:
print("\n--- Menú de Inventario ---")
print("1. Agregar Producto")
print("2. Buscar Producto")
print("3. Listar Productos")
print("4. Calcular Valor Total del Inventario")
print("5. Salir")
opcion = input("Seleccione una opción: ")
if opcion == "1":
try:
nombre = input("Ingrese el nombre del producto: ")
precio = float(input("Ingrese el precio del producto: "))
cantidad = int(input("Ingrese la cantidad del producto: "))
nuevo_producto = Producto(nombre, precio, cantidad)
inventario.agregar_producto(nuevo_producto)
print("Producto agregado exitosamente.")
except ValueError as e:
print(f"Error: {e}")
except TypeError:
print("Error: Tipo de dato incorrecto.")
elif opcion == "2":
nombre = input("Ingrese el nombre del producto a buscar: ")
producto = inventario.buscar_producto(nombre)
if producto:
print(producto)
else:
print("Producto no encontrado.")
elif opcion == "3":
productos = inventario.listar_productos()
if productos:
for prod in productos:
print(prod)
else:
print("No hay productos en el inventario.")
elif opcion == "4":
valor_total = inventario.calcular_valor_inventario()
print(f"Valor total del inventario: {valor_total}")
elif opcion == "5":
print("Saliendo del sistema de inventario.")
break
else:
print("Opción no válida. Por favor, intente de nuevo.")
if __name__ == "__main__":
menu_principal()