<commit_before>package CMRestaurant.roles;

import java.util.*;
import java.util.concurrent.Semaphore;

import CMRestaurant.gui.CMCashierGui;
import market.interfaces.DeliveryMan;
import city.PersonAgent;
import city.roles.DeliveryManRole;
import city.roles.Role;
import restaurant.Restaurant;
import restaurant.interfaces.Cashier;
import restaurant.interfaces.Customer;
import restaurant.interfaces.Waiter;
import restaurant.test.mock.EventLog;
import restaurant.test.mock.LoggedEvent;

public class CMCashierRole extends Role implements Cashier {
	public List<Order> orders = Collections.synchronizedList(new ArrayList<Order>());
	public List<Bill> bills = Collections.synchronizedList(new ArrayList<Bill>());
	Timer timer = new Timer();
	
	public EventLog log = new EventLog();

	public Restaurant restaurant;
	public CMCashierGui cashierGui = null;
	Map<String, Double> pricingMap = new HashMap<String, Double>(); 
	public double bank = 5000; 
	enum State {none, goToWork, working, leaving, releaveFromDuty};
	State state = State.none;

	private Semaphore waitingResponse = new Semaphore(0,true);
	PersonAgent replacementPerson = null;
	
	static final int MAKE_CHECK_TIME = 5000;
	public class Order{
		public Waiter waiter;
		public Customer customer;
		public String choice;
		public double check;
		public double cashIn;
		public OrderState state;
		
		public Order(Waiter w, Customer ca, String c, OrderState s){
			waiter = w;
			customer = ca;
			choice = c;
			state = s;	
		}
		
	}
	public enum OrderState {requested, printingCheck, deliverCheck, awaitingPayment, paymentRecieved, done, inDebt};
	
	public class Bill{
		public DeliveryMan deliveryMan;
		public double bill;
		public BillState state;
		
		Bill(DeliveryMan DMR, double b, BillState s){
			deliveryMan=DMR;
			bill = b;
			state = s;
		}
	}
	public enum BillState {requested, payed, informed};
	
	public static final double SALAD_COST = 4.99;
	public static final double STEAK_COST = 15.99;
	public static final double CHICKEN_COST = 10.99;
	public static final double COOKIE_COST = 3.99;	
	
	public CMCashierRole(){
		super();
		
		pricingMap.put("salad", SALAD_COST);
		pricingMap.put("steak", STEAK_COST);
		pricingMap.put("chicken", CHICKEN_COST);
		pricingMap.put("cookie", COOKIE_COST);
	}

	public void setGui(CMCashierGui g) {
		cashierGui = g;
	}

	public CMCashierGui getGui() {
		return cashierGui;
	}

	public void goesToWork() {
		state = State.goToWork;
		stateChanged();
	}

	public void msgReleaveFromDuty(PersonAgent p) {
		replacementPerson = p;
		state = State.leaving;
		this.stateChanged();
	}
	public void msgAnimationHasLeftRestaurant() {
		state = State.releaveFromDuty;
		waitingResponse.release();
	}
	public void msgProduceCheck(Waiter w, Customer c, String choice)
	{
		orders.add(new Order(w, c, choice, OrderState.requested));
		stateChanged();
		log.add(new LoggedEvent("Received msgProduceCheck from waiter. Choice = "+ choice));
	}
	
	public void msgCheckPrinted(Order o)
	{
		Order deleteIt = null;
		synchronized(orders){
			for(Order order: orders){
				if(order.state == OrderState.inDebt && o.customer == order.customer){
					o.check += order.check;
					order.state = OrderState.done;
					deleteIt = order;
				}
			}
		}
		orders.remove(deleteIt);
		o.state = OrderState.deliverCheck;
		log.add(new LoggedEvent("Received msgCheckPrinted from cashier. Total of check = "+ o.check));
		stateChanged();
	}
	
