Files
csharp-kvasnickiy/works/lab-02/Program.cs
T
2026-09-23 23:25:59 +03:00

240 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using HelpMethods;
namespace Test
{
internal class Program
{
static void Main(string[] args)
{
short initialBalance = 1500;
PokemonLeveling();
BuyPotion(initialBalance);
ShowCondition();
TUI();
}
/// <summary>
/// Метод для задания №1.
/// </summary>
static void PokemonLeveling()
{
const sbyte MARGIN_1 = -16;
short pokemonAttack = SetPokemonAttack();
byte ernedLevels = LevelsRequest();
string message =
$@"=== Pokemon leveling ===
{"Initial attack:",MARGIN_1}{pokemonAttack}
{"Earned levels:",MARGIN_1}{ernedLevels}
Final attack: {pokemonAttack + ernedLevels * 5}
------------------------
";
Console.WriteLine(message);
SystemHelp.Waiting(true);
static short SetPokemonAttack()
{
const short
MAX_ATTACK = 250
, MIN_ATTACK = 1;
string ERROR = $"The attack power must be between {MIN_ATTACK} and {MAX_ATTACK}.";
while (true)
{
try
{
Console.Write("Set the attack strength.\nAttack strength: ");
short attack = short.Parse(Console.ReadLine());
if (attack < MIN_ATTACK || attack > MAX_ATTACK)
throw new Exception(ERROR);
Console.Clear();
return attack;
}
catch (Exception e) { SystemHelp.PrintError(e); }
}
}
static byte LevelsRequest()
{
const short MAX_COUNT = 5;
string ERROR = $"The number of levels cannot exceed {MAX_COUNT}.";
while (true)
{
try
{
Console.Write("How many levels have been earned?\nCount: ");
byte lvl = byte.Parse(Console.ReadLine());
if (lvl > MAX_COUNT)
throw new Exception(ERROR);
Console.Clear();
return lvl;
}
catch (Exception e) { SystemHelp.PrintError(e); }
}
}
}
/// <summary>
/// Метод для задания №2.
/// </summary>
/// <param name="balance"></param>
static void BuyPotion(short balance)
{
const byte COST_POTION = 200;
byte countPotions = GetCountPotions(balance, COST_POTION);
PrintCheque(balance, countPotions, COST_POTION);
SystemHelp.Waiting(true);
static byte GetCountPotions(short balance, byte costPotion)
{
const string ERROR = "The purchase amount cannot exceed your balance.";
string message = $"Your balance: {balance}\nHow many potions do you want to buy?\nCount: ";
while (true)
{
try
{
Console.Write(message);
byte count = byte.Parse(Console.ReadLine());
if (balance - costPotion * count < 0)
throw new Exception(ERROR);
return count;
}
catch (Exception e) { SystemHelp.PrintError(e); }
}
}
static short PrintCheque(short balance, byte countPotions, byte costPotion)
{
const sbyte MARGIN_1 = -10;
string cheque =
$@"
====== Cheque ======
{"Balance:",MARGIN_1}{balance}
{"Purchase:",MARGIN_1}{costPotion} X{countPotions}
Amount to be paid:{costPotion * countPotions}
Remains:{balance - costPotion * countPotions}
---------------------
";
Console.Write(cheque);
return (short)(balance - costPotion * countPotions);
}
}
/// <summary>
/// Метод для задания №3.
/// </summary>
static void ShowCondition()
{
const string
MESSAGE_1 = "Pokemon lost consciousness!"
, MESSAGE_2 = "The Pokemon is in critical condition!"
, MESSAGE_3 = "The Pokemon is wounded!"
, MESSAGE_4 = "The Pokemon has full HP!";
short pokemonHp = GetPokemonHp();
if (pokemonHp == 0)
Console.WriteLine(MESSAGE_1);
else if (pokemonHp < 21)
Console.WriteLine(MESSAGE_2);
else if (pokemonHp < 51)
Console.WriteLine(MESSAGE_3);
else
Console.WriteLine(MESSAGE_4);
StringHelp.Br();
SystemHelp.Waiting(true);
static short GetPokemonHp()
{
const short
MIN_HP = 0
, MAX_HP = 5000;
string ERROR = $"Hp should be between {MIN_HP} and {MAX_HP}";
while (true)
{
try
{
Console.Write("Enter the number of HP.\nHP: ");
short hp = short.Parse(Console.ReadLine());
if (hp <MIN_HP || hp > MAX_HP)
throw new Exception(ERROR);
return hp;
}
catch (Exception e) { SystemHelp.PrintError(e); }
}
}
}
/// <summary>
/// Метод для задания №4.
/// </summary>
static void TUI()
{
const string hotBar =
@"
====== Actions ======
Attack [1]
Backpack [2]
Change the Pokemon [3]
Escape [4]
-----------------------
";
Console.Write(hotBar);
ModuleAction(Console.ReadLine() ?? String.Empty);
StringHelp.Br();
static void ModuleAction(string action)
{
const string
ACTION_1 = "The Pokemon delivers a quick strike!"
, ACTION_2 = "You open the backpack!"
, ACTION_3 = "You choose other Pokemon!"
, ACTION_4 = "You escape!"
, DEFAULT_ACTION = "You didn’t do anything!";
switch (action)
{
case "1": Console.WriteLine(ACTION_1); break;
case "2": Console.WriteLine(ACTION_2); break;
case "3": Console.WriteLine(ACTION_3); break;
case "4": Console.WriteLine(ACTION_4); break;
default: Console.WriteLine(DEFAULT_ACTION); break;
}
}
}
}
}