/
matveyrazjivin
/
java_tasks
Обзор
Документация
Войти
/
matveyrazjivin
/
java_tasks
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
task2/src/main/java/org/example/UserInterface.java
50 строк
2 KB
matveyrazjivin
Консольная версия конвертера валют.
30 сен 2025, 05:12
30 сен 2025, 05:12
a647bae
Код
Авторство
О чём код?
package org.example; import java.util.Scanner; public class UserInterface { private final Converter converter; private final Scanner scanner; public UserInterface(Converter converter) { this.converter = converter; this.scanner = new Scanner(System.in); } public void run() { while (true) { System.out.print("Введите сумму для конвертации ('help' для просмотра доступных валют или 'exit' для выхода): "); String input = scanner.nextLine().trim(); if ("exit".equalsIgnoreCase(input)) { System.out.println("Выход из программы."); break; } else if ("help".equalsIgnoreCase(input)) { System.out.println("Доступные валюты: RUB (Рубль), USD (Доллар), EUR (Евро), BYN (Беллорусский рубль), CNY (Китайский юань)"); continue; } try { double amount = Double.parseDouble(input); System.out.print("В какой валюте вы указали сумму (RUB, USD, EUR, BYN, CNY)? "); String fromCurrency = scanner.nextLine().trim().toUpperCase(); if (!converter.getExchangeRates().containsKey(fromCurrency)) { System.out.println("Попробуйте еще раз."); continue; } System.out.println("По текущему курсу вы получите:"); for (String toCurrency : converter.getExchangeRates().keySet()) { if (!toCurrency.equals(fromCurrency)) { double result = converter.convert(amount, fromCurrency, toCurrency); System.out.printf("%.2f %s = %.2f %s%n", amount, fromCurrency, result, toCurrency); } } } catch (NumberFormatException e) { System.out.println("Введите корректное число."); } } } }