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
68 changes: 68 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Calculator {
public int count;
private static final Scanner scanner = new Scanner(System.in);
public Calculator(int count) {
this.count = count;
}
static ArrayList<Product> products = new ArrayList<>();
float sum =0;
void add() {

while (true) {
if(products.isEmpty()){
System.out.println("Введите название товара ");
String name = scanner.next();
System.out.println("Введите цену товара: ");
while (!scanner.hasNextFloat()) {
System.out.println("Некорректный ввод для цены. Введите цену товара:");
scanner.next();
}
float price = scanner.nextFloat();
if (price < 0) {
System.out.println("Стоимость товара не может быть отрицательной. Повторите ввод товара.");
continue;
}
sum =sum+price;
Product product = new Product(name, price);
products.add(product);
System.out.println("Товар добавлен!");
}else {
System.out.println("Добавьте еще товар или закончите добавление, написав - Завершить :");
String name = scanner.next();
if(!name.equalsIgnoreCase("завершить")) {
System.out.println("Введите цену товара: ");
while (!scanner.hasNextFloat() ) {
System.out.println("Некорректный ввод для цены. Введите цену товара:");
scanner.next();
}
float price = scanner.nextFloat();
if (price < 0) {
System.out.println("Стоимость товара не может быть отрицательной. Повторите ввод товара.");
continue;
}
sum =sum+price;
Product product = new Product(name, price);
products.add(product);
System.out.println("Товар добавлен!");
}else {
print();
break;
}
}}
}
void print(){

System.out.println("Добавленные товары:");
for (Product product : Calculator.products) {
System.out.println(product.name +" - " +Formatter.twoPoints(product.price)+ " " + Formatter.ending(product.price));

}
System.out.println("Сумма всех товаров - "+ Formatter.twoPoints(sum) +" " + Formatter.ending(sum));
System.out.println("Каждый человек должен заплатить - "+ Formatter.twoPoints(sum/count) +" " + Formatter.ending(sum/count));
}

}

19 changes: 19 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Formatter {

static String twoPoints(float calc){

return String.format("%.2f",calc);
}

static String ending(float calc){

int calc1= (int) Math.floor(calc)%10;
return switch (calc1) {
case 2, 3, 4 -> "рубля";
case 5, 6, 7, 8, 9, 0 -> "рублей";
default -> "рубль";
};


}
}
40 changes: 39 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@

import java.util.Scanner;

public class Main {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Hello world!");

System.out.println("На скольких человек необходимо разделить счёт?");
while (!scanner.hasNextInt()) {
System.out.println("Некорректный ввод количества человек. Повторите ввод:");
scanner.next();
}
int countPersons = scanner.nextInt();

while(true){
if(countPersons<1){
System.out.println("Некорректное значение");
System.out.println("На скольких человек необходимо разделить счёт?");
while (!scanner.hasNextInt()) {
System.out.println("Некорректный ввод количества человек. Повторите ввод:");
scanner.next();
}
countPersons = scanner.nextInt();
}else if(countPersons==1){
System.out.println("Нет смысла считать!");
System.out.println("На скольких человек необходимо разделить счёт?");
while (!scanner.hasNextInt()) {
System.out.println("Некорректный ввод количества человек. Повторите ввод:");
scanner.next();
}
countPersons = scanner.nextInt();
}else {

break;
}
}
Calculator calculator = new Calculator(countPersons);
calculator.add();




}
}
9 changes: 9 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Product {
public String name;
public float price;

public Product(String name, float price) {
this.name = name;
this.price = price;
}
}