-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRentControl.java
More file actions
92 lines (72 loc) · 4.03 KB
/
RentControl.java
File metadata and controls
92 lines (72 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package src.anearcan;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class RentControl{
private Date currentDate;
private Date returnDate;//Date that the user should return item
Statement stmt;
public RentControl(Date current) throws SQLException{
this.currentDate = current;
returnDate = new Utility().addDays(current, 3);
}
public void connect() throws ClassNotFoundException, SQLException{
new Utility().connect();
stmt = new Utility().con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
public Rental rent(MemberAccount member, Employee employee, Item copy, int charge, Date rentalDate,
Date returnDate) throws SQLException{
//Notify the system that one item copy is no longer in stock and the no of copies in stock is one less
//add Item to Member's list of items he/she is currently in possesion of.
Rental r = new Rental(0, copy, employee, member, charge, rentalDate, returnDate);
String newRentalInformation = null;
try {
connect();
newRentalInformation = "('"+Integer.toString(r.getRentalId())+"', '"+ r.getEmployee().toString()+ "', '"+r.getMember().toString()
+"', '"+ Double.toString(r.getCharge())+ "', '" + r.getRentalDate().toString() + "', '" + r.getReturnDate().toString() + ")";
int rs = stmt.executeUpdate("INSERT INTO rental ('rentalID', 'copy', 'Employee', 'member', 'charge', 'rentalDate',"
+ " 'returnDate')"+ " VALUES" + newRentalInformation, Statement.RETURN_GENERATED_KEYS);
ResultSet results = stmt.getGeneratedKeys();
if ( results.next() ) {
// Retrieve the auto generated key(s).
r.setRentalId( results.getInt(1));
}
results = stmt.executeQuery("SELECT currentItems FROM members WHERE id = '"+ Integer.toString(r.getMember().getMemberID())+"'");
while(results.next()){
System.out.println(results.getString("currentItems"));
results.updateString("currentItems", results.getString("curremtItems")+ ", "+ r.getItem().getTitle());
results.updateRow();
}
ResultSet gameQuery = stmt.executeQuery("SELECT noOfCopies FROM games WHERE id = "+Integer.toString(r.getItem().getProductID())
+" AND title = "+r.getItem().getTitle());
ResultSet videoQuery = stmt.executeQuery("SELECT noOfCopies FROM games WHERE id = "+Integer.toString(r.getItem().getProductID())
+" AND title = "+r.getItem().getTitle());
while(gameQuery.next()){
System.out.println(gameQuery.getInt("noOfCopies"));
gameQuery.updateInt("noOfCopies", gameQuery.getInt("noOfCopies") - 1);
gameQuery.updateRow();
}
while(videoQuery.next()){
System.out.println(videoQuery.getInt("noOfCopies"));
videoQuery.updateInt("noOfCopies", videoQuery.getInt("noOfCopies") - 1);
videoQuery.updateRow();
}
new Utility().con.close();
} //end try
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
returnDate = new Utility().addDays(currentDate, 3);
r.setReturnDate(returnDate);
System.out.println(r.toString());
return r;
}
}