Kalkulyator ilovasi - Sharof rashidov nomidagi samarqand davlat universiteti intellektual tizimlar va kompyuter...

Kalkulyator ilovasi
Theme.java
package com.houarizegai.calculator.theme.properties;
public class Theme {
private String name;
private String applicationBackground;
private String textColor;
private String btnEqualTextColor;
private String operatorBackground;
private String numbersBackground;
private String btnEqualBackground;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getApplicationBackground() {
return applicationBackground;
}
public void setApplicationBackground(String applicationBackground) {
this.applicationBackground = applicationBackground;
}
public String getTextColor() {
return textColor;
}
public void setTextColor(String textColor) {
this.textColor = textColor;
}
public String getBtnEqualTextColor() {
return btnEqualTextColor;
}
public void setBtnEqualTextColor(String btnEqualTextColor) {
this.btnEqualTextColor = btnEqualTextColor;
}
public String getOperatorBackground() {
return operatorBackground;
}
public void setOperatorBackground(String operatorBackground) {
this.operatorBackground = operatorBackground;
}
public String getNumbersBackground() {
return numbersBackground;
}
public void setNumbersBackground(String numbersBackground) {
this.numbersBackground = numbersBackground;
}
public String getBtnEqualBackground() {
return btnEqualBackground;
}
public void setBtnEqualBackground(String btnEqualBackground) {
this.btnEqualBackground = btnEqualBackground;
}
}
ThemeList.java
package com.houarizegai.calculator.theme.properties;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ThemeList {
private List<Theme> themes;
public List<Theme> getThemes() {
return themes;
}
public void setThemes(List<Theme> themes) {
this.themes = themes;
}
public Map<String, Theme> getThemesAsMap() {
return themes.stream().collect(Collectors.toMap(Theme::getName, Function.identity()));
}
}
CalculatorUITest.java
package com.houarizegai.calculator;
import com.houarizegai.calculator.ui.CalculatorUI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CalculatorUITest {
private CalculatorUI calculatorUI;
@BeforeEach
void setUp() {
calculatorUI = new CalculatorUI();
}
@ParameterizedTest
@CsvSource({"3,5,+,8", "2,8,-,-6", "44.5,10,*,445", "320,5,/,64", "3,5,%,3", "5,3,^,125"})
void testCalculation(double firstNumber, double secondNumber, char operator, double expectedResult) {
assertEquals(expectedResult, calculatorUI.calculate(firstNumber, secondNumber, operator));
}
}
Ranglarni o’rnatib oldik
themes:
- name: Light
applicationBackground: f0f3f9
textColor: 000000
operatorBackground: f7f9fc
numbersBackground: ffffff
btnEqualTextColor: ffffff
btnEqualBackground: 0067c0
- name: Dark
applicationBackground: 1c2028
textColor: ffffff
operatorBackground: 2d333d
numbersBackground: 363c47
btnEqualTextColor: ffffff
btnEqualBackground: 4cc2ff
Kalkulyator kodi
package com.houarizegai.calculator.ui;
import com.houarizegai.calculator.theme.properties.Theme;
import com.houarizegai.calculator.theme.ThemeLoader;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.util.Map;
import java.util.regex.Pattern;
import java.awt.Color;
import javax.swing.*;
import static com.houarizegai.calculator.util.ColorUtil.hex2Color;
public class CalculatorUI {
private static final String FONT_NAME = "Comic Sans MS";
private static final String DOUBLE_OR_NUMBER_REGEX = "([-]?\\d+[.]\\d*)|(\\d+)|(-\\d+)";
private static final String APPLICATION_TITLE = "Calculator";
private static final int WINDOW_WIDTH = 410;
private static final int WINDOW_HEIGHT = 600;
private static final int BUTTON_WIDTH = 80;
private static final int BUTTON_HEIGHT = 70;
private static final int MARGIN_X = 20;
private static final int MARGIN_Y = 60;
private final JFrame window;
private JComboBox<String> comboCalculatorType;
private JComboBox<String> comboTheme;
private JTextField inputScreen;
private JButton btnC;
private JButton btnBack;
private JButton btnMod;
private JButton btnDiv;
private JButton btnMul;
private JButton btnSub;
private JButton btnAdd;
private JButton btn0;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btn7;
private JButton btn8;
private JButton btn9;
private JButton btnPoint;
private JButton btnEqual;
private JButton btnRoot;
private JButton btnPower;
private JButton btnLog;
private char selectedOperator = ' ';
private boolean go = true; // For calculate with Opt != (=)
private boolean addToDisplay = true; // Connect numbers in display
private double typedValue = 0;
private final Map<String, Theme> themesMap;
public CalculatorUI() {
themesMap = ThemeLoader.loadThemes();
window = new JFrame(APPLICATION_TITLE);
window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
window.setLocationRelativeTo(null);
|