Repository navigation
Консольное приложение№1 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculate { | ||
| static String allItem = ""; //Строка с названиями всех товаров | ||
| static double allPrice = 0.0; //счётчик общей цены | ||
| static int i = 0; //счётчик количества товаров | ||
|
|
||
| public static void calculate() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| Item item= new Item(" ", 0); | ||
| System.out.println("Введите название товара:"); | ||
|
|
||
| while (true) { | ||
| item.name = scanner.next(); | ||
| scanner.nextLine(); | ||
| if (item.name.equalsIgnoreCase("завершить")) { | ||
| System.out.println("Вы завершили ввод товаров"); | ||
| break; | ||
| } else { | ||
| //проверка на число и положительность числа | ||
| while (true) { | ||
| System.out.println("Введите стоймость товара в формате '00,00':"); | ||
| if (scanner.hasNextDouble()) { | ||
| item.price = scanner.nextDouble(); | ||
| scanner.nextLine(); | ||
| if (item.price > 0) { | ||
| System.out.println("Товар добавлен. Вы можете добавить ещё один товар. Для подсчёта введите 'завершить'"); | ||
| allItem = allItem.concat(item.name).concat("\n"); | ||
| allPrice += item.price; | ||
| i++; | ||
| break; | ||
| } | ||
| if (item.price <= 0) { | ||
| System.out.println("Пожалуйста, введите положительное число."); | ||
| } | ||
| } else { | ||
| System.out.println("Пожалуйста, используйте числа."); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| public class Format { | ||
| public static String GetRuble(double num){ | ||
| int desayt = ((int) num % 100) / 10; | ||
| if (desayt == 1) | ||
| { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В Java переносить открывающую фигурную скобку в if/switch/методе не нужно |
||
| return "рублей"; | ||
| } | ||
|
|
||
| switch ((int)num % 10) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Чтобы не дублировать приведение типов |
||
| { | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| default: | ||
| return "рублей"; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Item { | ||
| String name; | ||
| double price; | ||
|
|
||
| Item(String itemName, double itemPrice){ | ||
| name = itemName; | ||
| price = itemPrice; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,39 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| // dev branch for Y.Practicum | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| int people; | ||
| System.out.println("Добрый день! На сколько людей надо разделить счёт?"); | ||
| while(true) { | ||
| if (scanner.hasNextInt()) { | ||
| people = scanner.nextInt(); | ||
| if (people == 1) { | ||
| System.out.println("Введите количество людей больше одного:"); | ||
| } | ||
| if (people < 1) { | ||
| System.out.println("Введите корректное количество людей:"); | ||
| } | ||
| if (people > 1){ | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Пожалуйста, введите количество людей, используя число:"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| Calculate.calculate(); | ||
|
|
||
| double personMoney = Calculate.allPrice / people; | ||
| String tempPrice = "Всего сумма: %.2f"; | ||
| String tempPerson = "На каждого человека: %.2f"; | ||
|
|
||
| System.out.println("Добавленные товары:\n" + Calculate.allItem); | ||
| System.out.println("Всего людей: " + people); | ||
| System.out.println(String.format(tempPrice, Calculate.allPrice) + " " + Format.GetRuble(Calculate.allPrice)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если используешь String.format можно избавиться от конкатенации через оператор +, подставив нужные параметры в паттерн |
||
| System.out.println(String.format(tempPerson, personMoney) + " " + Format.GetRuble(personMoney)); | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучшее переиспользовать инстанс
Scanner, а не создавать каждый раз новый.