Posted 18 July 2012 - 07:31 PM
Hello computercraft forums, can someone please explain to me why isn't this working correctly.
Problem: When borrowing book, the re-assignment of the bookID's value does not actually update to "borrowed". It just simply stays as "here" (unborrowed)
My guess of the error: Possible incorrect use of reassigning the value to key?
If anyone can enlighten me with the actual error/solution to this then I would be absolutely delighted. (Aswell as any other tips, that would be nice as well!) :P/>/>
Problem: When borrowing book, the re-assignment of the bookID's value does not actually update to "borrowed". It just simply stays as "here" (unborrowed)
My guess of the error: Possible incorrect use of reassigning the value to key?
If anyone can enlighten me with the actual error/solution to this then I would be absolutely delighted. (Aswell as any other tips, that would be nice as well!) :P/>/>
function borrowbook()
print("Please enter the book id of the book you want to borrow.")
bookid = read()
if books.bookid==here then
books[bookid] = "borrowed"-- <<<=== for some odd reason, this guy doesn't actually make key "1" 's value into "borrowed"
print("Book: ",bookid," is now borrowed...") -- let user know the book he choice is now borrowed by him/her.
-- Quick debug info
for k, v in ipairs(books) do
print("Book: ", k, " State: ", v)
end
print("---debug seperator line--")
junkvar = tostring(io.read())
-- Quick debug info
else
print("That book is already borrowed by someone.") -- if book is "borrowed" then tell user he can't borrow it.
print(books.bookid) -- Tell user what book he/she tried borrowing
end
sleep(1)
main()
end
function returnbook() -- This function will be fixed/updated once I get "book" function to work.
print("Please enter the book id of the book you want to return.")
unbookid = read()
if books[unbookid]=="borrowed" then
books[unbookid]="here"
else
print("That book is already borrowed by someone.")
end
main()
end
function status()
for k, v in ipairs(books) do
print("Book: ", k, " State: ", v)
end
junkvar = tostring(io.read()) --This is so user can read the list of books' statuses until he/she press a key.
main()
end
function re() -- This actually didn't work... Will remove later.. (Was only used to quick reloading of the program)
shell.run(lib.txt)
end
function main()
term.clear()
term.setCursorPos(1,1)
print("Library Computer (IBOP)")
print("Do you want to borrow a book, return a book or see status of books?")
print("")
term.setCursorPos(1,4)
userchoice = tostring(io.read())
if userchoice == "borrow" then
borrowbook()
elseif userchoice== "return" then
returnbook()
elseif userchoice == "status" then
status()
elseif userchoice == "re" then
re()
end
end
--=============== Function stuff above, program startup code below===========
term.clear()
term.setCursorPos(1,1)
books = {} -- Creates table called "books"
for z=1, 12 do --This guy assigns table "books" some keys and values.
books[z] = "here"
end
main()
-- To do: 1. Add code to borrowing and returning functions to activate dispenser. 2. Add code to open secrect librarian door. 3. Add exit program option for userchoice. 4. change program name to startup incase server restarts. 5.Refine script?