private DRUM save_inst()
        {
            int  x;
            DRUM drum = new DRUM();

            drum.materials = comboBox3.Text;
            drum.name      = drum_name.Text;


            drum.size = comboBox2.Text;

            drum.type = comboBox1.Text;

            bool success1 = int.TryParse(price.Text, out x);

            if (success1 == true)
            {
                drum.price = int.Parse(price.Text);
            }

            bool success2 = int.TryParse(id.Text, out x);

            if (success2 == true)
            {
                drum.id = int.Parse(id.Text);
            }



            bool success3 = int.TryParse(shell.Text, out x);

            if (success3 == true)
            {
                drum.shell_thickness = int.Parse(shell.Text);
            }
            bool success4 = int.TryParse(pieces.Text, out x);

            if (success4 == true)
            {
                drum.nbr_of_pieces = int.Parse(pieces.Text);
            }
            drum.edge = comboBox4.Text;


            return(drum);
        }
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                int y;
                shell.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, DRUM> guitar_dict  = new Dictionary <int, DRUM>();
                    using (StreamReader sr = new StreamReader(path))
                    {
                        string line = sr.ReadLine();
                        line = sr.ReadLine();
                        while (line != null)
                        {
                            list_guitars.Add(line);
                            line = sr.ReadLine();
                            line = sr.ReadLine();
                        }
                    }
                    //adding to dictionary
                    foreach (var item in list_guitars)
                    {
                        string[] broken_str = item.Split('/');

                        DRUM a = new DRUM();

                        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.shell_thickness = int.Parse(broken_str[5]);
                        a.nbr_of_pieces   = int.Parse(broken_str[6]);
                        a.size            = broken_str[7];
                        a.edge            = broken_str[8];

                        guitar_dict.Add(int.Parse(broken_str[0]), a);
                        Array.Clear(broken_str, 0, broken_str.Length);
                    }
                    //contains?
                    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));
                        }


                        using (StreamWriter sw = new StreamWriter(path))
                        { sw.WriteLine("DRUM ID|NAME|TYPE|MATERIAL|PRICE|SHELL|PIECES|SIZE|EDGE");
                          foreach (var item in guitar_dict)
                          {
                              sw.WriteLine(item.Value.get_info());
                              sw.WriteLine("----");
                          }
                          MessageBox.Show("DRUM DELETED"); }
                    }
                    else
                    {
                        MessageBox.Show("enter a valid ID");
                        id.Clear();
                    }
                }
            }
            catch (FileNotFoundException) {
                MessageBox.Show("CREATING FILE...");
                var text = "DRUM ID|NAME|TYPE|MATERIAL|PRICE|SHELL|PIECES|SIZE|EDGE\n";
                File.WriteAllText(path, text);
            }
        }
Exemple #3
0
        public void instruments__list_to_instrument_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();
                    }
                }

                foreach (var item in list_instruments)
                {
                    string[] broken_str = item.Split('/');

                    DRUM a = new DRUM();

                    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.shell_thickness = int.Parse(broken_str[5]);
                    a.nbr_of_pieces   = int.Parse(broken_str[6]);
                    a.size            = broken_str[5];
                    a.edge            = broken_str[5];
                    instrument_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("SHELL THICKNESS");
                table.Columns.Add("PIECES");
                table.Columns.Add("SIZE");
                table.Columns.Add("EDGE");

                foreach (var item in instrument_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["SHELL THICKNESS"] = item.Value.shell_thickness;
                    row["PIECES"]          = item.Value.nbr_of_pieces;
                    row["SIZE"]            = item.Value.size;
                    row["EDGE"]            = item.Value.edge;
                    table.Rows.Add(row);
                }
                dataGridView2.DataSource = table;
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("FILE NOT FOUND");
                var text = "DRUM ID|NAME|TYPE|MATERIAL|PRICE|SHELL|PIECES|SIZE|EDGE\n"; File.WriteAllText(path1, text);
            }

            catch (IOException) { MessageBox.Show("I|O ERROR"); }
            catch (Exception) { MessageBox.Show("ERROR LOADING DRUMS"); }
        }