Monday, December 1, 2014

Query of products and stock

This is a java program to print out the quantity and stock when asked by a user. The index value is then sent to the server and the corresponding product and quantity is returned.
This is just a basic outline for people with doubts. You'll have to refine it for your own personal purposes.

CLIENT

import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
public class cli implements Runnable{
Thread th1;
String str1;
int str2;
PrintWriter pw;
int a=0;
BufferedReader br,br1;
String[] prod=new String[100];
int[] quan=new int[100];
public static void main(String args[])
{
cli sup=new cli();
sup.th1=new Thread(sup);
sup.th1.start();
}
public cli()
{
try{
Socket s=new Socket("localhost",3000);
br=new BufferedReader(new InputStreamReader(System.in));
br1=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream(),true);
}catch(IOException e){}
}
public void run()
{
int str;
String ans;
try{
while(true)
{
if(a==1)
{
ans=(br1.readLine());
System.out.println(ans);
}
a=1;
str=Integer.parseInt(br.readLine());
pw.println(str);
System.out.println("Sent");
}}catch(IOException e){}
}
public void prinnt()
{
pw.println(str1+str2);
}
}


SERVER


import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
public class serv implements Runnable{
Thread th1;
String str1;
int str2;
PrintWriter pw;
BufferedReader br;
String[] prod=new String[100];
int[] quan=new int[100];
public static void main(String args[])
{
serv sup=new serv();
sup.th1=new Thread(sup);
sup.th1.start();
}
public serv()
{
try{
ServerSocket ss=new ServerSocket(3000);
Socket s=ss.accept();

prod[0]="Carrot";
quan[0]=5;
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream(),true);
}catch(IOException e){}
}
public void run()
{
int str;
try{
while(true)
{
str=Integer.parseInt(br.readLine());
str1=prod[str];
str2=quan[str];
prinnt();
}}catch(IOException e){}
}
public void prinnt()
{
pw.println(str1+str2);
}
}