GithubHelp home page GithubHelp logo

fizzbuzzwizz's Introduction

FizzBuzzWizz

环境

  • win
  • jdk1.8

运行步骤

javar -jar Question.jar
  • 按要求输入

思路

  • 采用策略模式, 学生与策略建立联系, HashMap<Student, IStrategy>

    • IStratage -- 抽象接口
      • NormalStrategy 输出数字
      • FirstStrategy 输出 Fizz
      • ..
      • FirstSecondStrategy 输出 FizzBuzz
      • ...
      • HighestStartegy 包含第一个特殊数字的策略类,输出 Fizz
  • 为普通属性的 student 注册 普通的策略类

  • 为 固有倍数关系的 student 的注册倍数策略

  • 为 特殊属性的 student 注册 HighestStartegy 策略


核心代码

  • 倍数关系的策略绑定
    public static TreeMap<Integer, IStrategy> bindStrategy(int[] array) {
        TreeMap<Integer, IStrategy> map = new TreeMap<>();
        map.put(1, new NormalStartegy()); //最基础的
        map.put(array[0], new FirstStrategy());
        map.put(array[1], new SecondStrategy());
        map.put(array[2], new ThirdStrategy());
        map.put(array[0] * array[1], new FirstSecondStartegy());
        map.put(array[0] * array[2], new FirstThirdStartegy());
        map.put(array[1] * array[2], new SecondThirdStartegy());
        map.put(array[0] * array[1] * array[2], new ALLStartegy());
        return map;
    }
  • 添加学生, 按策略添加
    • 根据HashMap的属性,存在的元素 value 会覆盖,实现策略覆盖
   private void addStrategy() {
       strategyMap = Tools.bindStrategy(array); //数字与策略绑定
       //倍数策略
       for (Map.Entry<Integer, IStrategy> entry : strategyMap.entrySet()) {
           int index = entry.getKey(); //基数
           IStrategy strategy = entry.getValue();
           for (int j = 1; index * j <= maxSize; j++) { //按倍率添加
               stuMap.put(new Student(index * j), strategy);
           }
       }

       // 特殊策略 包含第一个数字的
       IStrategy strategy = new HighestStartegy();  // 最高的策略
       for (int i = 0; i < 10; i++) {
           //array[0]0 - array[0]9 如 40 - 49
           stuMap.put(new Student(array[0] * 10 + i), strategy);

           //0array[0] - 9array[0] 如: 04, 14, 24..94
           stuMap.put(new Student(i * 10 + array[0]), strategy);
       }
   }

fizzbuzzwizz's People

Contributors

7polo avatar

Stargazers

GanZhiQiang avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.