Hello everyone, I have work to do on a basic assignment that requires an ATM type of program working over XMLRPC so there is a server element where the data is held in a Hashtable and retrieved by a client form. I will state I am completely lost in the world of Java and do not understand it at all, as is the same for me with all programming languages. That said I do not want the program writing for me I would like a few pointers on where my program is falling down so I can attempt to work around it and learn from this exercise. So far I have spent a week playing around with the bits of code I have to get about as far as when I started ( We were given a previous exercise and code that founded the basis for this program so we could attempt to work it out)

Thank you for taking the time to even get this far it is much appreciated.

The code I have is as follows

The Client Form is

package XMLRPCClient;

import java.util.Vector;
import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;

public class theClientForm extends javax.swing.JFrame
{
private String serverURL;
private XmlRpcClient client;
private Vector argBankAccounts;
private int BankAccounts;
private Vector argVect;
private String theName;

public String getBankAccounts() 
    {
             try
        {
            theName = txtAccountBalance.getText();
            argBankAccounts = new Vector();
            argBankAccounts.addElement(theName);

            BankAccounts = (Integer)client.execute("GetSize.sendTelNo", argBankAccounts);
            System.out.println(argBankAccounts);
          }
            catch (XmlRpcException exception) {
            System.err.println("JavaClient: XML-RPC Consumer Fault #" +
            Integer.toString(exception.code) + ": " +
                               exception.getCause() + "" + exception.toString());
          } catch (Exception exception) {
            System.err.println("JavaClient: XML-RPC Consumer Fault #" + exception.toString());
          }
        String StrTelNo = Integer.toString(BankAccounts); 
        return StrTelNo;
    }
    public Vector getHash()
    {
    Vector Vect = new Vector();
        try
        {
        argVect = new Vector<String>();
        argVect.addElement("anything");
        Vect = (Vector) client.execute("GetSize.sendHashtable", argVect);
             }
            catch (XmlRpcException exception) {
            System.err.println("JavaClient: XML-RPC Consumer Fault #" +
                               Integer.toString(exception.code) + ": " +
                               exception.getCause() + "" + exception.toString());
        } catch (Exception exception) {
            System.err.println("JavaClient: XML-RPC Consumer Fault #" + exception.toString());
    }
    return Vect;
    }

    public theClientForm() {
        initComponents();

        try
    {
     serverURL = "http://localhost:8085/RPC2";
     client = new XmlRpcClient(serverURL);
}
     catch (Exception e)
     {
         e.printStackTrace();
     }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtAccountBalance = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Enter Pin Number");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Bank Of Jar Var");

        jLabel2.setText("Your Balance Is");

        txtAccountBalance.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtAccountBalance.setText(":");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(145, 145, 145)
                .addComponent(jLabel1)
                .addContainerGap(147, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(170, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtAccountBalance, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(89, 89, 89)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtAccountBalance))
                .addContainerGap(108, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        txtAccountBalance.setText(getBankAccounts());
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new theClientForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel txtAccountBalance;
    // End of variables declaration

}

The Bank Accounts Class in the server package is

package server;

import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;

public class BankAccounts {

private Hashtable BankAccounts = new Hashtable();

public BankAccounts() {
    fillHash();
}


public void fillHash()
{
BankAccounts.put( "0001"     ,    "1025");
}

public int getBankAccounts(String BankAccounts)
{
    int intfoundit;
    String foundit =  (String)BankAccounts.get(BankAccounts);
    intfoundit = Integer.parseInt(foundit);
    return intfoundit;
}

public Vector getBankAccounts(){

Vector vhashtovect = new Vector();
String s = null;
String v = null;
Enumeration enumer; 
Enumeration enumervalue;

enumer = BankAccounts.keys ();
enumervalue = BankAccounts.elements();
while ((enumer.hasMoreElements ()) && (enumervalue.hasMoreElements()))
    {
        s = (String) enumer.nextElement ();
        v = (String) s + enumervalue.nextElement();
        vhashtovect.add(v);
    }
return vhashtovect;
    }

}

And last but not least the XMLRPC Class looks like this

package server;

import org.apache.xmlrpc.WebServer;
public class XMLRPCServer
{
private BankAccounts theHashtable;
private int tNumber = 0;

   public XMLRPCServer()
   {
    theHashtable = new BankAccounts();
   }

public int sendTelNo(String TelRequest)
   {

    tNumber = (Integer)theHashtable.getBankAccounts(TelRequest);
    return tNumber;
   }


      public static void main(String [] args)
   {
      try
      {
               int serverPort = 8085;

                  WebServer server = new WebServer(serverPort);

         //server.addHandler("ourExample", new XMLRPCServer());
         //server.addHandler("GetSize", new XMLRPCServer());
         server.addHandler("GetBankAccounts", new XMLRPCServer());
         server.addHandler("GetHashtable", new XMLRPCServer());

         System.out.println("Server waiting for clients...");
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }
}

Recommended Answers

All 4 Replies

Hi;
it seem that you'r using a deprecated version of the apach xmlrpc;
the class XmlRpcClient in new version (published 15-Feb-2010) is located in the package org.apache.xmlrpc.client instead of org.apache.xmlrpc.XmlRpcClient.
ANd there are some littel changes.

Hope it helps

I made some changes to your BankAccount class. I suggest you revise your program logic though, because somethings are quite peculiar.

import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;

public class BankAccount {

	private Hashtable<String, String> BankAccounts = new Hashtable<String, String>(); // you have to set a type when you initialize
		// a collection

	public BankAccount() {
		fillHash();
	}

	public void fillHash() {
		BankAccounts.put("0001", "1025"); // so you put only one key and one value. or you are trying to put values for
											// keys from 0001 to 1025
	}
	
