diff --git a/src/main/java/LogicCalc.java b/src/main/java/LogicCalc.java new file mode 100644 index 000000000..c08215252 --- /dev/null +++ b/src/main/java/LogicCalc.java @@ -0,0 +1,26 @@ +import java.util.ArrayList; +import java.util.HashMap; + +public class LogicCalc { + ArrayList nameItems = new ArrayList(); + ArrayList allOfCost = new ArrayList(); + Double allCosts = 0.0; + + + + void addItem(String nameItem, float account) { + nameItems.add(nameItem); + allOfCost.add(account); + allCosts += account; + } + + // + ArrayList outputAllItem() { + return nameItems; + } + + // + Double outputAllCost() { + return this.allCosts; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..83a5ef6b3 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,119 @@ +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Scanner; public class Main { + + public static void main(String[] args) { - System.out.println("Hello world!"); + PrintStream out = null; + Integer peopleInt; + LogicCalc item = new LogicCalc(); + try { + out = new PrintStream(System.out, true, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + while(true) { + Scanner scanner = new Scanner(System.in); + out.println("На скольких человек необходимо разделить счёт?"); + try { + peopleInt = scanner.nextInt(); + if(peopleInt> 1){ + break; + }else{ + out.println("Ошибка. Нет сысла в счете. Попробуйте заново.\n"); + } + }catch (Exception e){ + out.println("Ошибка. Введите число.\n"); + scanner.reset(); + } + } + + while(true) { + Scanner scanner = new Scanner(System.in); + String nameItem = ""; + out.println("Напишите название товара?"); + nameItem = scanner.next(); + scanner.reset(); + out.println("Напишите стоимость товара? Формат: 10.45, где 10 - руб, 45 - копейки"); + if (scanner.hasNextFloat()) { + float cost = scanner.nextFloat(); + if(cost> 0) { + item.addItem(nameItem, cost); + out.println("Товар успешно добавлен"); + out.println("Хотите ли добавить ещё один товар? Если хотите завершить программу напишите: \"Завершить\""); + String exit = scanner.next(); + if (exit.equalsIgnoreCase("Завершить")) { + break; + } + }else{ + out.println("Ошибка. Отрицательная или нулевая стоимость товара. Попробуйте снова!\n"); + } + } else { + out.println("Извините, но это явно не число. Попробуйте снова!\n"); + } + } + + + ArrayList allItems = item.outputAllItem(); + for(int i = 0; i < allItems.size(); i++){ + out.println("Добавленные товары: " + allItems.get(i)); + } + + String rubleSuffix; + + Double allcostDouble = item.outputAllCost()/peopleInt; + rubleSuffix = rubleRight(allcostDouble); + + String template = "\nКаждый человек должен заплатить по %.2f %s"; + out.println(String.format(template, allcostDouble, rubleSuffix)); + + + } + + private static boolean rightCost() { + + return true; + } + + + private static String rubleRight(Double allcost){ + allcost = Math.floor(allcost); + //String allc = String.valueOf(allcost); + String template = "%.0f"; + String allc = String.format(template, allcost); + int lastDigit; + if(allc.length() == 1){ + lastDigit = Integer.parseInt(allc); + if(lastDigit == 0){ + return "рублей"; + }else if(lastDigit == 1){ + return "рубль"; + }else if(lastDigit> 1 && lastDigit < 5){ + return "рубля"; + }else{ + return "рублей"; + } + }else if (allc.length()> 1){ + lastDigit = Integer.parseInt(allc.substring(allc.length() - 2)); + if(lastDigit < 21){ + return "рублей"; + }else{ + lastDigit = lastDigit % 10; + if(lastDigit == 0){ + return "рублей"; + }else if(lastDigit == 1){ + return "рубль"; + }else if(lastDigit> 1 && lastDigit < 5){ + return "рубля"; + }else{ + return "рублей"; + } + } + } + return ""; } } \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /