final commit

This commit is contained in:
2026-04-04 13:38:14 +05:00
parent caccb77fe0
commit b17564f0de
9 changed files with 235 additions and 50 deletions

View File

@@ -15,6 +15,14 @@ namespace UP01Task2App.Models
public int SubscriptionNumber { get; set; }
public string Gender { get; set; }
public string ShortName
{
get
{
return $"{Surname} {Name[0]}.{Patronymic[0]}.";
}
}
public override string ToString()
{
return Surname + " " + Name + " " + Patronymic;

View File

@@ -8,7 +8,15 @@ namespace UP01Task2App.Models
{
public Client Client { get; set; }
public Trainer Trainer { get; set; }
DateOnly RecordDate { set; get; }
TimeOnly RecordTime { set; get; }
public DateOnly RecordDate { set; get; }
public TimeOnly RecordTime { set; get; }
public string RecordInfo
{
get
{
return RecordDate.ToString() + " в " + RecordTime.ToString() + ", " + Trainer.Speciality + ", " + Trainer.ShortName;
}
}
}
}

View File

@@ -69,7 +69,7 @@ namespace UP01Task2App.Services
public void SaveRecordDataToJson(List<Record> recordsList) => SaveToJson(recordPath, recordsList);
#if DEBUG
private void CreateTrainersJson()
public void CreateTrainersJson()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

View File

@@ -4,6 +4,7 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using UP01Task2App.Models;
namespace UP01Task2App.Services
{
@@ -27,6 +28,11 @@ namespace UP01Task2App.Services
if (obj.SelectedDate > DateTime.Now)
throw new ArgumentException($"В поле \"{fieldName}\" выбранная дата не может быть в будущем");
}
public static void ValidateDateNotInThePastTextBox(DatePicker obj, string fieldName)
{
if (obj.SelectedDate < DateTime.Now.Date)
throw new ArgumentException($"В поле \"{fieldName}\" выбранная дата не может быть в прошлом");
}
public static void CheckBirthdate(DatePicker obj)
{
if (obj.SelectedDate > DateTime.Today.AddYears(-14))
@@ -49,6 +55,13 @@ namespace UP01Task2App.Services
if (!Regex.IsMatch(obj.Text.Trim(), pattern))
throw new ArgumentException($"Надпись в поле \"{fieldName}\" имеет неверный формат \nВерный формат: Фамилия");
}
public static void ValidateTime(TextBox obj)
{
string pattern = @"^([01]\d|2[0-3]):[0-5]\d$";
if (!Regex.IsMatch(obj.Text.Trim(), pattern))
throw new ArgumentException($"Время введено в неверном формате, верный формат: hh:mm");
}
public static void ValidatePhoneNumber(TextBox obj, string fieldName)
{
string pattern = @"^(7|8)\d{10}$";
@@ -61,5 +74,22 @@ namespace UP01Task2App.Services
if (obj.SelectedValue == null)
throw new ArgumentException($"Выберите значение в поле \"{fieldName}\"");
}
public static void ValidateEightDigitLenght(TextBox obj, string fieldName)
{
if (obj.Text.Trim().Length != 8)
throw new ArgumentException($"Допустимая длинна значения поля \"{fieldName}\" 8 символов");
}
public static void ValibateClientIdentity(List<Client> ClientsList, TextBox obj)
{
int subscriptionNumber = int.Parse(obj.Text);
if (ClientsList.Where(a => a.SubscriptionNumber == subscriptionNumber) != null)
throw new ArgumentException($"Клииент с таким номером абонемента уже существует");
}
public static void ValidateTimeNotInPast(TimeOnly time)
{
if (time < TimeOnly.FromDateTime(DateTime.Now))
throw new ArgumentException($"Время не может быть в прошлом");
}
}
}

View File

@@ -10,7 +10,7 @@
<StackPanel Margin="10" HorizontalAlignment="Center">
<TextBlock Text="Клиент:" FontWeight="Bold"/>
<TextBlock Text="{Binding SelectedClient.LastName}" Margin="0,0,0,10" FontSize="16"/>
<TextBlock Text="{Binding InClient.ShortName}" Margin="0,0,0,10" FontSize="16"/>
<TextBlock Text="Тренировка"/>
<ComboBox Name="TrainingTextBox"
@@ -24,12 +24,17 @@
Style="{StaticResource ComboBoxStyle}"/>
<TextBlock Text="Дата"/>
<DatePicker SelectedDate="{Binding SelectedDate}"
<DatePicker Name="DatePicker"
Style="{StaticResource DatePickerStyle}"/>
<TextBlock Text="Время"/>
<TextBox Name="TimeTextBox"
Style="{StaticResource TextBoxStyle}"/>
<Button Content="Записать"
Margin="0,30,0,0"
Width="200"
IsDefault="True"
Style="{StaticResource ButtonStyle}" Click="Button_Click"/>
</StackPanel>

View File

