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
150 changes: 150 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class Main {
public static float totalCost = 0;

public static void main(String[] args) {

int quantity = firstInput();
Map receipt = secondInput();
thirdOutput(quantity, receipt);

}

//Функция первой части.
//На сколько людей будем делить счет
static int firstInput() {

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

boolean proverka = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Лучше не использовать нейминг русских слов в транслите, это в целом ни на что не влияет, но тогда надо и все остальные переменные и функции также называть
можно назвать "check"

int quantity = 0;

while (proverka) {
if (in.hasNextInt()) {
quantity = in.nextInt();
if (quantity <= 1) {
System.out.println("Вы ошиблись, вы ввели меньше 2, а именно: " + quantity + "\n Пожалуйста повторите ввод.");
} else {
proverka = false;
}
} else {
System.out.println("Вы ошиблись, вы ввели " + in.next() + " вместо целого числа. \n Пожалуйста повторите ввод.");
}
}
return quantity;
}

//Функция второй части
//Что было заказано и по какой стоимости, с точностью до копеек.
static Map secondInput(){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно более осознанно назвать, например, ввод товаров, ввод количества человек и т.п.


boolean checkCost = true;
boolean checkEnter = true;
float cost = 0;
// int count = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Если не используется, лучше удалить

String product = "";
Map<String,Float> receipt = new HashMap<String,Float>();

System.out.println("Введите заказанное блюдо-товар.");
while (checkEnter){
Scanner in = new Scanner(System.in);

//Товар
product = in.nextLine(); // Товар может быть с пробелом в имени
if(product.toLowerCase().equals("завершить")){
checkEnter = false;
break;
}

//Стоимость
System.out.println("Введите стоимость до копеек(.00).");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Тут можно добавить, в каком формате надо вводить копейки (через запятую или точку)

while (checkCost) {
if(!(in.hasNextFloat())){
System.out.println("Вы ввели " + in.next() + " это не похоже на стоимость.\n Пожалуйста повторите ввод.");
}
else {
cost = Math.round(in.nextFloat()*100)/100.0f;
if(cost > 0){
checkCost = false;
}
else{
System.out.println("Вы ввели " + cost + ", это неправильная стоимость.\n Пожалуйста повторите ввод.");
}

}
}

//Добавим в массив
receipt.put(product, cost);
totalCost += cost;
System.out.println("Вы добавили в чек " + product + " по стоимости " + cost);
System.out.println("Если вы хотите завершить ввод - наберите \"Завершить\" или введите новый товар.");
checkCost = true;

// count += 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно удалить если не нужно

// if(count == 5){
// break;
// }
}

return receipt;
}

//Процедура третей части
static void thirdOutput(int quantity, Map receipt){

float perCost;
int intPerCost;
String sRubOne = "рубль";
String sRubls = "рубля";
String sMoreRubls = "рублей";


System.out.println("Добавленные товары:");

Iterator<Map.Entry<String, Float>> iterator = receipt.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, Float> entry = iterator.next();
String product = entry.getKey();
Float cost = entry.getValue();
// System.out.println(String.format(text,product,cost));
System.out.println(product + " стоит " + cost);
}
perCost = totalCost / quantity;
if (perCost * (double)(quantity) < totalCost){
perCost += 0.01f;
}
intPerCost = (int) Math.ceil(perCost);

System.out.println(String.format("Общая стоимость счета " + totalCost + ", каждый должен внести %.2f %s или %d %s", perCost, GetRubls(intPerCost), intPerCost, GetRubls(intPerCost)));

}

public static String GetRubls(int num){
int preLastDigit = num % 100/10;
if(preLastDigit == 1){
return "рублей";
}
switch(num % 10){
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубль";
default:
return "рублей";
}
}
}

// Напоминалка "Калькулятор счета"

// 1. Задаем вопросы в консоли
// 2. Считаем пользовательский ввод
// 3. Показываем список товаров и показываем сумму каждого из друзей
33 changes: 33 additions & 0 deletions src/main/java/Calculation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.Iterator;
import java.util.Map;

public class Calculation {
//Процедура третей части
static void calculationOutput(int quantity, Map receipt){

float perCost;
int intPerCost;
String sRubOne = "рубль";
String sRubls = "рубля";
String sMoreRubls = "рублей";


System.out.println("Добавленные товары:");

Iterator<Map.Entry<String, Float>> iterator = receipt.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, Float> entry = iterator.next();
String product = entry.getKey();
Float cost = entry.getValue();
System.out.println(product + " стоит " + cost);
}
perCost = Main.totalCost / quantity;
if (perCost * (double)(quantity) < Main.totalCost){
perCost += 0.01f;
}
intPerCost = (int) Math.ceil(perCost);

System.out.println(String.format("Общая стоимость счета " + Main.totalCost + ", каждый должен внести %.2f %s или %d %s", perCost, Main.GetRubls(intPerCost), intPerCost, Main.GetRubls(intPerCost)));

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

public class CountPeople{
//Функция первой части.
//На сколько людей будем делить счет
public static int countPeopleInput() {

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

boolean check = true;
int quantity = 0;

while (check) {
if (in.hasNextInt()) {
quantity = in.nextInt();
if (quantity <= 1) {
System.out.println("Вы ошиблись, вы ввели меньше 2, а именно: " + quantity + "\n Пожалуйста повторите ввод.");
} else {
check = false;
}
} else {
System.out.println("Вы ошиблись, вы ввели " + in.next() + " вместо целого числа. \n Пожалуйста повторите ввод.");
}
}
return quantity;
}
}

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

public class Goods {
//Функция второй части
//Что было заказано и по какой стоимости, с точностью до копеек.
static Map goodsInput(){

boolean checkCost = true;
boolean checkEnter = true;
float cost = 0;
String product = "";
Map<String,Float> receipt = new HashMap<String,Float>();

System.out.println("Введите заказанное блюдо-товар.");
while (checkEnter){
Scanner in = new Scanner(System.in);

//Товар
product = in.nextLine(); // Товар может быть с пробелом в имени
if(product.toLowerCase().equals("завершить")){
checkEnter = false;
break;
}

//Стоимость
System.out.println("Введите стоимость до копеек(,00). В качестве разделителя нужно использовать \",\"");
while (checkCost) {
if(!(in.hasNextFloat())){
System.out.println("Вы ввели " + in.next() + " это не похоже на стоимость.\n Пожалуйста повторите ввод.");
}
else {
cost = Math.round(in.nextFloat()*100)/100.0f;
if(cost > 0){
checkCost = false;
}
else{
System.out.println("Вы ввели " + cost + ", это неправильная стоимость.\n Пожалуйста повторите ввод.");
}

}
}

//Добавим в массив
receipt.put(product, cost);
Main.totalCost += cost;
System.out.println("Вы добавили в чек " + product + " по стоимости " + cost);
System.out.println("Если вы хотите завершить ввод - наберите \"Завершить\" или введите новый товар.");
checkCost = true;

}

return receipt;
}
}
Loading