Advertisement
Guest User

Application

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