This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ClassicRockFan's profile picture

Cash Register Prog. Help

Started by ClassicRockFan, 04 May 2014 - 10:52 PM
ClassicRockFan #1
Posted 05 May 2014 - 12:52 AM
Hey guys, I'm working on a project(cash register) and I want to be able to check the inventory of a chest(with openPeripherals) so that a price can be assigned to an item and stored in a database. I cant figure out which function I need to call to print the items in the chest to the CPU so a price can be assigned. Any ideas? I'm also open to other ideas on how to do it but I don't have any other CC addons aside from openP (Tekkit Galacticraft for MC 1.6.4)
ClassicRockFan #2
Posted 05 May 2014 - 01:06 AM
I meant open CCSensors,but I also have openP.Addons.

And Here is the code…


local regNum = 1
local defaultB = colors.gray
local defaultT = colors.black
local user = " "
local storeName = "Classic's Store"
local menuBoard = "monitor_0"
function resetPost()
term.setBackgroundColor(defaultB)
term.clear()
term.setCursorPos(1,1)
titleBar()
end
function reset()
term.setBackgroundColor(defaultB)
term.clear()
term.setCursorPos(1,1)
end
function titleBar()
term.setBackgroundColor(colors.red)
term.setTextColor(defaultT)
term.setCursorPos(2,1)
term.clearLine()
print("Register Number: "..regNum)
term.setCursorPos(2,2)
term.clearLine()
print("You are logged in as: "..user)
term.setBackgroundColor(defaultB)
end
function login()
reset()
username = {"this slot", "00000"}
name = {"never works", "Admin"}
i = #username
write("Enter Your Cashier ID: ")
user = read()
for i = 1, i do
  if user == username[i] then
   access = true
   user = name[i]
   break
  else
   access = false
  end
end
if access == true then
  print("Logging in...")
  sleep(1)
  cash()
  elseif access == false then
  print("Incorrect username and password combination")
  sleep(2)
  os.reboot()
end
end
function cash()
resetPost()
term.setCursorPos(1,4)
print("[1] Write a Receipt")
print("[2] Do Inventory")
print("[3] Reboot Menu Board")
print("[4] Logout")
term.write("Enter your selection: ")
input = read()
if input == "1" then
  receipt()
elseif input == "2" then
  inventory()
elseif input == "3" then
  menureboot()
elseif input == "4" then
  os.reboot()
else
  print("Invalid Response")
  sleep(2)
  cash()
end
end
local inventoryItems = {"blah", "blah2", "blah3", "blah4", "blah5"}
local price = {"1", "2", "3", "4", "5000"}
function menureboot()
	m = peripheral.wrap(menuBoard)
iitems = #inventoryItems
monWidth,monHeight = m.getSize()
m = peripheral.wrap("monitor_0")
m.clear()
m.setCursorPos(1,1)
m.write("Welcome to")
m.setCursorPos(1,2)
m.write(storeName)
local trueH = iitems/monHeight
local appH = math.ceil(trueH)
if appH > 5 then
  appH = 5
end
m.setTextScale(appH)
for i = 1, iitems do
  m.setCursorPos(1,i+3)
  m.write(inventoryItems[i])
  local totalList = #inventoryItems[i] + #price[i] + 1
  local emptySpaces = monWidth - totalList
  for i = 1, emptySpaces do
   m.write(".")
  end
  m.write(price[i].."$")
end
end
menureboot()
--login()
NOTE: I am aware of the error in line 88. And I'm sorry that I cant figure out how to make it so you can show/hide the code.
Edited on 04 May 2014 - 11:24 PM
Bomb Bloke #3
Posted 05 May 2014 - 03:46 AM
Fairly easy with OpenPeripherals. Say you want to know what's in the first slot of the chest, it'd go something like this:

local chest = peripheral.wrap("back") -- Or where ever.

local mySlot = chest.getStackInSlot(1)

if mySlot then print("There are "..mySlot.qty.." "..thisSlot.name.."s (ID: "..thisSlot.id..":"..thisSlot.dmg..") in the first slot.") end

There'll be another function belonging to "chest" which tells you how many slots it has so you can loop through 'em, but I can't recall it at the moment. Typing this into the command prompt (as opposed to embedding it in a script) should show it to you though:

openp/docs back

Spoiler tags are used to hide code - [spoiler][/spoiler]
ClassicRockFan #4
Posted 05 May 2014 - 08:12 PM
Thank you very much. Now, I need to use this for this project and for another so how would I recreate a table with new and pre-existing data? (I know the function is textutils.unserialize() and serialize() but i dont know how to use them)
Lignum #5
Posted 05 May 2014 - 08:19 PM
Thank you very much. Now, I need to use this for this project and for another so how would I recreate a table with new and pre-existing data? (I know the function is textutils.unserialize() and serialize() but i dont know how to use them)
textutils.serialize(table) gives you the table as a string. textutils.unserialize(string) gives you the table back from the generated string.
ClassicRockFan #6
Posted 05 May 2014 - 09:53 PM
so a sample prog would be

function blah()
  for i = 1, #table do
	 print(table[i]
end
end
local table = {1, 2, 3}
blah()
local foo = textutils.serialize(table)
textutils.unserialize(foo.."4, 5, 6")
blah()

That should print out 1, 2, 3, and then 1, 2, 3, 4, 5, 6?

Then the new question is, how would I save that data all in one file?
Edited on 05 May 2014 - 07:55 PM
Bomb Bloke #7
Posted 06 May 2014 - 12:27 AM
With the fs API.

local myTable = {"this","is","a","table"}

-- Write table to file:
local myFile = fs.open("someFile","w")  -- Prepare a file for writing.
myFile.writeLine(textutils.serialize(myTable))  -- Serialise the table and dump it into the file.
myFile.close()  -- Done with the file for now.

-- Read table from file:
myFile = fs.open("someFile","r")  -- Prepare a file for reading.
myTable = textutils.unserialize(myFile.readAll())  -- Grab the data in the file, turn it into a table again and point myTable at the result.
myFile.close()  -- Done with the file for now.
ClassicRockFan #8
Posted 08 May 2014 - 12:48 AM
Cool, thank you very much