Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Web Service Client Futher Help

Posted on 2004-04-24
8
Medium Priority
?
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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
 
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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

722 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