Skip to content
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ repositories {

dependencies {
}

47 changes: 47 additions & 0 deletions src/main/java/GoodCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
public class GoodCalculator {
String goodName;
String goodSumName = "Добавленные товары:";
double goodPrice;
double goodSumPrice;

GoodCalculator() {
String goodName = "";
double goodPrice;
double goodSumPrice = 0.0;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Конструктор не используется. Его можно убрать.


void addGood(String goodName, double goodPrice) {
this.goodSumName = goodSumName + "\n" + goodName + ": " + goodPrice + " " + rubleEnding(goodPrice);
this.goodSumPrice += goodPrice;
}

String rubleEnding(double rubleValue) {
int rubleValueInt = (int) (Math.floor(rubleValue));
String result;

if ((rubleValueInt % 100 > 10) && (rubleValueInt % 100 < 20)) {
result = "рублей";
}
else {
switch (rubleValueInt % 10) {
case 1:
result = "рубль";
break;

case 2:
case 3:
case 4:
result = "рубля";
break;

default:
result = "рублей";
break;
}
}

return result;

}

}
81 changes: 80 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,87 @@
import java.util.Locale;
import java.util.Scanner;

public class Main {

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

GoodCalculator gc = new GoodCalculator();
Scanner scanner = new Scanner(System.in);
scanner.useLocale(Locale.US);

int peopleNumber;
String contunieGoodAdding, resultSumPerPerson;

System.out.println("На скольких человек разделить счет?");

while (true) {
if (scanner.hasNextInt()) {
peopleNumber = scanner.nextInt();
if (peopleNumber > 1) {
break;
}
else {
System.out.println("Введите целое число больше 1:");
scanner.nextLine();
}
}
else {
System.out.println("Введите целое число больше 1:");
scanner.nextLine();
}
}

while (true) {
System.out.println("Введите название товара:");
scanner.nextLine();
gc.goodName = scanner.nextLine();

System.out.println("Введите стоимость товара [копейки.рубли] (например, 40.25):");
while (true) {
if (scanner.hasNextDouble()) {
gc.goodPrice = scanner.nextDouble();
if (gc.goodPrice > 0) {
break;
}
else {
System.out.println("Введите число больше 0:");
scanner.nextLine();
}
}
else {
System.out.println("Введите число больше 0:");
scanner.nextLine();
}
}

gc.addGood(gc.goodName, gc.goodPrice);
System.out.println("\n\nТовар успешно добавлен! Текущая общая сумма: " + gc.goodSumPrice + " " + gc.rubleEnding(gc.goodSumPrice));

System.out.println("Хотите добавить еще товар? Чтобы завершить, введите \'Завершить\'");
contunieGoodAdding = scanner.next();
if (contunieGoodAdding.equalsIgnoreCase("Завершить")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно сделать из этого цикла while цикл do-while и вынести это условие в while()

break;
}
}
resultSumPerPerson = String.format("%.2f" ,gc.goodSumPrice/peopleNumber);
System.out.println("\n\n" + gc.goodSumName + "\n\nОбщая сумма: " + gc.goodSumPrice + " " + gc.rubleEnding(gc.goodSumPrice) + "\nС каждого участника: " + resultSumPerPerson + " " + gc.rubleEnding(gc.goodSumPrice/peopleNumber));












}



}