Repository navigation
commit HW #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?
commit HW #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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Calculator { | ||
| private final List<Product>list = new ArrayList<>(); | ||
| private int person = 0; | ||
| Calculator(int person){ | ||
| this.person = person; | ||
| } | ||
|
|
||
| public List<Product> getList() { | ||
| return list; | ||
| } | ||
| public void add(Product product){ | ||
| list.add(product); | ||
| } | ||
| public double sum(){ | ||
| double sum = 0.0; | ||
| for(Product p : list){ | ||
| sum += p.getPrice(); | ||
| } | ||
| return ((double)Math.round(sum * 100)) / 100; | ||
| } | ||
| public double calcPastPay(){ | ||
| if(this.person > 0) | ||
| return ((double)Math.round(sum() / this.person * 100)) / 100; | ||
| else | ||
| return 0.0; | ||
| } | ||
| public void printCheck(){ | ||
| for(Product p: getList()){ | ||
| System.out.println(p.toString()); | ||
| } | ||
| } | ||
| public String rubRightWrite(double sum){ | ||
| int dec = (((int) sum) / 10) % 10; | ||
| if(dec == 1){ | ||
| return "рублей"; | ||
| }else { | ||
| int delta = ((int) sum) % 10; | ||
| switch (delta) { | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| case 0: | ||
| case 5: | ||
| case 6: | ||
| case 7: | ||
| case 8: | ||
| case 9: | ||
| return "рублей"; | ||
| } | ||
| } | ||
| return "руб."; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,78 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| System.out.println("На сколько персон необходимо поделить счет: "); | ||
| Scanner scanner = new Scanner(System.in); | ||
| int countPerson = 0; | ||
|
|
||
| Calculator calc = null; | ||
|
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. Необязательно писать инициализаторы явно. По умолчанию double имет значение 0, строка и объект null |
||
|
|
||
| countPerson = scanner.hasNextInt() ? scanner.nextInt() : 0; | ||
| if (countPerson <= 0){ | ||
| System.out.println("Количество человек не может быть отрицательным или равным нулю! Либо введено слишком большое число!"); | ||
| }else if(countPerson == 1){ | ||
| System.out.println("Нет смысла в работе программы при количестве персон равном 0"); | ||
| }else { | ||
|
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, можно было бы вынести в отдельный метод, чтобы немного разгрузить main, а также чтобы не было сильной вложенности кода. (сначала в if, потом в while, это создает лишние отступы) |
||
| System.out.println("Вы планируете разделить счета на "+countPerson+" чел."); | ||
| calc = new Calculator(countPerson); | ||
| buildCheck(calc); | ||
|
|
||
| System.out.println("Итоговый чек: "); | ||
| calc.printCheck(); | ||
| System.out.println("К оплате итого: "+calc.sum()+" "+calc.rubRightWrite(calc.sum())); | ||
| System.out.println("В том числе с каждого члена команды "+calc.calcPastPay()+" "+calc.rubRightWrite(calc.calcPastPay())); | ||
| } | ||
|
|
||
| } | ||
| public static void buildCheck(Calculator calc){ | ||
| String next = "" ; | ||
| double price ; | ||
| Scanner scanner = new Scanner(System.in); | ||
| while(!next.equals("завершить")){ | ||
| System.out.println("Введите наименование товара: "); | ||
| System.out.print("-> "); | ||
| String tmp = scanner.next(); | ||
|
|
||
| boolean check = false; | ||
| String strPrice = ""; | ||
| while (!check) { | ||
| System.out.println("Стоимость:"); | ||
| System.out.print("-> "); | ||
| strPrice = scanner.next(); | ||
| check = verifyDouble(strPrice); | ||
| if(!check){ | ||
| System.out.println("Не правильный ввод. Поробуйте еще."); | ||
| } | ||
| } | ||
| price = Double.parseDouble(strPrice); | ||
| calc.add(new Product(tmp,price)); | ||
| System.out.println("Добавить еще товар? Да: любой символ + Enter, Закончить ввод: Завершить + Enter"); | ||
| next = (scanner.next().toLowerCase()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public static boolean verifyDouble(String str){ | ||
| boolean rez = false; | ||
| char[] chArr = str.toCharArray(); | ||
| int countPoint = 0; | ||
| for(char i: chArr){ | ||
| if(Character.isDigit(i)){ | ||
| rez = true; | ||
| }else if(i == '.'){ | ||
| countPoint++; | ||
| }else{ | ||
| rez = false; | ||
| return rez; | ||
| } | ||
| } | ||
| if(countPoint > 1){ | ||
| rez = false; | ||
| } | ||
| return rez; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| public class Product { | ||
| private String name = ""; | ||
| private double price = 0.0; | ||
| Product(String name, double price){ | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public double getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Добавленный товар: " + name + ", Цена: " + price ; | ||
| } | ||
| } |
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.
По идее эта проверка всегда будет true, так как в программе еще до этого проверяется, что количество больше нуля. Но в целом имеет место быть)