Solved

Web Service Client Futher Help

Posted on 2004-04-24
8
277 Views
Last Modified: 2008-03-06
Ok,

I was told to keep everything in the form that is needed for the WSDL file in this program, so I've redesigned it so it does that, however, now I need to know how to store these different type variables in an Object array, or for that matter any array that can hold a double and a string or and int and a double.  Can someone please tell me how to do this?  The specific area where this is a problem is after the first three if statements.

package dii;

import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.ParameterMode;
import java.io.*;
import java.lang.*;

public class TransactionClient {

      public static String readLine() {
            StringBuffer response = new StringBuffer();
            try {
                  BufferedInputStream buff = new BufferedInputStream(System.in);
                  int in = 0;
                  char inChar;
                  do {
                        in = buff.read();
                        inChar = (char) in;
                        if (in != -1) {
                              response.append(inChar);
                        }
                  } while ((in != -1) & (inChar != '\n'));
                  buff.close();
                  return response.toString();
            } catch (IOException e) {
                  System.out.println("Exception: " + e.getMessage());
                  return null;
            }
      }

    private static String qnameService = "TransactionService";
    private static String qnamePort = "TransactionIF";

    private static String BODY_NAMESPACE_VALUE = "urn:Foo";
    private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri";
    private static String NS_XSD = "http://www.w3.org/2001/XMLSchema";
    private static String URI_ENCODING = "http://schemas.xmlsoap.org/soap/encoding/";

    public static void main(String[] args) {

        System.out.println("1. Open Account");
            System.out.println("2. Deposit");
            System.out.println("3. Withdrawl");
            System.out.println("0. Exit");
            System.out.println("Selection: ");

            String input = TransactionClient.readLine();
            int selection;
            selection = Integer.parseInt(input.trim());
            
        try {
            ServiceFactory factory = ServiceFactory.newInstance();
            Service service = factory.createService(new QName(qnameService));

            QName port = new QName(qnamePort);

            Call call = service.createCall(port);
            call.setTargetEndpointAddress("http://152.15.21.56:8080/webservice/transaction");

            call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true));
            call.setProperty(Call.SOAPACTION_URI_PROPERTY, "");
            call.setProperty(ENCODING_STYLE_PROPERTY, URI_ENCODING);
            QName QNAME_TYPE_STRING = new QName(NS_XSD, "string");
                  QName QNAME_TYPE_DOUBLE = new QName(NS_XSD, "double");
                  QName QNAME_TYPE_INTEGER = new QName(NS_XSD, "int");
            call.setReturnType(QNAME_TYPE_STRING);

