You could throw all of your prices into a table:
local monitor = peripheral.wrap ("back")
monitor.setTextColor (colors.yellow)
monitor.setBackgroundColor (colors.black)
local prices = {
item1 = { price = 0, quantity = 0 },
item2 = { price = 0, quantity = 0 },
item3 = { price = 0, quantity = 0 }
}
local function writePrices (prices)
monitor.clear()
local lineNumber = 1
for itemName, itemData in pairs (prices) do
monitor.setCursorPos (1, lineNumber)
monitor.write (itemName .. " : $" .. itemData.price .. " each | Quantity: " .. itemData.quantity)
lineNumber = lineNumber + 1
end
end
print ("Displaying info...")
writePrices()
You could also set up the information in a file like:
item1:price=0|quantity=0
item2:price=0|quantity=0
item3:price=0|quantity=0
Then read them like this:
local monitor = peripheral.wrap ("back")
local itemInfoFilePath = "/itemInfo"
monitor.setTextColor (colors.yellow)
monitor.setBackgroundColor (colors.black)
local prices = {}
local function getItemInfo (itemInfoFilePath)
local itemInfoFileHandle = io.open (itemInfoFilePath)
local prices = {}
if itemInfoFileHandle then
for itemData in itemInfoFileHandle:lines() do
local itemName, itemPrice, itemQuantity = itemData:match ("(.-):.-(%d+)|.-(%d+)")
prices[itemName] = { price = itemPrice, quantity = itemQuantity }
end
else
error ("Item info file path was invalid.")
end
return prices
end
local function writePrices (prices)
monitor.clear()
local lineNumber = 1
for itemName, itemData in pairs (prices) do
monitor.setCursorPos (1, lineNumber)
monitor.write (itemName .. " : $" .. itemData.price .. " each | Quantity: " .. itemData.quantity)
lineNumber = lineNumber + 1
end
end
prices = getItemInfo (itemInfoFilePath)
print ("Displaying info...")
writePrices()