/
BorisovaValentina
/
Hash
Обзор
Документация
Войти
/
BorisovaValentina
/
Hash
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Main.java
46 строк
2 KB
BorisovaValentina
«Коллекция HashMap и HashSet»
15 авг 2025, 23:51
15 авг 2025, 23:51
270c63d
Код
Авторство
О чём код?
package Delivery; import java.util.*; public class Main { public static void main(String[] args) { Map<Address, Integer> costPerAddress = new HashMap<>(); costPerAddress.put(new Address("Россия", "Москва"), 200); costPerAddress.put(new Address("Италия", "Рим"), 1000); costPerAddress.put(new Address("Китай", "Пекин"), 1200); costPerAddress.put(new Address("США", "Вашингтон"), 1500); costPerAddress.put(new Address("Россия", "СПБ"), 200); Scanner scanner = new Scanner(System.in); int total = 0; Set<String> countries = new HashSet<>(); while (true) { System.out.println("Заполнение нового заказа."); System.out.print("Введите страну: "); String country = scanner.nextLine(); if ("end".equals(country)) { break; } System.out.print("Введите город: "); String city = scanner.nextLine(); System.out.print("Введите вес (кг): "); int weight = Integer.parseInt(scanner.nextLine()); Address address = new Address(country, city); if (costPerAddress.containsKey(address)) { int price = weight * costPerAddress.get(address); total += price; countries.add(country); System.out.printf("Стоимость доставки составит: %d руб.\n", price); System.out.printf("Общая стоимость всех доставок: %d руб.\n", total); System.out.printf("Количество стран: %d.\n", countries.size()); } else { System.out.println("Доставки по этому адресу нет"); } } } }