Skip to content
Snippets Groups Projects
Commit 2e8045b4 authored by Brutzman, Don's avatar Brutzman, Don
Browse files

Merge origin/master

parents 5e78f357 b85b4172
No related branches found
No related tags found
No related merge requests found
Showing
with 358 additions and 0 deletions
.DS_Store 0 → 100644
File added
File added
File added
File added
File added
import java.io.*;
import java.net.*;
public class Client_Customer {
public static void main(String[] args) {
// TODO - Fill out the following questionairre:
double averageBeefPurchase = 2; // How many pounds of beef do you typically purchase when you buy groceries?
double averageVariationInBeefPurchase = 0.5; // How much do you typically vary from the above amount (also in pounds)?
double averagePorkPurchase = 4; // How many pounds of pork do you typically purchase when you buy groceries?
double averageVariationInPorkPurchase = 0.5; // How much do you typically vary from the above amount (also in pounds)?
double averageChickenPurchase = 6; // How many pounds of chicken do you typically purchase when you buy groceries?
double averageVariationInChickenPurchase = 0.5; // How much do you typically vary from the above amount (also in pounds)?
double averageEggsPurchase = 2; // How many eggs do you typically purchase when you buy groceries (amount in dozens)?
double averageVariationInEggsPurchase = 0.5; // How much do you typically vary from the above amount (also in dozens)?
// End of TODO section
Customer myCustomer = new Customer(averageBeefPurchase, averageVariationInBeefPurchase, averagePorkPurchase, averageVariationInPorkPurchase, averageChickenPurchase, averageVariationInChickenPurchase, averageEggsPurchase, averageVariationInEggsPurchase);
try {
// Create client socket and connect to the server
Socket clientSocket = new Socket("localhost", 12345);
System.out.println("Connected to server.");
// Send request to server
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String request = "doTransaction," + myCustomer.getBeefPreference().generate() + "," + myCustomer.getPorkPreference().generate() + "," + myCustomer.getChickenPreference().generate() + "," + myCustomer.getEggsPreference().generate();
out.println(request);
System.out.println("Sent request to server: " + request);
// Receive response from server
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
StringBuilder responseBuilder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
responseBuilder.append(line).append("\n");
}
String response = responseBuilder.toString();
System.out.print("Received response from server: " + response);
// Close connections
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
public class Client_Farm {
public static void main(String[] args) {
// TODO - Fill out the following questionairre:
int cattleCount = 50; // How many cows are on the farm?
int pigCount = 50; // How many pigs are on the farm?
int broilerChickenCount = 300; // How many broiler chickens (think supermarket chickens - they don't lay eggs) are on the farm?
int layerChickenCount = 500; // How many egg laying chickens are on the farm?
// End of TODO section
LivestockRanch myFarm = new LivestockRanch(cattleCount, pigCount, broilerChickenCount, layerChickenCount);
try {
// Create client socket and connect to the server
Socket clientSocket = new Socket("localhost", 12345);
System.out.println("Connected to server.");
// Send request to server
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String request = "doResupply," + myFarm.getCattleProcessRate() + "," + myFarm.getPigProcessRate() + "," + myFarm.getChickenProcessRate() + "," + myFarm.getEggLayingRate();
out.println(request);
System.out.println("Sent request to server: " + request);
// Receive response from server
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
StringBuilder responseBuilder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
responseBuilder.append(line).append("\n");
}
String response = responseBuilder.toString();
System.out.print("Received response from server: " + response);
// Close connections
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import simkit.SimEntityBase;
import simkit.random.RandomVariate;
import simkit.random.RandomVariateFactory;
public class Customer extends SimEntityBase{
private RandomVariate beefPreference;
private RandomVariate porkPreference;
private RandomVariate chickenPreference;
private RandomVariate eggsPreference;
public Customer(double averagePurchaseOfBeef, double beefPlusMinus, double averagePurchaseOfPork, double porkPlusMinus, double averagePurchaseOfChicken, double chickenPlusMinus, double averagePurchaseOfEggs, double eggsPlusMinus) {
this.beefPreference = RandomVariateFactory.getInstance("Normal", averagePurchaseOfBeef, beefPlusMinus);
this.porkPreference = RandomVariateFactory.getInstance("Normal", averagePurchaseOfPork, porkPlusMinus);
this.chickenPreference = RandomVariateFactory.getInstance("Normal", averagePurchaseOfChicken, chickenPlusMinus);
this.eggsPreference = RandomVariateFactory.getInstance("Normal", averagePurchaseOfEggs, eggsPlusMinus);
}
public void doRun() {
waitDelay("Transaction", 0.0);
}
public void doTransaction() {
double beef = beefPreference.generate();
beef = beef < 0 ? 0 : beef;
double pork = porkPreference.generate();
pork = pork < 0 ? 0 : pork;
double chicken = chickenPreference.generate();
chicken = chicken < 0 ? 0 : chicken;
double eggs = eggsPreference.generate();
eggs = eggs < 0 ? 0 : eggs;
waitDelay("Transaction", 0.0, beef, pork, chicken, eggs);
waitDelay("Transaction", 7.0);
}
public RandomVariate getBeefPreference() {
return beefPreference;
}
public RandomVariate getPorkPreference() {
return porkPreference;
}
public RandomVariate getChickenPreference() {
return chickenPreference;
}
public RandomVariate getEggsPreference() {
return eggsPreference;
}
}
import java.text.DecimalFormat;
public class FarmStore {
private double bankAccount;
private double BEEFCOST = 10; // Cost per pound
private double PORKCOST = 5; // Cost per pound
private double CHICKENCOST = 2; // Cost per pound
private double EGGSCOST = 2; // Cost per dozen
protected double beefQuantity; // Quantity in pounds
protected double porkQuantity; // Quantity in pounds
protected double chickenQuantity; // Quantity in pounds
protected double eggQuantity; // Quantity in dozens
public FarmStore() {
this.bankAccount = 0;
this.beefQuantity = 0;
this.porkQuantity = 0;
this.chickenQuantity = 0;
this.eggQuantity = 0;
}
public String doTransaction(double beef, double pork, double chicken, double eggs) {
DecimalFormat df = new DecimalFormat("#.##");
if (this.beefQuantity < beef) {
beef = Math.max(this.beefQuantity, 0);
System.out.println("We are out of beef");
}
else {beef = Double.parseDouble(df.format(beef));}
if (this.porkQuantity < pork) {
pork = Math.max(this.porkQuantity, 0);
System.out.println("We are out of pork");
}
else {pork = Double.parseDouble(df.format(pork));}
if (this.chickenQuantity < chicken) {
chicken = Math.max(this.chickenQuantity, 0);
System.out.println("We are out of chicken");
}
else {chicken = Double.parseDouble(df.format(chicken));}
if (this.eggQuantity < eggs) {
eggs = Math.max(this.eggQuantity, 0);
System.out.println("We are out of eggs");
}
else {eggs = Double.parseDouble(df.format(eggs));}
this.beefQuantity -= beef;
this.porkQuantity -= pork;
this.chickenQuantity -= chicken;
this.eggQuantity -= eggs;
double cost = beef * BEEFCOST + pork * PORKCOST + chicken * CHICKENCOST + eggs * EGGSCOST;
cost = Double.parseDouble(df.format(cost));
this.bankAccount += cost;
return "Product purchased:\nBeef: " + beef + " lbs\nPork: " + pork + " lbs\nChicken: " + chicken + " lbs\nEggs: " + eggs + " dozen\nTransaction total: $" + cost;
}
public String doResupply(double beef, double pork, double chicken, double eggs) {
this.beefQuantity += beef;
this.porkQuantity += pork;
this.chickenQuantity += chicken;
this.eggQuantity += eggs;
return "Product received:\nBeef: " + beef + " lbs\t\tCurrent Stock: " + beefQuantity + " lbs\nPork: " + pork
+ " lbs\tCurrent Stock: " + porkQuantity + " lbs\nChicken: " + chicken + " lbs\tCurrent Stock: " + chickenQuantity
+ " lbs\nEggs: " + eggs + " dozen\tCurrent Stock: " + eggQuantity + " dozen\nCurrent Profit: $" + bankAccount;
}
}
\ No newline at end of file
public class LivestockRanch {
private int totalCattle;
private int totalPigs;
private int totalBroilerChickens;
private int totalLayerChickens;
private double cattleProcessRate;
private double pigProcessRate;
private double chickenProcessRate;
private double eggLayingRate;
public LivestockRanch (int totalCattle, int totalPigs, int totalBroilerChickens, int totalLayerChickens) {
this.totalCattle = totalCattle;
this.totalPigs = totalPigs;
this.totalBroilerChickens = totalBroilerChickens;
this.totalLayerChickens = totalLayerChickens;
this.cattleProcessRate = totalCattle * 500 / (3 * 12); // Processing 1/3 of the total population per year, 1 per month, at 500 lbs of beef per cow
this.pigProcessRate = totalPigs * 150 / 6; // Processing 1/6 of the total population per month, at 150 lbs of pork per pig
this.chickenProcessRate = totalBroilerChickens * 6 / 3; // Processing 1/3 of the total population per month, at 6 lbs per chicken
this.eggLayingRate = totalLayerChickens * (25 / 12); // 25 eggs per layer chicken (divided by 12 to be sold in dozens)
}
public int getTotalCattle() {
return totalCattle;
}
public int getTotalPigs() {
return totalPigs;
}
public int getTotalBroilerChickens() {
return totalBroilerChickens;
}
public int getTotalLayerChickens() {
return totalLayerChickens;
}
public double getCattleProcessRate() {
return cattleProcessRate;
}
public double getPigProcessRate() {
return pigProcessRate;
}
public double getChickenProcessRate() {
return chickenProcessRate;
}
public double getEggLayingRate() {
return eggLayingRate;
}
}
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
FarmStore market = new FarmStore();
try {
// Create server socket
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Server listening on port 12345...");
while (true) {
// Accept client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());
// Start a new thread to handle the client connection
ClientHandler clientHandler = new ClientHandler(clientSocket, market);
clientHandler.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class ClientHandler extends Thread {
private Socket clientSocket;
private FarmStore market;
public ClientHandler(Socket socket, FarmStore market) {
this.clientSocket = socket;
this.market = market;
}
public void run() {
try {
// Read data from client
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String request = in.readLine();
String[] parsedRequest = request.split(",");
System.out.println("Received request from client: " + request);
// Process request
String response = processRequest(parsedRequest[0], Double.parseDouble(parsedRequest[1]), Double.parseDouble(parsedRequest[2]), Double.parseDouble(parsedRequest[3]), Double.parseDouble(parsedRequest[4]));
System.out.print("Sending response to client: " + response);
// Send response to client
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println(response);
// Close connections
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private String processRequest(String request, double beef, double pork, double chicken, double eggs) {
// Process the request and execute the corresponding method
if (request.equals("doTransaction")) {
return market.doTransaction(beef, pork, chicken, eggs); // Assuming appropriate arguments for the method
} else if (request.equals("doResupply")) {
return market.doResupply(beef, pork, chicken, eggs);
} else {
return "Invalid request!";
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment