-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReturnBooks.cs
124 lines (114 loc) · 4.72 KB
/
ReturnBooks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// DON-CODE
using Terminal.Gui;
using System;
using System.Text.RegularExpressions;
public class ReturnBooksWindow {
private bool IsNumeric(string input) {
return int.TryParse(input, out _);
}
private bool IsValidDateFormat(string date) {
DateTime parsedDate;
return DateTime.TryParseExact(date, "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out parsedDate);
}
public Window CreateReturnBooksWindow(Window leftWindow, String databasePath) {
string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
var createReturnBooksWindow = new Window() {
X = Pos.Right(leftWindow), // Position it to the right of the leftWindow
Y = 1, // Leave one row for the top-level menu
Width = Dim.Fill(), // Fill the remaining space
Height = Dim.Fill()
};
var mainTitleLabel = new Label(" ___ _ ___ \n | _ \\___| |_ _ _ _ _ _ _ | _ ) \n | / -_| _| || | '_| ' \\ | _ \\_ \n |_|_\\___|\\__|\\_,_|_| |_||_| |___(_)") {
X = 6,
Y = 1,
Width = Dim.Fill(),
Height = 1,
LayoutStyle = LayoutStyle.Computed, // Use computed layout
};
var transactionIDLabel = new Label("Transaction ID") {
X = 2,
Y = Pos.Bottom(mainTitleLabel) + 2,
Width = 17
};
var transactionIDInput = new TextField ("") {
X = Pos.Right(transactionIDLabel) + 1,
Y = Pos.Top(transactionIDLabel),
Width = 30,
Height = 1
};
var returnDateLabel = new Label("Return Date") {
X = Pos.Left(transactionIDLabel),
Y = Pos.Bottom(transactionIDLabel) + 1,
Width = 17
};
var returnDateInput = new TextField ("") {
X = Pos.Right(returnDateLabel) + 1,
Y = Pos.Top(returnDateLabel),
Width = 30,
Height = 1
};
var explainLabel = new Label("*[Date format: YYYY-MM-DD]") {
X = Pos.Left(returnDateLabel) + 10,
Y = Pos.Bottom(returnDateLabel) + 1,
};
var clearButton = new Button("Clear") {
X = Pos.Left(transactionIDLabel) + 29,
Y = Pos.Bottom(returnDateLabel) + 3,
};
clearButton.Clicked += () => {
// Clear the text fields when the Clear button is clicked
transactionIDInput.Text = "";
returnDateInput.Text = "";
};
var okButton = new Button(" Ok ") {
X = Pos.Right(clearButton) + 1,
Y = Pos.Bottom(returnDateLabel) + 3,
};
okButton.Clicked += () => {
string transactionID = transactionIDInput.Text.ToString();
string returnDate = returnDateInput.Text.ToString();
// Check if any of the required fields are empty
if (string.IsNullOrWhiteSpace(transactionID) || string.IsNullOrWhiteSpace(returnDate)) {
MessageBox.ErrorQuery("Error", "All required fields must be filled in.", "OK");
return; // Exit the event handler
}
// Check if Transaction ID contains only numbers
if (!IsNumeric(transactionID)) {
MessageBox.ErrorQuery("Error", "Transaction ID must contain only numbers.", "OK");
return; // Exit the event handler
}
// Check if Return Date is in the format "YYYY-MM-DD"
if (!IsValidDateFormat(returnDate)) {
MessageBox.ErrorQuery("Error", "Return Date must be in the format 'YYYY-MM-DD'.", "OK");
return; // Exit the event handler
}
// Convert strings to integers and parse dates
int parsedTransactionID;
if (!int.TryParse(transactionID, out parsedTransactionID)) {
MessageBox.ErrorQuery("Error", "Failed to parse Transaction ID.", "OK");
return; // Exit the event handler
}
DateTime parsedReturnDate;
if (!DateTime.TryParse(returnDate, out parsedReturnDate)) {
MessageBox.ErrorQuery("Error", "Failed to parse Return Date.", "OK");
return; // Exit the event handler
}
// Call the ReturnBook function
var (success, message) = new LibraryFunctions(databasePath).ReturnBook(parsedTransactionID, parsedReturnDate);
if (success) {
MessageBox.Query("Return Book Result", "Book returned successfully.", "OK");
} else {
MessageBox.ErrorQuery("Error", message, "OK");
}
};
transactionIDLabel.FocusFirst();
createReturnBooksWindow.Add(
mainTitleLabel,
transactionIDLabel, transactionIDInput,
returnDateLabel, returnDateInput,
explainLabel,
clearButton, okButton
);
return createReturnBooksWindow;
}
}