Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dev calc 2 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Kvazarakht wants to merge 2 commits into dev
base: dev
Choose a base branch
Loading
from dev_Calc_2
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 103 additions & 1 deletion src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,108 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
int persons = check();

Calculate oneCalc = new Calculate();
oneCalc.addProduct();
oneCalc.showSoppingList();
oneCalc.showCalculation(persons);
}

private static int check() {
String regex = "[0-9]+";
int num;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("На скольких человек необходимо разделить счёт ?");
String count = scanner.nextLine();
if (count.matches(regex)) {
num = Integer.parseInt(count);
if (num > 1) break;
}
System.out.println("Введено некорректное значение для подсчёта.");
}
return num;
}
}

class Calculate {
HashMap<String, Double> products = new HashMap<>();

void addProduct() {
String regex = "[0-9]+\\.[0-9]{2}";
String regexZero = "0+\\.0+";
Scanner scanner = new Scanner(System.in);
boolean choice = true;
while (choice) {
System.out.println("Введите название товара :");
String key = scanner.nextLine();
if (key.equalsIgnoreCase("Завершить") && products.isEmpty()) {
break;
}
if (key.equalsIgnoreCase("Завершить") && !products.isEmpty()) break;
double volume = 0.0;
String price;
while (true) {
System.out.println("Введите стоимость товара :");
price = scanner.nextLine();

if (products.isEmpty() && price.equalsIgnoreCase("Завершить")) {

choice = false;
break;
}
if (price.equalsIgnoreCase("Завершить")) {
System.out.println("!!!!!!!");
choice = false;
break;
}

if (price.matches(regex)) {
volume = Double.parseDouble(price);
break;
} else if (price.matches(regex) && price.matches(regexZero)) {
System.out.println("Цена не может равняться нулю");
break;
} else System.out.println("Указана неверная цена");
if (!choice) break;

}
if (!key.equals("") && !price.equals("") && volume != 0.0) products.put(key, volume);
if (!price.equalsIgnoreCase("завершить")) {
System.out.println("Товар добавлен в расчет");
System.out.println("Хотите добавить ещё один товар ?");
}
if (price.equalsIgnoreCase("завершить") || scanner.nextLine().equalsIgnoreCase("завершить"))
break;
}
}

void showSoppingList() {
int count = 1;
System.out.println("Добавленные товары:");
for (Map.Entry<String, Double> product : products.entrySet()) {
System.out.println(count + ") товар: " + product.getKey() + "; цена: " + product.getValue());
count++;
}
if (products.size() == 0) System.out.println("отсутствуют");
}

void showCalculation(int persons) {
double sum = 0;
for (Map.Entry<String, Double> product : products.entrySet()) {
sum += product.getValue();
}
double moneyOnePerson = sum / persons;
String rubChar = String.valueOf(((int) moneyOnePerson));
String rubl = switch (rubChar.charAt(rubChar.length() - 1)) {
case '1' -> "рубль";
case '2', '3', '4' -> "рубля";
default -> "рублей";
};
System.out.println("Каждый человек должен заплатить: " + String.format("%.2f", moneyOnePerson) + " " + rubl);
}
}

AltStyle によって変換されたページ (->オリジナル) /