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 _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 getGrades() { return _grades; } public void setGrades(HashMap _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; } }