Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/LogicCalc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.ArrayList;
import java.util.HashMap;

public class LogicCalc {
ArrayList<String> nameItems = new ArrayList<>();
ArrayList<Float> allOfCost = new ArrayList<>();
Double allCosts = 0.0;



void addItem(String nameItem, float account) {
nameItems.add(nameItem);
allOfCost.add(account);
allCosts += account;
}

//
ArrayList<String> outputAllItem() {
return nameItems;
}

//
Double outputAllCost() {
return this.allCosts;
}
}
115 changes: 114 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -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<String> 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 "";
}
}