	public void msgPayment(Customer c, double cash) {
		Order order = findOrder(c);
		order.cashIn = cash;
		order.state = OrderState.paymentRecieved;
		log.add(new LoggedEvent("Received msgPayment from customer. Total cash in = "+ cash));
		stateChanged();
	}

	public void msgHereIsInvoice(double price,DeliveryMan DMR) {
		bills.add(new Bill(DMR,price,BillState.informed));
		stateChanged();
	}
	
	@Override
	public void msgHereIsBill(DeliveryMan DM, double bill) {
		bills.add( new Bill(DM, bill, BillState.requested));
		log.add(new LoggedEvent("Received msgHereIsBill from market. Total of Bill = "+ bill));
		stateChanged();	
	}
	/*public void msgHereIsBill(Market m, double bill) {
		bills.add( new Bill(m, bill, BillState.requested));
		log.add(new LoggedEvent("Received msgHereIsBill from market. Total of Bill = "+ bill));
		stateChanged();
	}	*/
	
	private Order findOrder(Customer c) {
		synchronized(orders){
			for(Order o: orders){
				if(o.customer == c)
				{
					return o;
				}
			}
		}
		return null;
	}

	public boolean pickAndExecuteAnAction() {
		if(state == State.releaveFromDuty){
			state = State.none;
			myPerson.releavedFromDuty(this);
			if(replacementPerson != null){
				replacementPerson.waitingResponse.release();
			}
		}
		if(state == State.goToWork){
			state = State.working;
			cashierGui.DoEnterRestaurant();
			return true;
		}
		if(state == State.leaving){
			state = State.none;
			if(!"Saturday".equals(myPerson.dayOfWeek) || !"Sunday".equals(myPerson.dayOfWeek))
				DepositBusinessCash();
			cashierGui.DoLeaveRestaurant();
			try {
				waitingResponse.acquire();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return true;<commit_msg>Fixed cashier going to closed bank<commit_after>package CMRestaurant.roles;

import java.util.*;
import java.util.concurrent.Semaphore;

import CMRestaurant.gui.CMCashierGui;
import market.interfaces.DeliveryMan;
import city.PersonAgent;
import city.roles.DeliveryManRole;
import city.roles.Role;
import restaurant.Restaurant;
import restaurant.interfaces.Cashier;
import restaurant.interfaces.Customer;
import restaurant.interfaces.Waiter;
import restaurant.test.mock.EventLog;
import restaurant.test.mock.LoggedEvent;

public class CMCashierRole extends Role implements Cashier {
	public List<Order> orders = Collections.synchronizedList(new ArrayList<Order>());
	public List<Bill> bills = Collections.synchronizedList(new ArrayList<Bill>());
	Timer timer = new Timer();
	
	public EventLog log = new EventLog();

	public Restaurant restaurant;
	public CMCashierGui cashierGui = null;
	Map<String, Double> pricingMap = new HashMap<String, Double>(); 
	public double bank = 5000; 
	enum State {none, goToWork, working, leaving, releaveFromDuty};
	State state = State.none;

	private Semaphore waitingResponse = new Semaphore(0,true);
	PersonAgent replacementPerson = null;
	
	static final int MAKE_CHECK_TIME = 5000;
	public class Order{
		public Waiter waiter;
		public Customer customer;
		public String choice;
		public double check;
		public double cashIn;
		public OrderState state;
		
		public Order(Waiter w, Customer ca, String c, OrderState s){
			waiter = w;
			customer = ca;
			choice = c;
			state = s;	
		}
		
	}
	public enum OrderState {requested, printingCheck, deliverCheck, awaitingPayment, paymentRecieved, done, inDebt};
	
	public class Bill{
		public DeliveryMan deliveryMan;
		public double bill;
		public BillState state;
		
		Bill(DeliveryMan DMR, double b, BillState s){
			deliveryMan=DMR;
			bill = b;
			state = s;
		}
	}
	public enum BillState {requested, payed, informed};
	
	public static final double SALAD_COST = 4.99;
	public static final double STEAK_COST = 15.99;
	public static final double CHICKEN_COST = 10.99;
	public static final double COOKIE_COST = 3.99;	
	
	public CMCashierRole(){
		super();
		
		pricingMap.put("salad", SALAD_COST);
		pricingMap.put("steak", STEAK_COST);
		pricingMap.put("chicken", CHICKEN_COST);
		pricingMap.put("cookie", COOKIE_COST);
	}

	public void setGui(CMCashierGui g) {
		cashierGui = g;
	}

	public CMCashierGui getGui() {
		return cashierGui;
	}

	public void goesToWork() {
		state = State.goToWork;
		stateChanged();
	}

	public void msgReleaveFromDuty(PersonAgent p) {
		replacementPerson = p;
		state = State.leaving;
		this.stateChanged();
	}
	public void msgAnimationHasLeftRestaurant() {
		state = State.releaveFromDuty;
		waitingResponse.release();
	}
	public void msgProduceCheck(Waiter w, Customer c, String choice)
	{
		orders.add(new Order(w, c, choice, OrderState.requested));
		stateChanged();
		log.add(new LoggedEvent("Received msgProduceCheck from waiter. Choice = "+ choice));
	}
	
	public void msgCheckPrinted(Order o)
	{
		Order deleteIt = null;
		synchronized(orders){
			for(Order order: orders){
				if(order.state == OrderState.inDebt && o.customer == order.customer){
					o.check += order.check;
					order.state = OrderState.done;
					deleteIt = order;
				}
			}
		}
		orders.remove(deleteIt);
		o.state = OrderState.deliverCheck;
		log.add(new LoggedEvent("Received msgCheckPrinted from cashier. Total of check = "+ o.check));
		stateChanged();
	}
	
	public void msgPayment(Customer c, double cash) {
		Order order = findOrder(c);
		order.cashIn = cash;
		order.state = OrderState.paymentRecieved;
		log.add(new LoggedEvent("Received msgPayment from customer. Total cash in = "+ cash));
		stateChanged();
	}

	public void msgHereIsInvoice(double price,DeliveryMan DMR) {
		bills.add(new Bill(DMR,price,BillState.informed));
		stateChanged();
	}
	
	@Override
	public void msgHereIsBill(DeliveryMan DM, double bill) {
		bills.add( new Bill(DM, bill, BillState.requested));
		log.add(new LoggedEvent("Received msgHereIsBill from market. Total of Bill = "+ bill));
		stateChanged();	
	}
	/*public void msgHereIsBill(Market m, double bill) {
		bills.add( new Bill(m, bill, BillState.requested));
		log.add(new LoggedEvent("Received msgHereIsBill from market. Total of Bill = "+ bill));
		stateChanged();
	}	*/
	
	private Order findOrder(Customer c) {
		synchronized(orders){
			for(Order o: orders){
				if(o.customer == c)
				{
					return o;
				}
			}
		}
		return null;
	}

	public boolean pickAndExecuteAnAction() {
		if(state == State.releaveFromDuty){
			state = State.none;
			myPerson.releavedFromDuty(this);
			if(replacementPerson != null){
				replacementPerson.waitingResponse.release();
			}
		}
		if(state == State.goToWork){
			state = State.working;
			cashierGui.DoEnterRestaurant();
			return true;
		}
		if(state == State.leaving){
			state = State.none;
			if(!"Saturday".equals(myPerson.dayOfWeek) && !"Sunday".equals(myPerson.dayOfWeek))
				DepositBusinessCash();
			cashierGui.DoLeaveRestaurant();
			try {
				waitingResponse.acquire();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return true;
{'fraction_non_alphanumeric': 0.11133819951338199, 'fraction_numerical': 0.004476885644768856, 'mean_word_length': 4.340956340956341, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6876246', 'n_tokens_mistral': 3791, 'n_tokens_neox': 3317, 'n_words': 834}