Chapter 19 - Exercise 9

(5) Modify class PostOffice so that it uses an EnumMap. Project:2 Specialized languages like Prolog use backward chaining in order to solve problems like this. Using PostOffice.java for inspiration, research such languages and develop a program that allows new "rules" to be easily added to the system.


Mail.java



package com.ciaoshen.thinkinjava.chapter19;
import java.util.*;

public class Mail {
    // The NO’s lower the probability of random selection:
    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 Forward {YES,NO}
    enum ForwardAddress {INCORRECT,OK1,OK2,OK3,OK4,OK5}
    enum ReturnAddress {MISSING,OK1,OK2,OK3,OK4,OK5}
    
    GeneralDelivery generalDelivery;
    Scannability scannability;
    Readability readability;
    Address address;
    Forward forward;
    ForwardAddress forwardAddress;
    ReturnAddress returnAddress;
    
    static long counter = 0;
    long id = counter++;
    public String toString() { return "Mail " + id; }
    public String details() {
        return toString() +
        ", General Delivery: " + generalDelivery +
        ", Address Scanability: " + scannability +
        ", Address Readability: " + readability +
        ", Address Address: " + address +
        ", Forward: " + forward +
        ", Forward Address: " + forwardAddress +
        ", Return address: " + returnAddress;
    }
    // Generate test Mail:
    public static Mail randomMail() {
        Mail m = new Mail();
        m.generalDelivery= Enums.random(GeneralDelivery.class);
        m.scannability = Enums.random(Scannability.class);
        m.readability = Enums.random(Readability.class);
        m.address = Enums.random(Address.class);
        m.forward = Enums.random(Forward.class);
        m.forwardAddress = Enums.random(ForwardAddress.class);
        m.returnAddress = Enums.random(ReturnAddress.class);
        return m;
    }
    public static Iterable<Mail> generator(final int count) {
        return new Iterable<Mail>() {
            int n = count;
            public Iterator<Mail> iterator() {
                return new Iterator<Mail>() {
                    public boolean hasNext() { return n-- > 0; }
                    public Mail next() { return randomMail(); }
                    public void remove() { // Not implemented
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }
}


PostOffice9.java



package com.ciaoshen.thinkinjava.chapter19;
import java.util.*;

public class PostOffice9 {
    enum MailHandler { GENERAL_DELIVERY, MACHINE_SCAN, VISUAL_INSPECTION, FORWARD_STEP, RETURN_TO_SENDER }
    
    public static interface Handler{public boolean handle(Mail m);}
    
    private static EnumMap<MailHandler, PostOffice9.Handler> em=new EnumMap<MailHandler,PostOffice9.Handler>(MailHandler.class);
    
    static{
        em.put(MailHandler.GENERAL_DELIVERY,new PostOffice9.Handler(){
            public boolean handle(Mail m) {
                switch(m.generalDelivery) {
                    case YES:
                        System.out.println("Using general delivery for " + m);
                        return true;
                    default: return false;
                }
            }
        });
        em.put(MailHandler.MACHINE_SCAN, new PostOffice9.Handler(){
            public boolean handle(Mail m) {
                switch(m.scannability) {
                    case UNSCANNABLE: return false;
                    default:
                        switch(m.address) {
                            case INCORRECT: return false;
                            default:
                                System.out.println("Delivering "+ m + " automatically");
                                return true;
                        }
                }
            }
        });
        em.put(MailHandler.VISUAL_INSPECTION, new PostOffice9.Handler(){
            public boolean handle(Mail m) {
                switch(m.readability) {
                    case ILLEGIBLE: return false;
                    default:
                        switch(m.address) {
                            case INCORRECT: return false;
                            default:
                                System.out.println("Delivering " + m + " normally");
                                return true;
                        }
                }
            }
        });
        em.put(MailHandler.FORWARD_STEP, new PostOffice9.Handler(){
            public boolean handle(Mail m) {
                switch(m.forward){
                    case NO:
                        return false;
                    default:
                        switch(m.forwardAddress){
                            case INCORRECT: return false;
                            default:
                                System.out.println("Forward "+ m +" normally");
                                return true;
                        }
                }
            }
        });
        em.put(MailHandler.RETURN_TO_SENDER, new PostOffice9.Handler(){
            public boolean handle(Mail m) {
                switch(m.returnAddress) {
                    case MISSING: return false;
                    default:
                        System.out.println("Returning " + m + " to sender");
                        return true;
                }
            }
        });
    }


    public static void handle(Mail m) {
        for(Map.Entry<MailHandler,PostOffice9.Handler> entry: PostOffice9.em.entrySet()){
            if(entry.getValue()!=null){
               if(entry.getValue().handle(m)){
                   return;
               }
            }
        }
        System.out.println(m + " is a dead letter");
    }
    
    public static void showHandleMap(){
        System.out.println(em);
    }
    
    public static void main(String[] args){
        
    }
}


Exercise9.java



package com.ciaoshen.thinkinjava.chapter19;
import java.util.*;

public class Exercise9 {
    public static void main(String[] args) {
        for(Mail mail : Mail.generator(10)) {
            System.out.println(mail.details());
            PostOffice9.handle(mail);
            System.out.println("*****");
        }
    }
}



Need Java Developers? Hire Me »

I know Java. Talk is cheap, watch my code.

I live in CANADA now.