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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Пустой репозиторий для работы с Java кодом в Android Studio
IOException - что это и с чем его едят ?

try catch - мы это проходили ?

11 changes: 11 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Calculator {
double account = 0;
int people;

double personalAccount(){
return account / people;
}
Calculator(int people){
this.people = people;
}
}
33 changes: 33 additions & 0 deletions src/main/java/Formater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.Scanner;
public class Formater {

public static int peopleCorrectly (){
Scanner scanner = new Scanner(System.in);
boolean numberOfPeopleCorrectly = false;
String people = "";
while (!numberOfPeopleCorrectly) { // пока не корректное колво людей
System.out.println("на скольких человек необходимо разделить счёт?");
// принимаем не известный тип данных
people = scanner.nextLine();
if (Main.isInt(people)) { // Проверяем на интовость кол-во чел и более 1
numberOfPeopleCorrectly = true; // Корректное колво людей
}
}
return Integer.parseInt (people);
}

public static String rub (double priceDouble){
int price = (int) priceDouble;
int units = price%10;
int dozens = price%100/10;
if (dozens == 1){
return "рублей";
} else if (units > 4 || units == 0){
return "рублей";
} else if (units == 1) {
return "рубль";
} else {
return "рубля";
}
}
}
97 changes: 91 additions & 6 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,91 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
public static boolean isInt(String people){
try {
int peopleInt = Integer.parseInt (people);
if (peopleInt <= 1){
System.out.println("Введено не корректное кол-во человек");
return false;
} else {
return true;
}
} catch (NumberFormatException e) {
System.out.println("Введено не корректное значение, используйте целые числа");
return false;
}
}

public static boolean isPriceDouble(String strPrice){
try {
double price = Double.parseDouble(strPrice);
if (price >= 0.01) {
return true;
} else {
System.out.println("Введено не корректное значение, стоимость должна быть больше 0");
}
} catch (NumberFormatException e) {
System.out.println("Введено не корректное значение, стоимость должна быть в формате {рубли.копейки}");
System.out.println("Пример:\n Помидор 10.45");
return false;
}
return false;
}

public static void main(String[] args) {

ArrayList<Product> products = new ArrayList<Product>(); // создаем список товаров
Scanner scanner = new Scanner(System.in);
int people = Formater.peopleCorrectly(); // получаем интовое колво людей

Calculator calculator = new Calculator(people);
System.out.println("Добавление товаров в калькулятор");
System.out.println("Пример:\n Помидор 10.45");

boolean isAddProduct = true;
while (isAddProduct) { // собираем товары

String product = scanner.next();
if (product.equalsIgnoreCase("завершить")) {
break;
}
// принимаем не известный тип данных
String strPrice = scanner.next();
if (strPrice.equalsIgnoreCase("завершить")) {
break;
}

if (isPriceDouble(strPrice)){
//товар корректный
double price = Double.parseDouble(strPrice);
products.add(new Product(product, price)); //добавление товара в товары
System.out.println(product + " " + Math.floor(price*100)/100 + ". Данный товар был успешно добавлен");
calculator.account += Math.floor(price*100)/100;
System.out.println("Общий чек составляет:" + calculator.account);
isAddProduct = false;
}
System.out.println("Хотите ли Вы добавить ещё один товар?");
while (!isAddProduct){ // проверяем хочет ли пользователь добавить еще товар и переспрашиваем при не понятном ответе
String ans = scanner.nextLine().trim();
if (ans.equalsIgnoreCase("нет") || ans.equalsIgnoreCase("завершить")){
break;
} else if (ans.equalsIgnoreCase("да")) {
System.out.println("Введите название и цену");
isAddProduct = true;
} else {
System.out.println("Введите да или нет");
}
}
}
// вывод всех товаров
System.out.println("Добавленные товары:\n");
for (Product product : products){
String output = product.title + " " + product.price + " " + Formater.rub(product.price);
System.out.println(output);
}
// вывод сколько должен заплатить каждый
System.out.print("Каждый должен заплатить: ");
System.out.println(Math.floor(calculator.personalAccount()*100)/100 + " " + Formater.rub(Math.floor(calculator.personalAccount()*100)/100));
}
}
9 changes: 9 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Product {
String title; // название товара
double price; // цена товара

Product(String title, double price){
this.title = title;
this.price = Math.floor(price*100)/100; // Округляем число в меньшую сторону до двух знаков после запятой
}
}