
public class Information
{
public string Name { get; init; }
public string Family { get; init; }
public int Age { get; init; }
}
public class CarInformation
{
public string CarName { get; init; }
public string CarModel { get; init; }
public int CarSpeed { get; init; }
}
public class Tools
{
// class Methods
private int _speed;
public void SetSpeed(int speed)
{
if (speed > 120)
{
MessageBox.Show("Speed is to high");
speed = 0;
}
_speed = speed;
}
public int GetSpeed()
{
return _speed;
}
private int _age;
public void SetAge(int age)
{
if (age < 20 || age > 100)
{
MessageBox.Show("Your Age is not valid");
age = 0;
}
_age = age;
}
public int GetAge()
{
return _age;
}
\\ Code Behind
public partial class Form1 : Form
{
private int _age;
private int _speed;
Tools tools = new Tools();
public Form1()
{
InitializeComponent();
}
private void SetNumber()
{
try
{
if (txtAge.Text != "" )
{
_age = Convert.ToInt32(txtAge.Text);
}
else
{
MessageBox.Show("Please Complate Age Field");
}
if (txtCarSpeed.Text!="")
{
_speed = Convert.ToInt32(txtCarSpeed.Text);
}
else
{
MessageBox.Show("Please Complate Speed Field");
}
}
catch (FormatException)
{
MessageBox.Show("Please Enter Just Number");
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void btnEnter_Click(object sender, EventArgs e)
{
try
{
SetNumber();
// Information
Information information = new Information()
{
Name = txtName.Text,
Family = txtFamily.Text,
Age = _age
};
tools.SetAge(_age);
MessageBox.Show($"Name = {information.Name} Family = {information.Family} Age = " +
$"{tools.GetAge()}");
//Car information
CarInformation car = new CarInformation()
{
CarName = txtCarName.Text,
CarModel = txtCarModel.Text,
CarSpeed = _speed
};
tools.SetSpeed(_speed);
MessageBox.Show($"Car Name = {car.CarName} Car Model = {car.CarModel}" +
$" Car Speed = {tools.GetSpeed()}");
}
catch (Exception)
{
MessageBox.Show("Error");
}
}
سلام استاد عزیز وقتتون بخیر
استاد این تمرین رو بنده چند بار انجام دادم از روش های مختلفی که گفتید به اتمام سوندم و دوباره با روش های دیگه بازنویسیشون کردم اگر جایی اشتباه تایپی بود عذر میخوام.
سلام
بسیار عالی بود ، لذت بردم
احسنت
مچکرم از توجهتون استاد بزرگوار