Skip to content

Commit a47b826

Browse files
#11 - Python
1 parent 5ec3492 commit a47b826

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import os
2+
3+
text = """Jheison Duban Quiroga Quintero
4+
26
5+
Python"""
6+
7+
with open("JheisonQuiroga.txt", "w") as f:
8+
f.write(text)
9+
10+
file = "JheisonQuiroga.txt"
11+
12+
with open(file) as f:
13+
if os.path.exists("./JheisonQuiroga.txt"):
14+
text = f.read()
15+
print(text)
16+
else:
17+
print("The file is not exists")
18+
19+
20+
""" Extra """
21+
22+
my_file = "sells.txt"
23+
products = []
24+
25+
26+
def add_product(name:str, sell_total:int, price:int):
27+
if not os.path.exists(my_file):
28+
with open(my_file, "x") as file:
29+
file.write(f"{name}, {sell_total}, {price}\n")
30+
products.append({
31+
"name": name,
32+
"sell_total": sell_total,
33+
"price": price
34+
})
35+
else:
36+
with open(my_file, "a") as file:
37+
file.write(f"{name}, {sell_total}, {price}\n")
38+
products.append({
39+
"name": name,
40+
"sell_total": sell_total,
41+
"price": price
42+
})
43+
44+
45+
def get_info():
46+
print("-" * 5, "Inventario", "-" * 5)
47+
for dct in products:
48+
print(f"{dct['name']}, {dct['sell_total']}, {dct['price']}")
49+
50+
51+
def update(name, new_item_sell, new_price):
52+
with open(my_file, "w") as file:
53+
for product in products:
54+
if product["name"] == name:
55+
product["sell_total"] = new_item_sell
56+
product["price"] = new_price
57+
file.write(f"{product["name"]}, {product["sell_total"]}, {product["price"]}\n")
58+
59+
def remove_products(name):
60+
global products # Para asegurarme de borrar la lista original
61+
62+
filter_products = [product for product in products if product["name"] != name]
63+
64+
products = filter_products
65+
# Sobreescribe el archivo con los productos filtrados
66+
with open(my_file, "w") as file:
67+
for product in products:
68+
file.write(f"{product['name']}, {product['sell_total']}, {product['price']}\n")
69+
70+
def get_product():
71+
name = input("Nombre del producto: ")
72+
item_sell = int(input("Total de ventas: "))
73+
price = int(input("Precio: "))
74+
return name, item_sell, price
75+
76+
def sales_calculate():
77+
global products
78+
total_sales = 0
79+
for product in products:
80+
total_sale_per_product = product["sell_total"] * product["price"]
81+
total_sales += total_sale_per_product
82+
83+
return total_sales
84+
85+
def remove_file(): # Funcion para borrar archivo al salir
86+
if os.path.exists(my_file):
87+
os.remove(my_file)
88+
print("El archivo ha sido borrado!")
89+
90+
91+
def main():
92+
93+
while True:
94+
option = int(input("""----- Sistema de Gestión de ventas -----
95+
1. Agregar producto
96+
2. Consultar inventario
97+
3. Actualizar
98+
4. Eliminar producto
99+
5. Total ventas
100+
6. Venta por producto
101+
7. Salir
102+
Elige una opcion: """))
103+
104+
match option:
105+
case 1:
106+
name, item_sell, price = get_product()
107+
add_product(name, item_sell, price)
108+
case 2:
109+
get_info()
110+
case 3:
111+
name, item_sell, price = get_product()
112+
update(name, item_sell, price)
113+
case 4:
114+
name = input("Ingrese el nombre del producto a eliminar")
115+
remove_products(name)
116+
case 5:
117+
print(sales_calculate())
118+
case 6:
119+
name = input("Nombre del producto: ")
120+
with open(my_file, "r") as file:
121+
for line in file.readlines():
122+
components = line.split(", ")
123+
if name == components[0]:
124+
quantity = int(components[1])
125+
price = int(components[2])
126+
total = quantity * price
127+
print(total)
128+
break
129+
130+
case 7:
131+
remove_file()
132+
print("Saliendo del programa...")
133+
return
134+
135+
136+
main()

0 commit comments

Comments
 (0)