@@ -5,6 +5,7 @@ using System.Security.Policy;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
@@ -26,10 +27,11 @@ namespace UP01Task2App.Windows
public Record OutputRecord { get; private set; }
public ObservableCollection<Trainer> Trainers { get; set; } = new ObservableCollection<Trainer>();
public ObservableCollection<string> TrainersSpeciality { get; set; } = new ObservableCollection<string>();
public Client InClient { get; set; }
public AppointmentWindow(List<Trainer> _trainersList, Client client)
{
TrainersList = _trainersList;
InClient = client;
foreach (var item in TrainersList)
{
@@ -38,26 +40,55 @@ namespace UP01Task2App.Windows
TrainersSpecialities = TrainersList
.Select(t => t.Speciality)
.Distinct()
.Distinct()
.ToList();
foreach (var item in TrainersSpecialities)
{
TrainersSpeciality.Add(item);
MessageBox.Show(item.ToString());
}
InitializeComponent();
this.DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ValidationHelper.ValidateNotNullComboBox(TrainingTextBox, "Тренировка");
ValidationHelper.ValidateNotNullComboBox(TrainerTextBox, "Тренировка");
try
{
ValidationHelper.ValidateNotNullComboBox(TrainingTextBox, "Тренировка");
ValidationHelper.ValidateNotNullComboBox(TrainerTextBox, "Тренер");
ValidationHelper.ValidateNotNullDatePicker(DatePicker, "Дата");
ValidationHelper.ValidateDateNotInThePastTextBox(DatePicker, "Дата");
ValidationHelper.ValidateTime(TimeTextBox);
DateTime dt = DatePicker.SelectedDate.Value;
DateOnly date = DateOnly.FromDateTime(dt);
TimeOnly time = TimeOnly.Parse(TimeTextBox.Text);
if(dt == DateTime.Now.Date)
ValidationHelper.ValidateTimeNotInPast(time);
OutputRecord = new Record
{
Client = InClient,
Trainer = (Trainer)TrainerTextBox.SelectedValue,
RecordDate = date,
RecordTime = time
};
this.DialogResult = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void TrainingTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

View File

@@ -10,27 +10,27 @@
<Grid>
<StackPanel Margin="10">
<TextBlock Text="Клиент:" FontWeight="Bold"/>
<TextBlock Text="{Binding SelectedClient.LastName}" Margin="0,0,0,10" FontSize="16"/>
<TextBlock Text="{Binding InClient.ShortName}" Margin="0,0,0,10" FontSize="16"/>
<TextBlock Text="Список записей" FontSize="18" Margin="0,0,0,10"/>
<ListBox ItemsSource="{Binding Appointments}"
SelectedItem="{Binding SelectedAppointment}"
<ListBox ItemsSource="{Binding ClientRecords}"
Height="200"
DisplayMemberPath="LastName"/>
Name="RecordsTextBox"
DisplayMemberPath="RecordInfo"/>
<StackPanel Orientation="Horizontal">
<Button Content="Назад"
Command="{Binding BackCommand}"
Name="BackButton"
Margin="10"
Width="200"
Style="{StaticResource ButtonStyle}"/>
Style="{StaticResource ButtonStyle}" Click="BackButton_Click"/>
<Button Content="Отменить запись"
Command="{Binding CancelAppointmentCommand}"
Name="CancelRecord"
Margin="10"
Width="200"
Style="{StaticResource ButtonStyle}"/>
Style="{StaticResource ButtonStyle}" Click="CancelRecord_Click"/>
</StackPanel>
</StackPanel>
</Grid>

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
@@ -9,6 +10,7 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using UP01Task2App.Models;
namespace UP01Task2App.Windows
{
@@ -17,9 +19,41 @@ namespace UP01Task2App.Windows
/// </summary>
public partial class ClientAppointmentList : Window
{
public ClientAppointmentList()
public Record RecordForDelete { get; set; }
public Client InClient { get; set; }
public ObservableCollection<Record> ClientRecords { get; set; } = new();
public ClientAppointmentList(List<Record> recordsList, Client inClient)
{
InitializeComponent();
InClient = inClient;
ClientRecords = new ObservableCollection<Record>(
recordsList.Where(a => a.Client.SubscriptionNumber == inClient.SubscriptionNumber));
this.DataContext = this;
}
private void BackButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
private void CancelRecord_Click(object sender, RoutedEventArgs e)
{
if (RecordsTextBox.SelectedIndex != -1)
{
RecordForDelete = (Record)RecordsTextBox.SelectedValue;
this.DialogResult = true;
this.Close();
}
else
{
MessageBox.Show("Выберите запись перед отменой");
}
}
}
}

View File

@@ -70,8 +70,9 @@ namespace UP01Task2App.Windows
ValidationHelper.ValidateNotNullTextBox(SubscriptionMumberTextBox, "Номер абонемента");
ValidationHelper.ValidateNumTextBox(SubscriptionMumberTextBox, "Номер абонемента");
ValidationHelper.ValidateIntRangeTextBox(SubscriptionMumberTextBox, "Номер абонемента");
ValidationHelper.ValidateEightDigitLenght(SubscriptionMumberTextBox, "Номер абонемента");
if (rbMan.IsChecked == null && rbWoman.IsChecked == null)
if (rbMan.IsChecked != true && rbWoman.IsChecked != true)
{
throw new ArgumentException("Выберите пол");
}
@@ -89,28 +90,49 @@ namespace UP01Task2App.Windows
{
if (ValidateFields())
{
ClientsList.Add(new Client
{
Surname = SurnameTextBox.Text,
Name = NameTextBox.Text,
Adress = AdressTextBox.Text,
BirthDate = BirthDateDatePicker.SelectedDate,
Patronymic = PatronymicTextBox.Text,
PhoneNumber = PhoneNumberTextBox.Text,
SubscriptionNumber = int.Parse(SubscriptionMumberTextBox.Text)
});
int subscriptionNumber = int.Parse(SubscriptionMumberTextBox.Text);
try
{
Json.SaveClientDataToJson(ClientsList);
MessageBox.Show("Данные сохранены");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
bool exists = ClientsList.Any(c =>
c.SubscriptionNumber == subscriptionNumber
);
UpdateClientUI();
if (!exists)
{
string gender = "";
if (rbMan.IsChecked == true)
gender = "М";
else
gender = "Ж";
ClientsList.Add(new Client
{
Surname = SurnameTextBox.Text,
Name = NameTextBox.Text,
Adress = AdressTextBox.Text,
BirthDate = BirthDateDatePicker.SelectedDate,
Patronymic = PatronymicTextBox.Text,
PhoneNumber = PhoneNumberTextBox.Text,
SubscriptionNumber = int.Parse(SubscriptionMumberTextBox.Text),
Gender = gender
});
try
{
Json.SaveClientDataToJson(ClientsList);
MessageBox.Show("Данные сохранены");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
UpdateClientUI();
}
else
{
MessageBox.Show("Клиент с таким абонементом уже существует");
}
}
}
@@ -132,6 +154,15 @@ namespace UP01Task2App.Windows
AdressTextBox.Text = client.Adress;
PhoneNumberTextBox.Text = client.PhoneNumber;
SubscriptionMumberTextBox.Text = client.SubscriptionNumber.ToString();
if (client.Gender == "М")
{
rbMan.IsChecked = true;
}
else
{
rbWoman.IsChecked = true;
}
}
}
catch { }
@@ -152,13 +183,15 @@ namespace UP01Task2App.Windows
ValidationHelper.ValidateNumTextBox(PhoneNumberTextBox, "Телефон");
ValidationHelper.ValidatePhoneNumber(PhoneNumberTextBox, "Телефон");
ClientsUI[ClientsListBox.SelectedIndex].PhoneNumber = PhoneNumberTextBox.Text;
ClientsUI[ClientsListBox.SelectedIndex].Adress = AdressTextBox.Text;
ClientsList[ClientsListBox.SelectedIndex].PhoneNumber = PhoneNumberTextBox.Text;
ClientsList[ClientsListBox.SelectedIndex].Adress = AdressTextBox.Text;
try
{
Json.SaveClientDataToJson(ClientsList);
MessageBox.Show("Данные сохранены");
UpdateClientUI();
}
catch (Exception ex)
{
@@ -174,20 +207,56 @@ namespace UP01Task2App.Windows
private void Button_Click_2(object sender, RoutedEventArgs e)
{
AppointmentWindow wnd = new(TrainersList, new Client { Name = "Лох"}); //Временное добавление клиента
bool? res = wnd.ShowDialog();
if (res == true)
if (ClientsListBox.SelectedIndex != -1)
{
RecordsList.Add(new Record()); //Временная заглушка, добавить результат из Dialog
AppointmentWindow wnd = new(TrainersList, (Client)ClientsListBox.SelectedValue);
bool? res = wnd.ShowDialog();
Json.SaveRecordDataToJson(RecordsList);
if (res == true)
{
var record = wnd.OutputRecord;
RecordsList.Add(record);
try
{
Json.SaveRecordDataToJson(RecordsList);
MessageBox.Show("Запись успешно добавлена");
}
catch
{
MessageBox.Show("Не удалось добавить запись");
}
}
}
else
MessageBox.Show("Выберите клиента перед записью");
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
//Json.CreateTrainersJson();
if (ClientsListBox.SelectedIndex != -1)
{
ClientAppointmentList wnd = new(RecordsList, (Client)ClientsListBox.SelectedValue);
bool? res = wnd.ShowDialog();
if (res == true)
{
try
{
RecordsList.Remove(wnd.RecordForDelete);
Json.SaveRecordDataToJson(RecordsList);
MessageBox.Show("Запись успешно удалена");
}
catch
{
MessageBox.Show("Не удалось сохранить данные");
}
}
}
else
MessageBox.Show("Выберите клиента перед просмотром");
}
}
}