SHOW:
|
|
- or go back to the newest paste.
| 1 | //normal | |
| 2 | //I found this web server bird feeder script, pretty deep, 10_20_2023 got to study it. neat. | |
| 3 | ||
| 4 | ||
| 5 | import java.util.LinkedList; | |
| 6 | import java.util.Queue; | |
| 7 | import java.util.Scanner; | |
| 8 | import java.util.concurrent.ThreadLocalRandom; | |
| 9 | ||
| 10 | public class BirdFeedingSimulator {
| |
| 11 | public static void main(String[] args) {
| |
| 12 | Scanner scanner = new Scanner(System.in); | |
| 13 | ||
| 14 | System.out.print("Enter the number of grains: ");
| |
| 15 | int grainsOfWheat = scanner.nextInt(); | |
| 16 | ||
| 17 | System.out.print("Enter the number of small stones: ");
| |
| 18 | int smallStones = scanner.nextInt(); | |
| 19 | ||
| 20 | System.out.print("Enter the number of birds: ");
| |
| 21 | int totalBirds = scanner.nextInt(); | |
| 22 | ||
| 23 | int maxBirdsAtPlate = 6; | |
| 24 | ||
| 25 | // Create a queue to represent birds waiting to eat | |
| 26 | Queue<Integer> birdsQueue = new LinkedList<>(); | |
| 27 | for (int i = 1; i <= totalBirds; i++) {
| |
| 28 | birdsQueue.offer(i); | |
| 29 | } | |
| 30 | ||
| 31 | int[] birdResults = new int[totalBirds]; // 0 for not full, 1 for half full, 2 for full | |
| 32 | ||
| 33 | while (!birdsQueue.isEmpty()) {
| |
| 34 | int birdsAtPlate = Math.min(maxBirdsAtPlate, birdsQueue.size()); | |
| 35 | ||
| 36 | for (int i = 0; i < birdsAtPlate; i++) {
| |
| 37 | int birdId = birdsQueue.poll(); | |
| 38 | int attempts = 2; | |
| 39 | int grainsEaten = 0; | |
| 40 | int stonesEaten = 0; | |
| 41 | ||
| 42 | while (attempts > 0) {
| |
| 43 | int randomChoice = ThreadLocalRandom.current().nextInt(2); | |
| 44 | if (randomChoice == 0 && grainsOfWheat > 0) {
| |
| 45 | grainsEaten++; | |
| 46 | grainsOfWheat--; | |
| 47 | } else if (randomChoice == 1 && smallStones > 0) {
| |
| 48 | stonesEaten++; | |
| 49 | smallStones--; | |
| 50 | } | |
| 51 | attempts--; | |
| 52 | } | |
| 53 | ||
| 54 | if (grainsEaten == 2) {
| |
| 55 | birdResults[birdId - 1] = 2; // Full | |
| 56 | } else if (grainsEaten == 1 && stonesEaten == 1) {
| |
| 57 | birdResults[birdId - 1] = 1; // Half Full | |
| 58 | } else if (stonesEaten == 2) {
| |
| 59 | birdResults[birdId - 1] = 0; // Not Full | |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | ||
| 64 | int fullCount = 0; | |
| 65 | int halfFullCount = 0; | |
| 66 | int notFullCount = 0; | |
| 67 | ||
| 68 | for (int result : birdResults) {
| |
| 69 | if (result == 2) {
| |
| 70 | fullCount++; | |
| 71 | } else if (result == 1) {
| |
| 72 | halfFullCount++; | |
| 73 | } else {
| |
| 74 | notFullCount++; | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | System.out.println("Full Birds: " + fullCount);
| |
| 79 | System.out.println("Half Full Birds: " + halfFullCount);
| |
| 80 | System.out.println("Not Full Birds: " + notFullCount);
| |
| 81 | ||
| 82 | scanner.close(); // Close the scanner | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | ||
| 87 | ||
| 88 | //web server | |
| 89 | ||
| 90 | import com.sun.net.httpserver.HttpServer; | |
| 91 | import com.sun.net.httpserver.HttpHandler; | |
| 92 | import com.sun.net.httpserver.HttpExchange; | |
| 93 | import java.io.IOException; | |
| 94 | import java.io.OutputStream; | |
| 95 | ||
| 96 | public class BirdFeedingWebService {
| |
| 97 | public static void main(String[] args) throws IOException {
| |
| 98 | HttpServer server = HttpServer.create(); | |
| 99 | server.bind(new InetSocketAddress(8080), 0); | |
| 100 | server.createContext("/simulate", new SimulationHandler());
| |
| 101 | server.start(); | |
| 102 | System.out.println("Server started on port 8080...");
| |
| 103 | } | |
| 104 | ||
| 105 | static class SimulationHandler implements HttpHandler {
| |
| 106 | @Override | |
| 107 | public void handle(HttpExchange exchange) throws IOException {
| |
| 108 | // Parse query parameters from the request | |
| 109 | String query = exchange.getRequestURI().getQuery(); | |
| 110 | String[] params = query.split("&");
| |
| 111 | int grainsOfWheat = Integer.parseInt(params[0].split("=")[1]);
| |
| 112 | int smallStones = Integer.parseInt(params[1].split("=")[1]);
| |
| 113 | int totalBirds = Integer.parseInt(params[2].split("=")[1]);
| |
| 114 | ||
| 115 | // Simulate bird feeding (you can use the previous code here) | |
| 116 | ||
| 117 | // Prepare the response | |
| 118 | String response = "Full Birds: " + fullCount + "\n" + | |
| 119 | "Half Full Birds: " + halfFullCount + "\n" + | |
| 120 | "Not Full Birds: " + notFullCount; | |
| 121 | ||
| 122 | exchange.sendResponseHeaders(200, response.getBytes().length); | |
| 123 | OutputStream os = exchange.getResponseBody(); | |
| 124 | os.write(response.getBytes()); | |
| 125 | os.close(); | |
| 126 | } | |
| 127 | } | |
| 128 | } | |
| 129 | ||
| 130 | ||
| 131 | // web client | |
| 132 | ||
| 133 | import java.io.BufferedReader; | |
| 134 | import java.io.IOException; | |
| 135 | import java.io.InputStreamReader; | |
| 136 | import java.net.HttpURLConnection; | |
| 137 | import java.net.URL; | |
| 138 | ||
| 139 | public class BirdFeedingWebServiceClient {
| |
| 140 | public static void main(String[] args) throws IOException {
| |
| 141 | int grainsOfWheat = 50; | |
| 142 | int smallStones = 50; | |
| 143 | int totalBirds = 50; | |
| 144 | ||
| 145 | String urlString = "http://localhost:8080/simulate?grains=" + grainsOfWheat + | |
| 146 | "&stones=" + smallStones + "&birds=" + totalBirds; | |
| 147 | ||
| 148 | URL url = new URL(urlString); | |
| 149 | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
| 150 | connection.setRequestMethod("GET");
| |
| 151 | ||
| 152 | int responseCode = connection.getResponseCode(); | |
| 153 | if (responseCode == 200) {
| |
| 154 | BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
| 155 | String inputLine; | |
| 156 | StringBuilder response = new StringBuilder(); | |
| 157 | ||
| 158 | while ((inputLine = in.readLine()) != null) {
| |
| 159 | response.append(inputLine); | |
| 160 | response.append("\n");
| |
| 161 | } | |
| 162 | ||
| 163 | in.close(); | |
| 164 | System.out.println(response.toString()); | |
| 165 | } else {
| |
| 166 | System.out.println("HTTP GET request failed with response code " + responseCode);
| |
| 167 | } | |
| 168 | } | |
| 169 | } | |
| 170 | ||
| 171 | ||
| 172 | //webservice logic | |
| 173 | ||
| 174 | import javax.jws.WebMethod; | |
| 175 | import javax.jws.WebParam; | |
| 176 | import javax.jws.WebService; | |
| 177 | ||
| 178 | @WebService | |
| 179 | public class BirdFeedingService {
| |
| 180 | @WebMethod | |
| 181 | public FeedingResponse simulateFeeding( | |
| 182 | @WebParam(name = "grains") int grains, | |
| 183 | @WebParam(name = "stones") int stones, | |
| 184 | @WebParam(name = "birds") int totalBirds | |
| 185 | ) {
| |
| 186 | int maxBirdsAtPlate = 6; | |
| 187 | Queue<Integer> birdsQueue = new LinkedList<>(); | |
| 188 | for (int i = 1; i <= totalBirds; i++) {
| |
| 189 | birdsQueue.offer(i); | |
| 190 | } | |
| 191 | ||
| 192 | int[] birdResults = new int[totalBirds]; // 0 for not full, 1 for half full, 2 for full | |
| 193 | ||
| 194 | while (!birdsQueue.isEmpty()) {
| |
| 195 | int birdsAtPlate = Math.min(maxBirdsAtPlate, birdsQueue.size()); | |
| 196 | ||
| 197 | for (int i = 0; i < birdsAtPlate; i++) {
| |
| 198 | int birdId = birdsQueue.poll(); | |
| 199 | int attempts = 2; | |
| 200 | int grainsEaten = 0; | |
| 201 | int stonesEaten = 0; | |
| 202 | ||
| 203 | while (attempts > 0) {
| |
| 204 | int randomChoice = ThreadLocalRandom.current().nextInt(2); | |
| 205 | if (randomChoice == 0 && grains > 0) {
| |
| 206 | grainsEaten++; | |
| 207 | grains--; | |
| 208 | } else if (randomChoice == 1 && stones > 0) {
| |
| 209 | stonesEaten++; | |
| 210 | stones--; | |
| 211 | } | |
| 212 | attempts--; | |
| 213 | } | |
| 214 | ||
| 215 | if (grainsEaten == 2) {
| |
| 216 | birdResults[birdId - 1] = 2; // Full | |
| 217 | } else if (grainsEaten == 1 && stonesEaten == 1) {
| |
| 218 | birdResults[birdId - 1] = 1; // Half Full | |
| 219 | } else if (stonesEaten == 2) {
| |
| 220 | birdResults[birdId - 1] = 0; // Not Full | |
| 221 | } | |
| 222 | } | |
| 223 | } | |
| 224 | ||
| 225 | int fullCount = 0; | |
| 226 | int halfFullCount = 0; | |
| 227 | int notFullCount = 0; | |
| 228 | ||
| 229 | for (int result : birdResults) {
| |
| 230 | if (result == 2) {
| |
| 231 | fullCount++; | |
| 232 | } else if (result == 1) {
| |
| 233 | halfFullCount++; | |
| 234 | } else {
| |
| 235 | notFullCount++; | |
| 236 | } | |
| 237 | } | |
| 238 | ||
| 239 | FeedingResponse response = new FeedingResponse(); | |
| 240 | response.setFullBirds(fullCount); | |
| 241 | response.setHalfFullBirds(halfFullCount); | |
| 242 | response.setNotFullBirds(notFullCount); | |
| 243 | ||
| 244 | return response; | |
| 245 | } | |
| 246 | } | |
| 247 | ||
| 248 | ||
| 249 |