OnJava8-Examples/enums/PostOffice.java

198 lines
5.5 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// enums/PostOffice.java
2016-12-30 17:23:13 -08:00
// (c)2017 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Modeling a post office
2015-06-15 17:47:35 -07:00
import java.util.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
class Mail {
2017-01-20 21:30:44 -08:00
// The NO's reduce probability of random selection:
2015-06-15 17:47:35 -07:00
enum GeneralDelivery {YES,NO1,NO2,NO3,NO4,NO5}
enum Scannability {UNSCANNABLE,YES1,YES2,YES3,YES4}
enum Readability {ILLEGIBLE,YES1,YES2,YES3,YES4}
enum Address {INCORRECT,OK1,OK2,OK3,OK4,OK5,OK6}
enum ReturnAddress {MISSING,OK1,OK2,OK3,OK4,OK5}
GeneralDelivery generalDelivery;
Scannability scannability;
Readability readability;
Address address;
ReturnAddress returnAddress;
static long counter = 0;
long id = counter++;
@Override
public String toString() { return "Mail " + id; }
public String details() {
return toString() +
", General Delivery: " + generalDelivery +
", Address Scanability: " + scannability +
", Address Readability: " + readability +
", Address Address: " + address +
", Return address: " + returnAddress;
}
// Generate test Mail:
public static Mail randomMail() {
Mail m = new Mail();
2016-01-25 18:05:55 -08:00
m.generalDelivery =
Enums.random(GeneralDelivery.class);
2017-01-20 21:30:44 -08:00
m.scannability =
Enums.random(Scannability.class);
m.readability =
Enums.random(Readability.class);
2015-06-15 17:47:35 -07:00
m.address = Enums.random(Address.class);
2017-01-20 21:30:44 -08:00
m.returnAddress =
Enums.random(ReturnAddress.class);
2015-06-15 17:47:35 -07:00
return m;
}
2016-01-25 18:05:55 -08:00
public static
Iterable<Mail> generator(final int count) {
2015-06-15 17:47:35 -07:00
return new Iterable<Mail>() {
int n = count;
@Override
public Iterator<Mail> iterator() {
return new Iterator<Mail>() {
@Override
2017-01-20 21:30:44 -08:00
public boolean hasNext() {
return n-- > 0;
}
2015-06-15 17:47:35 -07:00
@Override
2017-01-20 21:30:44 -08:00
public Mail next() {
return randomMail();
}
2015-06-15 17:47:35 -07:00
@Override
public void remove() { // Not implemented
throw new UnsupportedOperationException();
}
};
}
};
}
}
public class PostOffice {
enum MailHandler {
GENERAL_DELIVERY {
@Override
boolean handle(Mail m) {
switch(m.generalDelivery) {
case YES:
2015-12-02 09:20:27 -08:00
System.out.println(
"Using general delivery for " + m);
2015-06-15 17:47:35 -07:00
return true;
default: return false;
}
}
},
MACHINE_SCAN {
@Override
boolean handle(Mail m) {
switch(m.scannability) {
case UNSCANNABLE: return false;
default:
switch(m.address) {
case INCORRECT: return false;
default:
2015-12-02 09:20:27 -08:00
System.out.println(
"Delivering "+ m + " automatically");
2015-06-15 17:47:35 -07:00
return true;
}
}
}
},
VISUAL_INSPECTION {
@Override
boolean handle(Mail m) {
switch(m.readability) {
case ILLEGIBLE: return false;
default:
switch(m.address) {
case INCORRECT: return false;
default:
2015-12-02 09:20:27 -08:00
System.out.println(
"Delivering " + m + " normally");
2015-06-15 17:47:35 -07:00
return true;
}
}
}
},
RETURN_TO_SENDER {
@Override
boolean handle(Mail m) {
switch(m.returnAddress) {
case MISSING: return false;
default:
2015-12-02 09:20:27 -08:00
System.out.println(
"Returning " + m + " to sender");
2015-06-15 17:47:35 -07:00
return true;
}
}
};
abstract boolean handle(Mail m);
}
static void handle(Mail m) {
for(MailHandler handler : MailHandler.values())
if(handler.handle(m))
return;
2015-11-03 12:00:44 -08:00
System.out.println(m + " is a dead letter");
2015-06-15 17:47:35 -07:00
}
public static void main(String[] args) {
for(Mail mail : Mail.generator(10)) {
2015-11-03 12:00:44 -08:00
System.out.println(mail.details());
2015-06-15 17:47:35 -07:00
handle(mail);
2015-11-03 12:00:44 -08:00
System.out.println("*****");
2015-06-15 17:47:35 -07:00
}
}
2015-09-07 11:44:36 -06:00
}
/* Output:
Mail 0, General Delivery: NO2, Address Scanability:
UNSCANNABLE, Address Readability: YES3, Address Address:
OK1, Return address: OK1
Delivering Mail 0 normally
2015-06-15 17:47:35 -07:00
*****
Mail 1, General Delivery: NO5, Address Scanability: YES3,
Address Readability: ILLEGIBLE, Address Address: OK5,
Return address: OK1
2015-06-15 17:47:35 -07:00
Delivering Mail 1 automatically
*****
Mail 2, General Delivery: YES, Address Scanability: YES3,
Address Readability: YES1, Address Address: OK1, Return
address: OK5
Using general delivery for Mail 2
2015-06-15 17:47:35 -07:00
*****
Mail 3, General Delivery: NO4, Address Scanability: YES3,
Address Readability: YES1, Address Address: INCORRECT,
Return address: OK4
Returning Mail 3 to sender
2015-06-15 17:47:35 -07:00
*****
Mail 4, General Delivery: NO4, Address Scanability:
UNSCANNABLE, Address Readability: YES1, Address Address:
INCORRECT, Return address: OK2
Returning Mail 4 to sender
2015-06-15 17:47:35 -07:00
*****
Mail 5, General Delivery: NO3, Address Scanability: YES1,
Address Readability: ILLEGIBLE, Address Address: OK4,
Return address: OK2
2015-06-15 17:47:35 -07:00
Delivering Mail 5 automatically
*****
Mail 6, General Delivery: YES, Address Scanability: YES4,
Address Readability: ILLEGIBLE, Address Address: OK4,
Return address: OK4
2015-06-15 17:47:35 -07:00
Using general delivery for Mail 6
*****
Mail 7, General Delivery: YES, Address Scanability: YES3,
Address Readability: YES4, Address Address: OK2, Return
2015-06-15 17:47:35 -07:00
address: MISSING
Using general delivery for Mail 7
2015-06-15 17:47:35 -07:00
*****
Mail 8, General Delivery: NO3, Address Scanability: YES1,
Address Readability: YES3, Address Address: INCORRECT,
Return address: MISSING
Mail 8 is a dead letter
2015-06-15 17:47:35 -07:00
*****
Mail 9, General Delivery: NO1, Address Scanability:
UNSCANNABLE, Address Readability: YES2, Address Address:
OK1, Return address: OK4
Delivering Mail 9 normally
2015-06-15 17:47:35 -07:00
*****
2015-09-07 11:44:36 -06:00
*/