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
35 changes: 35 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class Calculator {

double allPriceSum;
String basket;

public void addGoods(String newGoods, double goodsPrice) {
if (this.basket == null) {
this.basket = newGoods;
} else {
this.basket = this.basket + "\n" + ((this.basket.compareToIgnoreCase(newGoods) != 0) ? newGoods : "");
}
this.allPriceSum = this.allPriceSum + goodsPrice;
}

public String correctStringCurrency(int peopleCount) {
double lastValue = (allPriceSum / peopleCount) % 100 / 10;
String retValue;
if ((int)lastValue == 1) {
return "рублей";
} else {
switch ((int) Math.floor((allPriceSum / peopleCount) % 10)) {
case 1:
retValue = "рубль";
break;
case 2, 3, 4:
retValue = "рубля";
break;
default:
retValue = "рублей";
break;
}
}
return retValue;
}
}
53 changes: 52 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
// Объявим все что необходимо для работы
Calculator calculator = new Calculator();
Scanner scan = new Scanner(System.in);
int peopleCount;

while (true) {
System.out.println("На сколько человек необходимо разделить счет?");
try {
peopleCount = Integer.parseInt(scan.next());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

У Scanner есть удобный метод hasNextInt, можно сразу узнать без дополнительных проверок возможно ли интерпретировать строку как int

if (peopleCount < 1) {
System.out.println("Ошибка ввода попробуйте еще раз!");
} else if (peopleCount == 1) {
System.out.println("Счет не может быть разделен на одного человека. Попробуйте еще раз!");
} else {
break;
}
} catch (NumberFormatException ex) {
System.out.println("Ошибка ввода! Возможен только ввод целых чисел.");
}
}

// переменные для записи товара
double price;
String goodsName;
while (true) {
System.out.println("Введите наименование товара: ");
goodsName = scan.next();
while (true) {
System.out.println("Введите стоимость товара: ");
try {
price = Double.parseDouble(scan.next());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Также по аналогии у Scanner есть метод hasNextDouble
https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#hasNextDouble%28%29

if (price <= 0) {
System.out.println("Стоимость товара не может быть отрицательной или меньше 0");
}
break;
} catch (NumberFormatException ex) {
System.out.println("Ошибка ввода! Необходимо ввести стоимость товара в формате рубли.копейки.");
}
}
calculator.addGoods(goodsName, price);
System.out.println("Товар добавлен в корзину!");
System.out.println("Хотите ли вы добавить еще товар?");
if (scan.next().toLowerCase().compareTo("завершить") == 0) {
break;
}
}

System.out.println("Добавленные товары: " + "\n" + calculator.basket);
System.out.println("Стоимость счета на каждого человека составляет: " +
String.format("%.2f", calculator.allPriceSum / peopleCount) + " " + calculator.correctStringCurrency(peopleCount));
scan.close();
}
}