|
Checklists
A Programming Pattern |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
String[] (named checklist)
is very flexibleboolean[] assumes that everything
accomplished is on the checklistString[] (named completed)
is more flexibledone will contain true
if and only if checklist[index] has been completed
/**
* An n out of n checklist.
*
* @param checklist The checklist
* @param accomplished The array of accomplished items
* @return true if the Checklist is satisfied; false otherwise
*/
private static boolean checkFor(String[] checklist, String[] accomplished) {
boolean checked;
for (int c = 0; c < checklist.length; c++) {
checked = false;
for (int a = 0; a < accomplished.length; a++) {
if (checklist[c].equals(accomplished[a])) {
checked = true;
break;
}
}
if (!checked) return false; // An item was not accomplished
}
return true; // All items were accomplished
}
/**
* An m (required) out of n (size) checklist.
*
* @param checklist The checklist
* @param accomplished The array of accomplished items
* @param needed The required number of checklist items
* @return true if the checklist is satisfied; false otherwise
*/
private static boolean checkFor(String[] checklist, String[] accomplished,
int needed) {
int count;
count = 0;
for (int c = 0; c < checklist.length; c++) {
for (int a = 0; a < accomplished.length; a++) {
if (checklist[c].equals(accomplished[a])) {
count++;
if (count >= needed) return true;
else break;
}
}
}
return false; // Not enough items were accomplished
}
checklist:
"Shirts",
"Socks",
"Pants",
"Skirts"
completed for the Inflexible Variant:
"Shirts",
"Socks",
"Pants",
"Dresses",
"Shoes"
"Socks",
"Shirts",
"Skirts",
"Pants"
completed for the Flexible Variant
when needed is 2):
"Shirts",
"Socks",
"Pants",
"Dresses",
"Shoes"
"Dresses",
"Shirts"