private GUITAR save_inst() { int x; GUITAR guitar = new GUITAR(); guitar.color = comboBox2.Text; guitar.name = name.Text; guitar.type = comboBox1.Text; guitar.materials = comboBox3.Text; bool success1 = int.TryParse(frets.Text, out x); if (success1 == true) { guitar.price = int.Parse(price.Text); } bool success2 = int.TryParse(id.Text, out x); if (success2 == true) { guitar.id = int.Parse(id.Text); } guitar.type = comboBox1.Text; bool success3 = int.TryParse(strings.Text, out x); if (success3 == true) { guitar.nbr_of_strings = int.Parse(strings.Text); } bool success4 = int.TryParse(frets.Text, out x); if (success4 == true) { guitar.nbr_of_frets = int.Parse(frets.Text); } return(guitar); }
private void button5_Click(object sender, EventArgs e) { try { int y; id.Enabled = true; if (id.Text == "") { MessageBox.Show("please select an ID"); } else if (int.TryParse(id.Text, out y) == false) { MessageBox.Show("ENTER A NUMBER"); id.Clear(); } else { IList <string> list_guitars = new List <string>(); IDictionary <int, GUITAR> guitar_dict = new Dictionary <int, GUITAR>(); using (StreamReader sr = new StreamReader(path)) { string line = sr.ReadLine(); line = sr.ReadLine(); //line = sr.ReadLine(); while (line != null) { list_guitars.Add(line); line = sr.ReadLine(); line = sr.ReadLine(); } } foreach (var item in list_guitars) { string[] broken_str = item.Split('/'); GUITAR a = new GUITAR(); a.id = int.Parse(broken_str[0].Trim()); a.name = broken_str[1].Trim(); a.type = broken_str[2]; a.materials = broken_str[3]; a.price = int.Parse(broken_str[4].TrimEnd('$')); a.color = broken_str[5]; a.nbr_of_frets = int.Parse(broken_str[6]); a.nbr_of_strings = int.Parse(broken_str[7]); guitar_dict.Add(int.Parse(broken_str[0]), a); Array.Clear(broken_str, 0, broken_str.Length); } if (guitar_dict.Keys.Contains(int.Parse(id.Text))) { int x; bool guitars = int.TryParse(id.Text, out x); if (guitars == true) { guitar_dict.Remove(int.Parse(id.Text)); } // write to the file using (StreamWriter sw = new StreamWriter(path)) { sw.WriteLine("ID|NAME|TYPE|MATERIALS|PRICE|COLOR|FRETS|STRINGS"); foreach (var item in guitar_dict) { sw.WriteLine(item.Value.get_info()); sw.WriteLine("----"); } MessageBox.Show("GUITAR DELETED"); } } else { MessageBox.Show("enter a valid ID"); id.Clear(); } } } catch (FileNotFoundException) { MessageBox.Show("File not found:+" + path + "\n CREATING FILE..."); var text = "ID|NAME|TYPE|MATERIALS|PRICE|COLOR|FRETS|STRINGS \n"; File.WriteAllText("GUITAR.TXT", text); } catch (IOException) { MessageBox.Show("I/O ERROR \nREDELETE THE ITEM"); } }
public void GUITAR__list_to_GUITAR_dict() { try { using (StreamReader sr = new StreamReader(path1)) { string line = sr.ReadLine(); line = sr.ReadLine(); while (line != null) { list_instruments.Add(line); line = sr.ReadLine(); line = sr.ReadLine(); } } //converting to a dict foreach (var item in list_instruments) { string[] broken_str = item.Split('/'); GUITAR a = new GUITAR(); a.id = int.Parse(broken_str[0].Trim()); a.name = broken_str[1].Trim(); a.type = broken_str[2]; a.materials = broken_str[3]; a.price = int.Parse(broken_str[4].TrimEnd('$')); a.color = broken_str[5]; a.nbr_of_frets = int.Parse(broken_str[6]); a.nbr_of_strings = int.Parse(broken_str[7]); GUITAR_dict.Add(int.Parse(broken_str[0]), a); Array.Clear(broken_str, 0, broken_str.Length); } DataTable table = new DataTable(); table.Columns.Add("ID"); table.Columns.Add("NAME"); table.Columns.Add("TYPE"); table.Columns.Add("MATERIALS"); table.Columns.Add("PRICE"); table.Columns.Add("COLOR"); table.Columns.Add("FRETS"); table.Columns.Add("STRINGS"); foreach (var item in GUITAR_dict) { DataRow row = table.NewRow(); row["ID"] = item.Key; row["NAME"] = item.Value.name; row["TYPE"] = item.Value.type; row["MATERIALS"] = item.Value.materials; row["PRICE"] = item.Value.price; row["COLOR"] = item.Value.color; row["FRETS"] = item.Value.nbr_of_frets; row["STRINGS"] = item.Value.nbr_of_strings; table.Rows.Add(row); } dataGridView2.DataSource = table; } catch (FileNotFoundException) { MessageBox.Show("FILE NOT FOUND"); var text = "ID|NAME|TYPE|MATERIALS|PRICE|COLOR|FRETS|STRINGS \n"; File.WriteAllText(path1, text); } catch (IOException) { MessageBox.Show("I|O ERROR"); } catch (Exception) { MessageBox.Show("ERROR LOADING GUITARS"); } }