	// This now works correctly when invokes
	public int getBankAccounts(String searchedForBankAccounts) {
		int intfoundit;
		String foundit = BankAccounts.get(searchedForBankAccounts); // try to use unique variable names it's confusing
		intfoundit = Integer.parseInt(foundit);
		return intfoundit;
	}

	public Vector<String> getBankAccounts() {

		Vector<String> vhashtovect = new Vector<String>();
		String s = null;
		String v = null;
		Enumeration<String> enumer;
		Enumeration<String> enumervalue;

		enumer = BankAccounts.keys();
		enumervalue = BankAccounts.elements();
		while ((enumer.hasMoreElements()) && (enumervalue.hasMoreElements())) {
			s = (String) enumer.nextElement();
			v = (String) s + enumervalue.nextElement();
			vhashtovect.add(v);
		}
		return vhashtovect;
	}

}

Thank you both of you for your help Moutanna I am using an older version of XMLRPC which probably explains why my code is the way it is. It works fine on my program I am attempting to butcher to make this work and it was supplied to us from our lecturer. I will try the newer version a little later.

gmunk Thank you very much for taking the time to write that for me. I am attempting to get it all working now and see what I have wrong in other areas of my code. My logic is extremely flawed and at the present moment I am struggling to get my head around how the entire Java programming way works. I liken my programming experiences to being in a dark tunnel I can see the light but not quite reach it. That said I want to learn and Java is something I need to grasp as I wish to develop my future career in Web Design. Sometimes I just wonder if I am too old to learn these things.

Thank you very much once again for your help it really is appreciated.

Sorry to come back again with my problems. Well I will be back a lot but hopefully not with problems in the future. I have re written my program and have it working I enter the pin number it retrieves the balance from the hashtable correctly. I have been attempting to now get it to display the balance in a decimal format ie 10.25 instead of 1025 but I cannot fathom it out. I have tried using Float and Double and in respect of getting code there that does not have an error highlight I can get there, however when I run the program I recieve a lot of errors and a failure at retrieving the data. I hate to repost my code that I have but I will so people can see the stage I am at. The code that I will post is clean so it makes more sense but in the hashtable I have used code like.

BankAccounts.put( "0002", new Double(1.00));

And then updated the other parts of my program where I have it set to use Integer. I hope my code looks a little better than when I first started posting it yesterday. To me it seems more logical but I could be way off here.

My code is now like this

theClientForm (Please not I have omitted the button text box and form code to save space)

package client;

import java.util.Vector;
import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;

public class theClientForm extends javax.swing.JFrame {

private String serverURL;
private XmlRpcClient client;
private Vector argAccountBalance;
private int Balance;
private String PinNumber;

public String getAccountBalance()
    {
             try
        {
            PinNumber = txtPin.getText();
            argAccountBalance = new Vector();
            argAccountBalance.addElement(PinNumber);
            Balance = (Integer)client.execute("GetSize.sendAccountBalance", argAccountBalance);
            System.out.println(argAccountBalance);
          }
            catch (XmlRpcException exception) {
            System.err.println("JavaClient: XML-RPC Consumer Fault #" +
            Integer.toString(exception.code) + ": " +
                               exception.getCause() + "" + exception.toString());
          } catch (Exception exception) {
            System.err.println("JavaClient: XML-RPC Consumer Fault #" + exception.toString());
          }
        String StrBalance = Integer.toString(Balance);
        return StrBalance;
    }
public theClientForm() {
        initComponents();
    try
    {
     serverURL = "http://localhost:8085/RPC2";
     client = new XmlRpcClient(serverURL);
}
     catch (Exception e)
     {
         e.printStackTrace();
     }
    }       
    @SuppressWarnings("unchecked")]

XMLRPCServer

package server;

import org.apache.xmlrpc.WebServer;
public class XMLRPCServer
{
private BankAccounts theHashtable;
private int tNumber = 0;

   public XMLRPCServer()
   {
    theHashtable = new BankAccounts();
   }
public int sendAccountBalance(String TelRequest)
   {
    tNumber = (Integer)theHashtable.getAccountBalance(TelRequest);
    return tNumber;
   }
      public static void main(String [] args)
   {
      try
      {
      int serverPort = 8085;

         WebServer server = new WebServer(serverPort);        
         server.addHandler("ourExample", new XMLRPCServer());
         server.addHandler("GetSize", new XMLRPCServer());
         server.addHandler("GetTelephone", new XMLRPCServer());
         server.addHandler("GetHashtable", new XMLRPCServer());
         System.out.println("Server waiting for clients...");
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }
}

And the BankAccounts class

package server;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;

public class BankAccounts
{
private Hashtable BankAccounts = new Hashtable();
public BankAccounts()
{
    fillHash();
}
public void fillHash()
{
BankAccounts.put( "0001"     , "1025");

}
public int getAccountBalance(String PinNumber)
{
    int intfoundit;
    String foundit =  (String)BankAccounts.get(PinNumber);
    intfoundit = Integer.parseInt(foundit);
    return intfoundit;
}
public Vector getAccountBalance()
{
Vector vhashtovect = new Vector();
String s = null;
String v = null;
Enumeration enumer;
Enumeration enumervalue;

enumer = BankAccounts.keys ();
enumervalue = BankAccounts.elements();
while ((enumer.hasMoreElements ()) && (enumervalue.hasMoreElements()))
    {

        s = (String) enumer.nextElement ();
        v = (String) s + enumervalue.nextElement();
        vhashtovect.add(v);// add to the vector
    }
return vhashtovect;
    }

}

Thank you again for taking the time to look at this. I did use gmunks code too but after royally destroying my program earlied I had to rewrite it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.