Atlan
Atlan

Reputation: 53

if/elseif command not working properly

Dear StackOverflow community,

As I've already experienced your willingness to help over and over again, I came here once more for help.

As of now, I have a simulator that contains "customers". I want to categorize them by assigning various customer types (as a property). Up to a certain point, this assignment works pretty fine but somehow, there's still a bug in my code. But see for yourself:

Main function

ncust = 500 %//(number of customers)

%function call

for ii = 1:ncust;
    customers{ii} = Customer(ii);
    customers{ii}.setcustTypeDistrib(ncust);
end

Each customer has a property called "id" (customer 1 has id 1 -> who would've guessed that?)

Class customer

function obj = setcustTypeDistrib(obj, ncust) %sets types of customers
    if obj.id < ceil(ncust/2) % 50 percent of the customers are type 1
        obj.custType = 1;

    elseif ceil(ncust/2) < obj.id || obj.id < ceil(0.75*ncust)
        obj.custType = 2;

    elseif ceil(0.76*ncust) < obj.id 
        obj.custType = 3;
    end
end

However, if I check, for example, customer 450 (in MATLAB: customers{450}) he is customer type 2 although he should be customer type 3. What am I doing wrong? I feel completely lost!


Edit for example

Customer with properties:

id: 490
custType: 2
minInterval: 4.7846e+04

Upvotes: 0

Views: 53

Answers (1)

ShaneQful
ShaneQful

Reputation: 2236

The issue is you are using an 'or' statement in ceil(ncust/2) < obj.id || obj.id < ceil(0.75*ncust) so because of ceil(ncust/2) < obj.ideverything with an object id over half will be type two. Use an 'and' statement and you'll get the desired result.

e.g.

ceil(ncust/2) < obj.id && obj.id < ceil(0.75*ncust)

Upvotes: 1

Related Questions