Solved

Web Service Client Futher Help

Posted on 2004-04-24
8
279 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

773 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