Hello,
I’ve only spent about six months using Java, and about 8 months using Corona, but I come from a 20+ year programming background. Though I am sure it’s not perfect, here would be my suggested rewrite. I hope it helps.
Create a new file called mod_accounts.lua and enter the following:
[lua]–< ACCOUNTS LUA MOD
–< Init mod
local this = {}
–< Accounts table
local accounts = {}
–< ContainsUser check
local function containsUserName( userName )
for i=1, #accounts do
local accountUserName = accounts[i].userName
if ( accountUserName == userName ) then
return true
end
end
return false
end
–< Add account
this.addAccount = function( account )
--key
local userName = account.getUserName()
if ( containsUserName( userName ) ) then
return false
else
table.insert( accounts, account )
return true
end
return false
end
–< Remove account
this.removeAccount = function( userName )
for i=1, #accounts do
local accountUserName = accounts[i].userName
if ( accountUserName == userName ) then
table.remove( accounts, i )
return true
end
end
return false
end
–< Total number of accounts
this.getTotalNumberOfAccounts = function()
return #accounts
end
–< Get account
this.getAccount = function( userName )
for i=1, #accounts do
local account = accounts[i]
if ( account.userName == userName ) then
return account
end
end
return false
end
–< Get all accounts
this.getAllAccounts = function()
return accounts
end
–< Account object constructor
this.newAccount = function( userName, password )
local account = {}
account.userName = userName
account.password = password
account.getUserName = function()
return account.userName
end
return account
end
–< Return mod
return this[/lua]
To test, create a file named main.lua and enter the following:
[lua]–< ACCOUNTS LIB TESTS
–< REQUIRE MOD
local AccountLib = require( “mod_accounts” )
–< TESTS
print( “ACCOUNTS MOD TESTS” )
–< print accounts amount
print( “ACCOUNTS”, AccountLib.getTotalNumberOfAccounts() )
–< create new account “object”
local account = AccountLib.newAccount( “Chris”, “1234” )
–< print account userName
print( “userName”, account.getUserName() )
–< add to accounts
AccountLib.addAccount( account )
–< print accounts amount
print( “ACCOUNTS”, AccountLib.getTotalNumberOfAccounts() )
–< create and add more accounts
local account1 = AccountLib.newAccount( “Sandra”, “abcd” )
local account2 = AccountLib.newAccount( “David”, “9z8y7x” )
AccountLib.addAccount( account1 )
AccountLib.addAccount( account2 )
–< loop through accounts
local accounts = AccountLib.getAllAccounts()
for i=1, #accounts do
local account = accounts[i]
print( “USER”, account.userName, “PASSWORD”, account.password )
end
–< print accounts amount
print( “ACCOUNTS”, AccountLib.getTotalNumberOfAccounts() )[/lua]
Make sure both of those files are in the same directory and then run main.lua in the Corona simulator and check the output.
I come from a background in ActionScript, PHP, and a little Java. Throw out any ideas about privates, classes, inheritance, types, etc. Spend time learning about LUA tables. They are the only “object” type and can be as simple or as complex as you want. But there are some strange permutations so be sure to read up on them ( here is some helpful info ). Also be sure to look into how scope works in LUA.
Again, I’m still learning as well so I hope others can comment on other/better ways to approach this.
Cheers.