📄 member.java
字号:
/* * Member.java * */ package library;import java.util.*;import java.sql.*;/** * This bean represents a member. * Class <code>Member</code> inherits * from class <code>SystemUser</code>. * * @author dms * @version 1.0 */public class Member extends SystemUser { public static int MAXIMUM_ALLOWED_CHECKOUT = 2; private String driverLicState; private String driverLicNumber; /** * Class constructor */ public Member() {super();} /** * Class constructor which retrieves specified member * @param ssn int containing ssn of the member to retrieve */ public Member(int ssn) { super(); getMember(ssn); } protected void setVariables(ResultSet r) throws SQLException { super.setVariables(r); driverLicState = r.getString("driverlicstate"); driverLicNumber = r.getString("driverlicnum"); } /** * Retrieves the member specified by the ssn * @param ssn int containing ssn of the member to retrieve * @return <b>boolean</b> - true if success, false if otherwise */ public boolean getMember(int ssn) { ensureconnection(); try { String sql = "SELECT * FROM Member WHERE ssn = " + ssn; ResultSet r = db.runQuery(sql); if (r.next()) { setVariables(r); return true; } else return false; } catch (SQLException e) { System.err.println("Exception occurred when trying to retrive member:\n" + e.toString()); return false; } } /** * To validate a member's id and password use this method * with the member's ssn and password. * @param ssn int containing member's ssn * @param pass String containing password * @return boolean, true if validated, false if otherwise */ public boolean validate(int ssn, String pass) { ensureconnection(); try { String sql = "SELECT * FROM Member WHERE ssn = " + ssn + " AND passwd = '" + pass + "'"; ResultSet r = db.runQuery(sql); if (r.next()) { setVariables(r); return true; } else return false; } catch (SQLException se) { System.err.println(se.toString()); return false; } } /** * Determines the number of books this member has * checked out. * @return int containing number of books checked out */ public int getCheckedOutBookCount() { ensureconnection(); String sql = ""; sql = "SELECT Count(*) as counted FROM Book WHERE borrowerssn = " + this.getSSN(); try { ResultSet r = db.runQuery(sql); r.next(); return r.getInt("counted"); } catch (SQLException e) { System.err.println(e.toString()); return 0; } } /** * To iterate through all of the checked out books * for a given member, call this method, then use * method <code>getNext</code> on the resulting * <code>Book</code> object. * * @return Book containing the first checked out book */ public Book getCheckedOutBooks() throws SQLException { String sql = "SELECT * FROM Book WHERE borrowerssn = " + this.getSSN(); return new Book(sql); } /** * Determines the number of books this member has * held. * @return int containing number of books held out */ public int getHoldCount() { ensureconnection(); String sql = "SELECT Count(*) as counted FROM Hold WHERE ssn = " + this.getSSN(); try { ResultSet r = db.runQuery(sql); r.next(); return r.getInt("counted"); } catch (SQLException e) { System.err.println(e.toString()); return 0; } } /** * * To iterate through all of the holds for * a given member, call this method, then use * method <code>getNext</code> on the resulting * <code>Hold</code> object. * * @return Hold containing the first Hold for this member. */ public Hold getHolds() throws SQLException { String sql = "SELECT * FROM Hold WHERE ssn = " + this.getSSN(); return new Hold(sql); } /** * * To iterate through all of the holds for * a given member, call this method, then use * method <code>getNext</code> on the resulting * <code>Favorite</code> object. * * @return Favorite containing the first Hold for this member. */ public Favorite getFavorites() throws SQLException { String sql = "SELECT * FROM FavoriteTitles WHERE customerid = " + this.getSSN(); return new Favorite(sql); } /** * This method is a test for equality between this member and another. * @param m Member to test equality on * @return boolean, true if members are equal, false if otherwise */ public boolean equals(Member m) { if (this.getSSN() == m.getSSN()) return true; else return false; } /** * Places a hold on a title. * @param cn callnumber of title * @return boolean true if success, false otherwise */ public boolean placeHold(String cn) throws SQLException { // Make sure hold doesn't already exist for this title. Hold hold = this.getHolds(); boolean held = false; while (hold.getNext()) { if (hold.getCallNumber().equals(cn)) held = true; } // Make sure this member doesn't already have a // copy of this checked out. Book book = this.getCheckedOutBooks(); boolean checkedOut = false; while (book.getNext()) { if (book.getCallNumber().equals(cn)) checkedOut = true; } if (held || checkedOut) return false; else { db.executeQuery("INSERT INTO Hold VALUES (" + this.getSSN() + ", '" + cn + "', CURRENT_TIMESTAMP)"); return true; } } /** * Add a title to member's favorites list. * @param cn callnumber of title * @return boolean true if success, false otherwise */ public boolean addFav(String cn) throws SQLException { // Make sure hold doesn't already exist for this title. Favorite fav = this.getFavorites(); while (fav.getNext()) { if (fav.getCallNumber().equals(cn)) { return false; } } db.executeQuery("INSERT INTO FavoriteTitles VALUES ('" + cn + "', " + this.getSSN() + ")"); return true; } /** * Accessor method for driver license number * @return String containing Driver License Number */ public String getDriverLicNumber() {return driverLicNumber;} /** * Accessor method for driver license state * @return String containing Driver License state */ public String getDriverLicState() {return driverLicState;} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -