%%==================================================================================================
%%==================================================================================================
%%    START MENU OF THE PROGRAM                 (    BEYCAN KAHRAMAN - 040020337   )
%%==================================================================================================
%%==================================================================================================

start:-	% consult('database.pl'),    % if not in this file
		nl, write('============= BEYCAN KAHRAMAN - 040020337 =========================='), nl, nl,

		repeat,
		write('___________________________________________________________________'), nl, nl,
		
		write('  MENU'),nl,
		write('1 Display all customers (all info)'),nl,
		write('2 Display all products  (all info)'),nl,
		write('3 Display orders of a given customer (product names and amounts)'),nl,
		% Customer ID - Display all orders - Total Cost of Orders
		write('4 Display orders for a given product (customer names and amounts)'),nl,
		% Product ID - Display all orders - Total Cost of Orders
		write('5 Display all products whose critical level exceeded (product name, amount, critical level)'),nl,
		write('6 Exit'),nl,nl,
		
		write('CHOICE: '),
		read(CHOICE),
		
		isGoodChoice(CHOICE), nl,
		doYourDuty(CHOICE),
		CHOICE =:= 6, !.
		
%%==================================================================================================
%%    IS THE CHOICE VALID?
%%==================================================================================================
		
isGoodChoice(CH):- 	CH>0, CH<7, !.
isGoodChoice(CH):- 	nl, 
					write('  INVALID CHOICE'),nl,
					CH=:=1.
					
%%==================================================================================================
%%    CUSTOMERs ORDERS
%%==================================================================================================
					
customerOrders:-	write('Enter a Customer ID\t:\t'),
					read(ID), nl,
					customer(ID, CustomerName),
					write('Customer\t\t:\t'), write(CustomerName), nl, nl,
					order(ID,ProductID, Amount),
					product(ProductID,ProductName,_,_,_),
					customerTotalCost(TotalValue),
					NewTotalValue is TotalValue + Amount,
					retract(customerTotalCost(_)), 
					assert(customerTotalCost(NewTotalValue)),
					write(ProductName), write('>> '), write(Amount), nl,
					fail.
customerOrders:- true.

%%==================================================================================================
%%    ORDER OF PRODUCTs
%%==================================================================================================

orderOfProducts:- 	write('Enter a Product ID\t:\t'),
					read(ProductID), nl, 
					product(ProductID, ProductName,_,_,_),
					write('Product\t\t\t:\t'), write(ProductName), nl, nl,
					order(CustomerID,ProductID, Amount),
					customer(CustomerID,CustomerName),
					productTotalCost(TotalValue),
					NewTotalValue is TotalValue + Amount,
					retract(productTotalCost(_)),
					assert(productTotalCost(NewTotalValue)),
					write(CustomerName), write('\t\t>> '), write(Amount), nl,
					fail.
orderOfProducts:- 	true.

%%==================================================================================================
%%    SERVE THE DESIRED VALUES
%%==================================================================================================
					
doYourDuty(1):-	write('  CUSTOMERS'), nl, 
				write('------------------------------------------------------------------'), nl, 
				write('id\tname'), nl, 
				write('------------------------------------------------------------------'), nl, 
				customer(X,Y),
				write(X), write('.\t'), write(Y), nl,
				fail,
				!. 
doYourDuty(2):-	write('  PRODUCTS'), nl, 
				write('------------------------------------------------------------------'), nl, 
				write('id\tproduct name\t\t\tprice\tamount\tcritic'), nl, 
				write('------------------------------------------------------------------'), nl, 
				product(X, Y, Z, T, U),
				write(X), write('.\t'), write(Y), write('\t'), write(Z), 
				write('\t'), write(T), write('\t'), write(U), nl,
				fail,
				!. 
doYourDuty(3):-	write('  CUSTOMERS ORDERS'), nl, 
				assert(customerTotalCost(0)), 
				customerOrders,
				nl, write('Total Cost of Orders\t:\t'),
				customerTotalCost(TotalValue), write(TotalValue), nl,
				retract(customerTotalCost(_)),
				!. 
