public static void Option2() { Vaccine tempVaccine = new Vaccine(); string tempSKU; bool flag = true; //Get user input SKU\ do { //if there are any errors if (!flag) { Console.Clear(); Console.WriteLine("Something went wrong, please start again..."); flag = true; } Console.WriteLine("Please Search for a Vaccine using a SKU Number (7 Digit Whole Number):"); tempSKU = Console.ReadLine(); if (tempSKU.ToString().Length != 7) { flag = false; } } while (!flag); //Search the List for the requested SKU foreach (Vaccine vaccine in vaccines) { if (vaccine.getSKU() == int.Parse(tempSKU)) { tempVaccine = vaccine; } } if (tempVaccine != new Vaccine()) { Console.WriteLine("Vaccine with SKU# " + tempSKU + " found!\n" + tempVaccine.ToString()); } else { Console.WriteLine("Vaccine with SKU# " + tempSKU + " not found!\n"); } }
public virtual int CompareTo(object obj) { //is the argument null? if (obj == null) { //throw exception throw new ArgumentNullException("Unable to compare, object is null"); } Vaccine tempVaccine = obj as Vaccine; //Convert obj to Vaccine if (tempVaccine != null) //if the conversion worked { if (sortBy == SortBy.Quantity) { int thisSort = this.quantity; int compare = tempVaccine.quantity; return(thisSort.CompareTo(compare)); } else if (sortBy == SortBy.Cost) { float thisSort = this.cost; float compare = tempVaccine.cost; return(thisSort.CompareTo(compare)); } else { DateTime thisSort = new DateTime(expiration.getDay(), expiration.getMonth(), expiration.getYear()); DateTime compare = new DateTime(tempVaccine.expiration.getDay(), tempVaccine.expiration.getMonth(), tempVaccine.expiration.getYear()); return(thisSort.CompareTo(compare)); } } else //conversion failed { throw new ArgumentException("Object being compared cannot be converted to a Vaccine"); } }
public static void Option5() { Console.WriteLine("Delete Vaccine Selected"); Vaccine tempVaccine = new Vaccine(); string tempSKU; string input = ""; bool flag = true; //Get user input SKU\ do { //if there are any errors if (!flag) { Console.Clear(); Console.WriteLine("Something went wrong, please start again..."); flag = true; } Console.WriteLine("Please Search for a Vaccine using a SKU Number (7 Digit Whole Number):"); tempSKU = Console.ReadLine(); if (tempSKU.ToString().Length != 7) { flag = false; } } while (!flag); //Search the List for the requested SKU foreach (Vaccine vaccine in vaccines) { if (vaccine.getSKU() == int.Parse(tempSKU)) { tempVaccine = vaccine; } } //Vaccine is found if (tempVaccine != new Vaccine()) { Console.Clear(); Console.WriteLine("Vaccine with SKU# " + tempSKU + " found!\n"); Console.WriteLine(tempVaccine.ToString()); while ((input != "y") && (input != "n")) { Console.Write("\n\nAre you sure you want to delete this? Press Y/N: "); input = Console.ReadLine(); } switch (input) { case "y": Console.WriteLine("Vaccine with SKU# " + tempSKU + " has been deleted."); vaccines.Remove(tempVaccine); break; case "n": Console.WriteLine("No Vaccine Deleted, returning to main menu"); break; } } }
public static void Option4() { Console.WriteLine("Update Vaccine's Selected"); Vaccine tempVaccine = new Vaccine(); string tempSKU; string input = ""; bool flag = true; //Get user input SKU\ do { //if there are any errors if (!flag) { Console.Clear(); Console.WriteLine("Something went wrong, please start again..."); flag = true; } Console.WriteLine("Please Search for a Vaccine using a SKU Number (7 Digit Whole Number):"); tempSKU = Console.ReadLine(); if (tempSKU.ToString().Length != 7) { flag = false; } } while (!flag); //Search the List for the requested SKU foreach (Vaccine vaccine in vaccines) { if (vaccine.getSKU() == int.Parse(tempSKU)) { tempVaccine = vaccine; } } //Vaccine is found if (tempVaccine != new Vaccine()) { Console.Clear(); Console.WriteLine("Vaccine with SKU# " + tempSKU + " found!\n"); while ((input != "i") && (input != "d")) { Console.WriteLine("Increase or Decrease Quantity? Please press i key for Increase or d key for Decrease"); input = Console.ReadLine(); } bool increase = true; switch (input) { case "i": increase = true; break; case "d": increase = false; break; } flag = false; int changeAmount = 0; if (!increase) { while (!flag) { flag = true; Console.Write("Decrease quantity by: "); input = Console.ReadLine(); try { changeAmount = int.Parse(input); } catch (Exception e) { Console.WriteLine(e.Message); flag = false; } } tempVaccine.setQuantity(tempVaccine.getQuantity() - changeAmount); } else if (increase) { while (!flag) { flag = true; Console.Write("Increase quantity by: "); input = Console.ReadLine(); try { changeAmount = int.Parse(input); } catch (Exception e) { Console.WriteLine(e.Message); flag = false; } } tempVaccine.setQuantity(tempVaccine.getQuantity() + changeAmount); } WriteToFile(); } else { Console.WriteLine("Vaccine with SKU# " + tempSKU + " not found!\n"); } }
public static Vaccine[] ReadFile() //should be string of path to resources file { const int NUM_OF_COLUMNS = 6; //return the number of rows string[] lineCount = Properties.Resources.Vaccines.Split('\n'); int numberLines = lineCount.Length; int lineCounter = 0; string line; StreamReader csvFile = new StreamReader(@"../../../Resources/Vaccines.csv"); Vaccine[] vaccines = new Vaccine[numberLines]; //Array to store the vaccines while ((line = csvFile.ReadLine()) != null) { // Console.WriteLine(line); int columnCounter = 0; string[] columns = line.Split(','); //Take each line and break it into the relevant data Vaccine tempVaccine = new Vaccine(); //tempVaccine used to store all the data before adding to the array of vaccines while (columnCounter <= NUM_OF_COLUMNS) { if (columnCounter == 0) { tempVaccine.setSKU(int.Parse(columns[columnCounter])); //Add SKU Number } else if (columnCounter == 1) { tempVaccine.setName(columns[columnCounter]);//Add Vaccine Name } else if (columnCounter == 2) { tempVaccine.setCost(float.Parse(columns[columnCounter])); //Add Unit Cost } else if (columnCounter == 3) { tempVaccine.setQuantity(int.Parse(columns[columnCounter])); //Add Quantity } else if (columnCounter == 4) { //Add Expiry DateTime tempDate = new DateTime(); if (DateTime.TryParseExact(columns[columnCounter], "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out tempDate)) { // tempVaccine.setDate(tempDate); } } else if (columnCounter == 5) { tempVaccine.setInstructions(columns[columnCounter]); //Add Instructions } columnCounter++; } vaccines[lineCounter] = tempVaccine; lineCounter++; } return(vaccines); }