private void btnAddArticle_Click(object sender, EventArgs e) { //todo, see assignment 3 int id = Convert.ToInt32(tbID.Text); double bPrice = Convert.ToDouble(tbBasicPrice.Text); int inStock = Convert.ToInt32(tbInStock.Text); Article article; if (rrbWine.Checked) { article = new WineArt(id, tbName.Text, bPrice, inStock, Convert.ToInt32(tbYear.Text)); } else if (rbWCArticle.Checked) { article = new WCArt(id, tbName.Text, bPrice, inStock, cbDiscount.Checked); } else { article = new DepositArt(id, tbName.Text, bPrice, inStock, Convert.ToDouble(tbDeposit.Text)); } if (myShop.AddArticle(article)) { MessageBox.Show("Successfully added article."); } else { MessageBox.Show("Already exists an article with that ID number."); } }
private void btnLoadFromFile_Click(object sender, EventArgs e) { //todo, see assignment 3 using (OpenFileDialog ofd = new OpenFileDialog()) { if (ofd.ShowDialog() == DialogResult.OK) { FileStream fs; StreamReader sr = null; try { fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read); sr = new StreamReader(fs); bool temp; string line = sr.ReadLine(); int id; string name; double basicPrice; int nrInStock; Article article; while (!sr.EndOfStream) { id = Convert.ToInt32(sr.ReadLine()); name = sr.ReadLine(); basicPrice = Convert.ToDouble(sr.ReadLine()); nrInStock = Convert.ToInt32(sr.ReadLine()); if (line.Contains("wine")) { int year = Convert.ToInt32(sr.ReadLine()); article = new WineArt(id, name, basicPrice, nrInStock, year); } else if (line.Contains("wcart")) { if (sr.ReadLine() == "discount yes") { temp = true; } else { temp = false; } article = new WCArt(id, name, basicPrice, nrInStock, temp); } else { double deposit = Convert.ToDouble(sr.ReadLine()); article = new DepositArt(id, name, basicPrice, nrInStock, deposit); } myShop.AddArticle(article); sr.ReadLine(); line = sr.ReadLine(); } } catch (IOException ex) { MessageBox.Show(ex.Message); } finally { if (sr != null) { sr.Close(); } } } } }