7-7 雨刷程序功能扩展设计 (100 分)

7-7 雨刷程序功能扩展设计 (100 分)

在给定的汽车手动风挡玻璃雨刷程序的基础上,对程序进行重构(Refactoring),使得程序可以对功能进行扩展。

附件1: 附件1.pdf
作业指导书:作业指导书V1.0.pdf

输入格式:

输入共2行,第一行为一个整型数字,取值范围为[1,2],其中1代表表1所描述的雨刷系统,2代表表2所描述的雨刷系统;第二行为若干个用一个或多个空格分开且以数字0结束的整型数字,取值范围为[1,4],其中1代表控制杆升档操作、2代表控制杆降档操作、3代表刻度盘升刻度操作、4代表刻度盘降刻度操作、0代表操作结束(输入时只要遇到0即认为输入结束)。

输出格式:

程序的输出行数根据每一次对控制杆/刻度盘操作次数而定,每一次对控制杆/刻度盘进行了操作,则输出一行数据。格式为:操作类型/控制杆当前档位/刻度盘当前刻度/雨刷当前速度 其中,操作类型共四种,分别为Lever up、Lever down、Dial up、Dial down;控制杆当前档位显示中文内容,例如停止、间歇、低速、高速、超高速(表2);刻度盘当前刻度显示为数值,例如1、2、3、4、5(4、5见表2);雨刷当前速度显示为整型数值。

输入样例:

在这里给出一组输入。例如:

1
1 1 1 2 3 2 4 3 3 1 2 0

输出样例:

在这里给出相应的输出。例如:

Lever up/间歇/1/4
Lever up/低速/1/30
Lever up/高速/1/60
Lever down/低速/1/30
Dial up/低速/2/30
Lever down/间歇/2/6
Dial down/间歇/1/4
Dial up/间歇/2/6
Dial up/间歇/3/12
Lever up/低速/3/30
Lever down/间歇/3/12

题解:

