Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have some code that I can not get to run. I have submitted the code for review. Thank you in advance for your help.
C#
<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace IT481_Unit6_Assignment
{
    using System;
    using System.Threading;

    public class DressingRoom
    {
        private static Semaphore available;
    
        public static void Main()
        {
            int ClothingsItemsGenerator = 0;
            int GenNumberOfItems;
            int max = 10;
            int min = 1;

            //Create a random number of available rooms
            //create random class for number generation
            Random rndNum = new Random();
            int dressing = rndNum.Next(1, 20);
            //Program is only displaying this random number
            Console.WriteLine("The average number of items being tried on is " + dressing);
            Console.ReadKey();
            //create random number for customers
            Random rndCust = new Random();
            int cust = rndNum.Next(30, 50);
            Console.WriteLine("The average number of customers requesting rooms is " + cust);
            Console.ReadKey();
            Random rndSleep = new Random();
            int SleepTimer = rndSleep.Next(60000, 180000);
            Console.WriteLine("The average time spent in a dressing room is " + SleepTimer + "milliseconds.");
            Console.ReadKey();
            available = new Semaphore(min, max);

            for (int i = 1; i <= cust; i++)
            {
                if (ClothingsItemsGenerator == dressing)
                {
                    GenNumberOfItems = ClothingsItemsGenerator.Next((max - min) + 1) + min;
                    Console.WriteLine(ClothingsItemsGenerator + " itmes.");
                    NewMethod(GenNumberOfItems);
                }
                Console.WriteLine("Checking for available rooms...");

                Console.WriteLine("Customer {0} enters the dressing room", i);
                Console.WriteLine("Customer {0} leaves the dressing room", i);

                available.WaitOne();
                available.Release();

                Console.WriteLine("There are now {0} dressing rooms left.", dressing, available.Release());
                Console.Read();

                Thread t = new Thread(new ParameterizedThreadStart(DressingRoom));

                //Start letting customers use dressing rooms.
                DressingRoom.Start(i);
            }
        }

        private static void NewMethod(int GenNumberOfItems)
        {
            Customer cust = new Customer(GenNumberOfItems, CustNumCount);
        }

        private Semaphore avaliable;
        private static int ClothingsItmes = 0;
        private static readonly Random ClothingItemsGenerator = new Random();
        private static int sleepTime;
        private static int GenNumbersOfItems;
        private static readonly Random sleepTimeGenerator = new Random();
        private static int max = 20;
        private static int min = 1;
        private static int sleepMin = 60000;
        private static int sleepMax = 180000;
        private static int CustNumCount;

        public DressingRoom(int MAX_ROOMS_AVAILABLE, int CustNum, int NumberOfItems)
        {
            available = new Semaphore(CustNumCount, MAX_ROOMS_AVAILABLE);
            Console.WriteLine("Did it get here?");
            Console.WriteLine(CustNum);

            try
            {
                Console.WriteLine(CustNum);
                RequestRoom(NumberOfItems, CustNum);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine(e.StackTrace);
            }
        }

        public virtual void RequestRoom(int NumberOfItems, int CustNum)
        {
            Console.WriteLine("Is a customer requesting a room?");
            Console.WriteLine(CustNum);
            for (int i = 0; i < CustNum; i ++)
            {
                Console.WriteLine("How many itmes does the customer have");
                if (NumberOfItems == 0)
                {
                    GenNumbersOfItems = ClothingItemsGenerator.Next((max - min) + 1) + min;
                    Console.WriteLine(NumberOfItems + " itmes.");
                    Customer cust = new Customer(this, GenNumbersOfItems, CustNum);
                }
            }
        }

        public class Customer
        {
            private DressingRoom dressingRoom;
            private int genNumberOfItmes;
            private int custNum;
            private object custNum1;

            public Customer(int NumberOfItems)
            {
                int max = 20;
                int min = 1;
                Console.WriteLine("Does it make instantiate customer?");
                Console.WriteLine("Here is the total number items " + NumberOfItems);

                if (NumberOfItems ==0)
                {
                    NumberOfItems = ClothingItemsGenerator.Next(max - min + 1) + min;
                    Console.WriteLine(NumberOfItems);
                }
                else
                {
                    Console.WriteLine(NumberOfItems);
                }
            }

            public Customer(int NumberOfItmes, object custNum1)
                : this(NumberOfItmes)
            {
                this.custNum1 = custNum1;
            }

            public Customer(DressingRoom dressingroom, int genNumberOfItems, int custNum)
            {
                this.dressingRoom = dressingRoom;
                this.genNumberOfItmes = genNumberOfItmes;
                this.custNum = custNum;
            }
        }
        public virtual void Scenario()
        {
            try
            {
                Console.WriteLine("There are a total number of " + CustNumCount + " customer");
                available.WaitOne();
            }
            catch (ThreadInterruptedException)
            {
                Console.WriteLine("received thread interuption exception");
            }
            Console.WriteLine("Customer " + CustNumCount + " starts to use the room");
            Console.WriteLine(GenNumbersOfItems + " here is the number of itmes");

            for (int i = 0; i < GenNumbersOfItems; i++)
            {
                try
                {
                    sleepTime = sleepTimeGenerator.Next((sleepMax - sleepMin) + 1) + sleepMin;
                    Console.WriteLine("{0} goint to sleeo for {1:D} milliseconds. \n", sleepTime);
                    Thread.Sleep(sleepTime);
                }
                catch (ThreadInterruptedException)
                {
                    Console.WriteLine("Received thread interupted exception");
                }
                Console.WriteLine("Customer " + CustNumCount + " left the room");
                available.Release();
                
            }
        }
    }
}


What I have tried:

I have tried changing the code around to make it work.
Posted
Updated 1-Jun-19 8:49am

1 solution

We can't help: we have no idea what your code is supposed to do, what it is doing that you didn't expect, or not doing that you did.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it, then a quick Google for "Visua Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900