123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package me.blackphreak.Student;
- import me.blackphreak.Database;
- import me.blackphreak.Debug.Debug;
- import java.util.HashMap;
- public abstract class AbstractStudent {
-
- private String _studentID;
- private String _chtName; // chinese-traditional
- private String _engName; // english
- private String _homeAddress;
- private String _mobileNumber;
- private HashMap</*subject*/String, /*grade*/String> _grades = new HashMap<>();
- private int _semester; // current semester
- private String _nationality;
-
- public void assign(String stuID,
- String chtName,
- String engName,
- String homeAddress,
- String mobileNumber,
- String nationality,
- int semester)
- {
- this._studentID = stuID;
- this._chtName = chtName;
- this._engName = engName;
- this._homeAddress = homeAddress;
- this._mobileNumber = mobileNumber;
- this._nationality = nationality;
- this._semester = semester;
- }
-
- public void printInfo() {
- System.out.println(String.format("Student ID [%s]", getStudentID()));
- System.out.println(String.format(" %13s: %s", "Chinese Name", getChtName()));
- System.out.println(String.format(" %13s: %s", "English Name", getEngName()));
- System.out.println(String.format(" %13s: %s", "Home Address", getHomeAddress()));
- System.out.println(String.format(" %13s: %s", "Mobile Number", getMobileNumber()));
- System.out.println(String.format(" %13s: %s", "Nationality", getNationality()));
- System.out.println(String.format(" %13s: %d", "Semester", getSemester()));
-
- printInfoExtra();
- }
-
- /**
- * allocate a student ID from database and assign to the student directly
- * @return assign success / fail
- */
- public boolean allocateStudentID() {
- // get last studentID and then +1
- String id = null;
- try {
- id = (Database.getInstance()
- .query("SELECT MAX(StudentID) FROM Student;") // query last sID
- .getInt(1) + 1) // read & increment
- + ""; // toString
- } catch (Exception e) {
- e.printStackTrace();
- }
- return updateStudentID(id);
- }
-
- /**
- * update the studentID and check for duplicate
- * @param newStudentID a new studentID that want to assign with
- * @return false when duplicated
- */
- public boolean updateStudentID(String newStudentID) {
- // check for duplicate
- int numOfRowsFound;
- try {
- numOfRowsFound = Database.getInstance().query("SELECT COUNT(*) FROM Student WHERE studentID = \"" + newStudentID + "\";").getInt(1);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
-
- if (numOfRowsFound > 0) {
- // if duplicated
- Debug.err("Failed to update studentID ["+getStudentID()+"] to ["+newStudentID+"]");
- return false;
- } else {
- // no duplicate
- this._studentID = newStudentID;
- }
- return true;
- }
-
- public abstract String toJson();
-
- public abstract void promptQuestionEx();
-
- public abstract void printInfoExtra();
-
- // --- getter/setter ---
- public String getStudentID() {
- return _studentID;
- }
-
- public String getChtName() {
- return _chtName;
- }
-
- public void setChtName(String _chtName) {
- this._chtName = _chtName;
- }
-
- public String getEngName() {
- return _engName;
- }
-
- public void setEngName(String _engName) {
- this._engName = _engName;
- }
-
- public String getHomeAddress() {
- return _homeAddress;
- }
-
- public void setHomeAddress(String _homeAddress) {
- this._homeAddress = _homeAddress;
- }
-
- public String getMobileNumber() {
- return _mobileNumber;
- }
-
- public void setMobileNumber(String _mobileNumber) {
- this._mobileNumber = _mobileNumber;
- }
-
- public HashMap<String, String> getGrades() {
- return _grades;
- }
-
- public void setGrades(HashMap<String, String> _grades) {
- this._grades = _grades;
- }
-
- public int getSemester() {
- return _semester;
- }
-
- public void setSemester(int _semester) {
- this._semester = _semester;
- }
-
- public String getNationality() {
- return _nationality;
- }
-
- public void setNationality(String nationality) {
- this._nationality = nationality;
- }
-
- }
|