Advertisement
Guest User

Untitled

a guest
Jan 15th, 2018
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. package app;
  2.  
  3. import app.api.UserService;
  4. import app.dao.AccountDao;
  5. import app.model.Account;
  6. import app.model.User;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9. import org.springframework.stereotype.Component;
  10.  
  11. import java.util.Scanner;
  12.  
  13. @Component
  14. public class Application {
  15.  
  16. @Autowired
  17. UserService userService;
  18. //private AccountDao accountDao;
  19.  
  20. private Scanner stdin = new Scanner(System.in);
  21. String menu = "1";
  22.  
  23. public static void main(String[] args) {
  24. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  25. context.registerShutdownHook();
  26. Application app = context.getBean("application", Application.class);
  27. app.run();
  28. }
  29.  
  30. public void run() {
  31. // Account a1 = new Account();
  32. //a1.setUsername("username");
  33. //a1.setAddress("adress");
  34. //a1.setEmail("user@pwp.pl");
  35. //accountDao.save(a1);
  36.  
  37. //for (Account a:accountDao.findAll()) {
  38. // System.out.println(a.getUsername() + " " + a.getAddress());
  39. //}
  40. while(!menu.equals("9")){
  41. System.out.println("------------------");
  42. System.out.println("Menu");
  43. System.out.println("1. Add new User");
  44. System.out.println("2. Verify password");
  45. System.out.println("3. Delete");
  46. System.out.println("9. Exit");
  47. System.out.println("------------------");
  48. menu = stdin.nextLine().trim();
  49. switch (menu){
  50. case "1": {
  51. add(); break;
  52. }
  53. case "2": {
  54. verify(); break;
  55. }
  56. case "3": {
  57. delete(); break;
  58. }
  59. case "9": {
  60. exit(); break;
  61. }
  62. }
  63. }
  64. }
  65.  
  66. private void exit(){
  67. System.out.println("Bye");
  68. }
  69.  
  70. private void add(){
  71. System.out.println("------------------");
  72. System.out.println("Create new User");
  73. System.out.print("Username: ");
  74. String username = stdin.nextLine().trim();
  75. System.out.println("Password: ");
  76. String password = stdin.nextLine().trim();
  77. System.out.println("Email: ");
  78. String email = stdin.nextLine().trim();
  79. User user = new User(username, password, username, email) ;
  80. try {
  81. userService.addUser(user);
  82. System.out.println(" User is added ");
  83. } catch (UserService.IncorrectEmailException e){ //without @
  84. System.out.println(" incorrect email");
  85. } catch (UserService.IllegalUsernameException e1){ //illegal
  86. System.out.println("illegal username");
  87. }
  88.  
  89. System.out.println("------------------");
  90. }
  91.  
  92. private void verify(){
  93. System.out.println("------------------");
  94. System.out.println("Verify password");
  95. System.out.print("Username: ");
  96. String username = stdin.nextLine().trim();
  97. System.out.println("Password: ");
  98. String password = stdin.nextLine().trim();
  99. if(userService.verifyUser(username, password)){
  100. System.out.println("Password is correct");
  101. }else{
  102. System.out.println("Invalid password or username");
  103. }
  104. }
  105.  
  106. private void delete(){
  107. System.out.println("------------------");
  108. System.out.println("Verify password");
  109. System.out.print("Username: ");
  110. String username = stdin.nextLine().trim();
  111. System.out.println("Password: ");
  112. String password = stdin.nextLine().trim();
  113. if(userService.verifyUser(username, password)){
  114. userService.removeUser(username);
  115. System.out.println("User has been removed correctly");
  116. }else{
  117. System.out.println("Cannot delete user");
  118. }
  119. }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement