/
digit
/
java-oop-collections-hash
Обзор
Документация
Войти
/
digit
/
java-oop-collections-hash
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Main.java
79 строк
3 KB
digit
Create: Main.java, Address.java, Update: README.md
18 июл 2026, 13:25
Верифицирован
18 июл 2026, 13:25
6a412d4
Код
Авторство
О чём код?
import java.util.Map; import java.util.HashMap; import java.util.Set; import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { Map<Address, Integer> costPerAddress = getAddresses(); Set<String> uniqueCountries = new HashSet<>(); int totalDeliveryCost = 0; Scanner scanner = new Scanner(System.in); boolean isRunning = true; while (isRunning) { System.out.println("Заполнение нового заказа."); String userCountry; System.out.print("Введите страну: "); userCountry = scanner.nextLine(); String userCity; System.out.print("Введите город: "); userCity = scanner.nextLine(); String userWeight; // String! System.out.print("Введите вес (кг): "); userWeight = scanner.nextLine(); isRunning = isNotEnd(userCountry, userCity, userWeight); // ("end" || "end" || "end") -> false if (!isRunning) { // false -> выход из цикла break; } Address userAddress = new Address(userCountry, userCity); totalDeliveryCost += increaseTotal(userWeight, costPerAddress, userAddress); // Integer.parseInt(userWeight) if (costPerAddress.containsKey(userAddress)) { uniqueCountries.add(userCountry); } printDeliveryOverview(costPerAddress, userAddress, totalDeliveryCost, uniqueCountries); } } public static Map<Address, Integer> getAddresses() { Map<Address, Integer> costPerAddress = new HashMap<>(); Address address1 = new Address("Россия", "Москва"); Address address2 = new Address("Китай", "Пекин"); Address address3 = new Address("США", "Вашингтон"); Address address4 = new Address("Россия", "Санкт-Петербург"); costPerAddress.put(address1, 1000); costPerAddress.put(address2, 2000); costPerAddress.put(address3, 3000); costPerAddress.put(address4, 1500); return costPerAddress; } public static boolean isNotEnd(String country, String city, String weight) { return !(country.equals("end") || city.equals("end") || weight.equals("end")); } public static void printDeliveryOverview(Map<Address, Integer> costPerAddress, Address userAddress, int totalDeliveryCost, Set<String> uniqueCountries) { if (costPerAddress.containsKey(userAddress)) { System.out.printf("Стоимость доставки составит: %d руб.\n", costPerAddress.get(userAddress)); System.out.printf("Общая стоимость всех доставок: %d руб.\n", totalDeliveryCost); System.out.printf("Уникальных стран для доставки: %d\n\n", uniqueCountries.size()); } else { System.out.print("Доставки по этому адресу нет\n\n"); } } public static int increaseTotal(String userWeight, Map<Address, Integer> costPerAddress, Address userAddress) { return Integer.parseInt(userWeight) * costPerAddress.get(userAddress); } }