/// <summary> /// рекурсивно считает количество деталей у машиины и заносит данные в "buf_list_of_items" /// </summary> /// <param name="current_unit"></param> /// <param name="number_of_units"></param> private static void adding_values_to_main_buffer(structural_unit current_unit, int number_of_units) { foreach (KeyValuePair<item, int> current_item in current_unit.all_items) { int buf_number_of_all_items = 0; try { buf_number_of_all_items = buf_list_of_items[current_item.Key.name]; } catch (KeyNotFoundException) { } finally { try { buf_number_of_all_items += current_item.Value * number_of_units; buf_list_of_items.Add(current_item.Key.name, buf_number_of_all_items); } catch (ArgumentException) { buf_list_of_items.Remove(current_item.Key.name); buf_list_of_items.Add(current_item.Key.name, buf_number_of_all_items); } } } foreach (KeyValuePair<structural_unit, int> cur_unit in current_unit.all_structural_units) { adding_values_to_main_buffer(cur_unit.Key, cur_unit.Value); } }
static void Main(string[] args) { structural_unit car_unit = new structural_unit("car"); structural_unit shell_unit = new structural_unit("shell"); shell_unit.all_items.Add(new item("bolt"), 10); shell_unit.all_items.Add(new item("list_of_steel"), 8); structural_unit wheel_unit = new structural_unit("wheel"); wheel_unit.all_items.Add(new item("bolt"), 5); wheel_unit.all_items.Add(new item("tire"), 1); structural_unit engine_unit = new structural_unit("engine"); engine_unit.all_items.Add(new item("bolt"), 12); engine_unit.all_items.Add(new item("piston"), 20); structural_unit door_unit = new structural_unit("door"); door_unit.all_items.Add(new item("bolt"), 6); door_unit.all_items.Add(new item("door handle"), 1); door_unit.all_items.Add(new item("list_of_steel"), 2); shell_unit.all_structural_units.Add(door_unit, 4); car_unit.all_structural_units.Add(wheel_unit, 4); car_unit.all_structural_units.Add(engine_unit, 1); car_unit.all_structural_units.Add(shell_unit, 1); structural_unit.show_list_of_items(car_unit,1); Console.ReadKey(); }
/// <summary> /// выводит на экран список деталей /// </summary> /// <param name="current_unit"></param> /// <param name="number_of_units"></param> public static void show_list_of_items(structural_unit current_unit, int number_of_units) { adding_values_to_main_buffer(current_unit, number_of_units); Console.WriteLine(current_unit.name + ":\n\n"); foreach (KeyValuePair<string, int> current_item in buf_list_of_items) { Console.WriteLine(current_item.Key + " " + current_item.Value.ToString()); } buf_list_of_items.Clear(); }