コード例 #1
0
        private void ReadRandomNouns()
        {
            //int counter = 0;
            string line;
            Random random = new Random();
            // Read the file and display it line by line.
            StreamReader file = new StreamReader(Path.Combine(Environment.CurrentDirectory, DirectoryName, "random150nouns.txt"));
            while ((line = file.ReadLine()) != null)
            {
                Product product = new Product
                {
                    Name = Regex.Replace(line, @"\s+", ""),
                    SurvivalPoints = random.Next(0, 30),
                    Weight = random.Next(0, 30)
                };
                
                Things things = new Things();
                things.AddProductIfNoExistis(product);
                Console.WriteLine(line);
                //counter++;
            }

            file.Close();
        }
コード例 #2
0
        private void SaveItemsToFile(object sender, RoutedEventArgs e)
        {
            String newItemName = ItemNameBox.Text;
            int newItemWeight = 0;
            int newItemSurvivalPoints = 0;

            #region validation
            if (String.IsNullOrWhiteSpace(newItemName) || String.IsNullOrEmpty(newItemName))
            {
                MessageBox.Show("ERROR wrong NAME: tried to add " + newItemName);
                return;
            }

            try
            {
                newItemWeight = Convert.ToInt32(WeightBox.Text);
                if (newItemWeight < 0) throw new Exception("Negative weight");
            }
            catch
            {
                MessageBox.Show("ERROR wrong WEIGHT: tried to add " + WeightBox.Text);
                return;
            }

            try
            {
                newItemSurvivalPoints = Convert.ToInt32(SurvivalPointsBox.Text);
                if (newItemSurvivalPoints < 0) throw new Exception("Negative Survival points");
            }
            catch
            {
                MessageBox.Show("ERROR wrong SURVIVAL POINTS: tried to add " + SurvivalPointsBox.Text);
                return;
            }
            #endregion

            Things things = new Things();
            things.AddProductIfNoExistis(new Product()
            {
                Name = newItemName,
                SurvivalPoints = newItemSurvivalPoints,
                Weight = newItemWeight
            });
            OnPropertyChanged("Items");
            //var json = JsonConvert.SerializeObject(Items);
            ////string path = Path.Combine(Environment.CurrentDirectory, DirectoryName, FileName);
            //File.WriteAllText(PathToItemsJsonFile(), json);
        }