THINK FIRST·CODE LATER

← All labs

Library cards: static vs instance members

Problem

Write a class Member with:

  • static fields nextNumber (starts at 1) and activeCount, and a constant PREFIX = "LIB";
  • instance fields id, name, active;
  • a constructor that gives each member the next id LIB-0001, LIB-0002, … (String.format("%04d", …));
  • cancel(), static getActiveCount() and static getIssuedCount(), and toString() → LIB-0002 Omar (cancelled).

LibraryCards.main reads commands join Name and cancel LIB-0002 until the end of input (Welcome Lina, your card is LIB-0001, Cancelled LIB-0002 or No card LIB-0009), then prints all members, Cards issued: 3 and Active members: 2.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l12-2-data-1.txt

join Lina join Omar join Mei cancel LIB-0002
What your program must print for l12-2-data-1.txt
Welcome Lina, your card is LIB-0001
Welcome Omar, your card is LIB-0002
Welcome Mei, your card is LIB-0003
Cancelled LIB-0002
--- members ---
LIB-0001 Lina
LIB-0002 Omar (cancelled)
LIB-0003 Mei
Cards issued: 3
Active members: 2

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l12-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content l12-2-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.