The sample contract serves as a demonstration of general QVM logic.
It is capable of:
registering users into the database
storing the last registered user's name in the database
echoing the previous user's name to STDOUT
incrementing the total registered user count
storing above counter in the database
Sample code
publicclasscontract {staticpublicvoidmain(String[] args) {// THIS SAMPLE ONLY SUPPORTS THE "register" FUNCTIONif (args.length==2&& args[0].equals("register")) {// GET THE CURRENT USER'S NAMEString previousName =System.getenv("DB_USER_CURRENT");// OR DEFAULT TO "unknown" IF THIS IS THE FIRST CALLif (previousName ==null||previousName.length() ==0) { previousName ="unknown"; }// GET THE TOTAL USER COUNTint totalUserCount =envAtoi("DB_TOTALUSERS");// WRITE PREVIOUS USER NAME TO STDOUTSystem.out.println("OUT=prevname: "+ previousName);// UPDATE CURRENT USER NAME BY WRITING IT TO DBSystem.out.println("DBW=USER_CURRENT="+ args[1]);// STORE USER NAME UNDER A STORAGE SLOT FOR PERSISTENCE (CURRENT GETS OVERWRITTEN ON EACH CALL)System.out.println("DBW=USER_"+ totalUserCount +"="+ args[1]);// INCREMENT THE TOTAL USER COUNTSystem.out.println("DBW=TOTALUSERS="+ (totalUserCount+1));return; }// ENSURE PROPER INPUTif (args.length>=1) {System.err.println("Wrong CMD: "+ args[0]);System.exit(1); }System.err.println("Wrong args!");System.exit(1); };staticintenvAtoi(String env) {String val =System.getenv(env);if (val ==null||val.length() ==0) {return0; }try {returnInteger.valueOf(val); } catch (Exception e) {return0; } };};
Save the contract
Open a text editor and save above sample contract as contract.java file.