Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Problem Statement:

There is a factory production line around a single a conveyor belt.

Components (of type A and B) come onto the start of the belt at random

intervals; workers must take one component of each type from the belt
as they come past, and combine them to make a finished product.

The belt is divided into fixed-size slots; each slot can hold only one
component or one finished product. There are a number of worker

stations on either side of the belt, spaced to match the size of the
slots on the belt, like this (fixed-width font ASCII pic):
v v v v v workers
---------------------
-> | A | | B | A | P | -> conveyor belt

---------------------
^ ^ ^ ^ ^ workers

In each unit of time, the belt moves forwards one position, and there
is time for a worker on one side of each slot to EITHER take an item

from the slot or replace an item onto the belt. The worker opposite
them can't touch the same belt slot while they do this.
(So you can't have one worker picking something from a slot while
their counterpart puts something down in the same place).


Once a worker has collected one of both types of component, they can
begin assembling the finished product. This takes an amount of time,
so they will only be ready to place the assembled product back on the

belt on the fourth subsequent slot. While they are assembling the
product, they can't touch the conveyor belt. Workers can only hold
two items (component or product) at a time: one in each hand.

Create a simulation of this, with three pairs of workers. At each

time interval, the slot at the start of the conveyor belt should have
an equal (1/3) chance of containing nothing, a component A or a
component B.

Run the simulation for 100 steps, and compute how many finished

products come off the production line, and how many components of each
type go through the production line without being picked up by any
workers.


I have implemented the solution like this:

Is there any other better way to do this,if we can use any design pattern or make it more generic?

What I have tried:

C++
#include <string>
#include <iostream>
#include <vector> 
#include <list>
#include<ctime>
#include <math.h>

using namespace std;

#define NULL 0

enum {
	STATE_WORKING = 0,
	STATE_FINISHED = 1,
	STATE_SUBMITTED = 2
} WORKER_STATE;

enum CACHE_STATE {
	CACHE_STATE_EMPTY = 0,
	CACHE_STATE_ONLY_PART_A = 1,
	CACHE_STATE_ONLY_PART_B = 2,
	CACHE_STATE_BOTH_PARTA_PARTB = 3
};

class Conveyor {
public:
	string move();
private:
	vector<string> parts;
};

std::vector<string> randomize()
{
	srand(time(0));
	string choices[] = { "Part_A", "Part_B", "Part_A" };

	std::vector<string> random;
	for (int i = 0; i < 3; i++)
	{
		random.push_back(choices[rand() % 3]);
	}
	return random;
}

string Conveyor::move()
{
	srand(time(0));

	randomize();
	std::vector<string> randomResult = randomize();

	return randomResult[0];

	/*
	for (iter = randomResult.begin(), iterEnd = randomResult.end();
		iter != iterEnd; ++iter)
		cout << *iter << endl;

	return *iter;


	string partno;
	partno = parts.front();

	parts.erase(parts.begin());
	
	return partno;
	*/
}

class Worker {
public:
	void getPart(Conveyor& conveyor);
	void putItem(Conveyor& conveyor);
	void assembleItem(string part_a, string part_b);
	bool isAssembling;
	bool hasAllParts;
	bool hasAssembly;


	int cachestate;
private:
	int parts_reqd;
	int time_reqd;
	string slave;
	int worker_position;
};

void Worker::getPart(Conveyor& conveyor)
{
	bool cache_empty = true;
	CACHE_STATE cachestate;
	string incoming_part;
	
	incoming_part = conveyor.move();
	if (strcmp(incoming_part.c_str(), "part_A") == 0)
	{
		cachestate = CACHE_STATE::CACHE_STATE_ONLY_PART_A; 
		cache_empty = false;
	}

	if (strcmp(incoming_part.c_str(), "part_B") == 0)
	{
		cachestate = CACHE_STATE::CACHE_STATE_ONLY_PART_B;
		cache_empty = false;
	}
	if (strcmp(incoming_part.c_str(), "part_NULL") == 0)
	{
		cachestate = CACHE_STATE::CACHE_STATE_EMPTY;
		cache_empty = true;
	}
}

void Worker::putItem(Conveyor& conveyor)
{
	unsigned int finished_part = 0;
	
	finished_part++;
	cout << "No of finished part = ", finished_part;
}

