Saturday, 11 March 2017

Chapter 33 Exercise 7, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

33.7 (Guess birthday) Listing 4.3, GuessBirthday.java, gives a program for guessing a birthday. Write a JSF program that displays five sets of numbers, as shown in Figure 33.30a. After the user checks the appropriate boxes and clicks the Guess Birthday button, the program displays the birthday, as shown in Figure 33.30b.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class Exercise07 {


    private List<List<List<Integer>>> dates = new ArrayList<>();
    private List<Boolean> checked = new ArrayList<>();
    
    public Exercise07() {
        List<List<Integer>> tmpList = new ArrayList<>();
        tmpList.add(Arrays.asList(new Integer[]{1, 3, 5, 7}));
        tmpList.add(Arrays.asList(new Integer[]{9, 11, 13, 15}));
        tmpList.add(Arrays.asList(new Integer[]{17, 19, 21, 23}));
        tmpList.add(Arrays.asList(new Integer[]{25, 27, 29, 31}));
        dates.add(tmpList);
        tmpList = new ArrayList<>();
        tmpList.add(Arrays.asList(new Integer[]{2, 3, 6, 7}));
        tmpList.add(Arrays.asList(new Integer[]{10, 11, 14, 15}));
        tmpList.add(Arrays.asList(new Integer[]{18, 19, 22, 23}));
        tmpList.add(Arrays.asList(new Integer[]{26, 27, 30, 31}));
        dates.add(tmpList);
        tmpList = new ArrayList<>();
        tmpList.add(Arrays.asList(new Integer[]{4, 5, 6, 7}));
        tmpList.add(Arrays.asList(new Integer[]{12, 13, 14, 15}));
        tmpList.add(Arrays.asList(new Integer[]{20, 21, 22, 23}));
        tmpList.add(Arrays.asList(new Integer[]{28, 29, 30, 31}));
        dates.add(tmpList);
        tmpList = new ArrayList<>();
        tmpList.add(Arrays.asList(new Integer[]{8, 9, 10, 11}));
        tmpList.add(Arrays.asList(new Integer[]{12, 13, 14, 15}));
        tmpList.add(Arrays.asList(new Integer[]{24, 25, 26, 27}));
        tmpList.add(Arrays.asList(new Integer[]{28, 29, 30, 31}));
        dates.add(tmpList);
        tmpList = new ArrayList<>();
        tmpList.add(Arrays.asList(new Integer[]{16, 17, 18, 19}));
        tmpList.add(Arrays.asList(new Integer[]{20, 21, 22, 23}));
        tmpList.add(Arrays.asList(new Integer[]{24, 25, 26, 27}));
        tmpList.add(Arrays.asList(new Integer[]{28, 29, 30, 31}));
        dates.add(tmpList);
        for (int i = 0; i < 5; i++) {
            checked.add(Boolean.FALSE);
        }
    }

    public void setChecked(List<Boolean> checked) {
        this.checked = checked;
    }

    public void setDates(List<List<List<Integer>>> dates) {
        this.dates = dates;
    }
    
    public List<Boolean> getChecked() {
        return checked;
    }

    public List<List<List<Integer>>> getDates() {
        return dates;
    }
    
    public String getDay() {
        int day = 0;
        for (int i = 0; i < checked.size(); i++) {
            if(checked.get(i)) {
                day += dates.get(i).get(0).get(0);
            }
        }
        if(day == 0) {
            return "";
        } else {
            return "Your birth day is " + day;
        }
    }    
}

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Exercise07</title>
    </h:head>
    <h:body>
        <h:form>
            <center>
            <br />
            <br />
            <table>
                <tr><td  colspan="5"><center>Check the boxes if your birthday is in these sets</center></td></tr>
                <tr>
                <ui:repeat value="#{exercise07.dates}" var="dates1">
                    <td width = "120"><fieldset><table>
                        <ui:repeat value="#{dates1}" var="dates2">
                            <tr>
                                <ui:repeat value="#{dates2}" var="dates3">
                                    <td width = "20"><h:outputLabel value="#{dates3}"/></td>
                                </ui:repeat>
                            </tr>
                        </ui:repeat>
                    </table></fieldset></td>
                </ui:repeat>
                </tr>
                <tr>
                    <ui:repeat value="#{exercise07.checked}" var="checked" varStatus="loop">
                        <td width = "120"><center><h:selectBooleanCheckbox value="#{exercise07.checked[loop.index]}" /></center></td>
                    </ui:repeat>
                </tr>
            </table><br />
            <input type = "submit" value = "Find date" />
            <br /><br /><h:outputLabel  style="color:red"  value="#{exercise07.getDay()}"/>
            </center>
        </h:form>
    </h:body>
</html>

No comments :

Post a Comment