package market;

import java.awt.Graphics;
import java.util.concurrent.locks.ReentrantLock;

public class Market {
    int c; //kapacitet
    int q; //kolicina
    int x, y, w, h;
    ReentrantLock lock;
    
    public Market(int x, int y , int w, int h) {
	this.x=x;
	this.y=y;
	this.w=w;
	this.h=h;
	lock=new ReentrantLock();
    }
    
    boolean contains(Salesman s){
	return (s.x>=x && s.x<=x+w && s.y>=y && s.y<=y+h);
    }
    
    void drawSelf(Graphics g){
	g.drawRect(x,y,w,h);
	g.drawString(String.valueOf(q), x+10, y+10);
    }
    
    void sell(Salesman s){
	lock.lock();
	if(s.c<q){
	    ;
	}else{
	    s.full=true;
	    q-=s.c;
	}
	lock.unlock();
    }
    
    void buy(Salesman s){
	lock.lock();
	if(s.c+q>c){
	    ;
	}else{
	    s.full=false;
	    q+=s.c;
	}
	lock.unlock();
    }
    
    void doSale(Salesman s){
	if(s.full)
	    sell(s);
	else
	    buy(s);
    }
}
