V2EX-笔试题 设计个灯 通过按开灯键调节亮度 我这么写没问题吧?第一次求职 感觉有点简单 怕有坑?

gzk329:

/**
 * @Description: 台灯设计 按一次开 按两次提高亮度 按第三次关闭
 * @Author: GZK0329
 * @Date: 2021/5/31
 **/

public class Lamp {
    //台灯目前状态 初始为关闭状态
    private Boolean isOpen = false;

    //按开关键次数统计 按一次开 按两次提高亮度 按第三次关闭
    private int clickCount = 0;

    //亮度统计 0 关闭 1 亮度一级 2 亮度二级
    private int brightness = 0;

    public Lamp() {
    }

    public void click() {
        clickCount++;
        clickCount %= 3;
        switch (clickCount) {
            case 0:
                close();break;
            case 1:
                open();break;
            case 2:
                lightBoost();break;
            default:
                return;
        }
    }

    private void lightBoost() {
        //亮度增加 0 关闭 1 亮度一级 2 亮度二级
        brightness++;
        brightness %= 3;
        System.out.println("亮度提升!,当前亮度为:" + brightness + "当前台灯是否开启"+ isOpen);
    }

    public void open() {
        if (isOpen == false) {
            isOpen = true;
            lightBoost();
            System.out.println("台灯当前处于关闭状态,现已开启台灯。");
        }else{
            return;
        }
    }

    public void close() {
        if (isOpen == true) {
            isOpen = false;
            System.out.println("台灯当前处于开启状态,现已关闭台灯。");
            brightness = 0;
            clickCount = 0;
        }else{
            return;
        }
    }

    public Boolean getIsOpen() {
        return isOpen;
    }

    public void setIsOpen(Boolean isOpen) {
        this.isOpen = isOpen;
    }

    public int getClickCount() {
        return clickCount;
    }

    public void setClickCount(int clickCount) {
        this.clickCount = clickCount;
    }

    public int getBrightness() {
        return brightness;
    }

    public void setBrightness(int brightness) {
        this.brightness = brightness;
    }
}

本文地址:https://niul.net/522.html
牛C网:https://niuc.net
零九导航:https://09.gay
牛C网导航:https://9i1.net
福利APP中心:https://app.niuc.net
福利APP合集:https://niuc.net/share/1678.html
THE END
分享
二维码
< <上一篇
下一篇>>