Skip to content
Merged
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
52 changes: 52 additions & 0 deletions src/main/java/Check.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.LinkedList;
import java.util.Scanner;

public class Check {
LinkedList<String> name = new LinkedList<>();
LinkedList<Double> price = new LinkedList<>();
int count;
double sum;

double getSum() {return sum;}

Check(){
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("Введите товар №"+(count+1)+" или \"Завершить\", чтобы завершить ввод:");
String nameInput = scanner.nextLine();
if (nameInput.equalsIgnoreCase("Завершить")) break;
System.out.println("Сколько стоит " + nameInput + "? (формат ввода: \"0,00\"):");
double priceInput;
while (true) {
if (scanner.hasNextDouble()) {
priceInput = scanner.nextDouble();
scanner.nextLine();
break;
}
else {
System.out.println("Некорректный ввод, попробуйте еще раз");
scanner.nextLine();
}
}
name.addLast(nameInput);
price.addLast(priceInput);
count++;
sum += priceInput;
System.out.println("Товар №"+count+" \"" + nameInput + "\" успешно добавлен!\n");
}
scanner.close();
}


void print() {
System.out.println("Добавленные товары:");
System.out.println("-----------------------------------------------");
Formatter rub = new Formatter();
for (int i = 0; i < count; i++) {
System.out.println(String.format("%2d ",(i + 1)) + String.format(" |%-27s|",name.get(i)) + String.format("%7.2f", price.get(i)) + rub.format(price.get(i)));
}
System.out.println("-----------------------------------------------");
System.out.println("ИТОГО:\t\t\t\t\t\t\t|" + String.format("%7.2f", sum) + rub.format(sum));
System.out.println("-----------------------------------------------");
}
}
29 changes: 29 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class Formatter {

String format(double input) {
String rub;
int ending = (int)input % 100;
if(ending>10 && ending<20){
rub=" рублей";
}
else {
switch (ending % 10) {
case 1: {
rub = " рубль";
break;
}
case 2:
case 3:
case 4: {
rub = " рубля";
break;
}
default: {
rub = " рублей";
break;
}
}
}
return rub;
}
}
39 changes: 38 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
import java.util.Scanner;

// dev branch for Y.Practicum
public class Main {

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

// Часть 1 - вводные параметры для счетчика (количество гостей)
int guestCount;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nНа скольких человек необходимо разделить счет?");
while (true) {
if (scanner.hasNextInt()) {
guestCount = scanner.nextInt();
break;
}
else {
scanner.next();
System.out.println("Некорректный ввод, попробуйте еще раз");
}
}
if (guestCount>1) break;
else if (guestCount == 1) {
System.out.println("Если вы один, то вам за все и платить!");
}
else {
System.out.println("Некорректный ввод, попробуйте еще раз");
}
}
// scanner.close();

// Часть 2 - создание чека и добавление товаров (в конструкторе)
Check check = new Check();

// Часть 3 - вывод результатов
check.print();

double checkPerOne = check.getSum()/guestCount;
Formatter rub = new Formatter();
System.out.println("Вас было "+guestCount+", значит с каждого по " + String.format("%.2f", checkPerOne) + rub.format(checkPerOne));
}
}