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

public class Calculator {
private final List<Product>list = new ArrayList<>();
private int person = 0;
Calculator(int person){
this.person = person;
}

public List<Product> getList() {
return list;
}
public void add(Product product){
list.add(product);
}
public double sum(){
double sum = 0.0;
for(Product p : list){
sum += p.getPrice();
}
return ((double)Math.round(sum * 100)) / 100;
}
public double calcPastPay(){
if(this.person > 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

По идее эта проверка всегда будет true, так как в программе еще до этого проверяется, что количество больше нуля. Но в целом имеет место быть)

return ((double)Math.round(sum() / this.person * 100)) / 100;
else
return 0.0;
}
public void printCheck(){
for(Product p: getList()){
System.out.println(p.toString());
}
}
public String rubRightWrite(double sum){
int dec = (((int) sum) / 10) % 10;
if(dec == 1){
return "рублей";
}else {
int delta = ((int) sum) % 10;
switch (delta) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
case 0:
case 5:
case 6:
case 7:
case 8:
case 9:
return "рублей";
}
}
return "руб.";
}
}
74 changes: 73 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,78 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println("На сколько персон необходимо поделить счет: ");
Scanner scanner = new Scanner(System.in);
int countPerson = 0;

Calculator calc = null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Необязательно писать инициализаторы явно. По умолчанию double имет значение 0, строка и объект null


countPerson = scanner.hasNextInt() ? scanner.nextInt() : 0;
if (countPerson <= 0){
System.out.println("Количество человек не может быть отрицательным или равным нулю! Либо введено слишком большое число!");
}else if(countPerson == 1){
System.out.println("Нет смысла в работе программы при количестве персон равном 0");
}else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Все, что в этом else, можно было бы вынести в отдельный метод, чтобы немного разгрузить main, а также чтобы не было сильной вложенности кода. (сначала в if, потом в while, это создает лишние отступы)

System.out.println("Вы планируете разделить счета на "+countPerson+" чел.");
calc = new Calculator(countPerson);
buildCheck(calc);

System.out.println("Итоговый чек: ");
calc.printCheck();
System.out.println("К оплате итого: "+calc.sum()+" "+calc.rubRightWrite(calc.sum()));
System.out.println("В том числе с каждого члена команды "+calc.calcPastPay()+" "+calc.rubRightWrite(calc.calcPastPay()));
}

}
public static void buildCheck(Calculator calc){
String next = "" ;
double price ;
Scanner scanner = new Scanner(System.in);
while(!next.equals("завершить")){
System.out.println("Введите наименование товара: ");
System.out.print("-> ");
String tmp = scanner.next();

boolean check = false;
String strPrice = "";
while (!check) {
System.out.println("Стоимость:");
System.out.print("-> ");
strPrice = scanner.next();
check = verifyDouble(strPrice);
if(!check){
System.out.println("Не правильный ввод. Поробуйте еще.");
}
}
price = Double.parseDouble(strPrice);
calc.add(new Product(tmp,price));
System.out.println("Добавить еще товар? Да: любой символ + Enter, Закончить ввод: Завершить + Enter");
next = (scanner.next().toLowerCase());
}
}


public static boolean verifyDouble(String str){
boolean rez = false;
char[] chArr = str.toCharArray();
int countPoint = 0;
for(char i: chArr){
if(Character.isDigit(i)){
rez = true;
}else if(i == '.'){
countPoint++;
}else{
rez = false;
return rez;
}
}
if(countPoint > 1){
rez = false;
}
return rez;
}

}
17 changes: 17 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Product {
private String name = "";
private double price = 0.0;
Product(String name, double price){
this.name = name;
this.price = price;
}

public double getPrice() {
return price;
}

@Override
public String toString() {
return "Добавленный товар: " + name + ", Цена: " + price ;
}
}