Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //normal
- //I found this web server bird feeder script, pretty deep, 10_20_2023 got to study it. neat.
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Scanner;
- import java.util.concurrent.ThreadLocalRandom;
- public class BirdFeedingSimulator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter the number of grains: ");
- int grainsOfWheat = scanner.nextInt();
- System.out.print("Enter the number of small stones: ");
- int smallStones = scanner.nextInt();
- System.out.print("Enter the number of birds: ");
- int totalBirds = scanner.nextInt();
- int maxBirdsAtPlate = 6;
- // Create a queue to represent birds waiting to eat
- Queue<Integer> birdsQueue = new LinkedList<>();
- for (int i = 1; i <= totalBirds; i++) {
- birdsQueue.offer(i);
- }
- int[] birdResults = new int[totalBirds]; // 0 for not full, 1 for half full, 2 for full
- while (!birdsQueue.isEmpty()) {
- int birdsAtPlate = Math.min(maxBirdsAtPlate, birdsQueue.size());
- for (int i = 0; i < birdsAtPlate; i++) {
- int birdId = birdsQueue.poll();
- int attempts = 2;
- int grainsEaten = 0;
- int stonesEaten = 0;
- while (attempts > 0) {
- int randomChoice = ThreadLocalRandom.current().nextInt(2);
- if (randomChoice == 0 && grainsOfWheat > 0) {
- grainsEaten++;
- grainsOfWheat--;
- } else if (randomChoice == 1 && smallStones > 0) {
- stonesEaten++;
- smallStones--;
- }
- attempts--;
- }
- if (grainsEaten == 2) {
- birdResults[birdId - 1] = 2; // Full
- } else if (grainsEaten == 1 && stonesEaten == 1) {
- birdResults[birdId - 1] = 1; // Half Full
- } else if (stonesEaten == 2) {
- birdResults[birdId - 1] = 0; // Not Full
- }
- }
- }
- int fullCount = 0;
- int halfFullCount = 0;
- int notFullCount = 0;
- for (int result : birdResults) {
- if (result == 2) {
- fullCount++;
- } else if (result == 1) {
- halfFullCount++;
- } else {
- notFullCount++;
- }
- }
- System.out.println("Full Birds: " + fullCount);
- System.out.println("Half Full Birds: " + halfFullCount);
- System.out.println("Not Full Birds: " + notFullCount);
- scanner.close(); // Close the scanner
- }
- }
- //web server
- import com.sun.net.httpserver.HttpServer;
- import com.sun.net.httpserver.HttpHandler;
- import com.sun.net.httpserver.HttpExchange;
- import java.io.IOException;
- import java.io.OutputStream;
- public class BirdFeedingWebService {
- public static void main(String[] args) throws IOException {
- HttpServer server = HttpServer.create();
- server.bind(new InetSocketAddress(8080), 0);
- server.createContext("/simulate", new SimulationHandler());
- server.start();
- System.out.println("Server started on port 8080...");
- }
- static class SimulationHandler implements HttpHandler {
- @Override
- public void handle(HttpExchange exchange) throws IOException {
- // Parse query parameters from the request
- String query = exchange.getRequestURI().getQuery();
- String[] params = query.split("&");
- int grainsOfWheat = Integer.parseInt(params[0].split("=")[1]);
- int smallStones = Integer.parseInt(params[1].split("=")[1]);
- int totalBirds = Integer.parseInt(params[2].split("=")[1]);
- // Simulate bird feeding (you can use the previous code here)
- // Prepare the response
- String response = "Full Birds: " + fullCount + "\n" +
- "Half Full Birds: " + halfFullCount + "\n" +
- "Not Full Birds: " + notFullCount;
- exchange.sendResponseHeaders(200, response.getBytes().length);
- OutputStream os = exchange.getResponseBody();
- os.write(response.getBytes());
- os.close();
- }
- }
- }
- // web client
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- public class BirdFeedingWebServiceClient {
- public static void main(String[] args) throws IOException {
- int grainsOfWheat = 50;
- int smallStones = 50;
- int totalBirds = 50;
- String urlString = "http://localhost:8080/simulate?grains=" + grainsOfWheat +
- "&stones=" + smallStones + "&birds=" + totalBirds;
- URL url = new URL(urlString);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("GET");
- int responseCode = connection.getResponseCode();
- if (responseCode == 200) {
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String inputLine;
- StringBuilder response = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- response.append("\n");
- }
- in.close();
- System.out.println(response.toString());
- } else {
- System.out.println("HTTP GET request failed with response code " + responseCode);
- }
- }
- }
- //webservice logic
- import javax.jws.WebMethod;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public class BirdFeedingService {
- @WebMethod
- public FeedingResponse simulateFeeding(
- @WebParam(name = "grains") int grains,
- @WebParam(name = "stones") int stones,
- @WebParam(name = "birds") int totalBirds
- ) {
- int maxBirdsAtPlate = 6;
- Queue<Integer> birdsQueue = new LinkedList<>();
- for (int i = 1; i <= totalBirds; i++) {
- birdsQueue.offer(i);
- }
- int[] birdResults = new int[totalBirds]; // 0 for not full, 1 for half full, 2 for full
- while (!birdsQueue.isEmpty()) {
- int birdsAtPlate = Math.min(maxBirdsAtPlate, birdsQueue.size());
- for (int i = 0; i < birdsAtPlate; i++) {
- int birdId = birdsQueue.poll();
- int attempts = 2;
- int grainsEaten = 0;
- int stonesEaten = 0;
- while (attempts > 0) {
- int randomChoice = ThreadLocalRandom.current().nextInt(2);
- if (randomChoice == 0 && grains > 0) {
- grainsEaten++;
- grains--;
- } else if (randomChoice == 1 && stones > 0) {
- stonesEaten++;
- stones--;
- }
- attempts--;
- }
- if (grainsEaten == 2) {
- birdResults[birdId - 1] = 2; // Full
- } else if (grainsEaten == 1 && stonesEaten == 1) {
- birdResults[birdId - 1] = 1; // Half Full
- } else if (stonesEaten == 2) {
- birdResults[birdId - 1] = 0; // Not Full
- }
- }
- }
- int fullCount = 0;
- int halfFullCount = 0;
- int notFullCount = 0;
- for (int result : birdResults) {
- if (result == 2) {
- fullCount++;
- } else if (result == 1) {
- halfFullCount++;
- } else {
- notFullCount++;
- }
- }
- FeedingResponse response = new FeedingResponse();
- response.setFullBirds(fullCount);
- response.setHalfFullBirds(halfFullCount);
- response.setNotFullBirds(notFullCount);
- return response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment