Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Пишем консольное приложение No1 #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

Open
mfoxtrot wants to merge 7 commits into main
base: main
Choose a base branch
Loading
from dev
Open
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
121 changes: 118 additions & 3 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,8 +1,123 @@
// dev branch for Y.Practicum
import java.lang.invoke.ConstantCallSite;
import java.util.Scanner;

class Dish {
String name;
double price;

Dish(String name, double price) {
this.name = name;
this.price = price;
}
}

class Bill {
int numberOfPersons;
String listOfDishes;
double totalSum;

Bill(int numberOfPersons) {
this.numberOfPersons = numberOfPersons;
this.listOfDishes = "";
this.totalSum = 0;
}

public void addDish(Dish dish) {
this.listOfDishes = this.listOfDishes + (this.listOfDishes.length()==0 ? "" : "\n") + dish.name;
this.totalSum += dish.price;
System.out.println("Блюдо " + dish.name + " добавлено в чек");
}

public void print() {
System.out.println("Добавленные товары:");
System.out.println(listOfDishes);
}
}

class Calculator {
Bill bill;

Calculator(Bill bill) {
this.bill = bill;
}
public double payment() {
return bill.totalSum/bill.numberOfPersons;
}

public String formattedOutPut() {
double payment = this.payment();
return String.format("%.2f " + this.rubbleProperCase(payment), payment);
}

private String rubbleProperCase(Double sum) {
int intSum = (int)Math.floor(sum);

if (intSum%10==1 && intSum%100!=11) {
return "рубль";
}
else if (intSum%10==2 && intSum%100!=12) {
return "рубля";
}
else if (intSum%10==3 && intSum%100!=13) {
return "рубля";
}
else if (intSum%10==4 && intSum%100!=14) {
return "рубля";
}
else {
return "рублей";
}
}
}
public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Bill bill;

Scanner scanner = new Scanner(System.in);

//Получаем количество гостей
while (true) {
System.out.print("На сколько гостей разбить чек? ");
if (scanner.hasNextInt()) {
int numberOfPersons = scanner.nextInt();
if (numberOfPersons > 1) {
bill = new Bill(numberOfPersons);
break;
}
}
scanner.nextLine();
System.out.println("Неверное значение. Количество гостей должно быть больше 1. Попробуйте еще раз.");
}
scanner.nextLine();//Вынужденная мера для обхода ошибки https://stackoverflow.com/questions/23450524/java-scanner-doesnt-wait-for-user-input
Copy link

@ilshat-abdulin ilshat-abdulin Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍏 В данной ситуации это нормально


while (true) {
System.out.print("Введите название блюда или команду Завершить: ");
String inputString = scanner.nextLine();
if (inputString.equalsIgnoreCase("завершить")) {
break;
}

while (true) {
System.out.print("Введите цену блюда: ");
if (scanner.hasNextDouble()) {
double inputPrice = scanner.nextDouble();
if (inputPrice>0) {
Dish dish = new Dish(inputString, inputPrice);
bill.addDish(dish);
break;
}
}
scanner.nextLine();
}
scanner.nextLine();//аналогично, комментарию выше
}

bill.print();//выводим счет на экран
Calculator calc = new Calculator(bill);
System.out.println(calc.formattedOutPut());//выводим итог расчета
scanner.close();
}
}

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