package jyc.test;

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        int choice = 0;
        int pattern;
        Scanner input = new Scanner(System.in);
        pattern=input.nextInt();
        if(!(pattern==1||pattern==2)) {
            System.out.println("Wrong Format");
            return;
        }
        choice = input.nextInt();
        Lever lever = new Lever(1);
        Dial dial = new Dial(1);
        Brush brush = new Brush(0);
        Agent agent = new Agent(lever,dial,brush);
        while(choice != 0){
            if(pattern==1){
                switch(choice){
                    case 1://Lever up
                        System.out.print("Lever up");
                        agent.getLever().leverUp();
                        break;
                    case 2://Lever down
                        System.out.print("Lever down");
                        agent.getLever().leverDown();
                        break;
                    case 3://Dial up
                        System.out.print("Dial up");
                        agent.getDial().dialUp();
                        break;
                    case 4://Dial down
                        System.out.print("Dial down");
                        agent.getDial().dialDown();
                        break;
                    case 0://Terminate
                        System.exit(0);
                }

                agent.dealSpeed2();//Get brush’s speed
                int curspeed = agent.getBrush().getSpeed();
                if (curspeed == 0)
                    System.out.println("/停止/" + agent.getDial().getPos() + "/" + curspeed);
                else if (curspeed == 4 || curspeed == 12 || curspeed == 6)
                    System.out.println("/间歇/" + agent.getDial().getPos() + "/" + curspeed);
                else if (curspeed == 30)
                    System.out.println("/低速/" + agent.getDial().getPos() + "/" + curspeed);
                else if (curspeed == 60)
                    System.out.println("/高速/" + agent.getDial().getPos() + "/" + curspeed);

            }
            else if(pattern==2){
                switch(choice){
                    case 1://Lever up
                        System.out.print("Lever up");
                        agent.getLever().leverUp2();
                        break;
                    case 2://Lever down
                        System.out.print("Lever down");
                        agent.getLever().leverDown();
                        break;
                    case 3://Dial up
                        System.out.print("Dial up");
                        agent.getDial().dialUp2();
                        break;
                    case 4://Dial down
                        System.out.print("Dial down");
                        agent.getDial().dialDown();
                        break;

                    case 0://Terminate
                        System.exit(0);
                }

                agent.dealSpeed2();//Get brush’s speed
                int curspeed = agent.getBrush().getSpeed();
                if (curspeed == 0)
                    System.out.println("/停止/" + agent.getDial().getPos() + "/" + curspeed);
                else if (curspeed==20 || curspeed==15||curspeed == 4 || curspeed == 12 || curspeed == 6)
                    System.out.println("/间歇/" + agent.getDial().getPos() + "/" + curspeed);
                else if (curspeed == 30)
                    System.out.println("/低速/" + agent.getDial().getPos() + "/" + curspeed);
                else if (curspeed == 60)
                    System.out.println("/高速/" + agent.getDial().getPos() + "/" + curspeed);
                else if (curspeed == 90)
                    System.out.println("/超高速/" + agent.getDial().getPos() + "/" + curspeed);
            }

            choice = input.nextInt();
            }



        }
    }


 class Lever {
    private int pos;//档位
    public Lever(){
    }
    public Lever(int pos){
        this.pos = pos;
    }
    public int getPos() {
        return pos;
    }
    //升档
    public void leverUp() {
        if(this.pos < 4){
            this.pos ++;
        }
    }
     //升档二
     public void leverUp2() {
         if(this.pos < 5){
             this.pos ++;
         }
     }

    //降档
    public void leverDown(){
        if(this.pos > 1){
            this.pos --;
        }
    }
}
 class Dial {
    private int pos;//刻度
    public Dial(){
    }
    public Dial(int pos){
        this.pos = pos;
    }
    public int getPos() {
        return pos;
    }

    //升刻度
    public void dialUp() {
        if(this.pos < 3){
            this.pos ++;
        }
    }

    //升刻度二
    public void dialUp2() {
        if(this.pos < 5){
            this.pos ++;
        }
    }

    //降刻度
    public void dialDown(){
        if(this.pos > 1){
            this.pos --;
        }
    }
}
class Brush {
    private int speed;
    public Brush(){
    }
    public Brush(int speed){
        this.speed = speed;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
}
//为了减小实体类耦合性,采用中介模式,设计Agent代理类
 class Agent {
    //聚合型类设计
    private Lever lever;
    private Dial dial;
    private Brush brush;
    public Agent(){
    }
    public Agent(Lever lever,Dial dial,Brush brush){
        this.lever = lever;
        this.dial = dial;
        this.brush = brush;
    }
    public Lever getLever() {
        return lever;
    }
    public void setLever(Lever lever) {
        this.lever = lever;
    }
    public Dial getDial() {
        return dial;
    }
    public void setDial(Dial dial) {
        this.dial = dial;
    }
    public Brush getBrush() {
        return brush;
    }
    public void setBrush(Brush brush) {
        this.brush = brush;
    }
    //主要业务逻辑:根据控制杆档位、刻度盘刻度计算雨刷摆动速度
    public void dealSpeed2(){
        int speed2 = 0;
        switch(this.lever.getPos()){
            case 1://停止
                speed2 = 0;break;
            case 2://间歇
                switch(this.dial.getPos()){
                    case 1://刻度1
                        speed2 = 4;break;
                    case 2://刻度2
                        speed2 = 6;break;
                    case 3://刻度3
                        speed2 = 12;break;
                    case 4://刻度3
                        speed2 = 15;break;
                    case 5://刻度3
                        speed2 = 20;break;
                }
                break;
            case 3://低速
                speed2 = 30;break;
            case 4://高速
                speed2 = 60;break;
            case 5://刻度3
                speed2 = 90;break;
        }
        this.brush.setSpeed(speed2);
    }

}

此题做了3个半小时,我自己都吐了,一直有两个点不过,最后看作业指导书才发现选择模式一二要判断合法性。。。可是题上没说呀,害,白白让我浪费这么多时间,无语子。。。

你可能感兴趣的:(Java,java)