private List <Item> ReadFromFile(string filename) { XmlSerializer xmlserializer = new XmlSerializer(typeof(List <Item>)); StreamReader readFromFile = new StreamReader(filename); List <Item> items = (List <Item>)xmlserializer.Deserialize(readFromFile); if (itemsList.Count > 0) { itemsList.Clear(); } itemObject.RunningTotal = 0; try { if (items.Count() > 0) { itemsList = items; foreach (var item in itemsList) { if (item.GetType().Name == "Tire") { tire = (Tire)item; itemObject.RunningTotal += tire.Cost; } else if (item.GetType().Name == "Battery") { battery = (Battery)item; if (battery.Ship == true) { itemObject.RunningTotal += battery.Cost + battery.ShipItem(); } else { itemObject.RunningTotal += battery.Cost; } } else if (item.GetType().Name == "WindshieldWipers") { windShieldWipers = (WindshieldWipers)item; if (windShieldWipers.Ship == true) { itemObject.RunningTotal += windShieldWipers.Cost + windShieldWipers.ShipItem(); } else { itemObject.RunningTotal += windShieldWipers.Cost; } } } MessageBox.Show("Purchase history loaded successfully"); } else { MessageBox.Show("File Empty: There is no purchase history in the file to be loaded."); } } catch (Exception) { MessageBox.Show("Something went wrong while loading the purchase history data. Please try again!"); } readFromFile.Close(); return(items); }
private void BtnSubmit_Click(object sender, RoutedEventArgs e) { try { if (ItemType.SelectionBoxItem.ToString() == "Batteries" && (RadioYes.IsChecked == true || RadioNo.IsChecked == true)) { if (TxtItemName.Text.Trim() != "" && (TxtBatteryVoltage.Text.Trim() != "" && TxtBatteryVoltage.Text.Trim() != "0")) { if (RadioYes.IsChecked == true) { battery = new Battery(TxtItemName.Text, int.Parse(TxtBatteryVoltage.Text), true); itemsList.Add(battery); itemObject.RunningTotal = itemObject.RunningTotal + battery.Cost + battery.ShipItem(); } else { battery = new Battery(TxtItemName.Text, int.Parse(TxtBatteryVoltage.Text), false); itemsList.Add(battery); itemObject.RunningTotal = itemObject.RunningTotal + battery.Cost; } MessageBox.Show($"{battery.GetType().Name} purchased"); TxtItemName.Text = ""; TxtBatteryVoltage.Text = ""; } else { MessageBox.Show("Item Name and Battery Voltage are mandatory"); } } else if (ItemType.SelectionBoxItem.ToString() == "Windshield Wipers" && (RadioYes.IsChecked == true || RadioNo.IsChecked == true)) { if (TxtItemName.Text.Trim() != "" && (TxtWindShieldLength.Text.Trim() != "" && TxtWindShieldLength.Text.Trim() != "0")) { if (RadioYes.IsChecked == true) { windShieldWipers = new WindshieldWipers(TxtItemName.Text, int.Parse(TxtWindShieldLength.Text), true); itemsList.Add(windShieldWipers); itemObject.RunningTotal = itemObject.RunningTotal + windShieldWipers.Cost + windShieldWipers.ShipItem(); } else { windShieldWipers = new WindshieldWipers(TxtItemName.Text, int.Parse(TxtWindShieldLength.Text), false); itemsList.Add(windShieldWipers); itemObject.RunningTotal = itemObject.RunningTotal + windShieldWipers.Cost; } MessageBox.Show($"{windShieldWipers.GetType().Name} purchased"); TxtItemName.Text = ""; TxtWindShieldLength.Text = ""; } else { MessageBox.Show("Item Name and Windshield wiper length are mandatory"); } } else if (ItemType.SelectionBoxItem.ToString() == "Tires") { if (TxtItemName.Text.Trim() != "" && TxtTireModelName.Text.Trim() != "" && (TxtTireDiameter.Text.Trim() != "" && TxtTireDiameter.Text.Trim() != "0")) { tire = new Tire(TxtItemName.Text, int.Parse(TxtTireDiameter.Text), TxtTireModelName.Text); itemsList.Add(tire); itemObject.RunningTotal = itemObject.RunningTotal + tire.Cost; MessageBox.Show($"Tire purchased"); TxtItemName.Text = ""; TxtTireModelName.Text = ""; TxtTireDiameter.Text = ""; } else { MessageBox.Show("Please input all the required fields with appropriate data"); } } else { MessageBox.Show("Please enter all the mandatory fields."); } } catch (Exception) { MessageBox.Show("Oops! Some error occured while submitting buying the item. Please enter all the fields correctly and retry."); } }