Java Arena
Step 8 of 8

Putting it together, and the end of part 10

This last step of part 10 puts this module's tools side by side, and then looks back at the whole part.

Which tool when

You want to Use Instead of
count down, or step by more than one for (int i = 10; i > 0; i -= 2) a counter in a while loop
use the positions of a list's values for over the indexes a for-each loop
build a text in a loop StringBuilder and append += on a String
check the form of a string matches and a regular expression a loop with charAt
a value from a fixed set an enum, compared with == Strings or numbers that stand for the choices
remove elements while going through a collection an Iterator and its remove() removing inside a for-each loop

The tools work well together. A regex check that a code has the right form can decide which enum constant the code stands for, a StringBuilder can build a report with a line for each constant in values(), and an iterator can clear out the objects that a report no longer needs. The challenges of this step do all of that, for the tickets of a music festival.

The end of part 10

This step ends the material of the MOOC's tenth part. Module 37 brought streams: stream() and a chain of steps such as filter, map, sorted and distinct, ended by count, sum, average, collect or forEach, with lambda expressions and method references to say what each step does, and reduce, IntStream and the lines of a file as a stream. Module 38 put objects in order: a natural order with Comparable and compareTo, and any other order with a Comparator, built with Comparator.comparing, thenComparing and reversed. This module added a set of smaller tools: the for loop once more, StringBuilder, regular expressions, enums with fields and methods, and iterators. Part 11 comes next, starting with class diagrams. Part 10 ends with the MOOC's summary page on its own site.

Your task

Challenge 1 of 3

A music festival sells three kinds of passes. Pass.java is ready, with a price for each. A ticket's code is the first letter of its pass's name, a dash and four digits: D-0412 is a day pass, W-1001 a weekend pass and B-0007 a backstage pass. In Main.java, write the method passFor(String code), which returns the Pass that a code stands for, or null when the code doesn't have that form. Check the form with matches, and find the pass by going through Pass.values(). The check runs:

System.out.println(passFor("W-1001"));
System.out.println(passFor("B-0007"));
System.out.println(passFor("D-412"));
System.out.println(passFor("X-1234"));

and expects:

WEEKEND
BACKSTAGE
null
null

Challenge 2

The festival's passes once more. What does this program print?

Challenge 3

To end part 10: the festival's box office. Pass.java is the one from the first task, and Ticket.java is ready: a ticket has the name of its holder and a Pass. One person can hold several tickets. In BoxOffice.java, sell is ready. Write:

  • refund(String holder), for a person who can't come after all: it removes every ticket of that holder, with an iterator, and returns the money that goes back, the sum of the prices of the removed tickets' passes (0 when the holder has no tickets)
  • report(), which returns a String built with a StringBuilder: one line for each pass, in the order of Pass.values(), like DAY: 3 sold, 135 dollars, with a line break between the lines and none after the last

Main.java is a test drive, and it prints:

DAY: 3 sold, 135 dollars
WEEKEND: 1 sold, 110 dollars
BACKSTAGE: 1 sold, 250 dollars
Refunded: 405
DAY: 2 sold, 90 dollars
WEEKEND: 0 sold, 0 dollars
BACKSTAGE: 0 sold, 0 dollars