% customer activities order_book(Customer, Book) :- print(Customer," is ordering ", Book), receive_order(Customer, Book). send_payment(Customer, Invoice) :- print(Customer, " is paying invoice ", Invoice), receive_payment(Customer, Invoice). % dealer activities receive_order(Customer, Book) :- book_available(Book), invoice_book(Customer, Book), ship_book(Customer, Book), !. receive_order(Customer, Book) :- print("backorder for ", Book). receive_payment(Customer, Invoice) :- adjust_account(Customer, Invoice). invoice_book(Customer, Book) :- get_invoice_number(Invoice), book(Book, _, Price), assert(account(Customer, Invoice, Price)), send_invoice(Invoice, Customer, Book, Price). send_invoice(Invoice, Customer, Book, Price) :- print("invoice ", Invoice, " sent to ", Customer). ship_book(Customer, Book) :- adjust_inventory(Book, -1), print(Book, " shipped to ", Customer). % dealer support operations book_available(Book) :- book(Book, Qty, _), Qty >= 1. get_invoice_number(Next_number) :- last_invoice(Old_number), Next_number is Old_number + 1, retract(last_invoice(Old_number)), assert(last_invoice(Next_number)). adjust_inventory(Book, Adjustment) :- book(Book, Qty, Price), NewQty is Qty + Adjustment, retract(book(Book, Qty, Price)), assert(book(Book, NewQty, Price)). adjust_account(Customer,Invoice) :- account(Customer, Invoice, Price), print(Customer, " has paid invoice ", Invoice), retract(account(Customer, Invoice, Price)),!. % sample database % book(Title,Qty,Price) book(dictionary, 2, 15). book(novel, 1, 5). % last invoice number used last_invoice(100). % test test :- order_book(joe, dictionary), order_book(jill, novel), send_payment(joe, _), send_payment(jill, _).