From 306ac411b314bb6ae7ecb20d21a998d195a6a527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B2=E8=8F=AF?= <42814579+yunwah@users.noreply.github.com> Date: Wed, 13 Apr 2022 02:59:21 -0400 Subject: [PATCH] Further major refactoring of code This is another commit to mostly version the code. There have been a considerable number of changes and there is functionality that I am still determining whether it should lie within the manager or the model class itself. It makes the most sense to possibly add the "Update" or database manipulation functions on the models themselves. On the other hand, instead of creating and generating the ID in the model, the current design is to create the entry in the database first, get the last insert row ID and create a new and complete model that is returned back by the function. This allows us to leverage the autoincrement functionality of the database rather than trying to design a function and/ or make an additional call to the database. **NOTE: Code is non-functional due to some classes not having their errors resolved. --- OpheliasOasis/Managers/DatabaseManager.cs | 135 ++++++++++---------- OpheliasOasis/Managers/HotelManager.cs | 133 ++++++++++++++++++++ OpheliasOasis/Managers/ResTableManager.cs | 24 +--- OpheliasOasis/Managers/TxTableManager.cs | 1 - OpheliasOasis/Models/Guest.cs | 37 +++--- OpheliasOasis/Models/Reservation.cs | 29 ++--- OpheliasOasis/Models/Transaction.cs | 142 ++++++---------------- OpheliasOasis/OpheliasOasis.sqlite | 0 OpheliasOasis/Program.cs | 102 ++++++++++++++-- OpheliasOasis/Validation.cs | 49 ++++++++ 10 files changed, 397 insertions(+), 255 deletions(-) create mode 100644 OpheliasOasis/Managers/HotelManager.cs delete mode 100644 OpheliasOasis/OpheliasOasis.sqlite create mode 100644 OpheliasOasis/Validation.cs diff --git a/OpheliasOasis/Managers/DatabaseManager.cs b/OpheliasOasis/Managers/DatabaseManager.cs index 9ff5be9..253eca7 100644 --- a/OpheliasOasis/Managers/DatabaseManager.cs +++ b/OpheliasOasis/Managers/DatabaseManager.cs @@ -5,12 +5,11 @@ namespace Ophelias.Managers { internal class DatabaseManager : IDisposable { - internal SQLiteConnection con = new SQLiteConnection("OpheliasOasis.sqlite"); - internal SQLiteCommand? cur; + internal SQLiteConnection con = new SQLiteConnection("DataSource=database.sqlite3;Version=3;"); internal DatabaseManager() { - cur = new SQLiteCommand(con); + Connect(); } internal void Connect() { @@ -23,9 +22,6 @@ namespace Ophelias.Managers } internal void InitializeTables() { - if (cur == null) - return; - string tableCommands = @"CREATE TABLE IF NOT EXISTS [transactions] ( [ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, @@ -34,8 +30,6 @@ namespace Ophelias.Managers [Penalty] INTEGER NULL, [Multiplier] INTEGER NOT NULL, [RefundAmount] INTEGER NOT NULL, - [IsLate] BOOLEAN NOT NULL CHECK ([IsLate] IN (0,1)), - [PaidOff] BOOLEAN NOT NULL CHECK ([PaidOff] IN (0,1)), [PayBy] TEXT NOT NULL, [PaidOn] TEXT NULL); @@ -61,35 +55,40 @@ namespace Ophelias.Managers [ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, [Fname] TEXT NOT NULL, [Lname] TEXT NOT NULL, - [Email] TEXT NULL, - [Phone] TEXT NULL, + [Email] TEXT NOT NULL, [CreditCard] TEXT NULL, - [CCV] TEXT NULL, - [CCExpiration] TEXT NULL); + [Expiration] TEXT NULL, + [CCV] TEXT NULL); CREATE TABLE IF NOT EXISTS [rooms] ( - [ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT); + [ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + [Occupied] BOOLEAN NOT NULL CHECK ([Occupied] IN (0,1))); CREATE TABLE IF NOT EXISTS [rates] ( [ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, [Rate] INTEGER NOT NULL, [DateSet] TEXT NOT NULL);"; - cur.CommandText = tableCommands; - cur.ExecuteNonQuery(); + using (SQLiteCommand cmd = con.CreateCommand()) + { + cmd.CommandText = tableCommands; + cmd.ExecuteNonQuery(); + } } internal void InitializeRoomsTable() { - if (cur == null) - return; + using (SQLiteCommand cmd = con.CreateCommand()) + { + for (int i = 1; i < 46; i++) + { + cmd.CommandText = $"INSERT INTO ROOMS (Occupied) VALUES (0);"; + cmd.ExecuteNonQuery(); + } + } // Initialize Rooms - for (int i = 1; i < 46; i++) - { - cur.CommandText = $"INSERT INTO ROOMS (ID) VALUES ({i});"; - cur.ExecuteNonQuery(); - } + } public void Dispose() { @@ -99,66 +98,33 @@ namespace Ophelias.Managers internal static class DatabaseFunctions { - internal static int GetLastId(SQLiteCommand cmd, string tableName) - { - if (cmd == null) - throw new NotImplementedException(); - cmd.CommandText = $"SELECT SEQ FROM sqlite_sequence WHERE name=\"{tableName}\";"; - cmd.ExecuteNonQuery(); - using (SQLiteDataReader reader = cmd.ExecuteReader()) - { - reader.Read(); - return reader.GetInt32(0); - } - } - internal static int GetThirtyDayOccupancy(SQLiteCommand cmd, DateTime start) - { - if (cmd == null) - throw new NotImplementedException(); - - cmd.CommandText = $@"SELECT COUNT(*) - FROM reservations - WHERE StartDate => date({start.ToString("yyyy-MM-dd")}) AND EndDate <= date({start.AddDays(30).ToString("yyyy-MM-dd")});"; - cmd.ExecuteNonQuery(); - int thirtyDayOcc; - using (SQLiteDataReader reader = cmd.ExecuteReader()) - { - reader.Read(); - thirtyDayOcc = reader.GetInt32(0); - } - return thirtyDayOcc; - } } internal static class QueryBuilder { internal static string? UpdateTransaction(int id, - double? rate = null, double? owed = null, double? penalty = null, - double? multiplier = null, double? refund = null, bool? isLate = null, - bool? paidOff = null, DateTime? paidBy = null, DateTime? paidOn = null) + double? Rate = null, double? Owed = null, double? Penalty = null, + double? Multiplier = null, double? Refund = null, + DateTime? PayBy = null, DateTime? PaidOn = null) { List queryComponents = new List(); string query = "UPDATE transactions SET"; - if (rate.HasValue) - queryComponents.Add($"Rate = {rate}"); - if (owed.HasValue) - queryComponents.Add($"Owed = {owed}"); - if (penalty.HasValue) - queryComponents.Add($"Penalty = {penalty}"); - if (multiplier.HasValue) - queryComponents.Add($"Multiplier = {multiplier}"); - if (refund.HasValue) - queryComponents.Add($"Refund = {refund}"); - if (isLate.HasValue) - queryComponents.Add($"IsLate = {Convert.ToInt32(isLate)}"); - if (paidOff.HasValue) - queryComponents.Add($"PaidOff = {Convert.ToInt32(paidOff)}"); - if (paidBy.HasValue) - queryComponents.Add($"PaidBy = {paidBy.Value.ToString("yyyy-MM-dd")}"); - if (paidOn.HasValue) - queryComponents.Add($"PaidOn = {paidOn.Value.ToString("yyyy-MM-dd")}"); + if (Rate.HasValue) + queryComponents.Add($"Rate = {Rate}"); + if (Owed.HasValue) + queryComponents.Add($"Owed = {Owed}"); + if (Penalty.HasValue) + queryComponents.Add($"Penalty = {Penalty}"); + if (Multiplier.HasValue) + queryComponents.Add($"Multiplier = {Multiplier}"); + if (Refund.HasValue) + queryComponents.Add($"Refund = {Refund}"); + if (PayBy.HasValue) + queryComponents.Add($"PaidBy = {PayBy.Value.ToString("yyyy-MM-dd")}"); + if (PaidOn.HasValue) + queryComponents.Add($"PaidOn = {PaidOn.Value.ToString("yyyy-MM-dd")}"); if (queryComponents.Count == 0) query += " " + string.Join(", ", queryComponents) + " " + $"WHERE ID = {id};"; @@ -208,5 +174,30 @@ namespace Ophelias.Managers return query; } + + internal static string CreateTransaction(double Rate, double Owed, double Penalty, + double Multiplier, double Refund, bool isLate, + bool PaidOff, DateTime PaidBy, DateTime? PaidOn = null) + { + + return @$"INSERT INTO transactions (Rate, Owed, Penalty, Multiplier, Refund, IsLate, PaidOff, PaidBy, PaidOn) + VALUES ({Rate}, {Owed}, {Penalty}, {Multiplier}, {Refund}, {isLate}, {PaidOff}, {PaidBy}, {PaidOn});"; + } + + internal static string CreateReservation(int roomid, int guestid, int transactionid, bool isnoshow, ReservationType type, + ReservationStatus status, DateTime creationdate, DateTime startdate, DateTime enddate, + DateTime checkin, DateTime checkout, DateTime datechanged) + { + return @$"INSERT INTO reservations (RoomID, GuestID, TransactionID, IsNoShow, Type, Status, CreationDate, StartDate, EndDate, CheckIn, CheckOut, DateChanged) + VALUES ({roomid}, {guestid}, {transactionid}, {isnoshow}, {status}, {creationdate}, {startdate}, {enddate}, {checkin}, {checkout}, {datechanged});"; + } + + internal static string CreateGuest(string FirstName, string LastName, string Email, string? CreditCard, string? Expiration, string? CCV) + { + if (CreditCard != null && Expiration != null && CCV != null) + return $@"INSERT INTO guests (Fname, Lname, Email) VALUES ({FirstName}, {LastName}, {Email});"; + else + return $@"INSERT INTO guests (Fname, Lname, Email, CreditCard, Expiration, CCV) VALUES ({FirstName}, {LastName}, {Email}, {CreditCard}, {Expiration}, {CCV});"; + } } } diff --git a/OpheliasOasis/Managers/HotelManager.cs b/OpheliasOasis/Managers/HotelManager.cs new file mode 100644 index 0000000..cdd393a --- /dev/null +++ b/OpheliasOasis/Managers/HotelManager.cs @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Ophelias.Models; +using System.Data.SQLite; + +namespace Ophelias.Managers +{ + internal static class HotelManager + { + internal static int GetLastId(string tableName) + { + int lastId = 0; + using (DatabaseManager manager = new DatabaseManager()) + { + using (SQLiteCommand cmd = manager.con.CreateCommand()) + { + cmd.CommandText = $"SELECT SEQ FROM sqlite_sequence WHERE name=\"{tableName}\";"; + cmd.ExecuteNonQuery(); + using (SQLiteDataReader reader = cmd.ExecuteReader()) + { + reader.Read(); + lastId = reader.GetInt32(0); + } + } + } + return lastId; + } + internal static int GetThirtyDayOccupancy(DateTime start) + { + int thirtyDayOcc; + using (DatabaseManager Manager = new DatabaseManager()) + { + using (SQLiteCommand cmd = Manager.con.CreateCommand()) { + cmd.CommandText = $@"SELECT COUNT(*) + FROM reservations + WHERE StartDate => date({start.ToString("yyyy-MM-dd")}) + AND EndDate <= date({start.AddDays(30).ToString("yyyy-MM-dd")});"; + cmd.ExecuteNonQuery(); + using (SQLiteDataReader reader = cmd.ExecuteReader()) + { + reader.Read(); + thirtyDayOcc = reader.GetInt32(0); + } + } + } + + return thirtyDayOcc; + } + internal static int CreateReservation(int roomid, int guestid, int transactionid, bool isnoshow, ReservationType type, + ReservationStatus status, DateTime creationdate, DateTime startdate, DateTime enddate, + DateTime checkin, DateTime checkout, DateTime datechanged) + { + int id; + using (DatabaseManager Manager = new DatabaseManager()) + { + using (SQLiteCommand cmd = Manager.con.CreateCommand()) + { + cmd.CommandText = QueryBuilder.CreateReservation(roomid, guestid, transactionid, isnoshow, type, + status, creationdate, startdate, enddate, + checkin, checkout, datechanged); + cmd.ExecuteNonQuery(); + } + id = (int)Manager.con.LastInsertRowId; + } + return id; + } + internal static void ChangeReservationDates(Reservation r, Transaction t, DateTime start, DateTime end) + { + using (DatabaseManager Manager = new DatabaseManager()) + { + using (SQLiteCommand cmd = Manager.con.CreateCommand()) + { + string? query = QueryBuilder.UpdateReservation(id: r.Id, startdate: start, enddate: end, datechanged: DateTime.Now); + + if (query == null) + throw new Exception(); + + query += QueryBuilder.UpdateTransaction(id: r.TransactionId, Multiplier: 1.1); + cmd.CommandText = query; + cmd.ExecuteNonQuery(); + } + } + } + internal static Transaction CreateTransaction(double Rate, double Owed, double Penalty, + double Multiplier, double Refund, bool isLate, + bool PaidOff, DateTime PaidBy, DateTime? PaidOn = null) + { + int Id; + using (DatabaseManager Manager = new DatabaseManager()) + { + using (SQLiteCommand cmd = Manager.con.CreateCommand()) + { + cmd.CommandText = QueryBuilder.CreateTransaction(Rate, Owed, Penalty, Multiplier, Refund, isLate, PaidOff, PaidBy, PaidOn); + cmd.ExecuteNonQuery(); + } + Id = (int)Manager.con.LastInsertRowId; + } + return new Transaction(Id, Rate, Owed, Penalty, Multiplier, Refund, PaidBy, PaidOn); + } + internal static int CheckInGuest(DateTime CheckIn) + { + return 1; + } + internal static int CheckOutGuest(DateTime CheckOut) + { + return 1; + } + + internal static Guest CreateGuest(string FirstName, string LastName, string Email, string? CreditCard = null, string? Expiration = null, string? CCV = null) + { + string query = QueryBuilder.CreateGuest(FirstName, LastName, Email, CreditCard, Expiration, CCV); + + int Id; + using (DatabaseManager Manager = new DatabaseManager()) + { + using (SQLiteCommand cmd = Manager.con.CreateCommand()) + { + cmd.CommandText = QueryBuilder.CreateGuest(FirstName, LastName, Email, CreditCard, Expiration, CCV); + cmd.ExecuteNonQuery(); + } + Id = (int)Manager.con.LastInsertRowId; + } + + if (CreditCard != null && Expiration != null && CCV != null) + return new Guest(FirstName, LastName, Email, CreditCard, Expiration, CCV); + else + return new Guest(FirstName, LastName, Email); + } + } +} diff --git a/OpheliasOasis/Managers/ResTableManager.cs b/OpheliasOasis/Managers/ResTableManager.cs index 4a19832..16eb956 100644 --- a/OpheliasOasis/Managers/ResTableManager.cs +++ b/OpheliasOasis/Managers/ResTableManager.cs @@ -9,30 +9,8 @@ using System.Data.SQLite; namespace Ophelias.Managers { - internal class ReservationTableManager + internal static class ReservationTableManager { - DatabaseManager dbManager; - SQLiteCommand? cur; - internal ReservationTableManager(DatabaseManager manager) - { - dbManager = manager; - cur = manager.cur; - } - internal void ChangeReservationDates(Reservation r, DateTime start, DateTime end) - { - string? query = QueryBuilder.UpdateReservation(id: r.Id, startdate: start, enddate: end, datechanged: DateTime.Now); - - if (query == null) - throw new NotImplementedException(); - - query += QueryBuilder.UpdateTransaction(id: r.TransactionId, multiplier: 1.1); - - - QueryBuilder.UpdateTransaction(r.TransactionId); - - } - - } } diff --git a/OpheliasOasis/Managers/TxTableManager.cs b/OpheliasOasis/Managers/TxTableManager.cs index 32a06d6..27499f1 100644 --- a/OpheliasOasis/Managers/TxTableManager.cs +++ b/OpheliasOasis/Managers/TxTableManager.cs @@ -16,7 +16,6 @@ namespace Ophelias.Managers internal TransactionTableManager(DatabaseManager manager) { dbManager = manager; - cur = manager.cur; } } } diff --git a/OpheliasOasis/Models/Guest.cs b/OpheliasOasis/Models/Guest.cs index 58df59c..2fc8042 100644 --- a/OpheliasOasis/Models/Guest.cs +++ b/OpheliasOasis/Models/Guest.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Ophelias.Models; -using System.Net.Mail; namespace Ophelias.Models { @@ -14,34 +13,30 @@ namespace Ophelias.Models internal string FirstName; internal string LastName; internal string Email; - internal string PhoneNumber; internal string? CreditCard; internal string? CCV; - internal DateTime CreditCardExpiration; + internal string? Expiration; - internal Guest(int id, string fname, string lname, string email, string phone) + internal Guest(string FirstName, string LastName, string Email) { - Id = id; - FirstName = fname; - LastName = lname; - Email = email; - PhoneNumber = phone; + this.FirstName = FirstName; + this.LastName = LastName; + this.Email = Email; } - internal Guest(int id, string fname, string lname, string email, string phone, string cc, DateTime expiration, string ccv) + internal Guest(string FirstName, string LastName, string Email, string CreditCard, string Expiration, string CCV) { - Id = id; - FirstName = fname; - LastName = lname; - Email = email; - PhoneNumber = phone; - CreditCard = cc; - CreditCardExpiration = expiration; + this.FirstName = FirstName; + this.LastName = LastName; + this.Email = Email; + this.CreditCard = CreditCard; + this.Expiration = Expiration; + this.CCV = CCV; } - internal void SetCreditCardInformation(string cc, DateTime expiration, string ccv) + internal void SetCreditCardInformation(string CreditCard, string Expiration, string CCV) { - CreditCard = cc; - CreditCardExpiration = expiration; - CCV = ccv; + this.CreditCard = CreditCard; + this.Expiration = Expiration; + this.CCV = CCV; } } internal class GuestList diff --git a/OpheliasOasis/Models/Reservation.cs b/OpheliasOasis/Models/Reservation.cs index a3787ff..ba9c94b 100644 --- a/OpheliasOasis/Models/Reservation.cs +++ b/OpheliasOasis/Models/Reservation.cs @@ -1,4 +1,10 @@ -using Ophelias.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Ophelias.Models; +using Ophelias.Managers; namespace Ophelias.Models { @@ -43,18 +49,6 @@ namespace Ophelias.Models CheckOut = null; DateChanged = null; } - internal void ChangeReservation(ReservationType type, Transaction t, BaseRate b) - { - Status = ReservationStatus.Changed; - Type = type; - t.Penalize(this, b.Rate); - } - internal void CancelReservation(Transaction t) - { - Status = ReservationStatus.Cancelled; - t.Penalize(this); - - } } internal enum ReservationStatus { @@ -70,13 +64,4 @@ namespace Ophelias.Models Incentive, SixtyDayAdvance, } - internal class ReservationList - { - internal List Reservations; - - internal ReservationList() - { - Reservations = new List(); - } - } } diff --git a/OpheliasOasis/Models/Transaction.cs b/OpheliasOasis/Models/Transaction.cs index 44f9fc2..8f267d9 100644 --- a/OpheliasOasis/Models/Transaction.cs +++ b/OpheliasOasis/Models/Transaction.cs @@ -14,57 +14,28 @@ namespace Ophelias.Models internal int Id { get; set; } internal double Rate { get; set; } - internal double Paid { get; set; } internal double Owed { get; set; } - internal double? Penalty { get; set; } + internal double Penalty { get; set; } internal double Multiplier { get; set; } internal double RefundAmount { get; set; } - internal bool? Late { get; set; } - internal bool PaidOff { get; set; } internal DateTime PayBy { get; set; } - internal DateTime PaidOn { get; set; } + internal DateTime? PaidOn { get; set; } = null; - internal Transaction(int id, Reservation r, DateTime payby) + internal Transaction(int Id, double Rate, double Owed, double Penalty, + double Multiplier, double RefundAmount, + DateTime PayBy, DateTime? PaidOn = null) { - Id = id; - Paid = 0; - Owed = 0; - RefundAmount = 0; - PaidOff = false; - PayBy = SetPayByDate(r); - Multiplier = Fee(r.Type); + this.Id = Id; + this.Rate = Rate; + this.Owed = Owed; + this.Penalty = Penalty; + this.Multiplier = Multiplier; + this.RefundAmount = RefundAmount; + this.PayBy = PayBy; + this.PaidOn = PaidOn; } - private bool IsOverdue() - { - if (DateTime.Now > PayBy) - { - return true; - } - return false; - } - private DateTime SetPayByDate(Reservation r) - { - switch(r.Type) - { - case ReservationType.Conventional: return r.EndDate; - case ReservationType.Prepaid: return r.StartDate; - case ReservationType.Incentive: return r.EndDate; - case ReservationType.SixtyDayAdvance: return r.StartDate.AddDays(-30); - default: throw new NotImplementedException(); - } - } - private void SetChangeFees(ReservationType type, double rate) - { - switch (type) - { - case ReservationType.Conventional: return; - case ReservationType.Prepaid: SetFee(PenaltyMultipler); SetRate(rate); return; - case ReservationType.Incentive: return; - case ReservationType.SixtyDayAdvance: SetFee(PenaltyMultipler); SetRate(rate); return; - default: throw new NotImplementedException(); - } - } - private void CancellationHandler(ReservationType type) + + private void Cancellation(ReservationType type) { void SetRefund() { @@ -98,84 +69,43 @@ namespace Ophelias.Models } internal void Penalize(Reservation r, double rate) { - switch(r.Status) - { - case ReservationStatus.Changed: SetChangeFees(r.Type, rate); return; - default: throw new NotImplementedException(); - } - } - private double Fee(ReservationType type) - { - switch (type) - { - case ReservationType.Conventional: return 1.0; - case ReservationType.Prepaid: return 0.75; - case ReservationType.Incentive: return GetIncentiveRate(); - case ReservationType.SixtyDayAdvance: return 0.85; - default: throw new NotImplementedException(); - } - } - private double GetIncentiveRate() - { - return 0.80; - } - private void SetFee(double mult) - { - Multiplier = mult; - } - private void SetRate(double rate) - { - Rate = rate; + throw new NotImplementedException(); } internal void Pay(double amount) { - if (RefundAmount > -1) - { - return; - } - Paid += amount; - if (Paid == Owed) - { - Owed -= Paid; - PaidOn = DateTime.Now; - PaidOff = true; - } else if (Paid > Owed) - { - RefundAmount = Paid - Owed; - Owed = Owed - Paid + RefundAmount; - PaidOn = DateTime.Now; - PaidOff = true; - } else - { - Owed -= Paid; - PaidOn = DateTime.Now; - } + } } - internal static class TransactionFees + internal static class TransactionInfo { - static double ConventionalFee = 1.0; - static double PrepaidFee = 0.75; - static double IncentiveFee = OccupancyIncentive(); - static double SixtyDayFee = 0.85; + internal static double ConventionalFee = 1.0; + internal static double PrepaidFee = 0.75; + internal static double IncentiveFee = IncentiveRate(); + internal static double SixtyDayFee = 0.85; - private static double OccupancyIncentive() + private static double IncentiveRate() { int thirtyDayOcc; - using (DatabaseManager dbm = new DatabaseManager()) - { - if (dbm.cur == null) - throw new NotImplementedException(); - - thirtyDayOcc = DatabaseFunctions.GetThirtyDayOccupancy(dbm.cur, DateTime.Now); - } + thirtyDayOcc = HotelManager.GetThirtyDayOccupancy(DateTime.Now); if ((double)(thirtyDayOcc / 45.0) <= 0.6) return 0.80; return 1.0; } + internal static DateTime GetPayByDate(Reservation r) + { + switch (r.Type) + { + case ReservationType.Conventional: return r.EndDate; + case ReservationType.Prepaid: return r.StartDate; + case ReservationType.Incentive: return r.EndDate; + case ReservationType.SixtyDayAdvance: return r.StartDate.AddDays(-30); + default: throw new NotImplementedException(); + } + } + } internal class TransactionList { diff --git a/OpheliasOasis/OpheliasOasis.sqlite b/OpheliasOasis/OpheliasOasis.sqlite deleted file mode 100644 index e69de29..0000000 diff --git a/OpheliasOasis/Program.cs b/OpheliasOasis/Program.cs index 92aaad1..5ff948f 100644 --- a/OpheliasOasis/Program.cs +++ b/OpheliasOasis/Program.cs @@ -1,5 +1,6 @@ using Ophelias.Models; using Ophelias.Managers; +using Ophelias.Expressions; using System.Data.SQLite; class Program @@ -8,8 +9,7 @@ class Program { Reservation? activeReservation = null; Guest? activeGuest = null; - TransactionTableManager tx = new TransactionTableManager(null); - tx.UpdateTransactionInfo(1); + void help() { Console.WriteLine( @@ -17,7 +17,7 @@ class Program "\treservation create\n" + "\treservation update\n" + "\treservation cancel\n" + - "Account Commands:" + + "Account Commands:\n" + "\taccount create\n" + "\taccount update\n" + "Enter Q to quit.\n" @@ -25,18 +25,89 @@ class Program return; } + + void CreateNewReservation() { Reservation newReservation; } - while(true) + void CreateNewGuestPrompt() { - Console.WriteLine( + (string?, string?, string?) GetCreditCardInformation() + { + Console.Write("What is your credit card number: "); + string CreditCard = ""; + while (!Validation.ValidateCreditCard(CreditCard)) + { + CreditCard = Console.ReadLine().Trim().Replace("\t", ""); + if (CreditCard == "q" || CreditCard == "Q") + return (null, null, null); + if (!Validation.ValidateCreditCard(CreditCard)) + Console.Write("Please enter a valid credit card. If your card is expired, enter Q to cancel: "); + } + Console.Write("What is your credit card expiration date (MM/yy): "); + string CardExpiration = ""; + while (!Validation.ValidateExpirationDate(CardExpiration)) + { + CardExpiration = Console.ReadLine().Trim().Replace("\t", ""); + if (CardExpiration == "q" || CardExpiration == "Q") + return (null, null, null); + if (!Validation.ValidateExpirationDate(CardExpiration)) + Console.Write("Please enter a valid expiration date. If your card is expired, enter Q to cancel: "); + } + Console.Write("What is your credit card CCV: "); + string CCV = ""; + while (!Validation.ValidateCCV(CCV)) + { + CCV = Console.ReadLine().Trim().Replace("\t", ""); + if (CCV == "q" || CCV == "Q") + return (null, null, null); + if (!Validation.ValidateCCV(CCV)) + Console.Write("Please enter a valid credit card CCV. If your card is expired, enter Q to cancel: "); + } + return (CreditCard, CardExpiration, CCV); + } + Console.Write("What is your first name: "); + string FirstName = ""; + while (FirstName.Length == 0) + { + FirstName = Console.ReadLine(); + if (FirstName == "") + Console.Write("What is your first name: "); + } + Console.Write("What is your last name: "); + string LastName = ""; + while (LastName.Length == 0) + { + LastName = Console.ReadLine(); + if (LastName == "") + Console.Write("What is your last name: "); + } + Console.Write("What is your email: "); + string email = ""; + while (!Validation.ValidateEmail(email)) + { + email = Console.ReadLine(); + if (!Validation.ValidateEmail(email)) + Console.Write("Please enter a valid email: "); + } + Console.Write("Would you like to enter your credit card details? (Y/n): "); + string? CreditCard = null; + string? CardExpiration = null; + string? CCV = null; + if (Console.ReadLine().Equals("y") || Console.ReadLine().Equals("Y")) + (CreditCard, CardExpiration, CCV) = GetCreditCardInformation(); + + } + Console.Write( "Welcome to the Ophelias Oasis Hotel Registration System!\n" + "Type help to get a full list of commands or enter a command.\n" + "Command: " - ); + ); + while (true) + { + string? input = Console.ReadLine(); switch(input) { @@ -44,11 +115,12 @@ class Program case "reservation create": break; case "reservation update": break; case "reservation cancel": break; + case "account create": CreateNewGuestPrompt(); break; case "account update": break; case "q": return; default: Console.WriteLine("Unknown command, enter help for more inforamtion."); break; } - + Console.Write("Command: "); } } private void AdminMode() @@ -70,15 +142,25 @@ class Program } static void Main() { - SQLiteConnection.CreateFile("OpheliasOasis.sqlite"); + + if (!File.Exists("database.sqlite3") || new FileInfo("database.sqlite3").Length == 0) + { + SQLiteConnection.CreateFile("database.sqlite3"); + using (DatabaseManager Manager = new DatabaseManager()) + { + Manager.InitializeTables(); + Manager.InitializeRoomsTable(); + } + } bool run = true; while (run) { - Console.WriteLine( + Console.Write( "Are you an employee or customer?\n" + "1. Employee\n" + - "2. Customer/ Guest" + "2. Customer/ Guest\n" + + ": " ); switch(Console.ReadLine().ToUpper()) { diff --git a/OpheliasOasis/Validation.cs b/OpheliasOasis/Validation.cs new file mode 100644 index 0000000..d7170cd --- /dev/null +++ b/OpheliasOasis/Validation.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Text.RegularExpressions; +using System.ComponentModel.DataAnnotations; +using System.Globalization; + +namespace Ophelias.Expressions +{ + internal static class Expressions + { + internal static Regex CardRx = new Regex(@"^[0-9]{16}$", RegexOptions.Compiled); + internal static Regex ExpriationRx = new Regex(@"^(0?[1-9]|1[012])/2[0-9]{1}$", RegexOptions.Compiled); + internal static Regex CCVRx = new Regex(@"^[0-9]{3}$", RegexOptions.Compiled); + } + internal static class Validation + { + internal static bool ValidateCreditCard(string CreditCard) + { + if (Expressions.CardRx.IsMatch(CreditCard)) + return true; + return false; + } + internal static bool ValidateExpirationDate(string Expiration) + { + if (Expressions.ExpriationRx.IsMatch(Expiration)) + { + DateTime dt = DateTime.ParseExact(Expiration, "MM/yy", CultureInfo.InvariantCulture); + if (dt.Year >= DateTime.Now.Year) + if (dt.Month >= DateTime.Now.Month) + return true; + } + return false; + } + internal static bool ValidateEmail(string email) + { + EmailAddressAttribute EmailChecker = new EmailAddressAttribute(); + return EmailChecker.IsValid(email); + } + internal static bool ValidateCCV(string CCV) + { + if (Expressions.CCVRx.IsMatch(CCV)) + return true; + return false; + } + } +}