void Worker::assembleItem(string part_a,string part_b)
{
	string fin_part;
	unsigned int time_taken = 0;
	fin_part = part_a + part_b;
	time_taken = 5;
}

class ProductionLine {
public:
	Conveyor the_conveyor;
	std::vector<Worker> workers;
	void runprodline(unsigned int pairs,int no,string ar[2]);
};

void ProductionLine::runprodline(unsigned int worker_pairs,int totalworkers,string partposns[2])
{
	string part[100];

	Worker workerone;

	Conveyor objConv;
	unsigned int  i = 0;
	   

	unsigned int assembled_part = 0;
	unsigned int elapsed_time_part_complete = 0;

	unsigned int finished_part = 0;
	Worker workeronline[2];
	Worker worker_two;

	cout << "Part no 0 = " << partposns[0] << endl;
	cout << "Part no 1 = " << partposns[1] << endl;
	cout << "Part no 1 = " << partposns[2] << endl;

	workeronline[1].hasAllParts = false;
	workeronline[1].isAssembling = false;

	for (i = 0; i < 3; i++)
	{
		for (int workerposition = 0; workerposition < 2; workerposition++)
		{
			if (strcmp(partposns[i].c_str(), "part_A") == 0)
			{
				workeronline[workerposition].cachestate = CACHE_STATE_ONLY_PART_A;

				partposns[i] = "";
			}
			if (strcmp(partposns[i].c_str(), "part_B") == 0)
			{
				workeronline[workerposition].cachestate = CACHE_STATE_ONLY_PART_B;

				partposns[i] = "";
			}
			if( (strcmp(partposns[i].c_str(), "part_A") == 0) && (workeronline[workerposition].cachestate == CACHE_STATE_ONLY_PART_B))
			{
				workeronline[workerposition].cachestate = CACHE_STATE_BOTH_PARTA_PARTB;

				workeronline[workerposition].hasAllParts = true;

				partposns[i] = "";
			}
			if ((strcmp(partposns[i].c_str(), "part_B") == 0) && (workeronline[workerposition].cachestate == CACHE_STATE_ONLY_PART_A))
			{
				workeronline[workerposition].cachestate = CACHE_STATE_BOTH_PARTA_PARTB;

				workeronline[workerposition].hasAllParts = true;

				partposns[i] = "";
			}
			if (workeronline[workerposition].hasAllParts == true)
			{
				workeronline[workerposition].isAssembling = true;

				elapsed_time_part_complete++;
			}
			if ((workeronline[workerposition].isAssembling == true) && (elapsed_time_part_complete % 4) == 0)
			{
				workeronline[workerposition].hasAssembly = true;

				workeronline[workerposition].isAssembling = false;

			}
			if ((strcmp(partposns[i].c_str(), "part_NULL") == 0) && (workeronline[workerposition].hasAssembly == true))
			{
				finished_part = finished_part + 1;

				workeronline[workerposition].hasAssembly = false;
			}
		}
	}		
	
	cout << "No of finished parts = " << finished_part << endl;
}

int main()
{
	ProductionLine obj_prodline;
	unsigned int pair = 3;
	unsigned int workerspositions;
	string partone;
	Conveyor conveyor;

	workerspositions = pair * 2;
	string conveyorpartposition[3];
	conveyorpartposition[0] = "";
	conveyorpartposition[1] = "";
	conveyorpartposition[2] = "";

	string part[2];

	for (int i = 0; i < 100; i++)
	{
		partone = conveyor.move();
		conveyorpartposition[0] = partone;

		obj_prodline.runprodline(pair, workerspositions, conveyorpartposition);

		conveyorpartposition[2] = conveyorpartposition[1];
		conveyorpartposition[1] = conveyorpartposition[0];		
	}
}
Posted
Updated 17-Dec-19 3:33am
v2

I would start by rewriting ProductionLine::runprodline(). All of the logic accessing workeronline[i] members should be a method of the Worker class. There is nearly a duplication of code in getPart() so there should be a common method to call that performs that logic. Also, why is the value of 3 in its for loop? You should avoid placing literal numbers in code like that. It should be a parameter to the method and defined as a constant somewhere else, like in the main function.
 
Share this answer
 