doYourDuty(4):-	write('  ORDER OF PRODUCTS'), nl, 
				assert(productTotalCost(0)), 
				orderOfProducts,
				nl, write('Total Cost of Orders\t:\t'),
				productTotalCost(TotalValue), write(TotalValue), nl,
				retract(productTotalCost(_)),
				!. 
doYourDuty(5):-	write('  CRITICAL LEVEL (which are under = exceeds critical level)'), nl, 
				write('------------------------------------------------------------------'), nl, 
				write('name\t\t\t\tamount\tcritic'), nl, 
				write('------------------------------------------------------------------'), nl, 
				product(_, Name, _, Stock, Critic),
				Stock < Critic,              % I Could Not Understand Which One Should Be Greater
				write(Name), write(':\t'), write(Stock), write('\t'),  write(Critic), nl,
				fail,
				!. 
doYourDuty(6):-	write('  PROGRAM EXITS'), nl, nl, 
				!.


%%==================================================================================================
%%==================================================================================================
%%    DATABASE
%%==================================================================================================
%%==================================================================================================

% FACTS IN THE DATABASE: CUSTOMERS, PRODUCTS, ORDERS

%CUSTOMERS: CustomerID,CustomerName
customer(1,'Andre Fonseca').
customer(2,'Antonio Moreno').
customer(3,'Carine Schmitt').
customer(4,'Carlos Gonzalez').
customer(5,'Elizabeth Brown').
customer(6,'Elizabeth Lincoln').
customer(7,'Fran Wilson').
customer(8,'Giovanni Rovelli').
customer(9,'Howard Snyder').
customer(10,'Janete Limeira').
customer(11,'John Steel').
customer(12,'Lino Rodriguez').
customer(13,'Liz Nixon').
customer(14,'Lucia Carvalho').
customer(15,'Maria Anders').
customer(16,'Maria Larsson').
customer(17,'Michael Holz').
customer(18,'Pascale Cartrain').
customer(19,'Patricio Simpson').
customer(20,'Rita Muller').
customer(21,'Sergio Gutierrez').
customer(22,'Sven Ottlieb').
customer(23,'Thomas Hardy').
customer(24,'Victoria Ashworth').


%PRODUCTS: ProductID,ProductName,UnitPrice,AmountInStock,CriticalStockLevel
product(1,'Chocolade               ',21,22,30).
product(2,'Clam Chowder            ',26,10,15).
product(3,'Cote de Blaye           ',38,86,0).
product(4,'Cranberry Sauce         ',46,17,25).
product(5,'Frankfurter grune Sosse ',10,5,15).
product(6,'Gorgonzola Telino       ',9,25,5).
product(7,'Guarana Fantastica      ',21,104,25).
product(8,'Hokkien Fried Mee       ',29,113,25).
product(9,'Hot Pepper Sauce        ',18,20,15).
product(10,'Hot Spiced Okra         ',19,112,20).
product(11,'Ipoh Coffee             ',124,0,0).
product(12,'Manchego La Pastora     ',53,20,10).
product(13,'Marmalade               ',49,17,0).
product(14,'Mozzarella di Giovanni  ',10,85,10).
product(15,'Nord-Ost Matjeshering   ',14,26,0).
product(16,'Pate chinois            ',10,36,0).
product(17,'Perth Pasties           ',20,10,15).
product(18,'Queso Cabrales          ',16,65,30).
product(19,'Ravioli Angelo          ',33,0,0).
product(20,'Schoggi Schokolade      ',55,79,0).
product(21,'Tarte au sucre          ',14,52,10).
product(22,'Tourtiere               ',22,26,0).
product(23,'Tunnbrod                ',35,14,0).


%ORDERS: CustomerID,ProductID,OrderAmount
order(3,3,5).
order(3,9,2).
order(3,10,30).
order(4,1,6).
order(4,2,2).
order(6,1,8).
order(10,1,25).
order(10,2,30).
order(10,3,8).
order(10,4,60).
order(11,16,7).
order(12,5,4).
order(12,6,10).
order(12,7,50).
order(12,8,15).
order(12,9,20).
order(12,15,5).
order(12,16,30).
order(12,17,4).
order(12,18,20).
order(21,3,25).
order(21,4,10).
order(22,1,5).

:-start. %Headerless predicate