Сделана логика для формы Tour, исправлена опечатка в название интерфейса ITourRepo и все связанное с ней

This commit is contained in:
Tonb73 2024-11-09 00:22:49 +04:00
parent 2043b7962e
commit 3a98780b1e
7 changed files with 253 additions and 39 deletions

View File

@ -13,9 +13,10 @@ public class Tour
public string Destination { get; private set; } = string.Empty;
public string Departure { get; private set; } = string.Empty;
public DateTime DepartureDate { get; private set; }
public int Duration { get; private set; }
public int Cost { get; private set; }
public static Tour CreateEntity(int id, string destination,
DateTime date, string departure, int cost)
DateTime date, string departure, int cost, int duration)
{
return new Tour
{
@ -23,7 +24,8 @@ public class Tour
Destination = destination,
Departure = departure,
DepartureDate = date,
Cost = cost
Cost = cost,
Duration = duration
};
}
}

View File

@ -57,7 +57,7 @@ public partial class FormClient : Form
try
{
if(string.IsNullOrWhiteSpace(textBoxName.Text) ||
string.IsNullOrWhiteSpace(textBoxNumber.Text))
string.IsNullOrWhiteSpace(textBoxNumber.Text) || comboBoxSocialStatus.SelectedIndex < 1)
{
throw new Exception("Имеются незаполненные поля");
}

View File

@ -28,12 +28,156 @@
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "FormTour";
dateTimePickerDepartureDate = new DateTimePicker();
textBoxDestination = new TextBox();
labelDestination = new Label();
labelDeparture = new Label();
textBoxDuration = new TextBox();
labelPrice = new Label();
textBoxDeparture = new TextBox();
labelDate = new Label();
labelDuration = new Label();
textBoxPrice = new TextBox();
buttonSave = new Button();
buttonCancel = new Button();
SuspendLayout();
//
// dateTimePickerDepartureDate
//
dateTimePickerDepartureDate.Location = new Point(144, 114);
dateTimePickerDepartureDate.Name = "dateTimePickerDepartureDate";
dateTimePickerDepartureDate.Size = new Size(226, 23);
dateTimePickerDepartureDate.TabIndex = 2;
//
// textBoxDestination
//
textBoxDestination.Location = new Point(144, 32);
textBoxDestination.Name = "textBoxDestination";
textBoxDestination.Size = new Size(226, 23);
textBoxDestination.TabIndex = 4;
//
// labelDestination
//
labelDestination.AutoSize = true;
labelDestination.Location = new Point(38, 32);
labelDestination.Name = "labelDestination";
labelDestination.Size = new Size(100, 15);
labelDestination.TabIndex = 3;
labelDestination.Text = "Место прибытия";
//
// labelDeparture
//
labelDeparture.AutoSize = true;
labelDeparture.Location = new Point(38, 78);
labelDeparture.Name = "labelDeparture";
labelDeparture.Size = new Size(91, 15);
labelDeparture.TabIndex = 5;
labelDeparture.Text = "Место отбытия";
//
// textBoxDuration
//
textBoxDuration.Location = new Point(144, 162);
textBoxDuration.Name = "textBoxDuration";
textBoxDuration.Size = new Size(226, 23);
textBoxDuration.TabIndex = 7;
//
// labelPrice
//
labelPrice.AutoSize = true;
labelPrice.Location = new Point(35, 205);
labelPrice.Name = "labelPrice";
labelPrice.Size = new Size(94, 15);
labelPrice.TabIndex = 6;
labelPrice.Text = "Стоимость тура";
//
// textBoxDeparture
//
textBoxDeparture.Location = new Point(144, 75);
textBoxDeparture.Name = "textBoxDeparture";
textBoxDeparture.Size = new Size(226, 23);
textBoxDeparture.TabIndex = 8;
//
// labelDate
//
labelDate.AutoSize = true;
labelDate.Location = new Point(38, 120);
labelDate.Name = "labelDate";
labelDate.Size = new Size(84, 15);
labelDate.TabIndex = 9;
labelDate.Text = "Дата отбытия";
//
// labelDuration
//
labelDuration.AutoSize = true;
labelDuration.Location = new Point(38, 162);
labelDuration.Name = "labelDuration";
labelDuration.Size = new Size(84, 15);
labelDuration.TabIndex = 10;
labelDuration.Text = "Дата отбытия";
//
// textBoxPrice
//
textBoxPrice.Location = new Point(144, 205);
textBoxPrice.Name = "textBoxPrice";
textBoxPrice.Size = new Size(226, 23);
textBoxPrice.TabIndex = 11;
//
// buttonSave
//
buttonSave.Location = new Point(63, 251);
buttonSave.Name = "buttonSave";
buttonSave.Size = new Size(75, 23);
buttonSave.TabIndex = 12;
buttonSave.Text = "button1";
buttonSave.UseVisualStyleBackColor = true;
buttonSave.Click += buttonSave_Click;
//
// buttonCancel
//
buttonCancel.Location = new Point(256, 251);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(75, 23);
buttonCancel.TabIndex = 13;
buttonCancel.Text = "button2";
buttonCancel.UseVisualStyleBackColor = true;
buttonCancel.Click += buttonCancel_Click;
//
// FormTour
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(414, 301);
Controls.Add(buttonCancel);
Controls.Add(buttonSave);
Controls.Add(textBoxPrice);
Controls.Add(labelDuration);
Controls.Add(labelDate);
Controls.Add(textBoxDeparture);
Controls.Add(textBoxDuration);
Controls.Add(labelPrice);
Controls.Add(labelDeparture);
Controls.Add(textBoxDestination);
Controls.Add(labelDestination);
Controls.Add(dateTimePickerDepartureDate);
Name = "FormTour";
StartPosition = FormStartPosition.CenterParent;
Text = "Тур";
ResumeLayout(false);
PerformLayout();
}
#endregion
private DateTimePicker dateTimePickerDepartureDate;
private TextBox textBoxDestination;
private Label labelDestination;
private Label labelDeparture;
private TextBox textBoxDuration;
private Label labelPrice;
private TextBox textBoxDeparture;
private Label labelDate;
private Label labelDuration;
private TextBox textBoxPrice;
private Button buttonSave;
private Button buttonCancel;
}
}

View File

@ -1,4 +1,7 @@
using System;
using ProjectTourAgency.Enities;
using ProjectTourAgency.Implementations;
using ProjectTourAgency.Repositories;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -12,9 +15,74 @@ namespace ProjectTourAgency.Forms
{
public partial class FormTour : Form
{
public FormTour()
private readonly ITourRepository _tourRepository;
private int? _tourId;
public int Id
{
set
{
try
{
var tour = _tourRepository.ReadTourById(value);
if (tour == null)
{
throw new InvalidOperationException(nameof(tour));
}
textBoxDestination.Text = tour.Destination;
textBoxDeparture.Text = tour.Departure;
dateTimePickerDepartureDate.Text = tour.DepartureDate.ToString();
textBoxDuration.Text = tour.Duration.ToString();
textBoxPrice.Text = tour.Cost.ToString();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"Ошибка при получении ланных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
public FormTour(ITourRepository tourRepository)
{
InitializeComponent();
_tourRepository = tourRepository ??
throw new ArgumentNullException(nameof(tourRepository));
}
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(textBoxDestination.Text) ||
string.IsNullOrWhiteSpace(textBoxDeparture.Text) || string.IsNullOrWhiteSpace(textBoxDuration.Text)
|| string.IsNullOrWhiteSpace(textBoxPrice.Text) || string.IsNullOrWhiteSpace(dateTimePickerDepartureDate.Text))
{
throw new Exception("Имеются незаполненные поля");
}
if (_tourId.HasValue)
{
_tourRepository.UpdateTour(CreateTour(_tourId.Value));
}
else
{
_tourRepository.CreateTour(CreateTour(_tourId.Value));
}
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e) => Close();
private Tour CreateTour(int id) => Tour.CreateEntity(id, textBoxDestination.Text, dateTimePickerDepartureDate,
textBoxDeparture.Text, int.Parse(textBoxPrice.Text),
int.Parse(textBoxDuration.Text));
}
}

View File

@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->

View File

@ -8,9 +8,9 @@ using System.Threading.Tasks;
namespace ProjectTourAgency.Implementations;
public class TourRepositiry : ITourRepositiry
public class TourRepositiry : ITourRepository
{
public void CreateTourt(Tour tour)
public void CreateTour(Tour tour)
{
}
@ -21,7 +21,7 @@ public class TourRepositiry : ITourRepositiry
public Tour ReadTourById(int id)
{
return Tour.CreateEntity(0, string.Empty, DateTime.Now, string.Empty, 0);
return Tour.CreateEntity(0, string.Empty, DateTime.Now, string.Empty, 0,0);
}
public IEnumerable<Tour> ReadTours()

View File

@ -7,13 +7,13 @@ using System.Threading.Tasks;
namespace ProjectTourAgency.Repositories;
public interface ITourRepositiry
public interface ITourRepository
{
IEnumerable<Tour> ReadTours();
Tour ReadTourById(int id);
void CreateTourt(Tour tour);
void CreateTour(Tour tour);
void UpdateTour(Tour tour);