Automatic Matching of Birthday Constellations

Keywords: Android Google github

2017/3/4, Saturday

Recently, the project to do personal information settings module, encountered a birthday constellation automatic matching problem, so Baidu, Google doubled, most of the online answers are like this:

  //Constellation Segmentation Time
    int[] date = {20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22};

    String[][] constellations = {{"Capricornus", "Aquarius"}, {"Aquarius", "Pisces"}, {"Pisces", "Aries"}, {"Aries", "Taurus"}, {"Taurus", "Gemini"}, {"Gemini", "Cancer"}, {"Cancer", "Leo"}, {"Leo", "Virgo"}, {"Virgo", "Libra"}, {"Libra", "Scorpio"}, {"Scorpio", "Sagittarius"}, {"Sagittarius", "Capricornus"}};

    //Constellation generation is passed in the date format of: yyyy-mm-dd
    public void setConstellations(String birthday) {
        String[] data = birthday.split("-");
        int day = date[Integer.parseInt(data[1]) - 1];
        String[] cl1 = constellations[Integer.parseInt(data[1]) - 1];
        if (Integer.parseInt(data[2]) >= day) {
            tvUserDetailAstrology.setText(cl1[1]);
        } else {
            tvUserDetailAstrology.setText(cl1[0]);
        }
    }

Each month's corresponding constellation is stored in a two-dimensional array, and then the day is used to determine which constellation belongs to. This is the most common way of implementation, and it's more common.

But for Android development and code cleanliness, we are sure to think of all ways to prevent Chinese characters from appearing in the code, so we can do this:

  private static String[] astrologyArray;
    private static String[][] constellations = new String[12][2];
    static {
        astrologyArray = AppContext.getInstance().getResources().getStringArray(R.array.astrology_array);
        for (int i = 0; i < constellations.length; i++) {
            int k = i;
            for (int j = 0; j < constellations[i].length; j++) {
                constellations[i][j] = astrologyArray[k];
                k += 1;
            }
        }
    }

In the string file:

 <string-array name="astrology_array">
        <item>Capricorn</item>
        <item>Aquarius</item>
        <item>Pisces</item>
        <item>Aries</item>
        <item>Taurus</item>
        <item>Gemini</item>
        <item>Cancer</item>
        <item>Leo</item>
        <item>Virgo</item>
        <item>Libra</item>
        <item>Scorpio</item>
        <item>Sagittarius</item>
        <item>Capricorn</item>
 </string-array>

Although the code is so much lost, but it is still worth it! ____________

In this way, our thinking is the same as before, just to recapture the two-dimensional array, looking at a lot of pleasant!

After carefully observing the constellation's one-dimensional array and two-dimensional array, we find that they are actually adjacent. Would this two-dimensional array be a little redundant? Yes, you're right. It's a junior three!

 //Write it here first for easy viewing. It must be written in the code as follows ___________ string Inside, there's no reason.~
    private static String[] astrologyArray = {"Capricorn, Aquarius, Pisces, Aries, Taurus, Gemini, Cancer, Leo, Virgo, Libra, Scorpio, Sagittarius, Capricorn"};
    //Constellation Segmentation Time
    int[] date = {20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22};

    //Constellation generation is passed in the date format of: yyyy-mm-dd
    public void setConstellations(String birthday) {
        String[] data = birthday.split("-");
        int month = Integer.parseInt(data[1]);
        int compareDay = date[month - 1];
        // The query date is indexed before the partition date-1,Otherwise it will remain unchanged
        if (Integer.parseInt(data[2]) >= compareDay) {
            tvUserDetailAstrology.setText(astrologyArray[month]);
        } else {
            tvUserDetailAstrology.setText(astrologyArray[month - 1]);
        }
    }

So we only need one-dimensional array of this constellation to find the constellation corresponding to that date accurately.

Due to my limited level, if there are any mistakes in this article, I would like to welcome criticism and correction. My younger brother will be very grateful.

Finally, I would like to thank the three brothers of the rabbit family (http://www.jianshu.com/u/87ae381f8e5b) for their help and guidance in my work. Thank you!

Demo address on GiutHub: https://github.com/IT-Talon/Constellation Select. If it is helpful for your study, I hope to order a star. Thank you!

Posted by ashwood on Wed, 10 Apr 2019 21:30:31 -0700