Skip to content

Instantly share code, notes, and snippets.

@Alexior3000
Created August 9, 2024 15:43
Show Gist options
  • Save Alexior3000/b6d3db4a83060db5036319a9a2c6c12c to your computer and use it in GitHub Desktop.
Save Alexior3000/b6d3db4a83060db5036319a9a2c6c12c to your computer and use it in GitHub Desktop.
-- Funkcja do uzyskania aktualnej daty i godziny w formacie YYYY-MM-DD_HH-MM-SS
local function getTimestamp()
local date = os.date("*t") -- Pobiera aktualną datę i godzinę
return string.format("%04d-%02d-%02d_%02d-%02d-%02d",
date.year, date.month, date.day,
date.hour, date.min, date.sec)
end
-- Generuje nazwę pliku na podstawie daty i godziny
local function generateFilename()
local timestamp = getTimestamp()
return string.format("paragon-%s.txt", timestamp) -- Zapisuje w bieżącym katalogu
end
-- Funkcja do konwersji wartości dziesiętnej z przecinkiem na kropkę
local function convertDecimal(value)
if type(value) ~= "string" then
return nil
end
local converted = value:gsub(",", ".")
local number = tonumber(converted)
return number
end
-- Otwórz plik do zapisu
local filename = generateFilename()
local file = io.open(filename, "w")
-- Funkcja do drukowania nagłówka
local function printHeader()
file:write("ELEMENT\n")
file:write("------------------------------\n")
end
-- Funkcja do drukowania szczegółów transakcji
local function printDetails(product, qty, price)
local total = qty * price
file:write(string.format("Product: %s Qty: %d Price: %.2f Cost: %.2f\n", product, qty, price, total))
return total
end
-- Funkcja do drukowania łącznej kwoty
local function printTotal(total)
file:write("------------------------------\n")
file:write(string.format("Total: %.2f\n", total))
end
-- Drukuj nagłówek paragonu
printHeader()
local grandTotal = 0
-- Główna pętla programu
while true do
-- Wczytaj produkt od użytkownika
print("Press Enter to continue...")
io.read() -- Czekaj na naciśnięcie Enter
-- Wczytaj dane produktu
io.write("Product: ")
local product = io.read()
-- Sprawdź, czy użytkownik wpisał "OK1", jeśli tak, zakończ program
if product == "OK1" then
break
end
-- Wczytaj ilość i cenę
io.write("Quantity: ")
local qtyInput = io.read()
io.write("Price: ")
local priceInput = io.read()
-- Konwertuj wartości wejściowe
local qty = tonumber(qtyInput)
local price = convertDecimal(priceInput)
-- Sprawdź, czy wartości są poprawne
if qty and price then
-- Oblicz koszt i zaktualizuj całkowity koszt
local total = printDetails(product, qty, price)
grandTotal = grandTotal + total
else
print("Invalid input. Please enter numeric values for quantity and price.")
end
end
-- Zapisz całkowity koszt
printTotal(grandTotal)
-- Zamknij plik
file:close()
print("Paragon saved to " .. filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment