Advertisement
Guest User

Client

a guest
Dec 30th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. public class PaymentsApplication {
  2.  
  3.     private static Map<Long, User> userBase = new HashMap<>();
  4.     static Scanner scan = new Scanner(System.in);
  5.     public static long id = 0;
  6.  
  7.     @SuppressWarnings("unused")
  8.     public static void main(String[] args) {
  9.         ApplicationContext ctx = SpringApplication.run(PaymentsApplication.class, args);
  10.         User mainBean = (User) ctx.getBean("user");
  11.  
  12.         try {
  13.             while (true) {
  14.                 System.out.println("Нажмите:");
  15.                 System.out.println("1 - для создания пользователя");
  16.                 System.out.println("2 - для отображения списка пользователей");
  17.                 //System.out.println("3 - для остановки работы программы");
  18.  
  19.                 String s = scan.nextLine();
  20.                 int ch = Integer.parseInt(s);
  21.  
  22.                 switch (ch) {
  23.                 case 1:
  24.  
  25.                     // Создать пользователей, также клиентов и админа
  26.                     insertUser();
  27.                     break;
  28.                 case 2:
  29.  
  30.                     // Получение объекта клиента по ид юзера.
  31.                     getUser(id);
  32.                     break;
  33.                 default:
  34.                     return;
  35.                 }
  36.             }
  37.         } finally {
  38.             System.out.println("request finished");
  39.         }
  40.     }
  41.  
  42.     // Метод, который создаст пользователей, клиентов для них и админа
  43.     public static void insertUser() {
  44.  
  45.         Long id = getLastId();
  46.         String mail;
  47.         String password;
  48.         User user;
  49.         Integer i = 0;
  50.  
  51.         try {
  52.             while (true) {
  53.                 System.out.println("Нажмите:");
  54.                 System.out.println("1 - для создания клиента");
  55.                 System.out.println("2 - для создания администратора");
  56.  
  57.                 String s = scan.nextLine();
  58.                 int ch = Integer.parseInt(s);
  59.  
  60.                 switch (ch) {
  61.                 case 1:
  62.  
  63.                     System.out.println("Введите имя клиента:");
  64.                     mail = scan.nextLine();
  65.                     System.out.println("Введите пароль:");
  66.                     password = scan.nextLine();
  67.  
  68.                     user = new User(mail, password);
  69.                     user.client = new Client(user);
  70.                     userBase.put((long) i, user);
  71.                     System.out.println("Введите номер счёта:");
  72.                     String accountNumber = scan.nextLine();
  73.  
  74.                     user.client.createAccount(accountNumber);
  75.  
  76.                     Account account = user.client.createAccount(accountNumber);
  77.  
  78.                     System.out.println("Введите номер карты для счёта:");
  79.                     String cardNumber = scan.nextLine();
  80.                     account.createCard(cardNumber);
  81.                    
  82.                     break;
  83.                 case 2:
  84.                     System.out.println("Введите имя администратора:");
  85.                     mail = scan.nextLine();
  86.                     System.out.println("Введите пароль:");
  87.                     password = scan.nextLine();
  88.  
  89.                     user = new User(mail, password);
  90.                     user.client = new Client(user);
  91.                     userBase.put((long) i, user);
  92.                     break;
  93.                 default:
  94.                     return;
  95.                 }
  96.                 id++;
  97.                 break;
  98.             }
  99.             id++;
  100.         } finally {
  101.             System.out.println("adding finished");
  102.         }
  103.        
  104.     }
  105.  
  106.     private static void getUser(long id) {
  107.  
  108.         // Набор элементов
  109.         Set<Map.Entry<Long, User>> set = userBase.entrySet();
  110.  
  111.         // Отобразим набор
  112.         for (Map.Entry<Long, User> me : set) {
  113.             System.out.print(me.getKey() + ": ");
  114.             System.out.println(me.getValue().username);
  115.         }
  116.     }
  117.  
  118.     // получение наибольшего id из существующих
  119.     // чтобы вставлять новых юзеров с несуществующим id
  120.     public static Long getLastId() {
  121.         Long result = (long) 0;
  122.         for (Entry<Long, User> entry : userBase.entrySet()) {
  123.             Long id = entry.getKey();
  124.             if (id > result) {
  125.                 result = id;
  126.             }
  127.         }
  128.         return result;
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement