Core Java · Chapter 21 of 48

Enums

By the end of this chapter you can model a fixed list of values as an enum, give each value its own data, switch over them safely, and count things per value with an EnumMap. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Objects
  • 5 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

The New Delhi counter sold a "Masla chai" on Tuesday, and on Wednesday the day report had a line for "masala Chai" too. Items are plain strings in the old till, so every typo becomes a new product, and Ravi's sales totals no longer add up. Anjali also wants each station to greet customers in its own language.

With what you know so far, you would keep the menu as String constants or numbers (1 for masala chai, 2 for cutting chai). Nothing stops a method receiving "Masla chai" or 7, the compiler cannot tell you which item you forgot in an if chain, and the price lives far away from the name.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Item.java: list the menu from the enum's values

    In Item.java, make menu() return one line per item, in the order the values are declared: the label, then ": ₹", then the price.

    For example, the list is Masala chai: ₹40, Cutting chai: ₹25, Bun maska: ₹50 and Parle-G: ₹10, in that order.

  2. Task 2 · interview question

    Interview: Item.java, parse an item name, knowing when valueOf throws

    In Item.java, make parse(text) return the item for what the counter typed: spaces around it are ignored, case does not matter, and a space or a hyphen inside becomes an underscore. Anything that is not an item, a blank text too, throws an IllegalArgumentException whose message is "Unknown item: " followed by exactly the text that was typed, spaces and all.

    For example, " masala chai " is MASALA_CHAI, "Cutting Chai" is CUTTING_CHAI, "parle-g" is PARLE_G and "BUN_MASKA" is BUN_MASKA. "Lassi" throws with the message Unknown item: Lassi, and two spaces throw with Unknown item: followed by a space and the two spaces.

  3. Task 3

    Station.java: greet customers with a switch on the station

    In Station.java, make greeting() return the welcome word in the station's language, using a switch on the station itself: SBC gives "Namaskara", MAS gives "Vanakkam", CSMT gives "Namaskar" and NDLS gives "Namaste".

    For example, Station.SBC.greeting() is Namaskara and Station.NDLS.greeting() is Namaste.

  4. Task 4

    Station.java: find the next station on the route

    In Station.java, make next() return the station after this one in route order, the order the values are declared. After the last station comes the first again.

    For example, SBC.next() is MAS, MAS.next() is CSMT, CSMT.next() is NDLS and NDLS.next() is SBC.

  5. Task 5

    Sales.java: tally a day's sales in an EnumMap

    In Sales.java, make tally(sold) return a new EnumMap with how many times each item appears in the list. Items that were not sold are left out (no key at all, not 0), so a day with no sales gives an empty map. The keys come out in the order the items are declared, which an EnumMap does on its own.

    For example, PARLE_G, MASALA_CHAI, PARLE_G, MASALA_CHAI, MASALA_CHAI gives MASALA_CHAI 3 and PARLE_G 2, with MASALA_CHAI first and no BUN_MASKA key.

What you will learn

  • enum: a type with a fixed set of values, each one object, compared safely with ==.
  • Fields, constructors and methods on an enum, and what values(), ordinal() and name() give you.
  • switch over an enum, where the compiler checks every value is covered.
  • valueOf and why it throws, and EnumMap, a map keyed by an enum.

More in Objects

Build "Enums" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.