v2
A few comments:

  • The choices array duplicates "Part_A" and omits "Part_Null".
  • The strcmps will fail because, for example, they compare "Part_A" to "part_A". This type of error is less likely if you use a constant to define a recurring string literal.
  • Strictly speaking, strcmp is C, not C++. I would just write
    if(incoming_part == "Part_A")

  • It's generally frowned upon to comment out code with /* ... */ because, depending on which editor someone is using, it can be hard to tell that the code isn't actually executed.
  • Strive to make all class data members private.
  • Design patterns are secondary to the fundamental principles of encapsulation, polymorphism, and inheritance. Don't focus on design patterns until you have these things nailed.
 
Share this answer
 
This code is a far cry from solving the task given. All it does is compile and output some meaningless lines of text.

The main issue I see here is that you consistently fail to structure your thoughts sufficiently to put them into meaningful code, even if the task you need to solve is trivial.

Point in case:
C++
void Worker::putItem(Conveyor& conveyor)
{
	unsigned int finished_part = 0;
	
	finished_part++;
	cout << "No of finished part = ", finished_part;
}

In my solution here[^] I explained what this function is meant to do: place the assembled product on the conveyor belt. Instead, you don't even talk to the conveyor! All you do is declare a local variable, initialize it to 0 (what does 0 even mean in this context?), increment it to 1 (what does that mean?) and discard it. Note that the line you print out doesn't even print that number! You should check the syntax and meaning of the comma operator in C++! The state of the conveyor and the rest of the program remains unchanged.

An equivalent piece of code would be:
C++
void Worker::putItem(Conveyor& )
{
    cout << "No of finished part = ";
}

That said, you don't even call the function...

At this point I have to ask, where did you get this task from, if you're having trouble at such a basic level? You're way over your head here, and I doubt any advice on how to solve this specific task will do you any good.

I strongly suggest to find a programming tutorial for beginners, and learn programming from the start. Learn to walk before you try to run!
 
Share this answer
 
#include <string>
#include <iostream>
#include <vector> 
#include <list>
#include<ctime>
#include <math.h>

using namespace std;

#define NULL 0



enum CACHE_STATE {
	CACHE_STATE_EMPTY = 0,
	CACHE_STATE_ONLY_PART_A = 1,
	CACHE_STATE_ONLY_PART_B = 2,
	CACHE_STATE_BOTH_PARTA_PARTB = 3
};

class Conveyor {
public:
	string move();
	void init();
	string conveyor_part_position[2];
private:
	vector<string> parts;
	//int conveyor_part_position[2];


};

void Conveyor::init()
{
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");


}

string Conveyor::move()
{


	string partno;

	if (!parts.empty())
		partno = parts.front();
	else
		partno = "part_A";

	if (!parts.empty())
		parts.erase(parts.begin());

	return partno;

}



class Worker {
public:

	bool isAssembling;
	bool hasAllParts;
	bool hasAssembly;


	int cachestate;
private:
	int parts_reqd;
	int time_reqd;
	string slave;
	int worker_position;

};




class ProductionLine {
public:
	Conveyor the_conveyor;
	std::vector<Worker> workers;
	void runprodline(unsigned int pairs, int no, string ar[2], Worker workeroneline[2]);

};

