[置顶] Java第三次作业参考代码

控制杆类

public class Lever {
	protected int lever = 1;
	
	public Lever()
	{
		lever = 1;
	}
	
	public Lever(int lever)
	{
		this.lever = lever;
	}
	
	public int getLever()
	{
		return lever;
	}
	
	public int leverUp()
	{
		if(lever < 4)
			lever++;
		return lever;
	}
	
	public int leverDown()
	{
		if(lever > 1)
			lever --;
		return lever;
	}
}

刻度盘类

public class Dial {
	protected int dial = 1;
	
	public Dial()
	{
		dial = 1;
	}
	
	public Dial(int lever)
	{
		this.dial = lever;
	}
	
	public int getDial()
	{
		return dial;
	}
	
	public int dialUp()
	{
		if(dial < 3)
			dial++;
		return dial;
	}
	
	public int dialDown()
	{
		if(dial > 1)
			dial --;
		return dial;
	}
}

雨刷类

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

}

代理类

public class Agent {
	protected Lever l;
	protected Dial d;
	protected Brush b;
	
	public Agent()
	{
		l = new Lever();
		d = new Dial();
		b = new Brush();
	}
	
	public int getLever()
	{
		return l.getLever();
	}
	
	public int getDial()
	{
		return d.getDial();
	}
	
	public void doSpeed()
	{
		int speed = 0;
		switch(l.getLever())
		{
		case 1:speed = 0;break;
		case 2:
			switch(d.getDial())
			{
			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;
		}
		b.setSpeed(speed);
	}
	
	public void show()
	{
		String slever = "";
		switch(l.getLever())
		{
		case 1: slever = "停止";break;
		case 2: slever = "间歇";break;
		case 3: slever = "低速";break;
		case 4: slever = "高速";break;
		}
		
		System.out.println("控制杆的档位为:" + slever );
		System.out.println("刻度盘的刻度为:" + d.getDial());
		System.out.println("雨刷的速度为:" + b.getSpeed());
	}
}

测试类

import java.util.Scanner;

public class Brush_Test {
	public static void menu() {
		System.out
				.println("==================Please choose operation============");
		System.out.println("\t==========1:Lever Up===========");
		System.out.println("\t==========2:Lever Down=========");
		System.out.println("\t==========3:Dial Up============");
		System.out.println("\t==========4:Dial Down==========");
		System.out.println("\t==========0:Quit===============");
		System.out
				.println("=====================================================");
		System.out.println("Input the number(0~4):");
	}

	public static void main(String[] args)
	{
		
			Agent agent = new Agent();
			Scanner input = new Scanner(System.in);
			int choice = 0;
			
			menu();
		try{ //很多同学问input为什么会报警告,其实input要进行close()操作,参考这段代码,以后会讲
			while(true)
			{
				choice = input.nextInt();
				if(choice == 0)
					break;
				switch(choice)
				{
				case 1:
					if(agent.getLever() >= 4)
					{
						System.out.println("控制杆已经处于高速档位,不能再升档!");
						menu();
						continue;
					}
					else
					{
						agent.l.leverUp();
					}
					break;
				case 2:
					if(agent.getLever() <= 1)
					{
						System.out.println("控制杆已经处于停止档位,不能再降档!");
						menu();
						continue;
					}
					else
					{
						agent.l.leverDown();
					}
					break;
				case 3:
					if(agent.getDial() >= 3)
					{
						System.out.println("刻度盘已经处于最高刻度,不能再升档!");
						menu();
						continue;
					}
					else
					{
						agent.d.dialUp();
					}
					break;
				case 4:
					if(agent.getDial() <= 1)
					{
						System.out.println("刻度盘已经处于最低刻度,不能再降档!");
						menu();
						continue;
					}
					else
					{
						agent.d.dialDown();
					}
					break;
				default:
					System.out.println("请输入数字0~4");
					menu();
					continue;
				} //switch
				
				agent.doSpeed();
				agent.show();
				menu();
			}  //while
		
		}finally{
			input.close();
		}		
	}
}


你可能感兴趣的:([置顶] Java第三次作业参考代码)