-
Notifications
You must be signed in to change notification settings - Fork 0
Проектная работа 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
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,53 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
| double totalPrice=0; | ||
| String totalProduct = "" ; | ||
|
|
||
| public void priceEntry() { | ||
|
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. ⏫ Так как функция |
||
| System.out.println("Введите стоимость товара в формате \"xx,xx\" цифрами"); | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
| while (true) { | ||
| if (scanner.hasNextDouble()) { | ||
| double inputPrice = scanner.nextDouble(); | ||
| if (inputPrice<=0){ | ||
| System.out.println("вы ввели некорректные данные"); | ||
| scanner.nextLine(); | ||
|
|
||
| } else{ | ||
| System.out.println("Стоимость- "+ inputPrice); | ||
| totalPrice = totalPrice + inputPrice; | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Похоже вы ввели буквы или \".\",введите стоимость товара в формате \"xx,xx\" цифрами "); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
| public void productEntry(){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Введите название продукта"); | ||
| String nameOfProduct = scanner.nextLine(); | ||
|
|
||
| while (true){ | ||
| if(nameOfProduct.equalsIgnoreCase("завершить")){ | ||
| System.out.println("операция завершена"); | ||
| break; | ||
| } | ||
| else if(nameOfProduct.isEmpty()) { | ||
| System.out.println("вы ввели некоректные данные, попробуйте еще раз"); | ||
| nameOfProduct = scanner.nextLine(); | ||
| } | ||
| else { | ||
| System.out.println("вы выбрали "+ nameOfProduct); | ||
| totalProduct = totalProduct +"\n"+ nameOfProduct; | ||
| priceEntry(); | ||
|
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 каждый раз, можно воспользоваться более оптимизированным способом формирования строки - с помощью |
||
| System.out.println("Хотите добавить что-то еще? Если нет, введите \"завершить\" ?"); | ||
| nameOfProduct = scanner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,61 @@ | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Person person = new Person(); | ||
| Calculator calculator = new Calculator(); | ||
| person.numberOfPerson(); | ||
|
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. ⏫ Обычно функции, которые выполняют какое-либо действие принято называть глаголами. В твоем случае было бы удобнее назвать:
|
||
| calculator.productEntry(); | ||
| System.out.println(String.format( "Добавленные товары: %s \nОбщая стоимость- %.2f",calculator.totalProduct,calculator.totalPrice));// System.out.println("Добавленные товары: "+ calculator.totalProduct + "\n Общая стоимость - "+ calculator.totalPrice); | ||
|
|
||
| double checkForEveryOne = calculator.totalPrice / person.valueOfPeople; | ||
| int sumCheck = (int) Math.floor(checkForEveryOne); | ||
| String rubles = ""; | ||
| switch (sumCheck % 10){ | ||
| case 0: | ||
| rubles = "рублей"; | ||
| break; | ||
| case 1: | ||
| rubles = "рубль"; | ||
| break; | ||
|
|
||
| case 2: | ||
| rubles = "рубля"; | ||
| break; | ||
| case 3: | ||
| rubles = "рубля"; | ||
| break; | ||
| case 4: | ||
| rubles = "рубля"; | ||
| break; | ||
| case 5: | ||
| rubles = "рублей"; | ||
| break; | ||
| case 6: | ||
| rubles = "рублей"; | ||
| break; | ||
| case 7: | ||
| rubles = "рублей"; | ||
| break; | ||
| case 8: | ||
| rubles = "рублей"; | ||
| break; | ||
| case 9: | ||
| rubles = "рублей"; | ||
| break; | ||
| } | ||
| switch (sumCheck % 100){ | ||
| case 11: | ||
| rubles ="рублей"; | ||
| case 12: | ||
| rubles ="рублей"; | ||
| case 13: | ||
| rubles ="рублей"; | ||
| case 14: | ||
| rubles ="рублей"; | ||
| } | ||
|
|
||
| System.out.println("Каждый платит - " + String.format("%.2f %s",checkForEveryOne, rubles)); | ||
|
|
||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Person { | ||
| int valueOfPeople ; | ||
|
|
||
|
|
||
| public void numberOfPerson() { | ||
| Scanner scanner1 = new Scanner(System.in); | ||
| System.out.println("На скольких человек необходимо разделить счёт?"); | ||
| // количество человек | ||
|
|
||
| while (true) { | ||
|
|
||
| if (scanner1.hasNextInt()) { | ||
| valueOfPeople = scanner1.nextInt(); | ||
|
|
||
| if (valueOfPeople == 1) { | ||
| System.out.println("Деление не требуется,за счёт платит " + valueOfPeople + " человек!"); | ||
| break; | ||
|
|
||
| } else if (valueOfPeople <= 0 ) { | ||
| System.out.println("Введены некорректные данные, попробуйте еще раз"); | ||
| scanner1.nextLine(); | ||
|
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. ⏫ Эти два условия можно было бы объединить под |
||
|
|
||
| }else if (valueOfPeople > 20000 ) { | ||
| System.out.println("Введены некорректные данные,попробуйте еще раз"); | ||
| scanner1.nextLine(); | ||
|
|
||
| } | ||
| else { | ||
| System.out.println("Счёт будет поделен на " + valueOfPeople ); | ||
| break; | ||
| } | ||
| } | ||
| else { | ||
| System.out.println("Некорректные данные, попробуйте еще раз"); | ||
| scanner1.nextLine(); | ||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||