Beispiel #1
0
        /**
         *	After generating a list of Items in the StartStore method Deliver method will put every
         *  Item up for sale one by one so that the Consumer knows what's for sale. DeliverItem method
         *  will also write a log for every Item recived to a file.
         */

        public void DeliverItem(Iitem item)
        {
            lock (_lock)
            {
                _items.Add(item);
                _log.Write(Name, "Recieved item: " +
                           item.GetName() +
                           " - " +
                           item.GetDesc() +
                           ". Sells for: " +
                           item.GetPrice() + "Yen");

                // Output that the store got a new item.
                lock (Console.Out)
                {
                    Console.ForegroundColor = ConsoleColor.Red;

                    Console.WriteLine("{0}\n - Got the new item: " +
                                      "{1}\n - " +
                                      "{2}\n | Selling for: " +
                                      "{3} ¥\n", Name, item.GetName(), item.GetDesc(), item.GetPrice());

                    Console.ForegroundColor = ConsoleColor.White;
                }
            }
        }
Beispiel #2
0
        /**
         *	This method will let the consumer buy the item one at the time and then
         *  return the item. The method will also write to a logg of the process off buying an item.
         */

        public Iitem Buy(Iitem item)
        {
            lock (_lock){                  // Lock so we don't have two threads trying to buy the same thing
                // is the item still for sale?
                if (_items.Contains(item)) // yes
                {
                    int   index        = _items.IndexOf(item);
                    Iitem itemToReturn = _items[index];
                    _items.Remove(item);

                    _log.Write(Name, "Sold item: " +
                               itemToReturn.GetName() +
                               " - " +
                               itemToReturn.GetDesc() +
                               ". For: " +
                               itemToReturn.GetPrice());

                    return(itemToReturn);
                }
                else                     // no

                {
                    _log.Write(Name, "Item allready sold: " +
                               item.GetName() +
                               " - " +
                               item.GetDesc());

                    return(null);
                }
            }
        }
Beispiel #3
0
        /**
         * This method will start the Store and generete random items and
         * put a pricetag on it.
         */

        public void StartStore()
        {
            while (_running || _items.Count > 0)
            {
                if (_running)
                {
                    double price = _rng.Next(10, 5500);

                    Iitem newItem = ItemFactory.CreateRandom(price);
                    _log.Write(Name, "Made item: " +
                               newItem.GetName() +
                               " - " +
                               newItem.GetDesc() +
                               ". Sells For: " +
                               newItem.GetPrice());
                    DeliverItem(newItem);
                }

                Thread.Sleep(1000);
            }
        }
 /**
  * Simple GetName method that will return the name of the item name.
  */
 public override string GetName()
 {
     return(_original_item.GetName());
 }
        /**
         * This method will check if the costumers wishlist has the item that it is looking for.
         * If it does have the item that is on their wishlist it will then start the process of
         * buying it and then print the statment of buying it. This method will also start to log this so
         * that we can easier print out the whole proccess of the program later in to a file.
         */

        private void OnNewItem(Store store, Iitem item)
        {
            _log.Write(Name, "Saw a new item in " + store.Name);

            Iitem bought;

            if (_wish_list.Contains(item))
            {
                bought = store.Buy(item);
            }
            else
            {
                Thread.Sleep(1);
                bought = store.Buy(item);
            }

            if (bought != null)
            {
                // right align text
                string[] strings =
                {
                    Name,
                    "- Bought the new item: ",
                    "- " + item.GetName(),
                    item.GetDesc(),
                    "| For: " + item.GetPrice() + " ¥"
                };
                int longestString = 0;

                for (int i = 0; i < strings.Length; i++)
                {
                    if (strings[i].Length > longestString)
                    {
                        longestString = strings[i].Length;
                    }
                }


                string output = String.Format("{0," + (Console.BufferWidth - (1 + longestString - strings[0].Length)) +
                                              "}\n" +
                                              "{1," + (Console.BufferWidth - (1 + longestString - strings[1].Length)) +
                                              "}\n" +
                                              "{2," + (Console.BufferWidth - (1 + longestString - strings[2].Length)) +
                                              "}\n" +
                                              "{3," + (Console.BufferWidth - (1 + longestString - strings[3].Length)) +
                                              "}\n" +
                                              "{4," + (Console.BufferWidth - (1 + longestString - strings[4].Length)) +
                                              "}\n",
                                              strings[0],
                                              strings[1],
                                              strings[2],
                                              strings[3],
                                              strings[4]);
                lock (Console.Out)
                {
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.WriteLine(output);
                    Console.ForegroundColor = ConsoleColor.White;
                }
                //Console.WriteLine("{0," + (Console.BufferWidth - 1) + "}", output);
            }
        }