From 26eee28d61c0d1e85c46a782c0a6069103685e51 Mon Sep 17 00:00:00 2001 From: Yujia Date: Sun, 15 Nov 2015 16:28:52 -0500 Subject: [PATCH 1/4] added "show plan list" and " show current rent list" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit added “show plan list" and “show current rent list" --- Query.java | 66 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/Query.java b/Query.java index ff44d0c..3ca331c 100644 --- a/Query.java +++ b/Query.java @@ -54,6 +54,12 @@ public class Query { private String _plan_sql = "Select plan from person where cid = ?"; private String _plan_details_sql = "Select * from plans where planName = ?"; + + private String _plan_list_sql = "Select * from plans"; + + private String _current_rent_list_sql = "Select * " + + "from movie m, rentals r " + + "where r.cid=? and r.movieId = m.id"; private PreparedStatement _director_mid_statement; private PreparedStatement _actor_mid_statement; @@ -63,9 +69,12 @@ public class Query { private PreparedStatement _plan_details_statement; private PreparedStatement _movie_actor_statement; private PreparedStatement _movie_director_statement; + private PreparedStatement _plan_list_statement; + private PreparedStatement _current_rent_list_sql_statement; private String currentUser; + private int currentUser_ID; private String _customer_login_sql = "SELECT * FROM person WHERE username = ? and password = ?"; private PreparedStatement _customer_login_statement; @@ -123,10 +132,7 @@ public void prepareStatements() throws Exception { _search_statement = _imdb.prepareStatement(_search_sql); _director_mid_statement = _imdb.prepareStatement(_director_mid_sql); - _actor_mid_statement = _imdb.prepareStatement(_actor_mid_sql); - _rent_statement = _customer_db.prepareStatement(_rent_sql); - _movie_actor_statement = _imdb.prepareStatement(_movie_actor_sql); - _movie_director_statement = _imdb.prepareStatement(_movie_director_sql); + _customer_login_statement = _customer_db.prepareStatement(_customer_login_sql); _begin_transaction_read_write_statement = _customer_db.prepareStatement(_begin_transaction_read_write_sql); @@ -136,9 +142,15 @@ public void prepareStatements() throws Exception { /* add here more prepare statements for all the other queries you need */ /* . . . . . . */ + _actor_mid_statement = _imdb.prepareStatement(_actor_mid_sql); + _rent_statement = _customer_db.prepareStatement(_rent_sql); + _movie_actor_statement = _imdb.prepareStatement(_movie_actor_sql); + _movie_director_statement = _imdb.prepareStatement(_movie_director_sql); _rent_count_statement = _customer_db.prepareStatement(_rent_count_sql); _plan_statement = _customer_db.prepareStatement(_plan_sql); _plan_details_statement = _customer_db.prepareStatement(_plan_details_sql); + _plan_list_statement = _customer_db.prepareStatement(_plan_list_sql); + _current_rent_list_sql_statement = _customer_db.prepareStatement(_current_rent_list_sql); } @@ -149,7 +161,31 @@ public int helper_compute_remaining_rentals(int cid) throws Exception { /* how many movies can she/he still rent ? */ /* you have to compute and return the difference between the customer's plan and the count of oustanding rentals */ - return (99); + _plan_statement.clearParameters(); + _plan_statement.setInt(1, cid); + ResultSet plan = _plan_statement.executeQuery(); + _plan_details_statement.clearParameters(); + + /* Find plan details from the given plan name. */ + if(plan.next()) + _plan_details_statement.setString(1, plan.getString(1)); + else + _plan_details_statement.setString(1, "valuePlan"); + + /* Get the plan in user's max rental count. */ + ResultSet details = _plan_details_statement.executeQuery(); + int rentalsLeft = 0; + if(details.next()) + rentalsLeft = details.getInt(3); + + /* Get the amount of currently rented movies. */ + _rent_count_statement.clearParameters(); + _rent_count_statement.setInt(1, cid); + ResultSet count_set = _rent_count_statement.executeQuery(); + + if(count_set.next()) + rentalsLeft -= count_set.getInt(1); + return (rentalsLeft); } public String helper_compute_customer_name(int cid) throws Exception { @@ -189,6 +225,8 @@ public int transaction_login(String name, String password) throws Exception { cid = cid_set.getInt(2); else cid = -1; + + currentUser_ID = cid; return(cid); } @@ -271,7 +309,7 @@ public void transaction_search(int cid, String movie_title) if(rent_set.next()== false) System.out.println("\t\tAVAILABLE"); else{ - if(rent_set.getString(1)==currentUser) + if(rent_set.getString(1)==(currentUser_ID+"")) { System.out.println("\t\tYOU CURRENTLY RENT IT"); } @@ -299,10 +337,26 @@ public void transaction_choose_plan(int cid, int pid) throws Exception { public void transaction_list_plans() throws Exception { /* println all available plans: SELECT * FROM plan */ + ResultSet plan_list_set = _plan_list_statement.executeQuery(); + + while(plan_list_set.next()){ + System.out.println("\t\tPlan name: "+plan_list_set.getString(1) + ", Price: " + plan_list_set.getString(2) + + ", Maximum rental: " + plan_list_set.getString(3)); + } + plan_list_set.close(); } public void transaction_list_user_rentals(int cid) throws Exception { /* println all movies rented by the current user*/ + _current_rent_list_sql_statement.clearParameters(); + _current_rent_list_sql_statement .setInt(1,currentUser_ID); + ResultSet currentRent_set = _current_rent_list_sql_statement.executeQuery(); + + System.out.println("The movies that are current renting:"); + while (currentRent_set.next()){ + System.out.println("\t\tMovie name: " + currentRent_set.getString(2)); + } + currentRent_set.close(); } public void transaction_rent(int cid, int mid) throws Exception { From d3079dc5b5878a479d2cd7ead83635544d5b9bc3 Mon Sep 17 00:00:00 2001 From: Yujia Date: Sun, 15 Nov 2015 17:07:28 -0500 Subject: [PATCH 2/4] add transaction control MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add transaction control to “plan check” and “rent number check” --- Query.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Query.java b/Query.java index 3ca331c..6cb6316 100644 --- a/Query.java +++ b/Query.java @@ -149,8 +149,7 @@ public void prepareStatements() throws Exception { _rent_count_statement = _customer_db.prepareStatement(_rent_count_sql); _plan_statement = _customer_db.prepareStatement(_plan_sql); _plan_details_statement = _customer_db.prepareStatement(_plan_details_sql); - _plan_list_statement = _customer_db.prepareStatement(_plan_list_sql); - _current_rent_list_sql_statement = _customer_db.prepareStatement(_current_rent_list_sql); + } @@ -337,6 +336,10 @@ public void transaction_choose_plan(int cid, int pid) throws Exception { public void transaction_list_plans() throws Exception { /* println all available plans: SELECT * FROM plan */ + _customer_db.setAutoCommit(false); + _customer_db.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); + + _plan_list_statement = _customer_db.prepareStatement(_plan_list_sql); ResultSet plan_list_set = _plan_list_statement.executeQuery(); while(plan_list_set.next()){ @@ -344,10 +347,16 @@ public void transaction_list_plans() throws Exception { ", Maximum rental: " + plan_list_set.getString(3)); } plan_list_set.close(); + _customer_db.commit(); + _customer_db.setAutoCommit(true); // default value } public void transaction_list_user_rentals(int cid) throws Exception { /* println all movies rented by the current user*/ + _customer_db.setAutoCommit(false); + _customer_db.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); + + _current_rent_list_sql_statement = _customer_db.prepareStatement(_current_rent_list_sql); _current_rent_list_sql_statement.clearParameters(); _current_rent_list_sql_statement .setInt(1,currentUser_ID); ResultSet currentRent_set = _current_rent_list_sql_statement.executeQuery(); @@ -357,6 +366,8 @@ public void transaction_list_user_rentals(int cid) throws Exception { System.out.println("\t\tMovie name: " + currentRent_set.getString(2)); } currentRent_set.close(); + _customer_db.commit(); + _customer_db.setAutoCommit(true); // default value } public void transaction_rent(int cid, int mid) throws Exception { From 40405c61b1e41b6b4e68d5bf506c6670d9c66dd1 Mon Sep 17 00:00:00 2001 From: Yujia Date: Sun, 15 Nov 2015 18:26:57 -0500 Subject: [PATCH 3/4] added planId as plan primary key, transaction control --- Query.java | 38 ++++++++++++++++++++++++++------------ setup.sql | 18 +++++++++--------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/Query.java b/Query.java index 6cb6316..9f2283e 100644 --- a/Query.java +++ b/Query.java @@ -51,15 +51,17 @@ public class Query { private String _rent_count_sql = "Select Count(*) from rentals where cid = ?"; - private String _plan_sql = "Select plan from person where cid = ?"; + private String _plan_sql = "Select planID from person where cid = ?"; - private String _plan_details_sql = "Select * from plans where planName = ?"; + private String _plan_details_sql = "Select * from plans where pid = ?"; private String _plan_list_sql = "Select * from plans"; private String _current_rent_list_sql = "Select * " + "from movie m, rentals r " + "where r.cid=? and r.movieId = m.id"; + + private String _valid_movie_sql = "Select * from movie where id = ?"; private PreparedStatement _director_mid_statement; private PreparedStatement _actor_mid_statement; @@ -71,6 +73,7 @@ public class Query { private PreparedStatement _movie_director_statement; private PreparedStatement _plan_list_statement; private PreparedStatement _current_rent_list_sql_statement; + private PreparedStatement _valid_movie_statement; private String currentUser; @@ -149,6 +152,7 @@ public void prepareStatements() throws Exception { _rent_count_statement = _customer_db.prepareStatement(_rent_count_sql); _plan_statement = _customer_db.prepareStatement(_plan_sql); _plan_details_statement = _customer_db.prepareStatement(_plan_details_sql); + _valid_movie_statement = _customer_db.prepareStatement(_valid_movie_sql); } @@ -167,15 +171,15 @@ public int helper_compute_remaining_rentals(int cid) throws Exception { /* Find plan details from the given plan name. */ if(plan.next()) - _plan_details_statement.setString(1, plan.getString(1)); + _plan_details_statement.setInt(1, plan.getInt(1)); else - _plan_details_statement.setString(1, "valuePlan"); + _plan_details_statement.setInt(1, 1); /* Get the plan in user's max rental count. */ ResultSet details = _plan_details_statement.executeQuery(); int rentalsLeft = 0; if(details.next()) - rentalsLeft = details.getInt(3); + rentalsLeft = details.getInt(4); /* Get the amount of currently rented movies. */ _rent_count_statement.clearParameters(); @@ -184,6 +188,9 @@ public int helper_compute_remaining_rentals(int cid) throws Exception { if(count_set.next()) rentalsLeft -= count_set.getInt(1); + plan.close(); + details.close(); + count_set.close(); return (rentalsLeft); } @@ -200,7 +207,14 @@ public boolean helper_check_plan(int plan_id) throws Exception { public boolean helper_check_movie(int mid) throws Exception { /* is mid a valid movie id ? you have to figure out */ - return true; + _valid_movie_statement.clearParameters(); + _valid_movie_statement.setInt(1,mid); + ResultSet movie_set = _valid_movie_statement.executeQuery(); + if (movie_set.next()) + { + return true; + } + return false; } private int helper_who_has_this_movie(int mid) throws Exception { @@ -239,15 +253,15 @@ public void transaction_personal_data(int cid) throws Exception { /* Find plan details from the given plan name. */ if(plan.next()) - _plan_details_statement.setString(1, plan.getString(1)); + _plan_details_statement.setInt(1, plan.getInt(1)); else - _plan_details_statement.setString(1, "valuePlan"); + _plan_details_statement.setInt(1, 1); - /* Get the plan in use's max rental count. */ + /* Get the plan in user's max rental count. */ ResultSet details = _plan_details_statement.executeQuery(); int rentalsLeft = 0; if(details.next()) - rentalsLeft = details.getInt(3); + rentalsLeft = details.getInt(4); /* Get the amount of currently rented movies. */ _rent_count_statement.clearParameters(); @@ -343,8 +357,8 @@ public void transaction_list_plans() throws Exception { ResultSet plan_list_set = _plan_list_statement.executeQuery(); while(plan_list_set.next()){ - System.out.println("\t\tPlan name: "+plan_list_set.getString(1) + ", Price: " + plan_list_set.getString(2) + - ", Maximum rental: " + plan_list_set.getString(3)); + System.out.println("\t\tPlan ID: "+ plan_list_set.getString(1) + ", Plan name: "+plan_list_set.getString(2) + ", Price: " + plan_list_set.getString(3) + + ", Maximum rental: " + plan_list_set.getString(4)); } plan_list_set.close(); _customer_db.commit(); diff --git a/setup.sql b/setup.sql index c181fa7..a28ccc1 100644 --- a/setup.sql +++ b/setup.sql @@ -1,18 +1,18 @@ -create table plans(planName text primary key, fee int, movieLimit int); -create table person(userName text, cid int primary key, password text, city text, state text, email text, phoneNumber text, plan text, foreign key(plan) references plans(planName)); +create table plans(pid int primary key, planName text, fee int, movieLimit int); +create table person(userName text, cid int primary key, password text, city text, state text, email text, phoneNumber text, planID int, foreign key(planID) references plans(pid)); CREATE TABLE MOVIE (id int PRIMARY KEY, name VARCHAR(150), year int); create table rentals(cid int, foreign key(cid) references person(cid), movieId int, foreign key(movieId) references movie(id), dateCheckedOut date, dateCheckedIn date); \copy MOVIE from 'C:/imdb2015/movie.txt' with delimiter '|' null as ''; -insert into plans values('valuePlan', 8.99, 2); -insert into plans values('economyPlan',12.99, 5); -insert into plans values('jumboPlan',19.99, 20); +insert into plans values(1, 'valuePlan', 8.99, 2); +insert into plans values(2, 'economyPlan',12.99, 5); +insert into plans values(3, 'jumboPlan',19.99, 20); -insert into person values('user1', 1, '12345', 'Houston', 'Texas', 'user1@emailClient.com', '555-4565', 'valuePlan'); -insert into person values('user2', 2, '6789', 'Houston', 'Texas', 'user2@emailClient.com', '555-4765', 'valuePlan'); -insert into person values('user3', 3, '12456', 'Boston', 'Massachussetts', 'user3@emailClient.com', '554-4568', 'valuePlan'); +insert into person values('user1', 1, '12345', 'Houston', 'Texas', 'user1@emailClient.com', '555-4565', 1); +insert into person values('user2', 2, '6789', 'Houston', 'Texas', 'user2@emailClient.com', '555-4765', 1); +insert into person values('user3', 3, '12456', 'Boston', 'Massachussetts', 'user3@emailClient.com', '554-4568', 1); insert into rentals values(1, 34, '2015-06-05', '2015-06-12'); insert into rentals values(1, 75, '2015-06-11', '2015-06-15'); -insert into rentals values(3, 77, '2015-06-07', '2015-06-16'); \ No newline at end of file +insert into rentals values(3, 77, '2015-06-07', '2015-06-16'); From 3dcca847994665e835f08a818a9c89af87c888ed Mon Sep 17 00:00:00 2001 From: Yujia Date: Sun, 15 Nov 2015 21:33:22 -0500 Subject: [PATCH 4/4] change list function is added 1. change list function is added 2. user1 rent 3 movies instead of 2 --- Query.java | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++- setup.sql | 4 +++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/Query.java b/Query.java index 9f2283e..f13b8e1 100644 --- a/Query.java +++ b/Query.java @@ -3,6 +3,7 @@ import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.*; import java.util.*; @@ -62,6 +63,8 @@ public class Query { + "where r.cid=? and r.movieId = m.id"; private String _valid_movie_sql = "Select * from movie where id = ?"; + + private String _update_plan_sql = "Update person set planID = ? where cid = ?"; private PreparedStatement _director_mid_statement; private PreparedStatement _actor_mid_statement; @@ -74,6 +77,7 @@ public class Query { private PreparedStatement _plan_list_statement; private PreparedStatement _current_rent_list_sql_statement; private PreparedStatement _valid_movie_statement; + private PreparedStatement _update_plan_statement; private String currentUser; @@ -153,6 +157,7 @@ public void prepareStatements() throws Exception { _plan_statement = _customer_db.prepareStatement(_plan_sql); _plan_details_statement = _customer_db.prepareStatement(_plan_details_sql); _valid_movie_statement = _customer_db.prepareStatement(_valid_movie_sql); + _plan_list_statement = _customer_db.prepareStatement(_plan_list_sql); } @@ -199,10 +204,42 @@ public String helper_compute_customer_name(int cid) throws Exception { return ("JoeFirstName" + " " + "JoeLastName"); } + + public int helper_current_rent_number(int cid) throws Exception { + /* returns the number of the rented movie for this user */ + int rentals = 0; + _rent_count_statement.clearParameters(); + _rent_count_statement.setInt(1, cid); + ResultSet count_set = _rent_count_statement.executeQuery(); + if(count_set.next()) + rentals = count_set.getInt(1); + count_set.close(); + return rentals; + } + + public int helper_plan_maximum (int pid) throws Exception { + /* returns the maximum number of movie to be rented for this plan */ + int allow = 0; + _plan_details_statement.clearParameters(); + _plan_details_statement.setInt(1, pid); + ResultSet plan_set = _plan_details_statement.executeQuery(); + if (plan_set.next()) + allow = plan_set.getInt(4); + plan_set.close(); + return allow; + } public boolean helper_check_plan(int plan_id) throws Exception { /* is plan_id a valid plan id ? you have to figure out */ - return true; + _plan_details_statement.clearParameters(); + _plan_details_statement.setInt(1,plan_id); + ResultSet plan_set = _plan_details_statement.executeQuery(); + if (plan_set.next()){ + plan_set.close(); + return true; + } + plan_set.close(); + return false; } public boolean helper_check_movie(int mid) throws Exception { @@ -212,8 +249,10 @@ public boolean helper_check_movie(int mid) throws Exception { ResultSet movie_set = _valid_movie_statement.executeQuery(); if (movie_set.next()) { + movie_set.close(); return true; } + movie_set.close(); return false; } @@ -346,6 +385,35 @@ public void transaction_search(int cid, String movie_title) public void transaction_choose_plan(int cid, int pid) throws Exception { /* updates the customer's plan to pid: UPDATE customers SET plid = pid */ /* remember to enforce consistency ! */ + _customer_db.setAutoCommit(false); + _customer_db.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); + + Savepoint save1 = _customer_db.setSavepoint(); + + // check current number of rented movie + int curNum = helper_current_rent_number(cid); + System.out.println(""); + System.out.println("\tCurrent rent number: " + curNum); + + // check the new plan maximum rented movie number + int newPlan = helper_plan_maximum(pid); + System.out.println("\tPlan " + pid + " rent limit: " + newPlan); + + if (curNum<=newPlan){ + _update_plan_statement = _customer_db.prepareStatement(_update_plan_sql); + _update_plan_statement.setInt(1, pid); + _update_plan_statement.setInt(2, cid); + _update_plan_statement.executeUpdate(); + //_customer_db.commit(); + System.out.println("\tSuccessfully switch to plan "+pid); + } + else{ + _customer_db.rollback(save1); + System.out.println("\tCannot switch to plan "+pid); + } + _customer_db.commit(); + _customer_db.setAutoCommit(true); // default value + } public void transaction_list_plans() throws Exception { diff --git a/setup.sql b/setup.sql index a28ccc1..86df185 100644 --- a/setup.sql +++ b/setup.sql @@ -9,10 +9,12 @@ insert into plans values(1, 'valuePlan', 8.99, 2); insert into plans values(2, 'economyPlan',12.99, 5); insert into plans values(3, 'jumboPlan',19.99, 20); -insert into person values('user1', 1, '12345', 'Houston', 'Texas', 'user1@emailClient.com', '555-4565', 1); +insert into person values('user1', 2, '12345', 'Houston', 'Texas', 'user1@emailClient.com', '555-4565', 1); insert into person values('user2', 2, '6789', 'Houston', 'Texas', 'user2@emailClient.com', '555-4765', 1); insert into person values('user3', 3, '12456', 'Boston', 'Massachussetts', 'user3@emailClient.com', '554-4568', 1); + insert into rentals values(1, 34, '2015-06-05', '2015-06-12'); insert into rentals values(1, 75, '2015-06-11', '2015-06-15'); insert into rentals values(3, 77, '2015-06-07', '2015-06-16'); +insert into rentals values(1, 89, '2015-06-02', '2015-06-12');