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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
# Задача, выполненная в рамках Яндекс.Практикум (2 спринт)
58 changes: 58 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Calculator {
private double sum;
private int guests;
private ArrayList<Good> goods;

public Calculator() {
guests = getGuests();
goods = new ArrayList<Good>();
}

private int getGuests() {
Scanner scanner = new Scanner(System.in);
int input = 0;
while (true) {
System.out.print("На сколько человек делим счёт?:");
if (scanner.hasNextInt()) {
input = scanner.nextInt();
if (input > 1) {
return input;
} else System.out.println("Значение должно быть больше 1");
} else System.out.println("Значение некорректно!!!");
String buf = scanner.nextLine();
}
}

public void addGoods() {
Scanner scanner = new Scanner(System.in);

System.out.print("Введите наименование товара:");
String name = scanner.nextLine();
double price = -1.00;
while (true) {
System.out.print("Введите цену:");
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (Double.compare(price, 0.00) >= 0) break;
}
System.out.println("Значение некорректно!!!");
String buf = scanner.nextLine();
}

goods.add(new Good(name, price));
sum += price;
}

public void print() {
System.out.println("Добавленные товары:");
for (Good good : goods) {
good.print();
}
System.out.println(String.format("ИТОГО: %.2f %s", sum, Format.rublFormat(sum)));
System.out.println("Всего человек : " + guests);
System.out.println(String.format("На каждого: %.2f %s", sum / guests, Format.rublFormat(sum / guests)));
}
}
12 changes: 12 additions & 0 deletions src/main/java/Format.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Format {
public static String rublFormat(double sum) {
String rubles;
int mainPart = (int) sum;
if (mainPart >= 100) mainPart %= 100;
if (mainPart >= 20) mainPart %= 10;
if (mainPart == 1) rubles = "рубль";
else if (mainPart < 5 && mainPart > 0) rubles = "рубля";
else rubles = "рублей";
return rubles;
}
}
20 changes: 20 additions & 0 deletions src/main/java/Good.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Good {
private String name;
private double price;

public void print() {
String rubles = Format.rublFormat(price);
System.out.println(String.format("%s %.2f %s", name, price, rubles));
}

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

public double getPrice() {
return price;
}


}
14 changes: 13 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");

Calculator calculator = new Calculator();
while (true) {
Scanner scanner = new Scanner(System.in);
calculator.addGoods();
System.out.print("Продолжить ввод товара? \n" +
"Если нет введите \"Завершить\"");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("завершить")) break;
}
;
calculator.print();
}
}