Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PaymentsApplication {
- private static Map<Long, User> userBase = new HashMap<>();
- static Scanner scan = new Scanner(System.in);
- public static long id = 0;
- @SuppressWarnings("unused")
- public static void main(String[] args) {
- ApplicationContext ctx = SpringApplication.run(PaymentsApplication.class, args);
- User mainBean = (User) ctx.getBean("user");
- try {
- while (true) {
- System.out.println("Нажмите:");
- System.out.println("1 - для создания пользователя");
- System.out.println("2 - для отображения списка пользователей");
- //System.out.println("3 - для остановки работы программы");
- String s = scan.nextLine();
- int ch = Integer.parseInt(s);
- switch (ch) {
- case 1:
- // Создать пользователей, также клиентов и админа
- insertUser();
- break;
- case 2:
- // Получение объекта клиента по ид юзера.
- getUser(id);
- break;
- default:
- return;
- }
- }
- } finally {
- System.out.println("request finished");
- }
- }
- // Метод, который создаст пользователей, клиентов для них и админа
- public static void insertUser() {
- Long id = getLastId();
- String mail;
- String password;
- User user;
- Integer i = 0;
- try {
- while (true) {
- System.out.println("Нажмите:");
- System.out.println("1 - для создания клиента");
- System.out.println("2 - для создания администратора");
- String s = scan.nextLine();
- int ch = Integer.parseInt(s);
- switch (ch) {
- case 1:
- System.out.println("Введите имя клиента:");
- mail = scan.nextLine();
- System.out.println("Введите пароль:");
- password = scan.nextLine();
- user = new User(mail, password);
- user.client = new Client(user);
- userBase.put((long) i, user);
- System.out.println("Введите номер счёта:");
- String accountNumber = scan.nextLine();
- user.client.createAccount(accountNumber);
- Account account = user.client.createAccount(accountNumber);
- System.out.println("Введите номер карты для счёта:");
- String cardNumber = scan.nextLine();
- account.createCard(cardNumber);
- break;
- case 2:
- System.out.println("Введите имя администратора:");
- mail = scan.nextLine();
- System.out.println("Введите пароль:");
- password = scan.nextLine();
- user = new User(mail, password);
- user.client = new Client(user);
- userBase.put((long) i, user);
- break;
- default:
- return;
- }
- id++;
- break;
- }
- id++;
- } finally {
- System.out.println("adding finished");
- }
- }
- private static void getUser(long id) {
- // Набор элементов
- Set<Map.Entry<Long, User>> set = userBase.entrySet();
- // Отобразим набор
- for (Map.Entry<Long, User> me : set) {
- System.out.print(me.getKey() + ": ");
- System.out.println(me.getValue().username);
- }
- }
- // получение наибольшего id из существующих
- // чтобы вставлять новых юзеров с несуществующим id
- public static Long getLastId() {
- Long result = (long) 0;
- for (Entry<Long, User> entry : userBase.entrySet()) {
- Long id = entry.getKey();
- if (id > result) {
- result = id;
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement