Advertisement
tankian202

Untitled

May 26th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.28 KB | None | 0 0
  1.  
  2. CREATE OR REPLACE FUNCTION checkAndInsertUser(
  3. p_username IN USERS_TABLE.USERNAME%TYPE,
  4. p_email IN USERS_TABLE.EMAIL%TYPE,
  5. p_password IN USERS_TABLE.PASSWORD%TYPE,
  6. p_balance IN USERS_TABLE.BALANCE%TYPE
  7. ) RETURN USERS_TABLE%ROWTYPE IS
  8. v_user users_table%ROWTYPE;
  9. BEGIN
  10. -- Ellenőrizzük, hogy a felhasználó regisztrált-e
  11. SELECT * INTO v_user FROM users_table WHERE EMAIL = p_email;
  12.  
  13. -- Ha a felhasználó megtalálható az adatbázisban, visszaadjuk
  14. RETURN v_user;
  15.  
  16. EXCEPTION
  17. WHEN NO_DATA_FOUND THEN
  18. -- Ha a felhasználót nem találjuk meg, beszúrjuk az új felhasználót
  19. BEGIN
  20. INSERT INTO users_table (USERNAME, EMAIL, PASSWORD, BALANCE)
  21. VALUES (p_username, p_email, p_password, p_balance);
  22. COMMIT;
  23.  
  24. -- Újra lekérdezzük az újonnan beszúrt felhasználót és visszaadjuk
  25. SELECT * INTO v_user FROM users_table WHERE EMAIL = p_email;
  26. RETURN v_user;
  27. EXCEPTION
  28. WHEN OTHERS THEN
  29. -- Ha valamilyen egyéb hiba történik, visszaadjuk a null-t
  30. RETURN NULL;
  31. END;
  32. WHEN OTHERS THEN
  33. -- Ha valamilyen egyéb hiba történik, visszaadjuk a null-t
  34. RETURN NULL;
  35. END;
  36. /
  37.  
  38. -- DECLARE
  39. -- v_result USERS_TABLE%ROWTYPE;
  40. -- BEGIN
  41. -- v_result := checkAndInsertUser('teszt_user', 'teszt_email', 'teszt_jelszo', 100);
  42. -- IF v_result.EMAIL IS NOT NULL THEN
  43. -- DBMS_OUTPUT.PUT_LINE('Felhasználó neve: ' || v_result.USERNAME);
  44. -- ELSE
  45. -- DBMS_OUTPUT.PUT_LINE('Hiba történt a felhasználó ellenőrzése vagy beszúrása közben.');
  46. -- END IF;
  47. -- END;
  48. -- /
  49.  
  50.  
  51. create or replace function checkBalance(p_user_id NUMBER, schedule_id NUMBER, p_amount NUMBER) return number is
  52. v_balance users_table.balance%type;
  53. v_price schedule.price%type;
  54. begin
  55. select balance into v_balance from users_table where id = p_user_id;
  56. select price into v_price from schedule where id = schedule_id;
  57.  
  58. if v_balance >= v_price * p_amount then
  59. return -1;
  60. else
  61. return ROUND(v_balance / v_price);
  62. end if;
  63. end;
  64.  
  65.  
  66.  
  67. create or replace function checkSeats(p_schedule_id NUMBER, amount NUMBER) return number is
  68. v_max_tickets number;
  69. v_sold_tickets number;
  70. begin
  71. select max_tickets into v_max_tickets from schedule where id = p_schedule_id;
  72. select count(*) into v_sold_tickets from tickets where schedule_id = p_schedule_id;
  73. if v_max_tickets - (amount + v_sold_tickets) >= 0 then
  74. return 0;
  75. else
  76. return -1;
  77. end if;
  78.  
  79. end;
  80.  
  81. create or replace function buyTicket(p_user_id NUMBER, schedule_id NUMBER, p_amount NUMBER, v_you_can_buy OUT number,
  82. v_available_tickets OUT number) return number is
  83. begin
  84. v_you_can_buy := checkBalance(p_user_id, schedule_id, p_amount);
  85. if v_you_can_buy = -1 then
  86. v_available_tickets := checkSeats(schedule_id, p_amount);
  87. if v_available_tickets = 0 then
  88. update users_table
  89. set balance = balance - p_amount * (select price from schedule where id = schedule_id)
  90. where id = p_user_id;
  91. for i in 1..p_amount
  92. loop
  93. insert into tickets (user_id, schedule_id) values (p_user_id, schedule_id);
  94. end loop;
  95. commit;
  96. return 1;
  97. else
  98. DBMS_OUTPUT.PUT_LINE('Nincs elég szabad hely a megadott mennyiségű jegy megvásárlásához.');
  99. return 0;
  100. end if;
  101.  
  102. else
  103. DBMS_OUTPUT.PUT_LINE('Nincs elég egyenlege a megadott mennyiségű jegy megvásárlásához. Elegendő jegy: ' ||
  104. v_you_can_buy);
  105. return -1;
  106. end if;
  107. end;
  108. /
  109.  
  110.  
  111.  
  112. create or replace function GETSCHEDULEBYPLAYTYPE_AND_DATE(p_type VARCHAR2, p_date DATE, schedule_id NUMBER)
  113. return SYS_REFCURSOR is
  114. v_cursor SYS_REFCURSOR;
  115. begin
  116. open v_cursor for
  117. select SCHEDULE.ID
  118. from schedule
  119. join plays on SCHEDULE.PLAY_ID = plays.id
  120. where PLAY_TYPE = p_type
  121. and TRUNC(PLAY_DATE) = p_date
  122. and SCHEDULE.ID != schedule_id;
  123. return v_cursor;
  124. end;
  125.  
  126. create or replace function GETSCHEDULEBYPLAYTYPE_ORDERED(p_type VARCHAR2, schedule_id NUMBER)
  127. return SYS_REFCURSOR is
  128. v_cursor SYS_REFCURSOR;
  129. begin
  130. open v_cursor for
  131. select SCHEDULE.ID
  132. from schedule
  133. join plays on SCHEDULE.PLAY_ID = plays.id
  134. where PLAY_TYPE = p_type
  135. and SCHEDULE.ID != schedule_id
  136. ORDER BY SCHEDULE.PRICE ;
  137.  
  138. return v_cursor;
  139. end;
  140.  
  141. create or replace function getScheduleWithLowestPrice(p_cursor IN SYS_REFCURSOR) return NUMBER is
  142. v_schedule_id NUMBER;
  143. v_price NUMBER;
  144. v_min_price NUMBER;
  145. v_min_schedule_id NUMBER;
  146. begin
  147.  
  148. select MAX(price) into v_min_price from schedule;
  149. LOOP
  150. FETCH p_cursor INTO v_schedule_id;
  151. EXIT WHEN p_cursor%NOTFOUND;
  152. select price into v_price from schedule where id = v_schedule_id;
  153. if v_price < v_min_price then
  154. v_min_price := v_price;
  155. v_min_schedule_id := v_schedule_id;
  156. end if;
  157. END LOOP;
  158. return v_min_schedule_id;
  159. end;
  160.  
  161.  
  162.  
  163. create or replace function get_Schedule_By_TheatreAndPlay(p_theatre_id Number, p_play_id NUMBER, schedule_id NUMBER)
  164. return SYS_REFCURSOR is
  165. v_cursor SYS_REFCURSOR;
  166. begin
  167. open v_cursor for
  168. select SCHEDULE.ID
  169. from schedule
  170. join plays on SCHEDULE.PLAY_ID = plays.id
  171. where PLAY_ID = p_play_id
  172. and THEATRE_ID = p_theatre_id
  173. and SCHEDULE.ID != schedule_id;
  174. return v_cursor;
  175. end;
  176.  
  177.  
  178.  
  179.  
  180.  
  181. -- jegy vasarlas.
  182. -- Ha nincs elegendo hely, akkor a tipus es datum szerint,
  183. -- keres mas eloadasokat, a legolcsobbikbol megveszi az osszeset.
  184.  
  185. -- A procedura kiirja a megvasarolt jegyek arat.
  186. -- PLSQL
  187.  
  188. CREATE OR REPLACE PROCEDURE main(p_username VARCHAR2,
  189. p_password VARCHAR2,
  190. p_email VARCHAR2,
  191. p_balance NUMBER,
  192. p_schedule_id NUMBER,
  193. p_amount NUMBER) IS
  194. v_user users_table%ROWTYPE;
  195. v_you_can_buy NUMBER;
  196. v_available_tickets NUMBER;
  197. v_purchase_status NUMBER;
  198. v_schedules_cursor SYS_REFCURSOR;
  199. v_play_type VARCHAR2(100); -- Specify appropriate size
  200. v_play_time DATE;
  201. v_cheapest_schedule_id NUMBER;
  202. v_price NUMBER;
  203. BEGIN
  204. v_user := CHECKANDINSERTUSER(p_username, p_email, p_password, p_balance);
  205. DBMS_OUTPUT.PUT_LINE('Felhasznalo neve: ' || v_user.username);
  206.  
  207. v_purchase_status := BUYTICKET(v_user.id,
  208. p_schedule_id,
  209. p_amount,
  210. v_you_can_buy,
  211. v_available_tickets);
  212.  
  213. IF v_purchase_status = 1 THEN
  214. SELECT price INTO v_price FROM schedule WHERE id = p_schedule_id;
  215. DBMS_OUTPUT.PUT_LINE('Sikeresen megvasarolt jegyek ara: ' || p_amount * v_price);
  216.  
  217. ELSIF v_purchase_status = 0 THEN
  218. SELECT play_type, TRUNC(PLAY_DATE)
  219. INTO v_play_type, v_play_time
  220. FROM schedule
  221. JOIN PLAYS ON SCHEDULE.PLAY_ID = PLAYS.ID
  222. WHERE SCHEDULE.ID = p_schedule_id;
  223.  
  224. v_schedules_cursor := GETSCHEDULEBYPLAYTYPE_AND_DATE(v_play_type, v_play_time, p_schedule_id);
  225. v_cheapest_schedule_id := GETSCHEDULEWITHLOWESTPRICE(v_schedules_cursor);
  226.  
  227. DBMS_OUTPUT.PUT_LINE('Probaljuk megvasarolni a legolcsobb jegyeket...');
  228.  
  229. v_purchase_status := BUYTICKET(v_user.id,
  230. v_cheapest_schedule_id,
  231. p_amount,
  232. v_you_can_buy,
  233. v_available_tickets);
  234.  
  235. IF v_purchase_status = 1 THEN
  236. SELECT price INTO v_price FROM schedule WHERE id = v_cheapest_schedule_id;
  237. DBMS_OUTPUT.PUT_LINE('Sikeresen megvasarolt jegyek ara: ' || p_amount * v_price);
  238. END IF;
  239. END IF;
  240.  
  241. EXCEPTION
  242. WHEN OTHERS THEN
  243. DBMS_OUTPUT.PUT_LINE('Hiba tortent: ' || SQLERRM);
  244. END;
  245. /
  246.  
  247. DECLARE
  248. p_username VARCHAR2(50) := 'teszt_user';
  249. p_password VARCHAR2(50) := 'test_password';
  250. p_email VARCHAR2(100) := 'teszt_email1';
  251. p_balance NUMBER := 100.00;
  252. p_schedule_id NUMBER := 1; -- Replace with a valid schedule ID from your database
  253. p_amount NUMBER := 1; -- Number of tickets to buy
  254. BEGIN
  255. -- Call the main procedure with the test values
  256. main(p_username, p_password, p_email, p_balance, p_schedule_id, p_amount);
  257. EXCEPTION
  258. WHEN OTHERS THEN
  259. DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
  260. END;
  261. /
  262.  
  263. -- Ha nincs hely,
  264. -- akkor az olcsobbik helyeken probalja
  265. -- meg megvenni a jegyeket,
  266. -- addig amig elfogy a penze vagy
  267. -- annyi jegyet tudott venni ahanyat akart.
  268.  
  269. create or replace procedure main2(p_username varchar2,
  270. p_password varchar2,
  271. p_email varchar2,
  272. p_balance number,
  273. p_schedule_id number,
  274. p_amount number) is
  275. v_user users_table%rowtype;
  276. v_you_can_buy number;
  277. v_purchase_status number;
  278. v_schedules_cursor sys_refcursor;
  279. v_play_type varchar2(100);
  280. v_play_time date;
  281. v_cheapest_schedule_id number;
  282. v_price number;
  283. v_total_price number;
  284. v_current_amount number;
  285. v_available_tickets_in number;
  286. v_available_tickets_out number;
  287. begin
  288. v_current_amount := p_amount;
  289. v_user := checkandinsertuser(p_username, p_email, p_password, p_balance);
  290. dbms_output.put_line('Felhasznalo neve: ' || v_user.username);
  291. v_purchase_status := buyticket(v_user.id, p_schedule_id, v_current_amount, v_you_can_buy, v_available_tickets_out);
  292. v_available_tickets_in := v_available_tickets_out;
  293. IF v_purchase_status = 1 THEN
  294. SELECT price INTO v_price FROM schedule WHERE id = p_schedule_id;
  295. DBMS_OUTPUT.PUT_LINE('Sikeresen megvasarolt jegyek ara: ' || v_current_amount * v_price);
  296. ELSIF v_purchase_status = 0 THEN
  297.  
  298. v_purchase_status := buyticket(v_user.id, p_schedule_id, v_available_tickets_in, v_you_can_buy,
  299. v_available_tickets_out);
  300. v_available_tickets_in := v_available_tickets_out;
  301. v_current_amount := v_current_amount - v_available_tickets_out;
  302. if v_purchase_status = -1 then
  303. SELECT price INTO v_price FROM schedule WHERE id = p_schedule_id;
  304. DBMS_OUTPUT.PUT_LINE('Sikeresen megvasarolt jegyek ara: ' || v_available_tickets_in * v_price ||
  305. 'de elfogyott a penze');
  306. end if;
  307. SELECT play_type, TRUNC(play_date)
  308. INTO v_play_type, v_play_time
  309. FROM schedule
  310. JOIN plays ON schedule.play_id = plays.id
  311. WHERE schedule.id = p_schedule_id;
  312.  
  313. v_current_amount := v_current_amount - v_available_tickets_in;
  314.  
  315. v_schedules_cursor := GETSCHEDULEBYPLAYTYPE_ORDERED(v_play_type, p_schedule_id);
  316. LOOP
  317. FETCH v_schedules_cursor INTO v_cheapest_schedule_id;
  318. EXIT WHEN v_cheapest_schedule_id IS NULL;
  319. v_purchase_status :=
  320. buyticket(v_user.id, v_cheapest_schedule_id, v_current_amount, v_you_can_buy, v_available_tickets_out);
  321. v_available_tickets_in := v_available_tickets_out;
  322. IF v_purchase_status = 1 THEN
  323. SELECT price INTO v_price FROM schedule WHERE id = v_cheapest_schedule_id;
  324. v_total_price := v_available_tickets_in * v_price;
  325. DBMS_OUTPUT.PUT_LINE('Sikeresen megvasarolt jegyek ara: ' || v_total_price);
  326. EXIT when 1 = 1;
  327. elsif v_purchase_status = 0 then
  328. v_purchase_status := buyticket(v_user.id, v_cheapest_schedule_id, v_available_tickets_in, v_you_can_buy,
  329. v_available_tickets_out);
  330. v_available_tickets_in := v_available_tickets_out;
  331. v_current_amount := v_current_amount - v_available_tickets_out;
  332. END IF;
  333. exit when v_current_amount = 0 or v_purchase_status = -1;
  334. END LOOP;
  335. end if;
  336. end;
  337.  
  338. /
  339. DECLARE
  340. p_username VARCHAR2(50) := 'teszt_user';
  341. p_password VARCHAR2(50) := 'test_password';
  342. p_email VARCHAR2(100) := 'test_email';
  343. p_balance NUMBER := 100.00;
  344. p_schedule_id NUMBER := 4; -- Replace with a valid schedule ID from your database
  345. p_amount NUMBER := 10; -- Number of tickets to buy
  346. BEGIN
  347. -- Call the main procedure with the test values
  348. main2(p_username, p_password, p_email, p_balance, p_schedule_id, p_amount);
  349. EXCEPTION
  350. WHEN OTHERS THEN
  351. DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
  352. END;
  353. /
  354.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement