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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions src/main/java/Calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

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

public class Calculate {
static Const aConst=new Const();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

static лучше убрать у всех полей и методов класса, необходимости в них нет

static Scanner scanner = new Scanner(System.in);
static ArrayList<String> product = new ArrayList<>();
static ArrayList<Double> price = new ArrayList<>();
static double score = 0;

public static void calculate() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

static можно убрать, поскольку ты вызываешь метод от создаваемого объекта класса Calculate

int count;
while (true){
while (true)//проверка ввода целого числа
{
System.out.println("На сколько человек разделить заказ?");
System.out.println("Введите целое больше 1");
if (scanner.hasNextInt())
{
count = Integer.parseInt(scanner.nextLine());
System.out.println("Вы ввели: " + count);
break;
}
else
{
System.out.println("Вы ввели не целое число");
scanner.nextLine();
}}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можешь применить в студии автоформатирование (в выбранном файле, сверху вкладка Code - Reformat Code, либо Ctrl+Alt+L), тогда автоматически код выправится.

if (count<=1) {
System.out.println("Оглянись! Ты тут один наел на 15 тысяч");
} else {
//блок заполнения листа и подсчета счета
System.out.println(aConst.ADD_PRODUCT_AND_PRICE);
String nameProduct = scanner.next();//название продукта
Double priceProduct = scanner.nextDouble();//цена

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Пользователь может ввести на вопрос про стоимость товаров вместо числа строку. Для количества человек ты обрабатываешь такую ситуацию через hasNextInt, стоит добавить обработку и для стоимости

addProduct(nameProduct);
addPrice(priceProduct);
System.out.println(aConst.ADD_MESSAGE_PRODUCT);
while (true) {
System.out.println(aConst.QUESTION_MESSAGE);
String question = scanner.next();
if (question.equalsIgnoreCase("Да")) {
System.out.println(aConst.ADD_PRODUCT_AND_PRICE);
String addNameProduct = scanner.next();
double addPriceProduct = scanner.nextDouble();
addProduct(addNameProduct);
addPrice(addPriceProduct);
System.out.println(aConst.ADD_MESSAGE_PRODUCT);
} else if (question.equalsIgnoreCase("Завершить")) {
printProductAndPrice();
score = score / count;
printResult(score);
break;
}
}
break;
}
}


}

private static void addProduct(String addProduct) {
product.add(addProduct);
}
private static void addPrice(Double addPrice) {
price.add(addPrice);
}

//ввывод товаров и стоимость каждого продукта
private static void printProductAndPrice() {
System.out.println("Добавленные товары:");
for (double price : price) {
score = score + Math.abs(price);
}
for (int i=0;i<product.size();i++){
System.out.println("Продукт: "+ product.get(i)+ " стоимость: "+ Math.abs(price.get(i)));
}
}

//метод для вывода окончания рубля
private static void printResult(double score1) {
if ( Math.floor(score1) % 100 >= 11 && Math.floor(score1) % 100 <= 19) {
System.out.printf("Каждый должен заплатить: %.2f Рублей%n",score1);
} else {
switch ((int) (Math.floor(score1) % 10)) {
case 1:
System.out.printf("Каждый должен заплатить: %.2f Рубль%n",score1);
break;
case 2:
case 3:
case 4:
System.out.printf("Каждый должен заплатить: %.2f Рубля%n",score1);
break;
default:
System.out.printf("Каждый должен заплатить: %.2f Рублей%n",score1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Код System.out.printf("Каждый должен заплатить: %.2f <...> %n",score1); повторяется, лучше его вынести за if-else, оставив только определение слова (можно записывать в переменную)

break;
}
}
}
}

5 changes: 5 additions & 0 deletions src/main/java/Const.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Const{
final String ADD_MESSAGE_PRODUCT="Товар успешно добавлен";
final String QUESTION_MESSAGE="Хотите ли вы добавить еще товар, выберите напишите 'Да' или напишите 'Завершить' для выхода";
final String ADD_PRODUCT_AND_PRICE="Укажите название товара и его стоимость";
}
30 changes: 26 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import java.util.Scanner;

public class Main {
static Calculate calculate=new Calculate();

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

calculate.calculate();
/*Scanner in = new Scanner(System.in);
int ab;
while (true)
{
System.out.println("Введите число");
if (in.hasNextInt())
{
ab = Integer.parseInt(in.nextLine());
System.out.println("Вы ввели: " + ab);
break;
}
else
{
System.out.println("Вы ввели не число");
in.nextLine();
}}*/
}

}
}