import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.DateFormat;
import java.util.Locale;
import java.util.Currency;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.Double;
import java.net.*;


public class WhereIsIt extends HttpServlet {
    class DNpair {
        public double rate;
        public NumberFormat nf;
        public DNpair(double s, NumberFormat n){
            rate=s;
            nf=n;
        }
    }
    List<String> currencies;
    Map<String,DNpair>  rate_map = new HashMap<>();
    double price;

    public void init(){
        price = 1000.25;
        currencies=Arrays.asList(new String []{"US Dollars","Japanese Yen","UK Pounds","EUROS"});
        rate_map.put("US Dollars",new DNpair(1.0,NumberFormat.getCurrencyInstance(Locale.forLanguageTag("en-US"))));
        rate_map.put("Japanese Yen",new DNpair(102.53,NumberFormat.getCurrencyInstance(Locale.forLanguageTag("jp"))));
        rate_map.put("UK Pounds",new DNpair(.60,NumberFormat.getCurrencyInstance(Locale.forLanguageTag("en-GB"))));
        rate_map.put("EUROS",new DNpair(.73,NumberFormat.getCurrencyInstance(Locale.forLanguageTag("fr"))));
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {  
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String the_date=new Date(System.currentTimeMillis()).toString();
        out.println("<html><head>");

        out.println("<meta charset=\"UTF-8\"></head>");
        out.println("<body><b>"); 
        out.println("Session started  "+the_date+"<br />&nbsp;<br />");
        String the_url=request.getScheme()+"://"+
                       request.getServerName()+
                       ":"+request.getServerPort()+request.getRequestURI();
        out.println("The calling URL is "+the_url+"<br />&nbsp;<br />");
        out.println("Another Way (still not it)&nbsp;&nbsp;&nbsp;  "+request.getRequestURL()+"<br />&nbsp;<br />");
        out.println("The calling IP Address is "+request.getRemoteAddr()+"<br />(some discussion required)<br />&nbsp;<br />");
        InetAddress the_address=InetAddress.getByName(request.getRemoteAddr());
        out.println("The calling Host Name is "+the_address.getHostName()+"<br />(some discussion required)<br />&nbsp;<br /></b>");
        out.println("<hr /><h1>Dates</h1><b>");
        out.println("Use the Locale to get the format<br>");
        out.println("My LanguageTag is "+request.getLocale()+".");
        int style = DateFormat.FULL; //MEDIUM, SHORT
        Date date = new Date();
        DateFormat df;
        out.println("<table border=\"1px\">");
        df = DateFormat.getDateInstance(style, Locale.forLanguageTag("en-US"));
        out.println("<tr><td>&nbsp;In the US: &nbsp;</td><td>&nbsp;" + df.format(date)+"&nbsp;</td></tr>");
        df = DateFormat.getDateInstance(style, Locale.forLanguageTag("en-GB"));
        out.println("<tr><td>&nbsp;In the United Kingdom:&nbsp;</td><td>&nbsp; " + df.format(date)+"&nbsp;</td></tr>");
        df = DateFormat.getDateInstance(style, Locale.forLanguageTag("fr"));
        out.println("<tr><td>&nbsp;In France: &nbsp;</td><td>&nbsp;" + df.format(date)+"&nbsp;</td></tr>");
        df = DateFormat.getDateInstance(style, Locale.ITALY);
        out.println("<tr><td>&nbsp;In Italy: &nbsp;</td><td>&nbsp;" + df.format(date)+"&nbsp;</td></tr>");
        df = DateFormat.getDateInstance(style, Locale.forLanguageTag("de"));
        out.println("<tr><td>&nbsp;In Germany: &nbsp;</td><td>&nbsp;" + df.format(date)+"&nbsp;</td></tr>");
        df = DateFormat.getDateInstance(style, Locale.CHINA);
        out.println("<tr><td>&nbsp;In China: &nbsp;</td><td>&nbsp;" + df.format(date)+"&nbsp;</td></tr>");
        out.println("<tr><td>&nbsp;In China&nbsp;(MEDIUM):&nbsp;</td><td>&nbsp; " + DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA).format(date)+"&nbsp;</td></tr>");
        out.println("<tr><td>&nbsp;At Client Locale:&nbsp;</td><td>&nbsp; " + DateFormat.getDateInstance(DateFormat.FULL, request.getLocale()).format(date)+"&nbsp;</td></tr>");
        out.println("</table><hr /> <h1> Currencies</h1> Amount 1000.25");
        NumberFormat this_nf;
        double this_rate;
        out.println("<table border=\"1px\">");
        for (String s :currencies) {
            this_nf=rate_map.get(s).nf;
            this_rate=rate_map.get(s).rate;
            out.println("<tr><td>&nbsp;In "+ s+" &nbsp;</td><td>&nbsp;" + this_nf.format(this_rate*price)+"&nbsp;</td></tr>");;
        }
        out.println("</table></b></body>");
        out.println("</html>");
    }

    public void destroy(){}

}