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
6 changes: 0 additions & 6 deletions src/main/java/Main.java

This file was deleted.

85 changes: 85 additions & 0 deletions src/main/java/org/example/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.example;

import java.util.ArrayList;
import java.util.Scanner;

public class Calculator {

float totalCost = 0.00f;
int numOfP;

ArrayList<Product> pList = new ArrayList<>();

static Scanner scan = new Scanner(System.in);

Calculator() {
System.out.println("Укажите количество человек,на которое нужно разделить счёт (целое число больше 1)");
this.numOfP = 0;
while (numOfP == 0) {
if (scan.hasNextInt()) {
numOfP = scan.nextInt();
if (numOfP == 1) {
System.out.println("Нет смысла ничего считать и делить");
numOfP = 0;
} else if (numOfP < 1) {
System.out.println("Некорректное значение для подсчета");
numOfP = 0;
}
} else {
scan.next();
System.out.println("Ошибка. Введено неверное значение");
}
}
}



static Product getProd() {
System.out.println("Укажите название товара");
String name = scan.next();
System.out.println("Введите стоимость товара в формате 'рубли,копейки'");
float cost;
while (true) {
if (scan.hasNextFloat()) {
cost = scan.nextFloat();
if (cost <= 0) {
System.out.println("Значение некорректно");
} else {
break;
}
} else {
System.out.println("Данные введены некорректно");
scan.next();
}
}
return new Product(name, cost);
}

void makeList() {
while (true) {
System.out.println("Давайте добавим новый товар");
Product newProd = Calculator.getProd();
pList.add(newProd);
totalCost += newProd.cost;
System.out.println("Товар успешно добавлен в список!\nДобавили все товары? Напишите 'Завершить'\nХотите добавить новый товар? Введите любой символ");
String cont;
cont = scan.next();
if ("завершить".equalsIgnoreCase(cont.trim())) {
break;
}
}
}

public void printTotal() {
System.out.println("Добавленные товары:");
for (Product one : pList) {
System.out.println(one.name);
}
FormatOut form = new FormatOut();
float ans = (float) totalCost / numOfP;
String ansCost = form.formatNum(ans);
String ansRub = form.formatRub(ans);
System.out.println("Каждый человек должен заплатить " + ansCost + " " + ansRub);
}
}

21 changes: 21 additions & 0 deletions src/main/java/org/example/FormatOut.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.example;

public class FormatOut {

static String formatNum(float n) {
return String.format("%.2f", n);
}

static String formatRub(float n) {
int fn = (int) n;
int fnn = fn % 10;
if (((fn >= 11)&&(fn <=14))||((fnn >= 5)||(fnn == 0))) {
return "рублей";
} else if (fnn == 1) {
return "рубль";
} else {
return "рубля";
}
}

}
13 changes: 13 additions & 0 deletions src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.example;

public class Main {
public static void main(String[] args) {
System.out.println("Здравствуйте! Я ваш калькулятор на этот вечер и я разделю ваш счёт.");
Calculator calculator = new Calculator();
calculator.makeList();
calculator.printTotal();
System.out.println("Спасибо за обращение. Буду ждать вас снова.");
}

}

12 changes: 12 additions & 0 deletions src/main/java/org/example/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.example;

public class Product {

String name;
float cost;

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