from Java to Lua - help me get my head around it

Hi,

I have studied Java for the past three years in uni, and now I’m learning Lua because of Corona, but I’m having some trouble to get my head into it.

My problem is with object and collections, if someone could write the equivalent in Lua of this Java code would help me a lot.

Thanks.

public class Account { private String username; private String password; // Account Object Constructor public Account(String username, String password) { this.username = username; this.password = password; } // Get username public String getUsername() { return username; } }

public class Library { Map\<String, DatingAccount\> accounts; // Library Constructor public Library() { accounts = new HashMap\<String, Account\>(); } // Add account public boolean addAccount(Account account) { String key = account.getUsername(); if (accounts.containsKey(key)) { return false; } else { accounts.put(key, account); return true; } } // Remove account public boolean removeAccount(String username) { if (accounts.get(username) != null) { accounts.remove(username); return true; } else { return false; } } // Get number of accounts public int getTotalNumberOfAccounts() { return accounts.size(); } // Get Account public Account getAccount(String username) { return accounts.get(username); } // Get All Accounts public Set\<Account\> getAllAccounts() { Set\<Account\> accountSet = new HashSet\<Account\>(); Set\<String\> theKeys = accounts.keySet(); for (String username: theKeys) { Account theAccount = accounts.get(username); accountSet.add(theAccount); } return accountSet; } }

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.

Thank you very much, I really appreciate it.

You’re very welcome.  :wink:

Cheers.

Thanks again for develephant, here the classes translated individually for clarity. 

Account class in Lua

Account = {} -- constructor function Account:new( username, password ) local account = {} -- this local function reduce the scope to make variable private local function init( username, password ) account.username = username account.password = password end -- get username function account.getUsername() return account.username end -- check password function account.verifyPassword( passwordIn ) if account.password == passwordIn then return true else return false end end -- get name function account.getName() return account.name end -- set name function account.setName( name ) account.name = name end init( username, password ) return account end return Account

Library in Lua

require "Account" Library = {} -- constructor function Library:new() local accounts = {} -- contains username check local function containsAccount( username ) for i=1, #accounts do local account = accounts[i].getUsername() if ( account == username ) then return true end end return false end -- add account function accounts.addAccount( account ) -- key local username = account.getUsername() if ( containsAccount( username ) ) then return false else table.insert( accounts, account ) return true end end -- remove account function accounts.removeAccount( username ) for i=1, #accounts do local accountUsername = accounts[i].getUsername() if ( accountUsername == username ) then table.remove( accounts, i ) return true end end return false end -- verify Log In function accounts.verifyLogin( username, password ) if containsAccount( username ) then account = accounts.getAccount( username ) if account.verifyPassword( password ) then print("password verified") return true else print("username and password not matching") return false end else print("verify login for", username, "is null") return false end end -- get number of accounts function accounts.getTotalNumberOfAccounts() return #accounts end -- get Account function accounts.getAccount( username ) for i=1, #accounts do local account = accounts[i] if ( account.getUsername() == username ) then return account end end return false end -- get All Accounts function accounts.getAllAccounts() return accounts end return accounts end return Library

Test them

require "Account" require "Library" -- create account account1 = Account:new("john", "6789") account2 = Account:new("fran", "1234") -- create collection accounts = Library:new() -- ACCOUNT OBJECT METHODS -- get username print( "username", account1.getUsername() ) -- check password print( account1.verifyPassword("6789") ) -- set name account1.setName("Johnny") -- get name print("name", account1.getName() ) -- LIBRARY COLLECTION METHODS -- add account accounts.addAccount( account1 ) accounts.addAccount( account2 ) -- remove account accounts.removeAccount( "john" ) -- verify Log In print( accounts.verifyLogin("fran", "1234") ) -- get number of accounts print( accounts.getTotalNumberOfAccounts() ) -- get Account acc = accounts.getAccount("fran") -- get All Accounts allAccounts = accounts.getAllAccounts()

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.

Thank you very much, I really appreciate it.

You’re very welcome.  :wink:

Cheers.

Thanks again for develephant, here the classes translated individually for clarity. 

Account class in Lua

Account = {} -- constructor function Account:new( username, password ) local account = {} -- this local function reduce the scope to make variable private local function init( username, password ) account.username = username account.password = password end -- get username function account.getUsername() return account.username end -- check password function account.verifyPassword( passwordIn ) if account.password == passwordIn then return true else return false end end -- get name function account.getName() return account.name end -- set name function account.setName( name ) account.name = name end init( username, password ) return account end return Account

Library in Lua

require "Account" Library = {} -- constructor function Library:new() local accounts = {} -- contains username check local function containsAccount( username ) for i=1, #accounts do local account = accounts[i].getUsername() if ( account == username ) then return true end end return false end -- add account function accounts.addAccount( account ) -- key local username = account.getUsername() if ( containsAccount( username ) ) then return false else table.insert( accounts, account ) return true end end -- remove account function accounts.removeAccount( username ) for i=1, #accounts do local accountUsername = accounts[i].getUsername() if ( accountUsername == username ) then table.remove( accounts, i ) return true end end return false end -- verify Log In function accounts.verifyLogin( username, password ) if containsAccount( username ) then account = accounts.getAccount( username ) if account.verifyPassword( password ) then print("password verified") return true else print("username and password not matching") return false end else print("verify login for", username, "is null") return false end end -- get number of accounts function accounts.getTotalNumberOfAccounts() return #accounts end -- get Account function accounts.getAccount( username ) for i=1, #accounts do local account = accounts[i] if ( account.getUsername() == username ) then return account end end return false end -- get All Accounts function accounts.getAllAccounts() return accounts end return accounts end return Library

Test them

require "Account" require "Library" -- create account account1 = Account:new("john", "6789") account2 = Account:new("fran", "1234") -- create collection accounts = Library:new() -- ACCOUNT OBJECT METHODS -- get username print( "username", account1.getUsername() ) -- check password print( account1.verifyPassword("6789") ) -- set name account1.setName("Johnny") -- get name print("name", account1.getName() ) -- LIBRARY COLLECTION METHODS -- add account accounts.addAccount( account1 ) accounts.addAccount( account2 ) -- remove account accounts.removeAccount( "john" ) -- verify Log In print( accounts.verifyLogin("fran", "1234") ) -- get number of accounts print( accounts.getTotalNumberOfAccounts() ) -- get Account acc = accounts.getAccount("fran") -- get All Accounts allAccounts = accounts.getAllAccounts()