Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 161 additions & 14 deletions Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.*;

import java.util.*;

Expand Down Expand Up @@ -51,9 +52,19 @@ 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 String _update_plan_sql = "Update person set planID = ? where cid = ?";

private PreparedStatement _director_mid_statement;
private PreparedStatement _actor_mid_statement;
Expand All @@ -63,9 +74,14 @@ 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 PreparedStatement _valid_movie_statement;
private PreparedStatement _update_plan_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;
Expand Down Expand Up @@ -123,10 +139,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);
Expand All @@ -136,9 +149,16 @@ 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);
_valid_movie_statement = _customer_db.prepareStatement(_valid_movie_sql);
_plan_list_statement = _customer_db.prepareStatement(_plan_list_sql);

}


Expand All @@ -149,23 +169,91 @@ 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.setInt(1, plan.getInt(1));
else
_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(4);

/* 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);
plan.close();
details.close();
count_set.close();
return (rentalsLeft);
}

public String helper_compute_customer_name(int cid) throws Exception {
/* you find the first + last name of the current customer */
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 {
/* 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())
{
movie_set.close();
return true;
}
movie_set.close();
return false;
}

private int helper_who_has_this_movie(int mid) throws Exception {
Expand All @@ -189,6 +277,8 @@ public int transaction_login(String name, String password) throws Exception {
cid = cid_set.getInt(2);
else
cid = -1;

currentUser_ID = cid;
return(cid);
}

Expand All @@ -202,15 +292,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();
Expand Down Expand Up @@ -271,7 +361,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");
}
Expand All @@ -295,14 +385,71 @@ 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 {
/* 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()){
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();
_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();

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();
_customer_db.commit();
_customer_db.setAutoCommit(true); // default value
}

public void transaction_rent(int cid, int mid) throws Exception {
Expand Down
20 changes: 11 additions & 9 deletions setup.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
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', 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 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 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(3, 77, '2015-06-07', '2015-06-16');
insert into rentals values(1, 89, '2015-06-02', '2015-06-12');