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
79 changes: 77 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,81 @@
import static java.lang.Math.floor;

import java.util.Scanner;

public class Main {

static Scanner scanner = new Scanner(System.in);
static String[] tovar = new String[100]; //массив сохранения наименований товара

static String buf; //буфер проверки ввода да - нет
static int i=0, N=0; //i - счетчик товара, N - кол.человек
static double[] cost = new double[100]; // массив цен

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

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

do {
System.out.println("Введите число больше 1");
while (!scanner.hasNextInt()) {
System.out.println("Это не целое число!");
scanner.next();
}
N = scanner.nextInt();
} while (N<=1);

for (; ; ) {
Tovar.inputTovar();
tovar[i] = Tovar.nameTovar;
Tovar.inputCost();
cost[i] = Tovar.bufCost;
i++;
System.out.println("Товар успешно введен.");

System.out.println("Желаете продолжить ввод товара? " + "Завершено - нет " + "любая буква - да. ");
buf = scanner.next();
if (buf.equalsIgnoreCase("завершено")) {
endProgram();
break;
}
}
}
}

private static void endProgram() {
scanner.close();
System.out.println("Добавлены товары:");
for (int j = 0; j < i; j++) {
System.out.println(tovar[j]);
}
double result = 0;
for (int j = 0; j < i; j++) {
result = result + cost[j];
}
result = result / N;

System.out.print("Каждый человек должен заплатить - ");

int x = (int)floor(result);
int lastX=x % 100;
while(lastX>20) {
lastX = lastX%10;
}
int cop = (int)floor((result-x)*100);

if(lastX==0) {
System.out.print(x + " рублей " + cop + " коп.");
}
if (lastX==1) {
System.out.println(x + " рубль " + cop + " коп.");
}
if(lastX>1 && lastX<5) {
System.out.printf(x + " рубля " + cop + " коп.");
}
if(lastX>4){
System.out.print(x + " рублей " + cop +" коп.");
}
}
}



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

public class Tovar {

static Scanner sc = new Scanner(System.in);
static String nameTovar;
static double bufCost;
public static void inputTovar() {

System.out.println("Введите название товара ");
nameTovar = sc.next();
}

public static void inputCost() {
System.out.println("Введите стоимость товара ");
do {
while (!sc.hasNextDouble()) {
System.out.println("Неправильно!Введите стоимость цифрами, а копейки через запятую!");
sc.next();
}
bufCost = sc.nextDouble();
if(bufCost<=0) {
System.out.println("Стоимость не может быть отрицательная стоимость или равняться 0!");
System.out.println("Повторите ввод стоимости!");
}
} while(bufCost<=0);
}
}