雨刷程序(一)

一、题目说明

控制杆的档位       停止           间歇        低速        高速  
刻度盘的刻度       ——       1 / 2 / 3        ——       ——
雨刷的速度         0       4 / 6 /12          30         60

二、程序代码

(1)控制杆类

public class Lever {
	private int pos;
	
	public Lever(){}
	
	public Lever(int pos){
		this.pos = pos;
	}
	
	public int getPos(){
		return this.pos;
	}
	
	public void upPos(){
		if(this.pos < 4)
			this.pos ++;
	}
	
	public void downPos(){
		if(this.pos > 1)
			this.pos --;
	}
}

(2)刻度盘类

public class Dial {
	private int pos;
	
	public Dial(){}
	
	public Dial(int pos){
		this.pos = pos;
	}
	
	public int getPos(){
		return this.pos;
	}
	
	public void upPos(){
		if(this.pos < 3)
			this.pos ++;
	}
	
	public void downPos(){
		if(this.pos > 1)
			this.pos --;
	}
}

(3)雨刷类

public class Brush {
	private int speed;
	
	public Brush(){}
	
	public Brush(int speed){
		this.speed = speed;
	}
	
	public int getSpeed(){
		return this.speed;
	}
	
	public void setSpeed(int speed){
		this.speed = speed;
	}
}

(4)中介类

public class Agent {
	private Lever lever;
	private Dial dial;
	private Brush brush;
	
	public Agent(){
		lever = new Lever(1);
		dial = new Dial(1);
		brush = new Brush(0);
	}
	
	public void leverUp(){
		this.lever.upPos();
		dealSpeed();
	}

	public void leverDown(){
		this.lever.downPos();
		dealSpeed();
	}

	public void dialUp(){
		this.dial.upPos();
		dealSpeed();
	}

	public void dialDown(){
		this.dial.downPos();
		dealSpeed();
	}
	
	public void dealSpeed(){
		int speed = 0;
		switch(this.lever.getPos()){
		case 1:speed = 0;break;
		case 2:
			switch(this.dial.getPos()){
			case 1:speed = 4;break;
			case 2:speed = 6;break;
			case 3:speed = 12;break;
			}break;
		case 3:speed = 30;break;
		case 4:speed = 60;break;
		}
		this.brush.setSpeed(speed);
	}
	
	public void show(){
		String[] leverPos = {"","停止","间歇","低速","高速"};
		System.out.println("The Lever's postion is:" + leverPos[this.lever.getPos()]);
		System.out.println("The Dial's postion is:" + this.dial.getPos());
		System.out.println("The Brush's speed is:" + this.brush.getSpeed()); 
	}
}

(5)测试类

import java.util.Scanner;

public class Brush_App {
	public static void menu(){
		System.out.println("===========Brush_APP==========");
		System.out.println("1:Lever up.");
		System.out.println("2:Lever down.");
		System.out.println("3:Dial up.");
		System.out.println("4:Dial down.");
		System.out.println("0:Exit.");
		System.out.println("==============================");
	}
	
	public static void main(String[]args){
		Scanner input = new Scanner(System.in);
		menu();
		int flag = input.nextInt();
		Agent agent = new Agent();
		while(flag != 0){			
			switch(flag){
			case 1:agent.leverUp();break;			
			case 2:agent.leverDown();break;
			case 3:agent.dialUp();break;
			case 4:agent.dialDown();break;
			case 0:System.exit(1);break;
			}
			agent.show();
			menu();
			flag = input.nextInt();		
		}		
	}
}
发布了53 篇原创文章 · 获赞 117 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_40431584/article/details/89762483