static void Main(string[] args) { Console.Title = "COMP2614Lab03"; try { Console.WriteLine("Natural Order: "); GroceryItemCollection groceries = GroceryItemRepository.GetAllGroceries(); ConsolePrinter.PrintGroceryCollection(groceries); Console.WriteLine("Descending Order: "); GroceryItemCollection groceries2 = GroceryItemRepository.GetAllGroceriesByPrice(); ConsolePrinter.PrintGroceryCollection(groceries2); } catch (SqlException ex) { Console.WriteLine($"Data Access Error\n\n{ex.Message}\n\n{ex.StackTrace}"); } catch (Exception ex) { Console.WriteLine($"Processing Error\n\n{ex.Message}\n\n{ex.StackTrace}"); } }
private void AddGroceryItems() { groceryList = new GroceryItemCollection(); groceryList.Add(new GroceryItem("4L Milk", 3.87m, new DateTime(2006, 05, 25))); groceryList.Add(new GroceryItem("500g Cottage Cheese", 4.28m, new DateTime(2006, 06, 13))); groceryList.Add(new GroceryItem("1 Tin Mushroom Soup", 0.99m, new DateTime())); groceryList.Add(new GroceryItem("15kg Dog Food", 29.99m, new DateTime(2020, 01, 01))); }
public static GroceryItemCollection GetAllGroceriesByPrice() { GroceryItemCollection groceries; using (SqlConnection conn = new SqlConnection(connString)) { // embedded SQL string query = @"SELECT GroceryItemId, Description, Price, ExpirationDate FROM GroceryItem ORDER BY Price DESC"; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = query; cmd.Connection = conn; conn.Open(); groceries = new GroceryItemCollection(); using (SqlDataReader reader = cmd.ExecuteReader()) { string description = null; decimal price = 0; DateTime expirationDate = DateTime.MinValue; while (reader.Read()) { if (!reader.IsDBNull(1)) { description = reader["Description"] as string; } if (!reader.IsDBNull(2)) { price = (decimal)reader["Price"]; } if (!reader.IsDBNull(3)) { expirationDate = (DateTime)reader["ExpirationDate"]; } groceries.Add(new GroceryItem(description, price, expirationDate)); expirationDate = DateTime.MinValue; description = null; price = 0; } } return(groceries); } } }
private GroceryItemCollection PopulateAndSerializeGroceryList() { var groceryList = new GroceryItemCollection() { new GroceryItem() { Id = 1 } }; groceryList.SerializeGroceryListToDisk(pathToFile); return(groceryList); }
public static void PrintList(GroceryItemCollection list) { Console.WriteLine("{0,-25} {1, 10} {2, -20}", "Grocery Item", "Price", "Expires"); Console.WriteLine("------------------------------------------------------"); foreach (GroceryItem item in list) { Console.WriteLine("{0,-25} {1, 10} {2, -20}", item.Description, item.Price, item.ExpirationDate == DateTime.MaxValue ? "<Never>" : String.Format("{0:ddd MMM d, yyyy}", item.ExpirationDate)); } Console.WriteLine("------------------------------------------------------"); Console.WriteLine("{0} {1,29:n}", "Total:", list.TotalPrice); }
public static void PrintGroceryCollection(GroceryItemCollection groceries) { string divider = new string('-', 58); Console.WriteLine($"{"Description",-30} {"Price",-10} {"ExpirationDate"}\n{divider}"); decimal totalPrice = 0; foreach (GroceryItem groceryItem in groceries) { PrintGroceries(groceryItem); totalPrice += groceryItem.Price; } Console.WriteLine(divider); Console.WriteLine($"{"Total: ",-30} {totalPrice,-10} "); Console.WriteLine("\n"); }