Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app;
- import app.api.UserService;
- import app.dao.AccountDao;
- import app.model.Account;
- import app.model.User;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.stereotype.Component;
- import java.util.Scanner;
- @Component
- public class Application {
- @Autowired
- UserService userService;
- //private AccountDao accountDao;
- private Scanner stdin = new Scanner(System.in);
- String menu = "1";
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
- context.registerShutdownHook();
- Application app = context.getBean("application", Application.class);
- app.run();
- }
- public void run() {
- // Account a1 = new Account();
- //a1.setUsername("username");
- //a1.setAddress("adress");
- //a1.setEmail("user@pwp.pl");
- //accountDao.save(a1);
- //for (Account a:accountDao.findAll()) {
- // System.out.println(a.getUsername() + " " + a.getAddress());
- //}
- while(!menu.equals("9")){
- System.out.println("------------------");
- System.out.println("Menu");
- System.out.println("1. Add new User");
- System.out.println("2. Verify password");
- System.out.println("3. Delete");
- System.out.println("9. Exit");
- System.out.println("------------------");
- menu = stdin.nextLine().trim();
- switch (menu){
- case "1": {
- add(); break;
- }
- case "2": {
- verify(); break;
- }
- case "3": {
- delete(); break;
- }
- case "9": {
- exit(); break;
- }
- }
- }
- }
- private void exit(){
- System.out.println("Bye");
- }
- private void add(){
- System.out.println("------------------");
- System.out.println("Create new User");
- System.out.print("Username: ");
- String username = stdin.nextLine().trim();
- System.out.println("Password: ");
- String password = stdin.nextLine().trim();
- System.out.println("Email: ");
- String email = stdin.nextLine().trim();
- User user = new User(username, password, username, email) ;
- try {
- userService.addUser(user);
- System.out.println(" User is added ");
- } catch (UserService.IncorrectEmailException e){ //without @
- System.out.println(" incorrect email");
- } catch (UserService.IllegalUsernameException e1){ //illegal
- System.out.println("illegal username");
- }
- System.out.println("------------------");
- }
- private void verify(){
- System.out.println("------------------");
- System.out.println("Verify password");
- System.out.print("Username: ");
- String username = stdin.nextLine().trim();
- System.out.println("Password: ");
- String password = stdin.nextLine().trim();
- if(userService.verifyUser(username, password)){
- System.out.println("Password is correct");
- }else{
- System.out.println("Invalid password or username");
- }
- }
- private void delete(){
- System.out.println("------------------");
- System.out.println("Verify password");
- System.out.print("Username: ");
- String username = stdin.nextLine().trim();
- System.out.println("Password: ");
- String password = stdin.nextLine().trim();
- if(userService.verifyUser(username, password)){
- userService.removeUser(username);
- System.out.println("User has been removed correctly");
- }else{
- System.out.println("Cannot delete user");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement