Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 7.55 KB | None | 0 0
  1. % Booking system only can start if user type 'book' in prolog intepreter.
  2. book:-
  3.     intro,
  4.     ask(book_room, [yes, no], firsttime).
  5.    
  6. intro:-
  7.     write('Hi, welcome to Aliah & Fuadah Hotel'), nl,
  8.     write('Please end up with (.) after put your answer, thank you'), nl, nl.
  9.  
  10. % Our hotel rules
  11. book_system(no):-
  12.     write('Thank you, see you again'), nl,
  13.     halt(0).
  14.    
  15. book_system(yes):-
  16.     question(check_in_date),
  17.     firstdate,
  18.     question(check_out_date),
  19.     seconddate,
  20.     count_person.
  21.  
  22. count_person:-
  23.     question(person),
  24.     read(X), getperson(X),
  25.     got_kid.
  26.    
  27. got_kid:-
  28.     question(got_kids),
  29.     getkids,
  30.     select_room.
  31.    
  32. select_room:-
  33.     ask(type_room, [single, twin, queen, king], selectroom).
  34.    
  35. want_breakfast:-
  36.     ask(breakfast, [yes, no], breakfast).
  37.    
  38. select_title:-
  39.     ask(calling_name, [mr, mrs, ms], selecttitle).
  40.    
  41. full_details:-
  42.     question(name),
  43.     read(X), assert(fullname(X)),
  44.     question(email),
  45.     read(Y), assert(fullemail(Y)),
  46.     question(phone),
  47.     read(Z), assert(fullphone(Z)),
  48.     payment_type.
  49.  
  50. payment_type:-
  51.     ask(type_payment, [cc, online_banking, cash], paymenttype).
  52.    
  53. type_of_payment(X):-
  54.     X = cc, ask(choose_one, [american_express, visa, mastercard], noncash);
  55.     X = online_banking, ask(choose_one, [cimb, maybank, bankislam], noncash);
  56.     X = cash, totalprice(Y), read(C), returnbalance(Y, C), printreceipt, goodbye.
  57.    
  58. goodbye:-
  59.     write('Thank you, goodbye.'), nl, halt(0).
  60.  
  61. % Questions for the knowledge base
  62. question(book_room):-
  63.     write('Would you like to book a room?'), nl.
  64.    
  65. question(check_in_date):-
  66.     write('Please enter check-in date, e.g(\"11-02-2017\")'), nl.
  67.    
  68. question(check_out_date):-
  69.     write('Please enter check-out date, e.g(\"12-02-2017\")'), nl.
  70.    
  71. question(person):-
  72.     write('How many person do you want to book? Counted if age >= 12.'), nl.
  73.    
  74. question(got_kids):-
  75.     write('How many kids do you have? put 0 if do not have.'), nl.
  76.    
  77. question(type_room):-
  78.     write('What type of room do you want?'), nl.
  79.    
  80. question(breakfast):-
  81.     write('Do you want breakfast?'), nl.
  82.    
  83. question(type_payment):-
  84.     write('What type of payment you want to use?'), nl.
  85.    
  86. question(calling_name):-
  87.     write('What do we need to call you?'), nl.
  88.    
  89. question(choose_one):-
  90.     write('Choose one'), nl.
  91.    
  92. question(name):-
  93.     write('What is your full name?'), nl.
  94.    
  95. question(email):-
  96.     write('What is your email?'), nl.
  97.    
  98. question(phone):-
  99.     write('What is your phone number'), nl.
  100.  
  101. answer(yes):-
  102.     write('Yes').
  103.  
  104. answer(no):-
  105.     write('No').
  106.    
  107. answer(single):-
  108.     write('Single bed: +RM10').
  109.  
  110. answer(twin):-
  111.     write('Twin bed: +RM20').
  112.    
  113. answer(queen):-
  114.     write('Queen bed: +RM30').
  115.    
  116. answer(king):-
  117.     write('King bed: +RM40').
  118.    
  119. answer(mr):-
  120.     write('Mr').
  121.    
  122. answer(mrs):-
  123.     write('Mrs').
  124.    
  125. answer(ms):-
  126.     write('Ms').
  127.    
  128. answer(cc):-
  129.     write('Credit Card').
  130.  
  131. answer(online_banking):-
  132.     write('Online Banking').
  133.    
  134. answer(cash):-
  135.     write('Cash').
  136.    
  137. answer(american_express):-
  138.     write('American Express').
  139.    
  140. answer(visa):-
  141.     write('Visa').
  142.    
  143. answer(mastercard):-
  144.     write('Master Card').
  145.    
  146. answer(cimb):-
  147.     write('CIMB Click').
  148.    
  149. answer(maybank):-
  150.     write('Maybank2u').
  151.    
  152. answer(bankislam):-
  153.     write('Bank Islam IB').
  154.    
  155. printreceipt:-
  156.     guesttitle(X), fullname(C), fullemail(V), fullphone(B),
  157.     firstday(A), secondday(S), personcount(D), kidcount(F),
  158.     wantbreakfast(G), Differenceday is S - A,
  159.     write(X), write(' '), write(C), nl,
  160.     write('your email is: '), write(V), nl,
  161.     write('your contact is: '), write(B), nl,
  162.     write('you book for: '), write(Differenceday), write(' days'), nl,
  163.     write('Total adult: '), write(D), nl,
  164.     write('Total kid: '), write(F), nl,
  165.     write('breakfast: '), write(G), nl.    
  166.    
  167. totalprice(Totalprice):-
  168.     month(X), calculateprice(X, Y),
  169.     firstday(Z), secondday(C), personcount(V), kidcount(B),
  170.     room(N), roomprice(N, M),
  171.     wantbreakfast(A), getbreakfast(A, S),
  172.     Differenceday is C - Z,
  173.     Totalprice is ((Y + M) * Differenceday) + ((V * 10 * S) + (B * 5 * S) * Differenceday),
  174.     write('Total price is: RM'), write(Totalprice), nl.  
  175.    
  176. calculateprice(X, Y):-
  177.     X =< 4, Y is 200;
  178.     X =< 8, Y is 100;
  179.     X =< 12, Y is 300.
  180.    
  181. roomprice(X, Y):-
  182.     X = single, Y is 10;
  183.     X = double, Y is 20;
  184.     X = queen, Y is 30;
  185.     X = king, Y is 40.
  186.    
  187. getbreakfast(X, Y):-
  188.     X = yes, Y is 1;
  189.     X = no, Y is 0.
  190.    
  191. returnbalance(X, Y):-
  192.     Y >= X, C is Y - X, write('Total balance is: '), write(C);
  193.     Y < X, write('not enough money, rejecting and bbye..'), halt(0).
  194.    
  195. parse(0, [First|_], First).
  196. parse(Index, [First|Rest], Response):-
  197.     Index > 0,
  198.     NextIndex is Index - 1,
  199.     parse(NextIndex, Rest, Response).
  200.  
  201. answers([], _).
  202. answers([First|Rest], Index):-
  203.     write(Index), write(' '), answer(First), nl,
  204.     NextIndex is Index + 1,
  205.     answers(Rest, NextIndex).
  206.  
  207. ask(Question, Choices, Time):-
  208.     question(Question),
  209.     answers(Choices, 0),
  210.     read(Index),
  211.     parse(Index, Choices, Response),
  212.     choice(Response, Time).
  213.  
  214. choice(Response, Time):-
  215.     Time = firsttime, book_system(Response);
  216.     Time = selectroom, assert(room(Response)), want_breakfast;
  217.     Time = breakfast, assert(wantbreakfast(Response)), select_title;
  218.     Time = selecttitle, assert(guesttitle(Response)), full_details;
  219.     Time = paymenttype, type_of_payment(Response);
  220.     Time = noncash, printreceipt, totalprice(_), goodbye.
  221.  
  222. firstdate:-
  223.     read(X), atom_codes(A, X), atom_chars(A, Z),  
  224.     correctmonth(Z, 0),
  225.     lengthmonth(Z, 0),
  226.     parsemonth(Z, 0, Month, Month2),
  227.     savemonth(Month2),
  228.     parsedate(Z, 0, Day, Day2),
  229.     savefirstday(Day2).
  230.    
  231. seconddate:-
  232.     read(X), atom_codes(A, X), atom_chars(A, Z),  
  233.     correctmonth(Z, 0),
  234.     lengthmonth(Z, 0),
  235.     parsemonth(Z, 0, Month, Month2),
  236.     parsedate(Z, 0, Day, Day2),
  237.     savesecondday(Day2).
  238.    
  239. parsemonth([], _, _, _).
  240. parsemonth([Head|Tail], X, Val, Val2):-
  241.     X = 3, atom_number(Head, N), Val is N, Y is X + 1, parsemonth(Tail, Y, Val, Val2);
  242.     X = 4, atom_number(Head, N), Val2 is (Val * 10) + N, Y is X + 1, parsemonth(Tail, Y, Val, Val2);
  243.     Y is X + 1, parsemonth(Tail, Y, Val, Val2).
  244.  
  245. savemonth(Val):-
  246.     assert(month(Val)).
  247.    
  248. checkmonth(Val):-
  249.     (month(X), Val = X) -> write('only accept same month'), nl, retract(month(X)), book_system(yes).
  250.  
  251. lengthmonth([], _).
  252. lengthmonth([Head|Tail], X):-
  253.     X > 9, write('Insert correct date'), nl, book_system(yes);
  254.     Y is X + 1, lengthmonth(Tail, Y).
  255.  
  256. correctmonth([], _).
  257. correctmonth([Head|Tail], X):-
  258.     X = 3, write('Insert correct date'), nl, book_system(yes);
  259.     Head = '-', Y is X + 1, correctmonth(Tail, Y);
  260.     correctmonth(Tail, X).
  261.  
  262. parsedate([], _, _, _).
  263. parsedate([Head|Tail], X, Val, Val2):-
  264.     X = 0, atom_number(Head, N), Val is N, Y is X + 1, parsedate(Tail, Y, Val, Val2);
  265.     X = 1, atom_number(Head, N), Val2 is (Val * 10) + N, Y is X + 1, parsedate(Tail, Y, Val, Val2);
  266.     Y is X + 1, parsedate(Tail, Y, Val, Val2).
  267.    
  268. savefirstday(Val):-
  269.     assert(firstday(Val)).
  270.  
  271. savesecondday(Val):-
  272.     (firstday(X), Val =< X) -> write('insert correct date'), retract(month(X)), retract(firstday(X)), book_system(yes);
  273.     assert(secondday(Val)).
  274.    
  275. getkids:-
  276.     read(X), assert(kidcount(X)).
  277.    
  278. getperson(X):-
  279.     X =< 0, write('Incorrect insert'), count_person;
  280.     assert(personcount(X)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement