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

public class Calculator {
ArrayList<String> name = new ArrayList<>();
ArrayList<Double> price = new ArrayList<>();
int count = 1;
double sum;
double getSum() {return sum;}


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

}
void print() {
System.out.println("Добавленные товары: \n"+"\n"+"\n");
RubFormat rub = new RubFormat();
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))+ "\n"+ "\n");
}
System.out.println("ИТОГО:" + String.format("%7.2f", sum) + rub.format(sum));
}


}
37 changes: 37 additions & 0 deletions src/main/java/Count.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.Scanner;

public class Count {
public static int count() {

int persons;
int number;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("На скольких человек необходимо разделить счет?");
while (true) {
if (sc.hasNextInt()) {
number = sc.nextInt();
break;
}
else {
sc.next();
System.out.println("Некорректный ввод, попробуйте еще раз");
}
}
if (sc.hasNextInt()) {
if (number < 1) {
System.out.println("Некорректный ввод, попробуйте еще раз");
} else if (number == 1) {
System.out.println("Вы один, вам за все и платить!");
} else {
persons = number;
break;
}
}
}
return persons;
}


}

16 changes: 14 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
// dev branch for Y.Practicum
public class Main {

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

Count guestCount = new Count();
Calculator check = new Calculator();
check.newTovar();
check.print();

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

}
}
}


26 changes: 26 additions & 0 deletions src/main/java/RubFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class RubFormat {
String format(double input) {
String rub;
int ending = (int) input % 100;
switch (ending % 10) {
case 1: {
rub = " рубль";
break;
}
case 2:
case 3:
case 4:
{rub = " рубля";
break;}
default : {
rub = " рублей";
break;
}
}
return rub;
}
}