jpX
jpX

Reputation: 29

How do I format the logic for 3 conditions and use a previous condition as well?

NOT ASKING FOR ANYONE TO WRITE THE PROGRAM FOR ME

I am having trouble understanding how to setup the logic for a program given these following conditions:

(Example: a book returned 10 days late incurs a late fee of $1.30 late fee, $0.10 for each of the first 7 days plus $0.20 for each of the remaining 3 days)

This is where I am having issues. I am not sure how to include the previous condition along with this one. My best guess is some sort of nested loop or an elseif containing this condition and something about the last one?

This is my best attempt, but I'm not sure this is the most efficient logic:

    if (daysLate > 90)
    {
        costDue = bookPrice + 10;
    }
    else if (daysLate <= 90)
    {
        costDue = (7*0.10) * (daysLate*0.20);
    }
    else if (daysLate <= 7)
    {
        costDue = daysLate*0.10;
    }
    else
    {
        IO.reportBadInput();
    }

Upvotes: 0

Views: 76

Answers (6)

darioo
darioo

Reputation: 47193

Another way to do this, while sacrificing readability a bit, and eliminating one else-if condition:

    if (daysLate > 90)  {
        costDue = bookPrice + 10;
    }
    else if (daysLate >= 0 && daysLate <= 90) {
        costDue = Math.min(7, daysLate) * 0.10 + Math.max(daysLate - 7,  0) * 0.20;
    } 
    else {
        IO.reportBadInput();
    }

Upvotes: 0

sstan
sstan

Reputation: 36513

if (daysLate > 90)
{
    costDue = bookPrice + 10;
}
else if (daysLate >= 0)
{
    costDue = daysLate * 0.10;
    if (daysLate > 7) {
        costDue += (daysLate - 7) * 0.10;
    }
} else {
    IO.reportBadInput();
}

Upvotes: 1

M A
M A

Reputation: 72874

The second condition should be changed, otherwise the third if won't be reached. Also the third condition should be changed to check if the daysLate variable is greater or equal to zero:

if (daysLate > 90)
{
    costDue = bookPrice + 10;
}
else if (daysLate >= 7)
{
    costDue = (7*0.10) + ((daysLate - 7) * 0.20);  // change here
}
else if (daysLate >= 0)
{
    costDue = daysLate*0.10;
}
else
{
    IO.reportBadInput();
}

Upvotes: 2

rgalbo
rgalbo

Reputation: 4465

When you use an if statement you have to make sure that it separates the cases that you want to keep separate. In your code, the first else if statement includes all instances where daysLate <= 90, so the second else if would never execute.

Also check your past 7 days due computation, its seems like you would be overcharging people for those first 7 days.

Upvotes: 1

Sharon Ben Asher
Sharon Ben Asher

Reputation: 14348

addition to all previous answers - the calculation for mid-term (between 7 days and 90) should have an addition instead of multiplication (7*0.10) + (daysLate*0.20); or even 1.30 + (daysLate*0.20); (as the problem description suggests)

Upvotes: 1

Will Hartung
Will Hartung

Reputation: 118714

   if (daysLate > 90)
    {
        costDue = bookPrice + 10;
    }
    else if (daysLate > 7 && daysLate <= 90)
    {
        costDue = (7*0.10) * (daysLate*0.20);
    }
    else
    {
        costDue = daysLate*0.10;
    }

Simply update the second clause and drop that last one, that'll straighten it out.

Upvotes: 1

Related Questions