📄 reservation.java
字号:
package web;
import lms.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class Reservation extends DataBase
{
private String UserID;
private String BookIndex;
private String ISBN;
private Date today;
private String ReservationDay;
public Reservation()
{
super();
this.UserID="";
this.BookIndex="";
this.ISBN="";
this.ReservationDay ="";
this.today =new Date();
}
public void setUserID(String UserID)
{
this.UserID=UserID;
}
public String getUserID()
{
return this.UserID;
}
/*Get ISBN by BookIndex*/
public String getISBN(String BookIndex)
{
String ISBN="";
String sql = "select ISBN from Book where BookIndex='"+BookIndex+"'";
try
{
//System.out.println(sql);
rs = stm.executeQuery(sql);
if(rs.absolute(1))
{
ISBN=rs.getString("ISBN");
}
//System.out.println("TEST---"+ISBN);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
return ISBN;
}
public void setISBN(String ISBN)
{
this.ISBN = ISBN;
}
/*用户是否已经预约过书, 返回true说明已预约过收*/
public boolean isBeingUser(String UserID)
{
boolean success = true;
String sql = "select count(*) sum from Reservation where UserID='"+UserID+"'";
try
{
//System.out.println(sql);
rs = stm.executeQuery(sql);
//System.out.println("A");
if(rs.absolute(1))
{
if (Integer.parseInt(rs.getString("sum")) == 0)
{
success = false;
}
else
{
success = true;
}
}
//System.out.println("B");
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
success = true;
}
//System.out.println(success);
return success;
}
/*书是否全被预约 返回true说明书已全被预约*/
public boolean allReservated(String ISBN)
{
boolean success = true;
String sql = "select * from BookInf where Amount>ReservationAmount and ISBN='"+ISBN+"'";
try
{
//System.out.println(sql);
rs = stm.executeQuery(sql);
if (rs.first())
{
success = false;
//System.out.println("a");
}
else
{
success = true;
//System.out.println("b");
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
success = true;
}
//System.out.println(success);
return success;
}
/*用户自己正在借阅此书。。不能预约。。返回true正在借中。。*/
public boolean borrowing(String UserID,String BookIndex)
{
boolean success = true;
String sql = "select * from BorrowRecord where UserID='"+UserID+"' and BookIndex='"+BookIndex+"' and event=0";
try
{
rs = stm.executeQuery(sql);
if(rs.first())
{
success = false;
//System.out.println("a");
}
else
{
success = true;
//System.out.println("b");
}
}
catch(Exception ex)
{
//System.out.println(ex.getMessage());
success = true;
}
//System.out.println(success);
return success;
}
public boolean updateBookInf(String ISBN)
{
boolean success = false;
String sql = "update BookInf set ReservationAmount=ReservationAmount+1 where ISBN='"+ISBN+"'";
try
{
//System.out.println(sql);
int i = stm.executeUpdate(sql);
//System.out.println(i);
if (i!=0)
{
success = true;
}
else
{
success = false;
}
}
catch(Exception ex)
{
success = false;
System.out.println(ex.getMessage());
}
return success;
}
public boolean reservation()
{
boolean success = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
this.ReservationDay = sdf.format(today);
String sql ="insert into Reservation values('"
+this.UserID+"','"+this.ISBN+"','"+this.ReservationDay+"',0)";
try
{
//System.out.println(sql);
int i = stm.executeUpdate(sql);
//System.out.println(i);
if(this.updateBookInf(this.ISBN))
{
success = true;
}
else
{
success = false;
}
this.close();
}
catch(Exception ex)
{
success = false;
System.out.println(ex.getMessage());
}
return success;
}
/*test*/
public static void main(String[] args)
{
Reservation r = new Reservation();
r.setUserID("33060422");
r.ISBN = "ISBN1";
r.reservation();
//r.isBeingUser("33060422");
//r.allReservated("ISBN");
//r.borrowing("33060423","Nx21");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -