Posted 01 February 2014 - 10:29 PM
Hi, i have a problem D:, im making an ATM working program with credit cards system using immimbis misc peripherals
I have a problem with the table that im using as a database for storing info(credit card number, name, pin and balance)
I have a database: (this is the default one)
.DATABASE DEFAULT
That is the database, in the ATM program, i load it with this
and i do changes in the fly with this
then, i save the database with this
My question is why i get this error: :176: '<name>' expected near '['
Im creating a new entry with database[tostring(srandom)] (srandom is math.random(0000000000, 9999999999), redefined at the start of the program, here is the complete program:)
I have a problem with the table that im using as a database for storing info(credit card number, name, pin and balance)
I have a database: (this is the default one)
.DATABASE DEFAULT
local database = {
["9999999999"] = {
pin = 1014;
balance = 100;
name = "quique18c";
};
["9999999999"] = {
pin = 4321;
balance = 100;
name = "test";
};
}
That is the database, in the ATM program, i load it with this
local file = fs.open('.database','r')
local database = textutils.unserialize(file.readAll())
file.close()
and i do changes in the fly with this
database[tostring(srandom)] = {
pin = 0;
balance = 0;
name = sinput;
} -- I create a new entry
database[tostring(srandom)].balance = 0
database[tostring(srandom)].pin = tonumber(input2) -- i change the values
database[tostring(srandom)].name = sinput
then, i save the database with this
local file = fs.open('.database','w')
file.write(textutils.serialize(database))
file.close()
My question is why i get this error: :176: '<name>' expected near '['
Im creating a new entry with database[tostring(srandom)] (srandom is math.random(0000000000, 9999999999), redefined at the start of the program, here is the complete program:)
local reader = peripheral.wrap("left")
local modem = peripheral.wrap("right")
local printer = peripheral.wrap("bottom")
local cmdblock = peripheral.wrap("back")
local vsnm = "0.4"
os.pullEvent = os.pullEventRaw
local function cprint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end
local function cwrite(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
write(text)
end
local function csp(text, var1)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
textutils.slowPrint(text, var1)
end
function clear()
term.clear()
term.setCursorPos(1, 1)
end
local function dateprint(text)
print(os.day()..":"..os.time().." -"..text)
end
local function wite(number, name)
reader.setInsertCardLight(true)
repeat
reader.beginWrite(number, name)
local event = os.pullEvent()
if event == "mag_write_done" then
reader.setInsertCardLight(false)
cprint("Done")
cprint("Card data:")
cprint(number.." - "..name)
sleep(3)
clear()
startup()
end
until event == "mag_write_done"
end
function startup()
local file = fs.open('.database','r')
local database = textutils.unserialize(file.readAll())
file.close()
if term.isColor() == true then
term.setTextColor(32)
end
local srandom = math.random(1000000000, 9999999999)
cprint("=================================")
cprint("======Quique's ATM V."..vsnm.."======")
cprint("=================================")
cprint("What do you want?")
cprint("1 - Make a new credit card")
cprint("2 - Check balance")
cprint("3 - Withdraw money")
cprint("4 - Deposit money - comming soon")
cprint("5 - Transfer money - comming soon")
cwrite("Option:")
finput = read()
if finput == "1" then
cprint("Selected option 1")
sleep(0.5)
clear()
uno()
elseif finput == "2" then
cprint("Selected option 2")
sleep(0.5)
clear()
dos()
elseif finput == "3" then
cprint("Selected option 3")
sleep(0.5)
clear()
tre()
elseif finput == "4" then
cprint("Selected option 4")
sleep(0.5)
clear()
cua()
elseif finput == "5" then
cprint("Selected option 5")
sleep(0.5)
clear()
cin()
else
cprint(finput.."Isnt an option!")
sleep(0.5)
clear()
startup()
end
end
function uno()
cprint("Important info, read ALL LINES")
cprint("Before making a new credit card, craft one")
cprint("If u have a blank credit card now, type next")
cprint("If not, type cancel to cancel operation")
tinput = read()
if tinput == "next" then
clear()
uno2()
elseif tinput == "cancel" then
cprint("Operation canceled")
sleep(1)
clear()
startup()
else
cprint(tinput.."isnt an option!")
sleep(0.5)
clear()
uno()
end
end
function uno2()
cprint("You have a credit card.")
cprint("Please enter your playername")
cprint("DO NOT FAIL THIS")
cprint("Or type cancel to cancel")
cprint("Example: quique18c")
cwrite("Name:")
sinput = read()
if sinput == database then
cprint("Already used.")
sleep(1)
clear()
uno2()
elseif sinput == "cancel" then
cprint("Operation canceled")
sleep(0.5)
clear()
startup()
else
cprint("Name: "..sinput)
cprint("Now, type your pin (MAX 4 DIGITS)")
input2 = read()
if input2 == tonumber(input2) > 9999 then
cprint("4 digits max, please")
sleep(1)
clear()
uno2()
else
clear()
cprint("Name: "..sinput)
cprint("Pin: "..input2)
cprint("Please, enter your card")
database[tostring(srandom)] = {
pin = 0;
balance = 0;
name = sinput;
}
database[tostring(srandom)].balance = 0
database[tostring(srandom)].pin = tonumber(input2)
database[tostring(srandom)].name = sinput
local file = fs.open('.database','w')
file.write(textutils.serialize(database))
file.close()
wite(srandom, sinput)
end
end
end
function dos()
cprint("Please, enter your card")
repeat
reader.setInsertCardLight(true)
local event,arg1,arg2 = os.pullEvent()
if event == "mag_swipe" then
if tostring(arg1) == database then
cprint("Balance:")
cprint("$"..database.[tostring(arg1)].balance)
sleep(2)
reader.setInsertCardLight(false)
clear()
startup()
else
cprint("Invalid Card")
sleep(1)
reader.setInsertCardLight(false)
clear()
startup()
end
end
until event == "mag_swipe"
end
function tre()
cprint("Please insert your card")
repeat
reader.setInsertCardLight(true)
local event,arg1,arg2 = os.pullEvent()
if event == "mag_swipe" then
nmb = tostring(arg1)
if nmb == database then
reader.setInsertCardLight(false)
clear()
cprint("Welcome, "database.[nmb].name)
cprint("Insert Your PIN")
cwrite("Pin:")
jinput = read("*")
if jinput == database.[nmb].pin then
cprint("Valid Pin!")
sleep(1)
clear()
cprint("How much money do you want to withdraw?")
cwrite(" ")
kinput = read()
if tonumber(kinput) > database.[nmb].balance then
cprint("U cant withdraw money that you dont have!")
sleep(2)
clear()
startup()
elseif tonumber(kinput) < 0 then
cprint("U cant withdraw negative money!")
sleep(1)
clear()
startup()
else
cprint("Operation in progress")
csp("#==================#", 4)
sleep(5)
database.[nmb].balance = database.[nmb].balance - tonumber(kinput)
cmdblock.setCommand("/eco give @p "..kinput)
cmdblock.runCommand()
cmdblock.setCommand(" ")
cprint("Operation completed")
local file = fs.open('.database','w')
file.write(textutils.serialize(database))
file.close()
sleep(2)
clear()
startup()
end
else
cprint("Invalid Pin!")
sleep(1)
clear()
startup()
end
else
cprint("Invalid Card")
sleep(1)
reader.setInsertCardLight(false)
clear()
startup()
end
end
until event == "mag_swipe"
end
function cua()
cprint("Comming soon")
cprint("Quique's ATM updates automatically, dont worry")
cprint("Current version:" ..vsnm)
sleep(2)
clear()
startup()
end
function cin()
cprint("Comming soon")
cprint("Quique's ATM updates automatically, dont worry")
cprint("Current version:" ..vsnm)
sleep(2)
clear()
startup()
end