CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 7 of 7 · 3 min read

Exercises

Easy

Easy 1: Spot the smell (20 minutes)

Write a method with an if chain over four payment types: cash, card, UPI and wallet. Then mark where a fifth type would be added.

Done when: you have the method and an arrow or comment showing the exact line that must be edited.

Hint

The new line goes just before the final return.

Easy 2: Run the rewrite (20 minutes)

Type the Pricer code from section 3 and run it for staff, member, student and guest at base 100.

Done when: you print 0, 80, 50 and 100 in that order.

Hint

The list order matters only when two rules match.

Easy 3: Count the edits (20 minutes)

For the old price method and the new Pricer, list which files change when a corporate rate is added.

Done when: you have two lists, one with an old method edited and one with only new code added.

Hint

Count the existing Pricer file as untouched.

Medium

Medium 1: Notification types (45 minutes)

A Notifier class sends email, then SMS, using a switch. Rewrite it with a Channel interface and one class per channel. Then add push notifications.

Done when: the push class was added without editing Notifier, and a short program prints three messages.

Hint

Notifier holds a List<Channel> and calls send on each.

Medium 2: Order of rules (45 minutes)

In Pricer, two rules both match: a member who is also a student. Decide which should win, and change the design so that the order is explicit.

Done when: a test shows the chosen price, and a comment explains how the order is set.

Hint

Use the first match, and place the rules in your preferred order, or give each rule a priority.

Medium 3: Find the repeated chain (45 minutes)

Take code (yours or from a small open project) with the same switch in two places. Describe what happens when a new case is added.

Done when: you list every location that must change and say how an interface would reduce that to one.

Hint

Search for the same constant or enum in several files.

Hard

Hard 1: Report formats (90 minutes)

Build a program that computes room usage totals and prints them as text. Add a CSV output and a "negative numbers in brackets" output without editing the calculation class.

Done when: the calculation class has no import or mention of any output format, and three outputs run.

Hint

Give the printer classes an interface that takes plain numbers.

Hard 2: Closed in what way? (90 minutes)

OCP is "closed against" a kind of change. For Pricer, list one change it is open to (a new customer type) and one change it is still closed to editing for (a rule that needs two inputs, such as time of day). Redesign so that the second is also an extension.

Done when: the new interface takes a context object, and an hour-based rule is added as a new class.

Hint

A Context record with kind, base price and hour is enough.

Hard 3: Argue against it (90 minutes)

Write a short memo for a team of three, with a program of 200 lines and one customer type, arguing that OCP should not be applied yet. Name the sign that would change your mind.

Done when: the memo is under 200 words and names a clear trigger for refactoring.

Hint

A growing list of customer kinds is a good trigger.