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
22 changes: 22 additions & 0 deletions src/main/java/Check.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Check {
private Double sum = 0D;
private String goods = "";

public void addSum(Double newSum) {
this.sum += newSum;
}
public String getGoods() {
return this.goods;
}
public Double getSum() {
return this.sum;
}
public void addGoodName(String goodName, Double goodPrice) {
String newNames = goods + "\n" + "Товар: "+ goodName + ", его цена: " + goodPrice + ";";
this.goods = newNames;
}
public Double div(int countClients) {
Double result = this.sum / countClients;
return result;
}
}
19 changes: 19 additions & 0 deletions src/main/java/Formator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Formator {
Double sumToOneClient;
public Formator(Double sumToOneClient) {
this.sumToOneClient = sumToOneClient;
}
public String formator() {
String result;
int intResult = this.sumToOneClient.intValue();
String strResult = String.valueOf(intResult);
if (strResult.endsWith("1")) {
result = "рублю";
} else if (strResult.endsWith("2") || strResult.endsWith("3") || strResult.endsWith("4")) {
result = "рубля";
} else {
result = "рублей";
}
return result;
}
}
18 changes: 18 additions & 0 deletions src/main/java/Good.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

public class Good {
private String name;
private Double price;

public String getName() {
return name;
}

public Double getPrice() {
return price;
}

public Good(String name, Double price) {
this.name = name;
this.price = price;
}
}
62 changes: 59 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
import java.util.Scanner;
public class Main
{
public static void main( String[] args )
{
System.out.println("Введите количество человек для разделения счета");
int countClients;
Check check = new Check();
while (true) {
Scanner sc = new Scanner(System.in);
String result = sc.nextLine();
try {
countClients = Integer.parseInt(result);
if (countClients <= 1) {
System.out.println("Введен не верный формат, либо количество человек - 1 и менее, повторите попытку");
} else {
System.out.println("Введено клиентов: " + countClients);
break;
}
} catch (Exception e) {
System.out.println("Введен не верный формат, либо количество человек - 1 и менее, повторите попытку");
}
}
while (true) {
System.out.println("Введите название товара. Для завершения работы - введите \"Завершить\"");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
if (name.equalsIgnoreCase("завершить")) {
break;
}
System.out.println("Введите цену товара.");
Double price = 0D;
while (true) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextDouble()) {
Double result = scanner.nextDouble();
if (result <= 1) {
System.out.println("Вы ввели неверный формат цены, либо она составляет менее 1 рубля" +
",\n введите цену в формате \"15.00\"");
} else {
price = result;
break;
}
} else {
System.out.println("Вы ввели неверный формат цены, либо она составляет менее 1 рубля" +
",\n введите цену в формате \"15.00\"");
}
}
Good good = new Good(name, price);
check.addSum(good.getPrice());
check.addGoodName(good.getName(), good.getPrice());
}
Double sumToOneClient = check.div(countClients);
Formator checker = new Formator(sumToOneClient);
String result = "Вот Ваш чек: \n" + check.getGoods() + "\n" +
"Общая стоимость: " + check.getSum() + "\n" +
"Счет разделен на " + countClients + " человек\n" +
"С каждого человека по " + String.format("%.2f", sumToOneClient) + " " + checker.formator();
System.out.println(result);
}
}