102 lines
2.3 KiB
Python
102 lines
2.3 KiB
Python
from typing import cast
|
|
|
|
|
|
def loadFile(filename: str):
|
|
in_file = open(filename, "r")
|
|
items = {}
|
|
|
|
for item in in_file:
|
|
ispl = item.split()
|
|
iname = ispl[0].strip()
|
|
cost = float(ispl[1])
|
|
count = int(ispl[2])
|
|
|
|
items[iname] = [cost, count]
|
|
|
|
in_file.close()
|
|
return items
|
|
|
|
|
|
def saveFile(filename: str, items: dict):
|
|
out_file = open(filename, "w")
|
|
|
|
for key in items:
|
|
out_file.write(key + "\t" + str(items[key][0]) + "\t" + str(items[key][1]) + "\n")
|
|
|
|
out_file.close()
|
|
|
|
|
|
def handleCommand(items: dict):
|
|
command = input("Inserisci un comando: ")
|
|
|
|
if command == "XXXXXX":
|
|
return False
|
|
|
|
command = command.split()
|
|
|
|
if len(command) != 2:
|
|
print("Comando non valido!")
|
|
return True
|
|
|
|
key = command[0]
|
|
op = command[1][0]
|
|
|
|
try:
|
|
delta = int(command[1][1:])
|
|
except ValueError:
|
|
print("Valore numerico non valido")
|
|
return True
|
|
|
|
if key not in items:
|
|
print("Il prodotto non è presente nel database!")
|
|
return True
|
|
|
|
if op != "+" and op != "-":
|
|
print("Operazione non valida")
|
|
return True
|
|
|
|
oldVal = items[key][1]
|
|
|
|
if op == "+":
|
|
newVal = oldVal + delta
|
|
if newVal > 10000:
|
|
print("Quantità di prodotto superiore al valore massimo")
|
|
return True
|
|
|
|
items[key][1] = newVal
|
|
print("Incrementa la quantità di prodotto", key, "di", delta)
|
|
else:
|
|
newVal = oldVal - delta
|
|
if newVal < 0:
|
|
print("Quantità di prodotto non disponibile")
|
|
return True
|
|
|
|
items[key][1] = newVal
|
|
print("Decrementa la quantità di prodotto", key, "di", delta)
|
|
|
|
print("Valore complessivo precedente:", oldVal)
|
|
print("Valore complessivo attuale", newVal)
|
|
|
|
return True
|
|
|
|
|
|
def main():
|
|
in_file_name = input("Inserisci il nome del vecchio file: ")
|
|
try:
|
|
items = loadFile(in_file_name)
|
|
except FileNotFoundError:
|
|
exit("Errore! File di input non trovato")
|
|
except:
|
|
exit("File di input malformato!")
|
|
|
|
while handleCommand(items):
|
|
pass
|
|
|
|
out_file_name = input("Inserisci il nome del nuovo file: ")
|
|
|
|
try:
|
|
saveFile(out_file_name, items)
|
|
except FileNotFoundError:
|
|
exit("Errore! File di output non trovato")
|
|
|
|
main() |