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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
.externalNativeBuild
.cxx
local.properties
.idea/
91 changes: 91 additions & 0 deletions src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import java.util.Scanner;
import java.lang.Math;
public class App {
Scanner sc = new Scanner(System.in);
Calculator calculator = new Calculator();

public void run() {
int peopleCount = getPeopleCount();
readItems();
double sum = calculator.getSum();
String info = calculator.getItemsAsStr();
System.out.println(info);
double avg = sum / peopleCount;
double roundedValue = Math.floor(avg);
String template;
if (roundedValue % 100 > 9 && roundedValue % 100 < 21){
template = "%.2f рублей";
}
else if (roundedValue % 10 == 1) {
template = "%.2f рубль";
}
else if ( roundedValue % 10 > 1 && roundedValue % 10 < 5 ) {
template = "%.2f рубля";
}
else {
template = "%.2f рублей";
}
System.out.println(String.format(template, avg));


}
private int getPeopleCount(){
while(true){
try {
System.out.println("Укажите колличество человек");
String myString = sc.next();
int peopleCount = Integer.parseInt(myString);
if (peopleCount <= 1) {
System.out.println("Колличество человек должно быть больше 1");
continue;
}
else {
return peopleCount;
}
} catch (NumberFormatException e) {
System.out.println("Неревный ввод");

}

}
}
private void readItems(){
while (true) {
System.out.println("Введите название продукта");
String title = sc.next();
while (true) {
try {
System.out.println("Введите цену продукта");
String myString1 = sc.next();
double price = Double.parseDouble(myString1);
if (price > 0) {
Item item = new Item(title, price);
calculator.addItem(item);
System.out.println("Товар успешно добавлен");
System.out.println("Если это все продукты, то введите 'exit', иначе введите любой символ");
String finish = sc.next();
if (finish.equalsIgnoreCase("exit")) {
break;
} else {
continue;
}
} else {
System.out.println("Неревный ввод");
continue;
}
} catch (NumberFormatException e) {
System.out.println("Неревный ввод");

}


}
break;



}
}


}
22 changes: 22 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.ArrayList;
public class Calculator{
ArrayList<Item> items = new ArrayList<Item>();
public double getSum(){
double sum = 0;
for (Item item: items){
sum += item.price;
}
return sum;
}
public void addItem(Item item){
items.add(item);
}
public String getItemsAsStr(){
String info = "Добавленные товары: \n";
for (Item item: items){
info += item.toString() + "\n";
}
return info;
}

}
12 changes: 12 additions & 0 deletions src/main/java/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Item {
String title;
double price;
Item(String Title, double Price) {
title = Title;
price = Price;
}
public String toString(){
String priceAsStr = Double.toString(price);
return title + " " + priceAsStr;
}
}
6 changes: 3 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
public class Main {

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