rendered paste body% Samuel Kroslak,
% 3. rocnik
% 24172
%
% Vytvorte jednoduchy databazovy system pre pracu s udajmi
% o receptoch, ktory by umoznil:
% - zaradit recept, surovinu
% - vyhladat recept (skupinu receptov) podla zadanych kriterii
% (meno, pouzite suroviny)
% - zoradit recepty podla zvoleneho typu udaja (mena, ceny,
% casu pripravy)
% - zistit cenu receptu pre dany pocet osob
% - zistit, ktore recepty pripadaju do uvahy, ak mame 'nejake'
% suroviny
% Na konci cinnosti vytvorenu databazu receptov ulozi do suboru
% (ktory bude obsahovat fakty o jednotlivych receptoch) a umozni
% nacitanie databazy zo suboru do prologovskej databazys.
%
% udaje v databaze musia obsahovat minimalne:
% - meno receptu
% - potrebne suroviny spolu s ich mnozstvom
% - cas pripravy v tvare (HH:MM)s
% - ceny surovin
% !!!! Pracujte s datovym svetom ARITY/PROLOGU
main :-
repeat,
[!menu, get(InputInt), nl, case(InputInt)!], fail.
menu :-
nl,
write('[1] Zarad recept'), nl,
write('[2] Zarad surovinu'), nl,
write('[3] Vyhladaj recept'), nl,
write('[4] Zorad recepty'), nl,
write('[5] Zisti cenu receptu'), nl,
write('[6] Najdi recepty podla surovin'), nl,
write('[7] Nacitaj z databazy'), nl,
write('[8] Uloz do databazy'), nl,
write('[0] Koniec'),nl,
% quit
case(48) :-
nl, write('Koniec programu'), nl, abort.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% add receipe
case(49) :-
nl, write('Zaradenie receptu'), nl,
enterName(Name),
write('Suroviny:'), nl,
ingredients(I),
enterTime(Time),
recipe(Name, I, Time)=..[recipe, Name, I, Time],
records(recipes, recipe(Name, I, Time), REF).
enterName(Name) :-
write('Meno receptu: '),
read_string(50, X), string_term(X, Name),
((string_length(X, 0), main);
(var(Name), write('Meno musi zacinat malym pismenom!'), nl, main);
(Y=..[recipe, Name, _, _],
not(recorded(recipes, Y, _)));
(write('Recept s tymto menom existuje!'), nl, main)).
enterTime(Time) :-
write('Cas pripravy [min]: '),
read_string(50, X), string_term(X, Y),
((string_length(X, 0), main);
(integer(Y), Y > 0, Time = Y);
(write('Nespravne zadany cas!'), nl, enterTime(Time))).
ingredients([H-H1|T]):-
write('Meno suroviny: '),
read_string(50, X), string_term(X, Name),
((string_length(X, 0), main);
(Z=..[surovina, Name, _],
recorded(ingredients, Z, _));
(write('Neznama surovina!'), nl, main)),
write('Mnozstvo: '),
read_string(50, Y), string_term(Y, Ammount),
((string_length(Y, 0), main);
((not(number(Ammount)); Ammount =< 0),
write('Zle zadane mnozstvo!'), nl, main );
(number(Ammount), Ammount > 0,
H = Name, H1 = Ammount)),
write('Dalsiu surovinu [A/N]: '), get(Next), nl,
ifthenelse(Next =:= 97, ingredients(T), T = []).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% adding ingredients
case(50) :-
ingrName(Name), ingrPrice(Price),
ingredient(Name, Price)=..[ingredient, Name, Price],
recordsFromDatabase(ingredients, ingredient(Name, Price), REF).
ingrName(Name) :-
write('Meno ingredients: '),
read_string(50, X), string_term(X, Y),
((string_length(X, 0), main);
(var(Y), write('Meno musi zacinat malym pismenom!'), nl, meno_sur(Name));
(Z=..[surovina, Y, _],
recorded(ingredients, Z, _),
write('Tato surovina je v databaze!'),
nl, ingrName(Name));
Name = Y).
ingrPrice(Price) :-
write('Cena ingredients za [kg], [l]: '),
read_string(50, X), string_term(X, Y),
((string_length(X, 0), main);
(number(Y), Y > 0, Price = Y);
(write('Zle zadana cena!'), nl, ingrPrice(Price))).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% search for recipes
case(51) :-
menuSearch, get(Choice), nl,
searchRecipes(Choice).
menuSearch :-
nl,
write('1. Vyhladat recept podla mena'), nl,
write('2. Vyhladat recept podla surovin'), nl,
write('3. Vyhladat vsetky recepty'), nl,
write('4. Vyhladat vsetky suroviny'), nl,
write('0. Navrat do hlavneho menu'), nl,
write('Zadaj volbu: ').
searchRecipes(48).
searchRecipes(49) :-
nl, write('Hladanie receptu podla mena:'), nl,
readName(Name), !,
(recipe(Name, Ingr, Time)=..[recipe, Name, Ingr, Time],
recorded(recepty, recipe(Name, Ingr, Time), _)),
nl, write('Meno: '), write(Name), nl,
write('Cas: '), write(Time), nl,
write('Suroviny: '), write(Ingr), nl.
searchOutput([]).
searchOutput([X|Y]):-
X=..[_, Name, Ingr, Time],
write('Meno: '), write(Name), nl,
write('Cas pripravy: '), write(Time), nl,
write('Pouzite suroviny: '), write(Ingr), nl, nl,
get0_noecho(_), searchOutput(Y).
ingrPrint([]).
ingrPrint([H|T]):-
write(H), nl, ingrPrint(T).
ingrPrint2([], 0).
ingrPrint2([Ingr-Ammount-Price|T], Price1) :-
write(Ingr), write(' - '),
write(Ammount), nl,
ingrPrint2(T, Price2),
Price1 is Price2 + Ammount * Price.
searchRecipes(50) :-
nl, write('Hladanie receptu podla surovin:'), nl,
searchRecipes2(Ingr), nl,
not(ingrFind(Ingr)).
searchRecipes(51) :-
findall(X, recorded(recipes, X, _), List),
searchOutput(List).
searchRecipes(52) :-
findall(X, recorded(ingredients, X, _), List),
ingrPrint(List).
searchRecipes2([H|T]):-
write('Meno suroviny: '),
read_string(50, X), string_term(X, Name),
((string_length(X, 0), main);
((ingredient(Name, _)=..[ingredient, Name, _],
not(recorded(ingredients, ingredient(Name, _), _))),
write('Neznama surovina!'), nl, main);
H = Name),
write('Dalsiu surovinu [A/N]: '), get(Choice), nl,
ifthenelse((Choice =:= 65; Choice =:= 97), ingrPrint2(T), T = []).
ingrFind(Ingr) :-
recipe(Name, X, Time)=..[recipe, Name, X, Time],
recorded(recipes, recipe(Name, X, Time), _),
isIn(Ingr, X),
write('Meno: '), write(Name), nl,
write('Cas: '), write(Time), nl,
write('Suroviny: '), write(X), nl, nl,
get0_noecho(_), fail.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% sort recipes
case(52) :-
menuSort, get(Choice), nl,
sortRecipes(Choice).
menu2 :-
nl,
write('1. Zoradit recepty podla mena'), nl,
write('2. Zoradit recepty podla ceny'), nl,
write('3. Zoradit recepty podla casu pripravy'), nl,
write('0. Spat do hlavneho menu').
sortRecipes(50) :-
findall(C-M, (recorded(recipes, recipe( M, S, _),_), sum1(S,C)), Y),
sort(Y, Z),
tr(Z,X),
recipesOutput(X).
tr([],[]).
tr([C-M|T],[M|T2]) :- tr(T,T2).
% sort by price
sortRecipes(49) :-
findall(Name, recorded(recipes, recipe(Name, _, _), _), Y),
sort(Y, Z),
recipesOutput(Z).
recipesOutput([Name|T]) :-
recorded(recipes, recipe(Name, X, Time), _),
write('Meno: '), write(Name), nl,
write('Cas: '), write(Time), nl,
write('Suroviny: '), write(X), nl,
write('Cena: '), sum1(X,Y), write(Y),nl,nl,
get0_noecho(_), recipesOutput(T).
recipesOutput([]).
sortRecipes(48).
sortRecipes(51) :-
findall(Time, recorded(recipes, recipe(_, _, Time), _), Y),
sort(Y, Z),
recipesOutput2(Z).
recipesOutput2([Cas|T]) :-
recorded(recipes, recipe(Name, X, Time), _),
write('Meno: '), write(Name), nl,
write('Cas: '), write(Time), nl,
write('Suroviny: '), write(X), nl, nl,
get0_noecho(_), recipesOutput2(T).
recipesOutput2([]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get recipe price
case(53) :-
nl, write('Zistenie ceny: '), nl,
readName(Name), readPeopleCount(Count),
recipe(Name, Ingr, _)=..[recipe, Name, Ingr, _], !,
recorded(recipes, recipe(Name, Ingr, _), _),
sum1(Ingr, Price),
Price2 is Price * Count,
write('Recept '), write(Name) ,
write(' pre pocet osob: '), write(Count),
write(' bude stat: '), write(Price2), nl.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% find recipes
case(54) :-
write('Suroviny:'), nl,
ingredients1(Ingr), nl,
searchRecipes3(Ingr).
hladaj(Ingr) :-
recipe(Name, X, Time)=..[recipe, Name, X, Time],
recorded(recipes, recipe(Name, X, Time), _),
do1(Ingr, X),
write('Meno: '), write(Name), nl,
write('Cas: '), write(Time), nl,
write('Suroviny: '), write(X), nl, nl,
get0_noecho(_),
fail.
do1(Ingr, []).
do1(Ingr, [X-_|T]) :- not(isInNot2(Ingr, X)), do1(Ingr, T).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% loading data from database
case(55) :-
fileerrors(_, off),
(open(H, database1, r); write('Subor s databazou neexistuje!'), nl, fail),
fileerrors(_, on),
repeat,
read(H, X),
(X = end_of_file, !, close(H);
((X =.. [recipe|T],not(recorded(recipes, X, _)),recordsFromDatabase(recipes, X, REF));
(X =.. [ingredient|T],not(recorded(ingredients, X, _)),recordsFromDatabase(ingredients, X, REF));
true),
fail).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% save into database
case(56) :-
create(H, database1),
(recorded(recipes, X, _);recorded(ingredients, X, _)),
[!writeq(H, X), writeq(H, .), nl(H)!],
fail.
case(56) :- open(H, database1, r), close(H), X is H - 1, close(X).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% universal clausules
readName(Name) :-
write('Meno receptu: '),
read_string(50, X), string_term(X, Y),
((string_length(X, 0), main);
(nonvar(Y),
Name = Y)).
readPeopleCount(Count) :-
write('Zadaj pocet osob: '), read_string(50, X),
string_term(X, Y),
((string_length(X, 0), main);
(integer(Y), Y > 0, Count = Y);
(write('Zly pocet osob!'), nl, readPeopleCount(Count))).
isIn([], X).
isIn([Ingr|T], X) :-
isInNot(isInNot(X, Ingr)),
isIn(T, X).
isInNot([], X).
isInNot([H-_|T], X) :- H \= X, isInNot(T, X).
isInNot2([], X).
isInNot2([H|T], X) :- H \= X, isInNot2(T, X).
sum1([], 0).
sum1([Meno-Mnozstvo|T], Cena) :-
surovina(Meno, C)=..[surovina, Meno, C],
recorded(ingredients, surovina(Meno, C), _),
scitaj(T, C1),
Cena is C * (Mnozstvo / 1000) + C1.