Requirements:
"You are required to develop an application that allows users to register for an online
account with the Kamarad mobile operator. The Kamarad online account will allow
users to top up their account, and to get the credit balance."
Solution:
kamarad.idl
module kamarad {
struct KamaradAccountDetails {
string name;
string address;
string phoneNumber;
string bankAcNumber;
double credit;
string uniqueId;
};
interface OnlineKamarad {
void register(in KamaradAccountDetails accountDetails, out any uniqueId);
void getAccountDetails(in string uniqueId, out KamaradAccountDetails accountDetails);
void topupAccount(in string uniqueId, in double amount)
};
};
OnlineKamaradImplemantation.java
import kamarad.*;
import org.omg.CORBA.*;
import java.util.*;
import java.util.Hashtable;
public class OnlineKamaradImplementation implements OnlineKamaradOperations {
private ORB orb;
//create HashMap to store userData
Hashtable<String, KamaradAccountDetails> hashTable = new Hashtable<String, KamaradAccountDetails>();
public OnlineKamaradImplementation (ORB orb) {
this.orb = orb;
}
public void register (kamarad.KamaradAccountDetails accountDetails, org.omg.CORBA.AnyHolder anyUniqueIdHolder) {
Any anyUniqueId = orb.create_any();
// Add logic to allocate a new unique id for this registration and store the new account
// for subsequent retrieval String uniqueId = UUID.randomUUID().toString();
uniqueId = uniqueId.substring(uniqueId.length() - 6);
anyUniqueId.insert_string(uniqueId);
anyUniqueIdHolder.value = anyUniqueId;
accountDetails.uniqueId = uniqueId;
hashTable.put(uniqueId, accountDetails);
}
public void getAccountDetails (String uniqueId, kamarad.KamaradAccountDetailsHolder accountDetailsHolder) {
// Look up and retrieve the account details for this user
KamaradAccountDetails accountdetails = new KamaradAccountDetails();
// Put the account into a holder object for returning
accountDetailsHolder.value = hashTable.get(uniqueId);
}
public void topupAccount (String uniqueId, double topup) {
// Look up the account details for this user and update the credit by adding the
// specified top-up to the exDsting balance
KamaradAccountDetails accountDetails = hashTable.get(uniqueId);
accountDetails.credit += topup;
}
}
OnlineKamaradRegister.java
import kamarad.*;
import org.omg.CORBA.*;
import java.io.* ;
import java.util.* ;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
public class OnlineKamaradRegister {
public static void main(String args[]) {
try{
ORB orb = ORB.init(args, null);
//Obtaining the object reference to the Name Service org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
//Narrowing the object reference of the Name Service to the correct Type NamingContext rootCtx = NamingContextHelper.narrow(objRef);
//Creating a name of the desired object that we want to get the object reference from the name server NameComponent[] path = new NameComponent[2];
path[0] = new NameComponent("Kamarad", "Context");
path[1] = new NameComponent("OnlineKamarad", "Object");
org.omg.CORBA.Object objRefKamarad = rootCtx.resolve(path);
// Converting the retireved object reference from the Name Service to the correct type OnlineKamarad kamaradRef = OnlineKamaradHelper.narrow (objRefKamarad);
// Prompt user for account details or get them as command line arguments String name = getName();
String address = getAddress();
String phone = getPhoneNumber();
String bankAcNumber ="";
double initialCredit = 0.0;
// Create our account details object KamaradAccountDetails accountDetails = new KamaradAccountDetails(name, address, phone, bankAcNumber, initialCredit, "REGISTER accountThingy");
// Creating an any holder that will hold the unique id parameter from the register operation call AnyHolder anyUniqueIdHolder = new AnyHolder();
// Register the user kamaradRef.register(accountDetails, anyUniqueIdHolder);
// Display the newly allocated user id String uniqueId = anyUniqueIdHolder.value.extract_string();
System.out.println("User registered as: " + uniqueId);
} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
private static String getName(){
String name = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Your Name:");
try{
name = br.readLine();
}
catch (IOException ioe){
System.out.println("Error trying to read your name!");
System.exit(1);
}
return name;
}
private static String getAddress(){
String address = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Your Address:");
try{
address = br.readLine();
}
catch (IOException ioe){
System.out.println("Error trying to read your adress!");
System.exit(1);
}
return address;
}
private static String getPhoneNumber(){
String phoneNumber = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Your Phone Number:");
try{
phoneNumber = br.readLine();
}
catch (IOException ioe){
System.out.println("Error trying to read your phoneNumber!");
System.exit(1);
}
return phoneNumber;
}
}