test.main1.java Source code

Java tutorial

Introduction

Here is the source code for test.main1.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import static java.lang.System.exit;
import java.util.ArrayList;
import static javafx.application.Platform.exit;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
 *
 * @author Bisan Co
 */
@WebService(serviceName = "main1")
public class main1 {
    private static JSONTokener jsonOut;
    ArrayList<user> users = new ArrayList<user>();

    public static void main(String myHelpers[]) throws JSONException, IOException {

        File f = new File(
                "C:\\Users\\Bisan Co\\Documents\\NetBeansProjects\\WebApplication1\\src\\java\\test\\newjson.json");

        String jsonString = readFile(f.getPath());

        jsonOut = new JSONTokener(jsonString);
        JSONObject output = new JSONObject(jsonOut);
        ArrayList<user> users = new ArrayList<user>();

        String test1 = "user";
        for (int i = 1; i < 5; i++) {
            user test = new user();
            String id = (String) output.getJSONObject(test1 + i).get("-ID");
            test.setID(id);
            String name = (String) output.getJSONObject(test1 + i).get("Name");
            test.setName(name);
            String dop = (String) output.getJSONObject(test1 + i).get("DOB");
            test.setDOP(dop);
            String email = (String) output.getJSONObject(test1 + i).get("Email");
            test.setEmail(email);
            String title = (String) output.getJSONObject(test1 + i).get("Title");
            test.setTitle(title);
            String phone = (String) output.getJSONObject(test1 + i).get("Phone-no.");
            test.setPhoneNo(phone);
            JSONArray Friends = (JSONArray) output.getJSONObject(test1 + i).getJSONArray("Friends");
            friend test2 = new friend();
            location test3 = new location();
            for (int j = 0; j < Friends.length(); j++) {
                String id1 = Friends.getJSONObject(j).getString("-ID");
                test2.setID(id1);
                String name1 = Friends.getJSONObject(j).getString("name");
                test2.setName(name1);
                String loca = Friends.getJSONObject(j).getString("location");
                test3.setGPS(loca);
                String add = Friends.getJSONObject(j).getString("address");
                test3.setADD(add);
                test2.setLocation(test3);
                test.AddFriend(test2);
            }
            users.add(test);
        }
    }

    private static String readFile(String file) throws IOException {

        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");

        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
            stringBuilder.append(ls);
        }

        return stringBuilder.toString();
    }

    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "retrieveListOfFriends")
    public void retrieveListOfFriends(@WebParam(name = "name") String ID) {
        user wanted = null;
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).ID == ID) {
                wanted = users.get(i);
            }
        }
        if (wanted == null) {
            System.out.println(ID + " does not exist");
        } else {
            friend te;
            for (int j = 0; j < wanted.Friends.size(); j++) {
                te = wanted.Friends.get(j);
                System.out.println(te.name);
            }
        }

    }

    @WebMethod(operationName = "showFriendLocation")
    public void showFriendLocation(@WebParam(name = "name") String ID1, String ID2) {
        user wanted = null;
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).ID == ID1) {
                wanted = users.get(i);
            }
        }
        if (wanted == null) {
            System.out.println(ID1 + " does not exist");
        } else {
            friend te;
            for (int j = 0; j < wanted.Friends.size(); j++) {
                if (wanted.Friends.get(j).ID == ID2) {
                    te = wanted.Friends.get(j);
                    System.out.println(te.loc);
                } else
                    System.out.println("friend" + ID2 + " does not exist");
            }
        }
    }
}