/
nff
/
lab_1
Обзор
Документация
Войти
/
nff
/
lab_1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/com/example/converter/ConverterController.java
92 строки
3 KB
nff
Первый коммит
01 мар 2026, 18:56
01 мар 2026, 18:56
39794b6
Код
Авторство
О чём код?
package com.example.converter; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.*; import java.net.URL; import java.util.ResourceBundle; public class ConverterController implements Initializable { @FXML private TextField amountField; @FXML private ComboBox<String> fromCombo; @FXML private ComboBox<String> toCombo; @FXML private Label resultLabel; @FXML private ListView<String> historyList; private ConverterModel model; private ObservableList<String> historyObservable; @Override public void initialize(URL location, ResourceBundle resources) { model = new ConverterModel(); ObservableList<String> units = FXCollections.observableArrayList( "мин", "ч", "дн", "нед", "работа_ч", "работа_дн" ); fromCombo.setItems(units); toCombo.setItems(units); fromCombo.setValue("ч"); toCombo.setValue("мин"); historyObservable = FXCollections.observableArrayList(); historyList.setItems(historyObservable); } @FXML public void handleConvert() { String amountText = amountField.getText(); if (amountText == null || amountText.trim().isEmpty()) { showAlert("Введите число"); return; } double amount; try { amount = Double.parseDouble(amountText.trim()); } catch (NumberFormatException e) { showAlert("Некорректный ввод."); return; } String from = fromCombo.getValue(); String to = toCombo.getValue(); if (from == null || to == null) { showAlert("Выберите единицы измерения"); return; } try { double result = model.convert(amount, from, to); String resultText = String.format("%.2f %s = %.2f %s", amount, from, result, to); resultLabel.setVisible(true); resultLabel.setText(resultText); model.addHistory(resultText); historyObservable.setAll(model.getHistory()); } catch (IllegalArgumentException e) { showAlert(e.getMessage()); } } private void showAlert(String message) { Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Ошибка"); alert.setHeaderText(null); alert.setContentText(message); alert.showAndWait(); } }