Skip to content

Instantly share code, notes, and snippets.

@vkorepanov
Last active September 7, 2017 17:54
Show Gist options
  • Save vkorepanov/7bfe6ae77944510ef1558d3ceb636497 to your computer and use it in GitHub Desktop.
Save vkorepanov/7bfe6ae77944510ef1558d3ceb636497 to your computer and use it in GitHub Desktop.
Training calculator
package TrainCalculator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.IndexOutOfBoundsException;
import java.lang.RuntimeException;
class TrainCalculator {
private class PhysicalExcercise {
public PhysicalExcercise(int r, int a) {
repeats = r;
approaches = a;
}
private int repeats = 0;
private int approaches = 0;
}
private PhysicalExcercise kettleExcercise;
private PhysicalExcercise barExcercise;
/**
* @brief Прочитать число из stdin.
*
* @param minBound Минимальное ожидаемое значение.
* @param maxBound Максимальное ожидаемое значение.
* @throw IndexOutOfBoundsException если прочитанное число не лежит на отрезке [minBound, maxBound].
* @throw RuntimeException если произошла ошибка ввода-вывода или введено невалидное числовое значение.
*
* @return int Число, лежащее на отрезке [minBound, maxBound].
*/
private int readInteger(int minBound, int maxBound) throws IndexOutOfBoundsException, RuntimeException {
try {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
final int result = Integer.parseInt(buf.readLine());
if (result < minBound || result > maxBound)
throw new IndexOutOfBoundsException("Значение " + Integer.toString(result)
+ " не находится в интервале [" + Integer.toString(minBound)
+ ", " + Integer.toString(maxBound) + "]");
return result;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* @brief Прочитать целое число из stdin.
* @throw RuntimeException если произошла ошибка ввода-вывода или введено невалидное числовое значение.
* @return int Целое число, введенное пользователем.
*/
private int readInteger() throws RuntimeException {
return readInteger(Integer.MIN_VALUE, Integer.MAX_VALUE);
}
final static int KettleApproachCount = 1;
final static int BarsApproachCount = 2;
final static int ShowKettleTable = 3;
final static int ShowBarsTable = 4;
final static int Exit = 5;
final static int FirstMenuItemNumber = KettleApproachCount;
final static int LastMenuItemNumber = Exit;
private int askForIntegerWith(String message, int minBound, int maxBound)
throws IndexOutOfBoundsException, RuntimeException {
System.out.print(message);
return readInteger(minBound, maxBound);
}
private PhysicalExcercise readExcercise() throws IndexOutOfBoundsException {
final int MinAproachCount = 3;
final int MaxAproachCount = 6;
final int MinRepeatsCount = 5;
final int MaxRepeatsCount = 20;
final int approaches = askForIntegerWith(" Нaчальное количество подходов "
, MinAproachCount, MaxAproachCount);
final int repeats = askForIntegerWith(" Максимальное количество повторений "
, MinRepeatsCount, MaxRepeatsCount);
return new PhysicalExcercise(repeats, approaches);
}
/**
* @brief Показать меню и выполнять действия из меню.
* @return boolean Возвращает ложь, если выбрано действие выхода.
*/
public boolean showMenu() throws IOException {
System.out.println("\n" + " Программа тренировки с гирями v0.1" + "\n");
System.out.println("1. Установить количество подходов и повторений для гирь");
System.out.println("2. Установить количество подходов и повторений для турника и брусьев");
System.out.println("3. Показать таблицу гирь");
System.out.println("4. Показать таблицу турника");
System.out.println("5. Выход");
try {
final int choice = readInteger(FirstMenuItemNumber, LastMenuItemNumber);
switch (choice) {
case KettleApproachCount:
kettleExcercise = readExcercise();
printKettleTable();
break;
case BarsApproachCount:
barExcercise = readExcercise();
printBarTable();
break;
case ShowKettleTable:
printKettleTable();
break;
case ShowBarsTable:
printBarTable();
break;
case Exit:
return false;
default:
System.out.println(" Несуществующий пункт меню или неккоректный ввод");
showMenu();
break;
}
} catch (IndexOutOfBoundsException e) {
System.out.println("\n" + " Недопустимое значение" );
return showMenu();
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
return true;
}
private void printExcercise(PhysicalExcercise excersize, int percentDecrease) {
int formula, counterOfPercent = 100;
for (int i = 0; i < excersize.approaches; i++, counterOfPercent -= percentDecrease) {
formula = ((excersize.repeats * counterOfPercent) / 100);
System.out.print(formula + " ");
}
System.out.println();
}
private void printKettleExcercise() {
final int KettlePercentDecrease = 20;
printExcercise(kettleExcercise, KettlePercentDecrease);
}
private void printBarExcercise() {
final int BarPercentDecrease = 18;
printExcercise(barExcercise, BarPercentDecrease);
}
private void printKettleTable() throws IOException {
final int kettleApproaches = kettleExcercise.approaches;
System.out.println();
System.out.print("1. Свинги с гирей\t" + kettleApproaches + " x "); printKettleExcercise();
System.out.print("2. Подъём на грудь\t" + kettleApproaches + " x "); printKettleExcercise();
System.out.print("3. Приседания\t\t" + kettleApproaches + " x "); printKettleExcercise();
System.out.print("4. Рывок гири\t\t" + kettleApproaches + " x "); printKettleExcercise();
System.out.print("5. Толчок гири\t\t" + kettleApproaches + " x "); printKettleExcercise();
showMenu();
}
private void printBarTable() throws IOException {
final int barApproaches = barExcercise.approaches;
System.out.println();
System.out.print("1. Подтягивания широким хватом\t" + barApproaches + " x ");
printBarExcercise();
System.out.print("2. Отжимания на брусьях\t\t\t" + barApproaches + " x ");
printBarExcercise();
System.out.print("3. Жим гирь на бицепс\t\t\t" + barApproaches + " x ");
printBarExcercise();
System.out.print("4. Жим лёжа на грудь\t\t\t" + barApproaches + " x ");
printBarExcercise();
System.out.print("5. Приседания\t\t\t\t\t" + barApproaches + " x ");
printBarExcercise();
showMenu();
}
}
public class Main {
public static void main(String[] args) throws IOException {
TrainCalculator trainCalc = new TrainCalculator();
while (trainCalc.showMenu()) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment