AbstractStudent.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package me.blackphreak.Student;
  2. import me.blackphreak.Database;
  3. import me.blackphreak.Debug.Debug;
  4. import java.util.HashMap;
  5. public abstract class AbstractStudent {
  6. private String _studentID;
  7. private String _chtName; // chinese-traditional
  8. private String _engName; // english
  9. private String _homeAddress;
  10. private String _mobileNumber;
  11. private HashMap</*subject*/String, /*grade*/String> _grades = new HashMap<>();
  12. private int _semester; // current semester
  13. private String _nationality;
  14. public void assign(String stuID,
  15. String chtName,
  16. String engName,
  17. String homeAddress,
  18. String mobileNumber,
  19. String nationality,
  20. int semester)
  21. {
  22. this._studentID = stuID;
  23. this._chtName = chtName;
  24. this._engName = engName;
  25. this._homeAddress = homeAddress;
  26. this._mobileNumber = mobileNumber;
  27. this._nationality = nationality;
  28. this._semester = semester;
  29. }
  30. public void printInfo() {
  31. System.out.println(String.format("Student ID [%s]", getStudentID()));
  32. System.out.println(String.format(" %13s: %s", "Chinese Name", getChtName()));
  33. System.out.println(String.format(" %13s: %s", "English Name", getEngName()));
  34. System.out.println(String.format(" %13s: %s", "Home Address", getHomeAddress()));
  35. System.out.println(String.format(" %13s: %s", "Mobile Number", getMobileNumber()));
  36. System.out.println(String.format(" %13s: %s", "Nationality", getNationality()));
  37. System.out.println(String.format(" %13s: %d", "Semester", getSemester()));
  38. printInfoExtra();
  39. }
  40. /**
  41. * allocate a student ID from database and assign to the student directly
  42. * @return assign success / fail
  43. */
  44. public boolean allocateStudentID() {
  45. // get last studentID and then +1
  46. String id = null;
  47. try {
  48. id = (Database.getInstance()
  49. .query("SELECT MAX(StudentID) FROM Student;") // query last sID
  50. .getInt(1) + 1) // read & increment
  51. + ""; // toString
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. return updateStudentID(id);
  56. }
  57. /**
  58. * update the studentID and check for duplicate
  59. * @param newStudentID a new studentID that want to assign with
  60. * @return false when duplicated
  61. */
  62. public boolean updateStudentID(String newStudentID) {
  63. // check for duplicate
  64. int numOfRowsFound;
  65. try {
  66. numOfRowsFound = Database.getInstance().query("SELECT COUNT(*) FROM Student WHERE studentID = \"" + newStudentID + "\";").getInt(1);
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. return false;
  70. }
  71. if (numOfRowsFound > 0) {
  72. // if duplicated
  73. Debug.err("Failed to update studentID ["+getStudentID()+"] to ["+newStudentID+"]");
  74. return false;
  75. } else {
  76. // no duplicate
  77. this._studentID = newStudentID;
  78. }
  79. return true;
  80. }
  81. public abstract String toJson();
  82. public abstract void promptQuestionEx();
  83. public abstract void printInfoExtra();
  84. // --- getter/setter ---
  85. public String getStudentID() {
  86. return _studentID;
  87. }
  88. public String getChtName() {
  89. return _chtName;
  90. }
  91. public void setChtName(String _chtName) {
  92. this._chtName = _chtName;
  93. }
  94. public String getEngName() {
  95. return _engName;
  96. }
  97. public void setEngName(String _engName) {
  98. this._engName = _engName;
  99. }
  100. public String getHomeAddress() {
  101. return _homeAddress;
  102. }
  103. public void setHomeAddress(String _homeAddress) {
  104. this._homeAddress = _homeAddress;
  105. }
  106. public String getMobileNumber() {
  107. return _mobileNumber;
  108. }
  109. public void setMobileNumber(String _mobileNumber) {
  110. this._mobileNumber = _mobileNumber;
  111. }
  112. public HashMap<String, String> getGrades() {
  113. return _grades;
  114. }
  115. public void setGrades(HashMap<String, String> _grades) {
  116. this._grades = _grades;
  117. }
  118. public int getSemester() {
  119. return _semester;
  120. }
  121. public void setSemester(int _semester) {
  122. this._semester = _semester;
  123. }
  124. public String getNationality() {
  125. return _nationality;
  126. }
  127. public void setNationality(String nationality) {
  128. this._nationality = nationality;
  129. }
  130. }