Skip to content
Open
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
24 changes: 24 additions & 0 deletions src/main/java/Calc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.Scanner;

public class Calc {
float sum=0;
String nameOfThings="";

void calcSum(){
Thing th = new Thing();
th.askAboutThing();
nameOfThings += th.nameThing+"\n";
sum+=th.price;
System.out.println("Товар успешно добавлен!");
}
void askAboutThings(){
Scanner scanner = new Scanner(System.in);
System.out.println("Хочешь добавить товар? Нажмите завершить, чтобы выйти");
String continueAdd = scanner.next();
while (!continueAdd.equalsIgnoreCase("завершить")){
calcSum();
System.out.println("Хочешь добавить ещё товаров? Нажмите завершить, чтобы выйти");
continueAdd = scanner.next();
}
}
}
53 changes: 51 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
System.out.println("Hello world!");
calculateIt();
}
public static void calculateIt (){
int human=0;
while(human<=1){
human=checkPeople ();
}
Calc clc = new Calc();
clc.askAboutThings();
System.out.println("Добавленные товары:");
System.out.println(clc.nameOfThings);
float delSum= (clc.sum/human);
int sumWithoutReal = (int)delSum;
String formatTmp;
if (sumWithoutReal%100>10&&sumWithoutReal%100<20){
formatTmp = "Каждый должен заплатить %.2f рублей";
}
else if (sumWithoutReal%10==1) {
formatTmp = "Каждый должен заплатить %.2f рубль";
}
else if (sumWithoutReal%10>1&&sumWithoutReal%10<5) {
formatTmp = "Каждый должен заплатить %.2f рубля";
}
else {
formatTmp = "Каждый должен заплатить %.2f рублей";
}
System.out.println(String.format(formatTmp,delSum));
}
public static int checkPeople (){
Scanner scanner = new Scanner(System.in);
System.out.println("На скольких человек необходимо разделить счёт?");
int userInput;
while (true){
if (!scanner.hasNextInt()) {
System.out.println("Введите корректное количество людей в штуках");
scanner.next();
}
else{
userInput = scanner.nextInt();
if(userInput<=1){
System.out.println("Число людей должно быть больше одного");
}
else{
break;
}
}
}
return userInput;
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/Thing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.Scanner;

public class Thing {
String nameThing;
float price=-1;
void askAboutThing (){
System.out.println("Введите название товара");
Scanner scanner = new Scanner(System.in);
nameThing = scanner.next();
System.out.println("Введите стоимость товара в формате \"рубли,копейки\"");
while (true){
if (!scanner.hasNextFloat()) {
System.out.println("Введите корректную стоимость товара в формате\"рубли,копейки\"");
scanner.next();
}
else{
price = scanner.nextFloat();
if(price<=0){
System.out.println("Введите корректную стоимость товара в формате\"рубли,копейки\"");
}
else{
break;
}
}
}
}
}