/
ValentinaE
/
HashMap
Обзор
Документация
Войти
/
ValentinaE
/
HashMap
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Main.java
74 строки
3 KB
ValentinaE
upload files
25 фев 2025, 15:46
25 фев 2025, 15:46
ee0d615
Код
Авторство
О чём код?
import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.*; public class Main { public static void main(String[] args) { Address address1 = new Address("Россия", "Санкт-Петербург"); Address address2 = new Address("Китай", "Пекин"); Address address3 = new Address("Россия", "Барнаул"); Map<Address, Integer> costPerAddress = new HashMap<>(); costPerAddress.put(address1, 700); costPerAddress.put(address2, 7000); costPerAddress.put(address3, 1000); for (Map.Entry<Address, Integer> entry : costPerAddress.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue() + " за kg"); } Scanner scanner = new Scanner(System.in); int totalCost = 0; Set<String> uniqueCountries = new HashSet<>(); while (true) { System.out.println("Введите новый заказ "); System.out.println("Ведите страну : "); System.out.println("Введите город : "); System.out.println("ВВедите Вес(кг) : "); System.out.println("end для выхода"); String input = scanner.nextLine(); if (input.equalsIgnoreCase("end")) { break; } String[] parts = input.split(" "); if (parts.length != 3) { System.out.println("Неверный ввод. Попробуйте еще раз"); continue; } String country = parts[0]; String city = parts[1]; double weight; try { weight = Double.parseDouble(parts[2]); } catch (NumberFormatException e) { System.out.println("Неверный вес."); continue; } Address address = new Address(country, city); Integer costPerKg = costPerAddress.get(address); if (costPerKg == null) { System.out.println("Этот адрес не обслуживается."); } else { double orderCost = costPerKg * weight; totalCost += orderCost; uniqueCountries.add(country); System.out.printf("Cтоимость доставки : %.2f%n", orderCost); System.out.printf("Общая стоимость всех поставок : %.2f%n", (double)totalCost); System.out.println("Количество уникальных стран : " + uniqueCountries.size()); } } scanner.close(); } }