Repository navigation
Seems ok #1
base: dev
Are you sure you want to change the base?
Seems ok #1
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 |
|---|---|---|
| @@ -1,6 +1,108 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| System.out.println("[Калькулятор счёта 1.0]"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| int people = 0; | ||
| while (true) { //очень долго пытался найти решение в практикуме, | ||
| try { //но походу готовой сортировки по вводу букв/цифр нет. Позаимствовал try/catch | ||
| System.out.println("1.Введите кол-во человек (Целое число >1)"); //из внешних | ||
| people = Integer.parseInt(scanner.next()); //ресурсов | ||
| if (people > 1) { | ||
| break; | ||
| } else { | ||
| System.out.println("Введено число <2. Счёт поделить нельзя."); | ||
| } | ||
| } catch (NumberFormatException iGuessItWasntAnInt) {//если ввели не int | ||
| System.out.println("Введено не целое число."); | ||
| } | ||
| } | ||
| String outcome = calculate(people); | ||
| System.out.println("Кол-во людей: "+ people +"\nДобавленные товары:\n"+outcome); | ||
| } | ||
| } | ||
|
|
||
| public static String calculate (int number){ | ||
| //Этот метод выдаст де-факто решение задачи в виде списка блюд+цен и сумма/кол-во людей (String) | ||
| Scanner scanner = new Scanner(System.in).useDelimiter("\\n");//если бы писал код с 0 | ||
| System.out.println("2.Подсчёт суммы счёта и деление."); //завел бы scanner в Main | ||
| String input; | ||
| double price=0; | ||
|
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. Здесь и на строку ниже студия подсказывает, что "Variable 'price' initializer '0' is redundant". Почему это происходит: это значение не используется нигде до того, как ему присвоят новое. То есть оно просто так полежит в переменной без дела |
||
| String result=""; | ||
| double sum=0; | ||
| int count=1;//решил проиндексировать блюда | ||
| System.out.println("Введите название блюда №"+count+".");//опции на 0 блюд пользавателю не дам | ||
| input=scanner.next(); | ||
| result="1."+input+" - "; | ||
| while (true) { | ||
|
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. Один из основных принципов разработки - это не допускать повторений кода. Этот while и следующий практически 1 в 1 идентичны ( за исключением дополнительных действий, но их можно корректно сделать и в одном while). В следующем дз одним из критериев успеха будет отсутствие дублирования кода. Сейчас отличная возможность потренироваться |
||
| try { | ||
| System.out.println("Введите цену блюда в рублях (Числа будут округлены до сотых)"); | ||
| price = Double.parseDouble(scanner.next()); | ||
|
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. 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. 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. Здесь происходит хитрая вещь, связанная с локалью компа. У меня локаль русская, а для нее характерен разделитель в виде запятой. Для английской локали характерна точка. %.2f делает в моем случае через запятую, что и не может по какой-то причине прочитать parseDouble 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. Придется придумать самостоятельно, как обойти вот такие места 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. |
||
| if (price > 0) { | ||
| input=String.format("%.2f", price); | ||
|
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 из double в лице input. Затем ты два раза переводишь его обратно в double) Как минимум можно было бы вынести перевод в double и сделать это один раз |
||
| sum=sum+Double.parseDouble(input); //сумма счёта | ||
| result=result+input+roubles(Double.parseDouble(input))+".\n"; | ||
| break; | ||
| } else { | ||
| System.out.println("Числа <= 0 рублей недопустимы."); | ||
| } | ||
| } catch (NumberFormatException iGuessItWasntAnInt) {//если ввели не int | ||
| System.out.println("Неверный формат ввода."); | ||
| } | ||
| } | ||
| while (true){//цикл ввода содержания чека до "завершить" | ||
| count++; | ||
| System.out.println("Введите название блюда №"+count+" или \"Завершить\", " + | ||
| "чтобы перейти к "); | ||
| input=scanner.next(); | ||
| if (input.toLowerCase().equals("завершить")) {break;} //условие завершения цикла | ||
| result=result+count+"."+input+" - "; | ||
| while (true) { //внутренний цикл для проверки правильного ввода цены | ||
| try { | ||
| System.out.println("Введите цену блюда в рублях (Числа будут округлены до сотых)"); | ||
| price = Double.parseDouble(scanner.next()); | ||
| if (price > 0) { | ||
| input=String.format("%.2f", price); | ||
| sum=sum+Double.parseDouble(input); | ||
| result=result+input+roubles(Double.parseDouble(input))+".\n"; | ||
| break; | ||
| } else { | ||
| System.out.println("Числа <= 0 рублей недопустимы."); | ||
| } | ||
| } catch (NumberFormatException iGuessItWasntAnInt) {//если ввели не int | ||
| System.out.println("Неверный формат ввода."); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| result=result+"Общая стоимость блюд = "+sum+roubles(sum)+".\n";//Добавим сумму | ||
| double costPerPerson = sum/number; | ||
| String round = String.format("%.2f", costPerPerson); //в следующий раз буду использовать decimal | ||
| costPerPerson= Double.parseDouble(round); //даже если мы его не покроем в Практикуме. | ||
| if ((costPerPerson*number)<sum) { //т.к. округление вверх без введения Math не сделать, а | ||
| while ((costPerPerson*number)<sum) { //делать import того что мы не проходили | ||
|
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. Делать импорт того, что вы не проходил совсем даже неплохо, особенно, если на это нет прямого запрета в тексте задания. Изучать новое всегда приветствуется, особенно, если оно может упростить жизнь. А Math - это базовая вещь, которую можно достать из коробки) |
||
| costPerPerson=costPerPerson+0.01; //я ОЧЕНЬ не хочу - добиваем по копейке | ||
| } // пока не начнёт делиться ровно. | ||
| } | ||
| //нам обязательны падежи для рублей... | ||
| result=result+"Округляя вверх до копейки, каждый участник должен заплатить "+ | ||
| String.format("%.2f", (costPerPerson))+roubles(costPerPerson)+"."; | ||
| return result; | ||
| } | ||
| public static String roubles (double amount){ //хозяин - барин, я бы поставил ₽ | ||
| int uno = (int)amount % 10; | ||
| int decem = (int)amount % 100; | ||
| if (decem>9 && decem<20) | ||
| { return(" рублей");} | ||
| else {switch (uno){ | ||
| case 1: return(" рубль"); | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return(" рубля"); //компилятор подсказал! Сам бы забыл... :) | ||
| default: return(" рублей");} //сообразил) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||



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.hasNextInt(), который вернет true или false в зависимости, введено ли корректное число или нет. Затем при успешном результате считать число. Это решение можно обыграть, чтобы избавиться от блока try catch