/// <summary> /// Save Order without prescription in Database /// </summary> public void SaveWithoutPrescription() { do { try { Console.WriteLine("MedicineID: "); int medID = Int32.Parse(Console.ReadLine()); Console.WriteLine($"Avaiable stock: {CheckMedicineStock(medID)}"); Console.WriteLine("Amount: "); int amount = Int32.Parse(Console.ReadLine()); if (amount > CheckMedicineStock(medID)) { throw new OutOfMemoryException("Order could not be realised becuase the amount is to high."); } Order ord = new Order(null, medID, Date, amount); using (var connection = ActiveRecord.Open()) { var sqlCommand = new SqlCommand(); sqlCommand.Connection = connection; sqlCommand.CommandText = @"INSERT INTO Orders (MedicineID, Date, Amount) VALUES (@MedicineID, @Date, @Amount); UPDATE Medicines SET Amount = Amount - @Amount WHERE MedicineID = @MedicineID;"; var sqlMedicineIdParam = new SqlParameter { DbType = System.Data.DbType.Int32, Value = ord.MedicineID, ParameterName = "@MedicineID" }; var sqlDateParam = new SqlParameter { DbType = System.Data.DbType.DateTime, Value = ord.Date, ParameterName = "@Date" }; var sqlAmountParam = new SqlParameter { DbType = System.Data.DbType.Int32, Value = ord.Amount, ParameterName = "@Amount" }; sqlCommand.Parameters.Add(sqlMedicineIdParam); sqlCommand.Parameters.Add(sqlDateParam); sqlCommand.Parameters.Add(sqlAmountParam); sqlCommand.ExecuteNonQuery(); ConsoleEx.WriteLine("Successfully added", ConsoleColor.Green); Console.WriteLine("Do you want to add another? [yes/no]"); string ans = Console.ReadLine(); if (ans.ToLower() == "no") { break; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } while (true); }
/// <summary> /// Reload order in Database /// </summary> /// <param name="id"> Order's id </param> public override void Reload(int id) { try { var sqlCommand = new SqlCommand(); sqlCommand.CommandText = @"UPDATE Orders SET MedicineID = @MedicineID, Amount = @Amount WHERE ID = @id"; int?presID = null; Console.WriteLine("MedicineID: "); int medID = Int32.Parse(Console.ReadLine()); if (new Medicine().CheckWithPrescription(medID) == true) { Console.WriteLine("Prescription ID: "); presID = Int32.Parse(Console.ReadLine()); sqlCommand.CommandText = @"UPDATE Orders SET PrescriptionID = @PrescriptionID, MedicineID = @MedicineID, Amount = @Amount WHERE ID = @id;"; var sqlPrescriptionIDParam = new SqlParameter { DbType = System.Data.DbType.Int32, Value = presID, ParameterName = "@PrescriptionID" }; sqlCommand.Parameters.Add(sqlPrescriptionIDParam); } Console.WriteLine("Amount: "); int amount = Int32.Parse(Console.ReadLine()); Order ord = new Order(presID, medID, amount); int oldAmount = CheckOrderAmount(id); int currentAmount = CheckMedicineStock(medID); using (var connection = ActiveRecord.Open()) { sqlCommand.Connection = connection; var sqlMedicineIdParam = new SqlParameter { DbType = System.Data.DbType.Int32, Value = ord.MedicineID, ParameterName = "@MedicineID" }; var sqlIdParam = new SqlParameter { DbType = System.Data.DbType.Int32, Value = id, ParameterName = "@id" }; var sqlAmountParam = new SqlParameter { DbType = System.Data.DbType.Int32, Value = ord.Amount, ParameterName = "@Amount" }; sqlCommand.Parameters.Add(sqlMedicineIdParam); sqlCommand.Parameters.Add(sqlIdParam); sqlCommand.Parameters.Add(sqlAmountParam); sqlCommand.ExecuteNonQuery(); ConsoleEx.WriteLine("Successfully edited", ConsoleColor.Green); } int correctAmount = currentAmount + (oldAmount - amount); new Medicine().ChangeStock(correctAmount, medID); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }
static void Main(string[] args) { while (true) { ConsoleEx.WriteLine(Console.ForegroundColor, "I. Zarządzania listą leków: "); ConsoleEx.Write(Console.ForegroundColor, " 1. Dodanie leku - ".PadRight(40)); ConsoleEx.WriteLine(ConsoleColor.Green, "add"); ConsoleEx.Write(Console.ForegroundColor, " 2. Edycja leku - ".PadRight(40)); ConsoleEx.WriteLine(ConsoleColor.Green, "mod"); ConsoleEx.Write(Console.ForegroundColor, " 3. Usuwanie leku - ".PadRight(40)); ConsoleEx.WriteLine(ConsoleColor.Green, "del"); ConsoleEx.Write(Console.ForegroundColor, " 4. Wyświetlenie listy leków - ".PadRight(40)); ConsoleEx.WriteLine(ConsoleColor.Green, "show"); ConsoleEx.Write(Console.ForegroundColor, " 5. Wyszukiwanie leków - ".PadRight(40)); ConsoleEx.WriteLine(ConsoleColor.Green, "find"); ConsoleEx.WriteLine(Console.ForegroundColor, "II. Sprzedaż leków: "); ConsoleEx.Write(Console.ForegroundColor, " 1. Dodanie zamówienia - ".PadRight(40)); ConsoleEx.WriteLine(ConsoleColor.Green, "ord"); ConsoleEx.WriteLine(Console.ForegroundColor, "\nWyjście - exit "); Console.WriteLine(); string input = Ask.ForString("Wpisz polecenie: ").ToLower(); if (input == "exit") { break; } if (input == "add") { DisplayHeader(ConsoleColor.Green, "DODWANIE LEKU"); bool addNext; do { ProgramLogic.AddMedicine(); Console.WriteLine(); addNext = Ask.ForBool("Dodać kolejny lek t/n: "); Console.WriteLine(); } while (addNext); } if (input == "mod") { DisplayHeader(ConsoleColor.Green, "MODYFIKACJA LEKU"); bool modNext; do { ProgramLogic.ModifyMedicine(); Console.WriteLine(); modNext = Ask.ForBool("Czy zmieniany będzie kolejny lek t/n: "); Console.WriteLine(); } while (modNext); } if (input == "show") { DisplayHeader(ConsoleColor.Green, "LISTA WSZYSTKICH LEKÓW"); ProgramLogic.DisplayMedicineList(Medicine.LoadAll()); Console.WriteLine(); } if (input == "del") { DisplayHeader(ConsoleColor.Green, "USUWANIE LEKU"); bool delNext; do { ProgramLogic.DeleteMedicine(); Console.WriteLine(); delNext = Ask.ForBool("Czy usuwany będzie kolejny lek t/n: "); Console.WriteLine(); } while (delNext); } if (input == "ord") { DisplayHeader(ConsoleColor.Green, "SKŁADANIE ZAMÓWIENIA"); bool ordNext; do { ProgramLogic.AddOrder(); Console.WriteLine(); ordNext = Ask.ForBool("Czy dodać kolejne zamówienie t/n: "); Console.WriteLine(); } while (ordNext); } if (input == "find") { DisplayHeader(ConsoleColor.Green, "WYSZUKIWANIE LEKU"); bool findNext; do { ProgramLogic.SearchForMedicine(); Console.WriteLine(); findNext = Ask.ForBool("Czy wyszukać kolejny lek t/n: "); Console.WriteLine(); } while (findNext); } } void DisplayHeader(ConsoleColor color, string header) { ConsoleEx.WriteLine(color, "".PadLeft(header.Length + 4, '-')); ConsoleEx.WriteLine(color, $"--{header}--"); ConsoleEx.WriteLine(color, "".PadLeft(header.Length + 4, '-')); } }