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

консольное приложение #1

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
alk8 wants to merge 8 commits into Yandex-Practicum:master
base: master
Choose a base branch
Loading
from alk8:master
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .idea/.gitignore
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions .idea/codeStyles/Project.xml
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/gradle.xml
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions settings.gradle
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ dependencyResolutionManagement {
}
}
rootProject.name = "Java-Module-Project"
include ':JDK'
112 changes: 112 additions & 0 deletions src/main/java/Counter.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Scanner;


public class Counter {

private final int countPerson;
private final HashMap<String, Double> goods = new HashMap<>();

Counter(int countPerson) {
this.countPerson = countPerson;
}

public void askUser() {

Scanner sc = new Scanner(System.in);

System.out.println("Введите товар");

// Получение товара
String product = sc.next();

if (endSession(product)) {
System.out.println("Сессия закончена. Спасибо!");
return;
}

System.out.println("Введите стоимость");

// Стоимость
String cost = addCost(sc);

if (endSession(cost)) {
System.out.println("Сессия закончена. Спасибо!");
return;
}

// добавляем продукт
goods.put(product, Double.parseDouble(cost));

System.out.println("Добавлено успешно!");

// Снова спрашиваем про товар (Рекурсия)
askUser();

}

private String addCost(Scanner sc) {

String cost = sc.next();

if (!cost.matches("\\d+.\\d{2}")) {

System.out.println("Значение не соответсвует маске XX.XX Введите стоимость заново");
// (Рекурсия)
cost = addCost(sc);
}

return cost;
}

private boolean endSession(String str) {

if (str.equalsIgnoreCase("ЗАВЕРШИТЬ")) {

DecimalFormat decimalFormat = new DecimalFormat("#.##");
// вывести результаты
System.out.println("Добавленные товары:");

goods.forEach((key, value) ->
System.out.println("*** " + key + " | Стоимость " + value + " " + GetRubleAddition(value)));

double total = goods.values().stream().mapToDouble(Double::doubleValue).sum();

System.out.println("Всего потрачено: " + decimalFormat.format(total) + " " + GetRubleAddition(total));

double fromOnePerson = total / countPerson;

System.out.println("С человека - " + decimalFormat.format(fromOnePerson) +
" " + GetRubleAddition(fromOnePerson));

return true;

}

return false;

}

private String GetRubleAddition(Double value) {

int num = value.intValue();

int preLastDigit = num % 100 / 10;
if (preLastDigit == 1) {
return "рублей";
}

switch (num % 10) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}

}
47 changes: 44 additions & 3 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");

int countPerson = 0;

System.out.println("Добрый день!");
Scanner sc = new Scanner(System.in);

while (true){

System.out.println("Сколько вас?");

try {

countPerson = sc.nextInt();

if (countPerson == 1) {

System.out.println("Для одного человека в приложении нет необходимости");

} else if (countPerson < 1) {

System.out.println("Ошибочное значение. Повторите ввод");

} else {

break;

}

} catch (Exception e){

System.out.println("Введено неправильное значение повторите ввод");

}

}

// Пользователь ввел корректное количество. Приступаем к расчетам
Counter counter = new Counter(countPerson);
counter.askUser();

}


}

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