/
IvanGB
/
java108
Обзор
Документация
Войти
/
IvanGB
/
java108
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
7_java108_HW_HASH/Main.java
86 строк
4 KB
IvanGB
upload files
02 янв 2026, 21:54
02 янв 2026, 21:54
dd1c55a
Код
Авторство
О чём код?
import java.util.*; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Map<Address, Double> costPerAddress = new HashMap<>(); costPerAddress.put(new Address("Россия", "Москва"), 500.0); costPerAddress.put(new Address("Россия", "Санкт-Петербург"), 550.0); costPerAddress.put(new Address("США", "Нью-Йорк"), 1500.0); costPerAddress.put(new Address("США", "Лос-Анджелес"), 1600.0); costPerAddress.put(new Address("Германия", "Берлин"), 1200.0); costPerAddress.put(new Address("Франция", "Париж"), 1300.0); costPerAddress.put(new Address("Япония", "Токио"), 1800.0); costPerAddress.put(new Address("Китай", "Пекин"), 1000.0); double totalCost = 0.0; Set<String> uniqueCountries = new HashSet<>(); System.out.println("=== Сервис доставки товаров ==="); System.out.println("Доступные направления: "); System.out.println(); for (Map.Entry<Address, Double> entry : costPerAddress.entrySet()) { System.out.printf("%s: %.2f руб/кг%n", entry.getKey(), entry.getValue()); } System.out.println(); System.out.println("Введите заказы (страна город вес) или end для завершения"); System.out.println(); while (true) { System.out.print("Введите заказ: "); String input = scanner.nextLine().trim(); if (input.equalsIgnoreCase("end")) { break; } String[] parts = input.split("\\s+", 3); if (parts.length < 3) { System.out.println("Ошибка: нужно ввести страну, город и вес!"); continue; } try { String country = parts[0]; String city = parts[1]; double weigth = Double.parseDouble(parts[2]); if (weigth <= 0) { System.out.println("Ошибка: вес должен быть положительным числом!"); continue; } Address address = new Address(country, city); if (costPerAddress.containsKey(address)) { double pricePerKg = costPerAddress.get(address); double deliveryCost = pricePerKg * weigth; totalCost += deliveryCost; uniqueCountries.add(country); System.out.printf("Стоимость доставки: %.2f руб%n", deliveryCost); System.out.printf("(%.2f руб/кг х %.2f кг)%n", pricePerKg, weigth); } else { System.out.printf("Адрес '%s, %s' не обслуживается! %n", country, city); } } catch (NumberFormatException e) { System.out.println("Ошибка: вес должен быть числом!"); } } scanner.close(); System.out.println("Итоговая статистика!"); System.out.printf("Общая стоимость всех доставок: %.2f руб%n", totalCost); System.out.printf("Количество уникальных стран: %d%n", uniqueCountries.size()); System.out.println("Список стран: " + String.join(", ", uniqueCountries)); } }