void ProductionLine::runprodline(unsigned int worker_pairs, int totalworkers, string partposns[2], Worker workeronline[2])
{
	string part[100];

	Worker workerone;

	Conveyor objConv;
	unsigned int  i = 0;


	unsigned int assembled_part = 0;
	static unsigned int elapsed_time_part_complete_worker_zero = 0;
	static unsigned int elapsed_time_part_complete_worker_one = 0;

	static unsigned int finished_part = 0;

	Worker worker_two;

	//	cout << "Part no 0 = " << partposns[0] << endl;
	//	cout << "Part no 1 = " << partposns[1] << endl;
	//	cout << "Part no 1 = " << partposns[2] << endl;



	for (i = 0; i < 3; i++)
	{

		for (int workerposition = 0; workerposition < 2; workerposition++)
		{
			if ((strcmp(partposns[i].c_str(), "part_A") == 0) && (workeronline[workerposition].cachestate == CACHE_STATE_EMPTY))
			{
				workeronline[workerposition].cachestate = CACHE_STATE_ONLY_PART_A;

				partposns[i] = "";
			}
			if ((strcmp(partposns[i].c_str(), "part_B") == 0) && (workeronline[workerposition].cachestate == CACHE_STATE_EMPTY))
			{
				workeronline[workerposition].cachestate = CACHE_STATE_ONLY_PART_B;

				partposns[i] = "";
			}
			if ((strcmp(partposns[i].c_str(), "part_A") == 0) && (workeronline[workerposition].cachestate == CACHE_STATE_ONLY_PART_B))
			{
				workeronline[workerposition].cachestate = CACHE_STATE_BOTH_PARTA_PARTB;

				workeronline[workerposition].hasAllParts = true;

				partposns[i] = "";
			}
			if ((strcmp(partposns[i].c_str(), "part_B") == 0) && (workeronline[workerposition].cachestate == CACHE_STATE_ONLY_PART_A))
			{
				workeronline[workerposition].cachestate = CACHE_STATE_BOTH_PARTA_PARTB;

				workeronline[workerposition].hasAllParts = true;

				partposns[i] = "";
			}
			if (workeronline[workerposition].hasAllParts == true)
			{
				workeronline[workerposition].isAssembling = true;

				if (workerposition == 0)
					elapsed_time_part_complete_worker_zero++;
				else if (workerposition == 1)
					elapsed_time_part_complete_worker_one++;
			}
			if ((workeronline[workerposition].isAssembling == true) && (elapsed_time_part_complete_worker_zero > 1) && ((elapsed_time_part_complete_worker_zero % 4) == 0))
			{
				workeronline[workerposition].hasAssembly = true;

				workeronline[workerposition].isAssembling = false;

			}
			if ((workeronline[workerposition].isAssembling == true) && (elapsed_time_part_complete_worker_one > 1) && ((elapsed_time_part_complete_worker_one % 4) == 0))
			{
				workeronline[workerposition].hasAssembly = true;

				workeronline[workerposition].isAssembling = false;

			}
			if ((strcmp(partposns[i].c_str(), "part_NULL") == 0) && (workeronline[workerposition].hasAssembly == true))
			{
				finished_part = finished_part + 1;


				workeronline[workerposition].hasAssembly == true;
				workeronline[workerposition].isAssembling == false;
				workeronline[workerposition].hasAllParts = false;
				workeronline[workerposition].cachestate == CACHE_STATE_EMPTY;

				partposns[i] = "";

			}
			if (partposns[i] == "")
				break;


		}

		if (partposns[i] == "")
			break;

	}


	cout << "No of finished parts = " << finished_part << endl;

}

int main()
{
	ProductionLine obj_prodline;
	unsigned int pair = 3;
	unsigned int workerspositions;
	string partone;
	Conveyor conveyor;

	workerspositions = pair * 2;
	string conveyorpartposition[3];
	conveyorpartposition[0] = "";
	conveyorpartposition[1] = "";
	conveyorpartposition[2] = "";

	string coninitialposition[3];

	conveyor.init();

	string part[2];
	static Worker workeronline[2];
	workeronline[1].hasAllParts = false;
	workeronline[1].isAssembling = false;
	workeronline[0].hasAllParts = false;
	workeronline[0].isAssembling = false;
	workeronline[0].cachestate = CACHE_STATE_EMPTY;
	workeronline[1].cachestate = CACHE_STATE_EMPTY;

	conveyor.conveyor_part_position[0] = conveyorpartposition[0];

	for (int i = 0; i < 100; i++)
	{

		partone = conveyor.move();

		cout << "the part get is" << partone << endl;
		conveyorpartposition[0] = partone;


		obj_prodline.runprodline(pair, workerspositions, conveyorpartposition, workeronline);

		conveyorpartposition[2] = conveyorpartposition[1];
		conveyorpartposition[1] = conveyorpartposition[0];



	}


}
 
Share this answer
 
Comments
Stefan_Lang 18-Dec-19 3:11am    
Hello

You shouldn't post additional information to your question as a solution. For one it isn't obvious that you are the same person who posted the question. Second you haven't added any comments on what this code dump is about. Third it is way too much text for anyone to take the time and read it, much less make an educated guess what it really is. I only recognized it for what it is, because I've already taken some time to look at your other code.

The expected way to update your original question, is modifying it. You can do so by pressing the link labeled (in green) as [Improve Question], at the lower right of your question. (you may need to hover the mouse pointer over the end of your question to make the edit options appear)

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