                  if (selection == 1)
                  {
                        System.out.println("Enter name: ");
                        String userName = TransactionClient.readLine();

                        System.out.println("Enter Deposit: ");
                        String initDeposit = TransactionClient.readLine();
                        double initDepositCon = Double.parseDouble(initDeposit);

                        call.setOperationName(new QName(BODY_NAMESPACE_VALUE, "openAcct"));
                        call.addParameter("String_1", QNAME_TYPE_STRING, ParameterMode.IN);
                        call.addParameter("double_2", QNAME_TYPE_DOUBLE, ParameterMode.IN);
                        Object[] params = { userName, initDepositCon };

                        String result = (String)call.invoke(params);
                        System.out.println(result);
                  }
                  else if (selection == 2)
                  {
                        System.out.println("Enter Account: ");
                        String account = TransactionClient.readLine();
                        int accountNum = Integer.parseInt(account);

                        System.out.println("Enter Deposit: ");
                        String deposit = TransactionClient.readLine();
                        double depositCon = Double.parseDouble(deposit);

                        call.setOperationName(new QName(BODY_NAMESPACE_VALUE, "deposit"));
                        call.addParameter("int_1", QNAME_TYPE_INTEGER, ParameterMode.IN);
                        call.addParameter("double_2", QNAME_TYPE_DOUBLE, ParameterMode.IN);
                        Object[] params = { accountNum, depositCon };

                        String result = (String)call.invoke(params);
                        System.out.println(result);
                  }
                  else if (selection == 3)
                  {
                        System.out.println("Enter Account: ");
                        String account = TransactionClient.readLine();
                        int accountNum = Integer.parseInt(account);

                        System.out.println("Enter Withdrawl: ");
                        String withdraw = TransactionClient.readLine();
                        double withdrawCon = Double.parseDouble(withdraw);


                        call.setOperationName(new QName(BODY_NAMESPACE_VALUE, "withdraw"));
                        call.addParameter("int_1", QNAME_TYPE_INTEGER, ParameterMode.IN);
                        call.addParameter("double_2", QNAME_TYPE_DOUBLE, ParameterMode.IN);
                        Object[] params = { accountNum, withdrawCon };

                        String result = (String)call.invoke(params);
                        System.out.println(result);
                  }
                  else if (selection == 0)
                  {
                        System.out.println("Goodbye!");
                  }
        } catch      (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Thx,
Randal
0
Comment
Question by:Azrael37
  • 4
  • 2
  • 2
8 Comments
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10908267
you mean to say , you want to store 2 different data types in a single array..? can u explain u r Q, a bit more..clearly..!

R.K

0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 250 total points
ID: 10908273
any way check this example...and see how its working..and change the code according to u r data types...

   static Console c = new Console();
    //static final int number = 5;//constant value representing how many items to store
    static int number = 0;// how many names are to be processed
    void show(String[] s, int[] g)//s is an array of strings and g is an array of integers
    {
      int i = 0;
      for (i=1; i<=number; i=i+1)
      {
          c.println(s[i] +"         " + g[i]);//print the ith name
      }
    }
   
    double getAverage(int[] g) //requires an integer array as parameter
    {
      int i = 0;// use as a counter
      int sum = 0;//hold the current sum
      for (i = 1; i <= number; i=i+1)
      {
          sum = sum + g[i];
      }
      return (double)(sum)/(double)number;
    }
       
   
    public static void main (String[] args)
    {
      //String[ ] names = new String[50];//declaration of an array that can hold up to  50 names
      String[] names;//declare an array whose length is obtained from the user.
      //int[ ] grades = new int[50];// declaration of an array that can hold up to 50 integers
      int[] grades;// declare an array whoselength is obtained from the user.
      int i = 0;//an integer used for counting
      
      c.println("How many names and grades do you have?");
      number = c.readInt();
      names = new String[number+1];//sets up the array and allows starting at index 1.
      grades = new int[number+1];
      Names member = new Names();// a class member for calling methods
      double average = 0.0;// variable to hold the average of the grades
      
      c.println(" Please enter "+number + " names, one per line, and their grades");// alert user to what will happen
      for (i = 1; i <= number; i=i+1)//do the loop body 10 times
      {
          c.println("Next name?");//ask the user for another name
          names[i]=c.readLine();//get a name and place it in the array at the ith entry position
          c.println("Next grade?");
          grades[i] = c.readInt();
          //c.println(names[i]);//show the names in the array
      }
       
       average = member.getAverage(grades);//call the getAverage method to find the average grade
      
       c.println();// skip a line
       c.println(" Here are the names and grades");//tell what will happen
       member.show(names, grades);//call the method that displays the names
       
       c.println();
       c.println(" The average grade is  " + average);

R.K
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10908607
btw - why is it name + deposit, as opposed to account number + deposit?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10908749
(i know you have an account number + deposit one as well, but surely that's not safe?)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 10908777
>>btw - why is it name

Forget that - i now see that's to *open* an account
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 10908818
What i'd do is store the details in the member variables of these classes. Let me know if you need further clarification:


public abstract class FinancialTransaction {
}

public class OpenAccount extends FinancialTransaction implements IFinancialTransaction {

// name
// initial deposit needed as members
// Call needed for execute

      public Object execute() {
            
            return null; // or something appropriate
      }
}


public class Withdrawal extends FinancialTransaction implements IFinancialTransaction {

// account number
// sum
// Call needed for execute

      public Object execute() {
            
            return null; // or something appropriate
      }
}

public class Deposit extends FinancialTransaction implements IFinancialTransaction {

// account number
// sum
// Call needed for execute

      public Object execute() {
            
            return null; // or something appropriate
      }
}

interface IFinancialTransaction {
      public Object execute();
}
0
 

Author Comment

by:Azrael37
ID: 10909191
CEHJ,

Yes I need further explanation.  I need to know how it will fit into my exisiting code, i.e. please show me what I'm doing wrong.

Thx,
Randal
0
 

Author Comment

by:Azrael37
ID: 10916240
Answered myself.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Passing list of object to Oracle Database Procedure 3 59
Java / Linux and Regular Expressions 11 69
Arrays.asList  VS  ArrayList 4 44
egit plugin on eclipse 8 31
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now