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

Choose a reason for hiding this comment

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

  • Форматирование кода - в Java принят немного другой стиль форматирования кода. Если не вдаваться в детали, то легко и быстро отформатировать код в Android Studio можно следующей комбинацией клавиш: в Windows Ctrl + Alt + L , в MacOs ⌘ + ⌥ + L.

static String formatRub(double sum) {
int a = (int) sum;
if (a > 100) {
a %= 100;
}
if (a > 20) {
a %= 10;
}
switch (a) {
case 1:
return " рубль";
case 2:
case 3:
case 4:
return " рубля";
default:
return " рублей";
}
}
}
61 changes: 60 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
double sum = 0;
int quantity = 0;

while (true) {
Scanner scanner = new Scanner(System.in);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Не очень хорошо, что объект сканера создается каждый раз при итерации запроса, лучше создать один( как было до этого, и его использовать

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Не знаю, возможно ли обойтись вообще одним сканером. Но у меня все ломается при таких попытках

System.out.println("На скольких человек необходимо разделить счёт?");

if (scanner.hasNextInt()) {
quantity = scanner.nextInt();
if (quantity < 2) {
System.out.println("Неверное значение");
} else {
break;
}
} else {
System.out.println("Неверное значение");
}
}

ArrayList<Product> productsList = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Введите название товара");
String name = sc.nextLine();
if (name.trim().isEmpty()) {
System.out.println("Что-нибудь еще, кроме пустоты?");
} else {
if (name.equalsIgnoreCase("Завершить")) {

for (int i = 0; i < productsList.size(); i++) {
Product element = productsList.get(i);
System.out.println(element.name + " " + element.price);
sum += element.price;
}
String rub = Format.formatRub(sum / quantity);
System.out.printf("Итого с человека: %.2f" + rub, sum / quantity);
break;

} else {
while (true) {
Scanner sc1 = new Scanner(System.in);
System.out.println("Введите цену");
if (sc1.hasNextDouble()) {
double price = sc1.nextDouble();
Product newProduct = new Product(name, price);
productsList.add(newProduct);
System.out.println("Добавленные товары: ");
for (Product k : productsList) {
System.out.println(k.name);
}
break;
} else {
System.out.println("Неверное значение цены");
}
}
}
}
}
}
}
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 {
String name;
double price;

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