Skip to content

Instantly share code, notes, and snippets.

@billowdood
Created November 20, 2012 02:36
Show Gist options
  • Save billowdood/4115566 to your computer and use it in GitHub Desktop.
Save billowdood/4115566 to your computer and use it in GitHub Desktop.
Contract net
public class Client{
/*Class which will represent the client who wants to send a package*/
private float budget;
private int xValue,yValue;
public void setClient(float budget,int xValue,int yValue){
this.budget = budget;
this.xValue = xValue;
this.yValue = yValue;
}
public int getXValue(){
return this.xValue;
}
public int getYValue(){
return this.yValue;
}
}
public class Contract{
private DeliveryOffice[] offices = new DeliveryOffice[4];
private Client client;
public void setContract(DeliveryOffice sendIt, DeliveryOffice fastDelivery, DeliveryOffice cheapSending,DeliveryOffice easySend,Client client){
this.offices[0] = sendIt;
this.offices[1] = fastDelivery;
this.offices[2] = cheapSending;
this.offices[3] = easySend;
this.client = client;
}
public void findBestOptionDistance(){
double[] distance = new double[4];
distance[0] = this.offices[0].calculateDistance(client);
distance[1] = this.offices[1].calculateDistance(client);
distance[2] = this.offices[2].calculateDistance(client);
distance[3] = this.offices[3].calculateDistance(client);
double farest = distance[0];
int farestInd = 0;
for(int i = 1;i < 4;i++){
if(farest < distance[i]){
farest = distance[i];
farestInd = i;
}
}
this.offices[farestInd] = null;
System.out.println("--Distances--");
System.out.println("Send it: "+distance[0]);
System.out.println("Fast Delivery: "+distance[1]);
System.out.println("Cheap Sending: "+distance[2]);
System.out.println("Easy Send: "+distance[3]);
System.out.println("--Farest Dissapeared! :O--");
System.out.println("Send it: "+this.offices[0]);
System.out.println("Fast Delivery: "+this.offices[1]);
System.out.println("Cheap Sending: "+this.offices[2]);
System.out.println("Easy Send: "+this.offices[3]);
}
public void findBestOptionTime(int distanceKm){
int[] days = new int[4];
for(int i = 0;i < 4;i++){
if(this.offices[i] != null){
days[i] = this.offices[i].getDaysForDelivery(distanceKm);
}
}
int moreDays = days[0];
int moreInd = 0;
for(int i = 1;i < 4;i++){
if(moreDays < days[i]){
moreDays = days[i];
moreInd = i;
}
}
this.offices[moreInd] = null;
System.out.println("--Time(days)--");
System.out.println("Send it: "+days[0]);
System.out.println("Fast Delivery: "+days[1]);
System.out.println("Cheap Sending: "+days[2]);
System.out.println("Easy Send: "+days[3]);
System.out.println("--Slowest gone! :O--");
System.out.println("Send it: "+this.offices[0]);
System.out.println("Fast Delivery: "+this.offices[1]);
System.out.println("Cheap Sending: "+this.offices[2]);
System.out.println("Easy Send: "+this.offices[3]);
}
public void findBestOptionBudget(float budget){
float[] price = new float[4];
for(int i = 0;i < 4;i++){
if(this.offices[i] != null){
price[i] = this.offices[i].getPriceForDelivery();
}
}
float moreExp = price[0];
int moreExpInd = 0;
for(int i = 1;i < 4;i++){
if(moreExp < price[i]){
moreExp = price[i];
moreExpInd = i;
}
}
this.offices[moreExpInd] = null;
System.out.println("--Prices--");
System.out.println("Send it: "+price[0]);
System.out.println("Fast Delivery: "+price[1]);
System.out.println("Cheap Sending: "+price[2]);
System.out.println("Easy Send: "+price[3]);
System.out.println("--Most expensive gone! :O--");
System.out.println("Send it: "+this.offices[0]);
System.out.println("Fast Delivery: "+this.offices[1]);
System.out.println("Cheap Sending: "+this.offices[2]);
System.out.println("Easy Send: "+this.offices[3]);
}
public String bestOffice(){
for(int i = 0;i < 4;i++){
if(this.offices[i] != null){
return this.offices[i].getName();
}
}
return "";
}
}
import java.lang.Math;
import java.util.Random;
public class DeliveryOffice{
/*Class which will represent the different delivery offices*/
private String nameOfOffice;
private int xValue,yValue;
private Random random = new Random();
public DeliveryOffice(String nameOfOffice,int xValue,int yValue){
this.nameOfOffice = nameOfOffice;
this.xValue = xValue;
this.yValue = yValue;
}
public double calculateDistance(Client client){
return Math.sqrt((float)(Math.pow((this.xValue - client.getXValue()),2) + Math.pow((this.yValue - client.getYValue()),2)));
}
public int getDaysForDelivery(int distanceKm){
return (int)random.nextInt(distanceKm) % 15;
}
public float getPriceForDelivery(){
return random.nextInt(20)*10;
}
public String getName(){
return this.nameOfOffice;
}
}
import java.util.Scanner;
import java.util.Random;
public class MainClass{
public static void main(String[] args){
DeliveryOffice sendIt = new DeliveryOffice("Send It!",2,15);
DeliveryOffice fastDelivery = new DeliveryOffice("Fast Delivery",25,30);
DeliveryOffice cheapSending = new DeliveryOffice("Cheap Sending",27,10);
DeliveryOffice easySend = new DeliveryOffice("Easy Send",15,20);
Client client = new Client();
Contract contract = new Contract();
Random random = new Random();
Scanner scanf = new Scanner(System.in);
float budget;
while(true){
System.out.print("Welcome to our site,please enter your budget: ");
budget = scanf.nextFloat();
int distanceKm = 2500;
client.setClient(budget,random.nextInt(2500)%30+1,random.nextInt(2500)%30+1);
System.out.println("--You are in x = "+client.getXValue()+" and in y = "+client.getYValue());
contract.setContract(sendIt,fastDelivery,cheapSending,easySend,client);
contract.findBestOptionDistance();
contract.findBestOptionTime(distanceKm);
contract.findBestOptionBudget(budget);
System.out.println("--Your best option--");
System.out.println(contract.bestOffice());
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment