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
40 changes: 40 additions & 0 deletions src/main/java/AddFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class AddFormat {
public static String format(int num, String s1, String s2, String s3, boolean print_num){

int count = num%100;

String result;

if (count >= 5 && count <= 20) {
result = s3;
} else {
count = count % 10;
if (count == 1) {
result = s1;
} else if (count >= 2 && count <= 4) {
result = s2;
} else {
result = s3;
}
}

if(print_num){
result = String.format("%d %s", num, result);
}

return result;
}

public static String rub(double price){

int num = (int)Math.floor(price);

String result = format(num, "рубль", "рубля", "рублей", false);

return String.format("%.2f %s", price, result);
}

public static String rub(float price){
return rub((double)price);
}
}
57 changes: 56 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@

import java.util.Locale;
import java.util.Scanner;

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

Locale.setDefault(Locale.US);

Scanner scanner = new Scanner(System.in);

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

int persons;

while (true){
try{
persons = scanner.nextInt();

if(!scanner.nextLine().trim().equals("")){
//100 раз! Уже 100 раз я повторял, что хочу поделить счет на 5 человек
System.out.println("Введите просто число без дополнительных знаков:");
continue;
}

if(persons>1){
break;
}

System.out.println("Введите число человек больше 1:");

}catch (Exception e) {
//System.out.println(e);
//scanner = new Scanner(System.in);
if(scanner.hasNextLine()) scanner.nextLine();
System.out.println("Введите число, а не строку:");
}
}

System.out.println("Будем делить счет на "+AddFormat.format(persons, "человека", "человека","человек", true));

MyCalc calc = new MyCalc(persons);

System.out.println("Теперь давайте добавим товары в счет");

while (true){

calc.addProduct();

System.out.println("Хотите добавить ещё один товар? [да/завершить]");

String str = scanner.nextLine();

if(str.trim().equalsIgnoreCase("завершить")){
break;
}
}

calc.calculateAndPrint();
}
}
37 changes: 37 additions & 0 deletions src/main/java/MyCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public class MyCalc {
private int persons;
public String products = "";
public double total_price = 0;

private int total = 0;
MyCalc(int persons){
this.persons = persons;
}

public void addProduct(){

ProductItem item = new ProductItem();

item.askProductData();

System.out.println("Добавлен товар " + item.name + " стоимостью "+item.getPrice());

products += item.getNameAndPrice() + "\n";

this.total_price += item.price;
this.total++;
}

public void calculateAndPrint(){
System.out.println("\n");
System.out.println("Заказанные товары:");
System.out.println(products.trim());

//продавец и копейки не простит, так что если поровну не поделится будут чаевые
double price_per_person = Math.ceil(100*total_price/persons)/100;

System.out.println("Итого "+AddFormat.format(total, "товар", "товара","товаров", true)+" на сумму "+AddFormat.rub(total_price));
System.out.println("Делим счет на "+ AddFormat.format(persons, "человека", "человек","человек", true));
System.out.println("Каждый должен заплатить по "+ AddFormat.rub(price_per_person));
}
}
59 changes: 59 additions & 0 deletions src/main/java/ProductItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.util.Scanner;

public class ProductItem {

public String name;
public double price;

public void askProductData(){

Scanner scanner = new Scanner(System.in);

while (true){
System.out.println("Введите название товара:");

name = scanner.nextLine();

if(!name.trim().equals("")){
break;
}

System.out.println("Ошибка! Название не должно быть пустой строкой");
}

while(true) {

System.out.println("Введите стоимость товара:");

try {

price = scanner.nextDouble();

if(!scanner.nextLine().trim().equals("")){
System.out.println("Введите только число в формате 0.00 без дополнительных знаков");
continue;
}

if (price > 0) {
break;
}

} catch (Exception e) {

if(scanner.hasNextLine()) scanner.nextLine();
//scanner = new Scanner(System.in);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

закомментированный код нужно удалять перед созданием РR, или подписывать для каких целей его оставил(но это очень редко делается). Это мусор лучше код от него очищать)

}

System.out.println("Введите число в формате 0.00");
}
}


public String getNameAndPrice(){
return name + " " + AddFormat.rub(price);
}

public String getPrice(){
return AddFormat.rub(price);
}
}