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

private double check = 0;
private Product product;
private Guest guest;

public Calculator(Product product, Guest guest) {
this.product = product;
this.guest = guest;
}

public void cheakCalcForm() {
check = product.sum / guest.guests;
double checkF = Math.floor(check);
int checkO = (int) checkF % 10;
switch (checkO)
{
case 1:
System.out.println(String.format("%.2f", check) + " рубль");
break;
case 2:
case 3:
case 4:
System.out.println(String.format("%.2f", check) + " рубля");
break;
default:
System.out.println(String.format("%.2f", check) + " рублей");
break;
}
}
}


20 changes: 20 additions & 0 deletions src/main/java/Guest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Guest {
int guests;
public void numGuests() {
System.out.println("Здравствуйте. Введите количество гостей:");
while (true) {
String guestsStr = Main.scanner.next();
try {
int ans = Integer.parseInt(guestsStr);
if (ans <= 1) {
throw new RuntimeException();
}
guests = ans;
return;
} catch (Exception e) {
System.out.println("Ошибка, попробуйте еще раз!\n----------");
System.out.println("Введите количество гостей:");
}
}
}
}
14 changes: 12 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import java.util.Scanner;

public class Main {
static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
System.out.println("Hello world!");
Guest guest = new Guest();
Product product = new Product();
Calculator calc = new Calculator(product, guest);

guest.numGuests();
product.products();
product.shoplist();
calc.cheakCalcForm();
}
}
}
51 changes: 51 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import java.util.ArrayList;

public class Product {
String name;
double price;
double sum = 0;
ArrayList<String> listPro = new ArrayList<>();

public void products() {

System.out.println("Укажите название и стоимость каждого товара?");
name = "";
boolean isProductInputFinished = false;
while (!isProductInputFinished) {
System.out.println("Название товара:");
name = Main.scanner.next();
if (name.equalsIgnoreCase("завершить")) {
isProductInputFinished = true;
continue;
}
listPro.add(name);
priceSum();
System.out.println("Товар успешно добавлен!\n----------");
System.out.println("Хотите добавить еще один товар? Если нет, то введите - Завершить");
}

}

private void priceSum() {
System.out.println("Стоимость товара = ");
String priceStr = Main.scanner.next();
try {
price = Double.parseDouble(priceStr);
} catch (Exception e) {
price = -1;
}
if (price < 0.0) {
System.out.println("Введите корректную стоимость товара:");
priceSum();
return;
}
sum = sum + price;
}
public void shoplist() {
System.out.println("----------\nДобавленные товары:");
for (int i = 0; i < listPro.size(); i++) {
System.out.println(listPro.get(i));
}
}
}