Guest User

Untitled

a guest
Nov 8th, 2017
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. package educationcenter;
  2.  
  3. import educationcenter.command.Command;
  4. import educationcenter.model.Lesson;
  5. import educationcenter.model.Student;
  6. import educationcenter.model.User;
  7. import educationcenter.storage.LessonDatabase;
  8. import educationcenter.storage.StudentDatabase;
  9. import educationcenter.storage.UserDatabase;
  10.  
  11. import java.util.Date;
  12. import java.util.Random;
  13. import java.util.Scanner;
  14.  
  15. public class EducationCenter implements Command {
  16.  
  17. static Scanner scanner = new Scanner(System.in);
  18. static StudentDatabase studentDatabase = new StudentDatabase();
  19. static LessonDatabase lessonDatabase = new LessonDatabase();
  20. static UserDatabase userDatabase = new UserDatabase();
  21. static Random random = new Random();
  22. static User currentUser = null;
  23.  
  24. public static void main(String[] args) {
  25. Student student = new Student("Karen", "Hovhannisyan", "04443", "mail.ru", "Manager", new Date());
  26. studentDatabase.addStudent(student);
  27. Student student1 = new Student("Artash", "Haroyan", "077698962", "art.aro@mail.ru", "Manager", new Date());
  28. studentDatabase.addStudent(student1);
  29. User user = new User(student.getName(), student.getSurname(), "login", "password", student);
  30. User user1 = new User(student1.getName(), student1.getSurname(), "milena", "vilena", student1);
  31. userDatabase.addUser(user);
  32. userDatabase.addUser(user1);
  33.  
  34. boolean isRun = true;
  35. while (isRun) {
  36. System.out.println("Please input: " + EXIT + " for exit");
  37. System.out.println("Please input: " + LOGIN + " for login");
  38. int answer = Integer.parseInt(scanner.nextLine());
  39. switch (answer) {
  40. case EXIT:
  41. isRun = false;
  42. break;
  43. case LOGIN:
  44. System.out.println("Please input login and password");
  45. String loginAndPassword = scanner.nextLine();
  46. String[] data = loginAndPassword.split(",");
  47. currentUser = userDatabase.getUserByLoginAndPassword(data[0], data[1]);
  48. if (currentUser == null) {
  49. System.out.println("Invalid login or password");
  50. } else {
  51. if (currentUser.getStudent().getType().equalsIgnoreCase("manager")) {
  52. managerPage();
  53. } else {
  54. studentPage();
  55. }
  56. }
  57.  
  58. }
  59. }
  60.  
  61.  
  62. }
  63.  
  64. private static void studentPage() {
  65. boolean isRun = true;
  66. while (isRun) {
  67. System.out.println("input " + EXIT + "for exit");
  68. System.out.println("Input 1 for print your lessons");
  69. System.out.println("Input 2 for print all lessons");
  70. int answer = Integer.parseInt(scanner.nextLine());
  71. switch (answer) {
  72. case EXIT:
  73. isRun = false;
  74. break;
  75. case 1:
  76. printYourlesson();
  77. break;
  78. case 2:
  79. lessonDatabase.printLessons();
  80. break;
  81. }
  82. }
  83. }
  84.  
  85.  
  86. private static void printYourlesson() {
  87. for (int i = 0; i < currentUser.getStudent().getLessons().length; i++) {
  88. System.out.println(currentUser.getStudent().getLessons()[i]);
  89. }
  90. }
  91.  
  92. private static String[] getLoginAndPassword(String[] strings) {
  93. String[] loginAndPassword = new String[2];
  94. boolean isRun = true;
  95. while (isRun) {
  96. loginAndPassword[0] = strings[0] + (1 + random.nextInt(999));
  97. if (userDatabase.isLogin(loginAndPassword[0])) {
  98. loginAndPassword[1] = strings[1] + (1 + random.nextInt(999));
  99. isRun = false;
  100. }
  101.  
  102. }
  103. return loginAndPassword;
  104. }
  105.  
  106. private static void managerPage() {
  107. boolean isRun = true;
  108. while (isRun) {
  109. printCommand();
  110. int answer = Integer.parseInt(scanner.nextLine());
  111. switch (answer) {
  112. case EXIT:
  113. isRun = false;
  114. break;
  115. case ADD_STUDENT:
  116. addedStudent();
  117. break;
  118. case ADD_LESSON:
  119. addedLesson();
  120. break;
  121. case PRINT_STUDENTS:
  122. studentDatabase.printStudents();
  123. break;
  124. case PRINT_LESSONS:
  125. lessonDatabase.printLessons();
  126. break;
  127. case CHANGE_STUDENT_BY_LESSON:
  128. changeStudentByLesson();
  129. break;
  130. case PRINT_STUDENTS_BY_LESSON_NAME:
  131. System.out.println("Please input lesson name");
  132. String lessonName = scanner.nextLine();
  133. studentDatabase.PrintStudentsByLessonName(lessonName);
  134. break;
  135. }
  136. }
  137. }
  138.  
  139.  
  140. private static void changeStudentByLesson() {
  141. System.out.println("OUR STUDENTS");
  142. studentDatabase.printStudents();
  143. System.out.println("Please input stundent name");
  144. String name = scanner.nextLine();
  145. Student student = studentDatabase.getStudentByStudentName(name);
  146. if (student == null) {
  147. System.out.println("Invalid student name");
  148. } else {
  149. lessonDatabase.printLessons();
  150. System.out.println("Input lessons name");
  151. String lessonsName = scanner.nextLine();
  152. String[] lessonData = lessonsName.split(",");
  153. Lesson[] lessons = new Lesson[lessonData.length];
  154. for (int i = 0; i < lessonData.length; i++) {
  155. lessons[i] = lessonDatabase.getLessonByLessonName(lessonData[i]);
  156. }
  157. student.setLessons(lessons);
  158. }
  159. }
  160.  
  161. private static void printCommand() {
  162. System.out.println("Welcome to the Education Center");
  163. System.out.println("Input " + EXIT + " to Exit");
  164. System.out.println("Input " + ADD_STUDENT + " to add student");
  165. System.out.println("Input " + ADD_LESSON + " to add lesson");
  166. System.out.println("Input " + PRINT_STUDENTS + " to print students");
  167. System.out.println("Input " + PRINT_LESSONS + " to print lessons");
  168. System.out.println("Input " + CHANGE_STUDENT_BY_LESSON + " to change student by lesson");
  169. System.out.println("Input " + PRINT_STUDENTS_BY_LESSON_NAME + " to print students by lesson name");
  170. }
  171.  
  172. private static void addedStudent() {
  173. System.out.println("Please Input student name,username,phone,email,type");
  174. String studentData = scanner.nextLine();
  175. String[] studentInfo = studentData.split(",");
  176. lessonDatabase.printLessons();
  177. System.out.println("Please input Lesson name");
  178. String studentlessons = scanner.nextLine();
  179. String[] lessons = studentlessons.split(",");
  180. Lesson[] lessons1 = new Lesson[lessons.length];
  181. for (int i = 0; i < lessons.length; i++) {
  182. lessons1[i] = lessonDatabase.getLessonByLessonName(lessons[i]);
  183. }
  184.  
  185. Student student = new Student(studentInfo[0], studentInfo[1], studentInfo[2],
  186. studentInfo[3], studentInfo[4], new Date(), lessons1);
  187. studentDatabase.addStudent(student);
  188. System.out.println("if you want open accaunt in Education Center");
  189. String answer = scanner.nextLine();
  190. if (answer.equalsIgnoreCase("Yes")) {
  191. String[] studentdata = {student.getName(), student.getSurname()};
  192. String[] loginAndPassword = getLoginAndPassword(studentdata);
  193. User user = new User(student.getName(), student.getSurname(),
  194. loginAndPassword[0], loginAndPassword[1], student);
  195. System.out.println("Your Login is: " + loginAndPassword[0]);
  196. System.out.println("Your Password is: " + loginAndPassword[1]);
  197. userDatabase.addUser(user);
  198. } else {
  199. if (answer.equalsIgnoreCase("No")) {
  200. System.out.println("Thank You,You successfully Added ");
  201. }
  202.  
  203. }
  204. }
  205.  
  206. private static void addedLesson() {
  207. System.out.println("");
  208. System.out.println("Please input lesson name,duration,price,lecture name");
  209. String studentLesson = scanner.nextLine();
  210. String[] lessonInfo = studentLesson.split(",");
  211. int duration = Integer.parseInt(lessonInfo[1]);
  212. double price = Double.parseDouble(lessonInfo[2]);
  213. Lesson lesson = new Lesson(lessonInfo[0], duration, price, lessonInfo[3]);
  214. lessonDatabase.addLesson(lesson);
  215. }
  216. }
Add Comment
Please, Sign In to add comment