Extended Design of PTA Wiper Program Function

Refactoring is a refactoring procedure based on the given manual windshield wiper program for automobiles, which allows the program to extend its functionality.

Input format:
Enter a total of 2 rows, the first behavior is an integer number with a value range of [1,2], where 1 represents the wiper system described in Table 1 and 2 represents the wiper system described in Table 2. The second behavior is a number of integer numbers separated by one or more spaces and ending with a number of zeros with a value range of [1,4], where 1 represents the control rod lift operation, 2 represents the control rod down operation, 3 represents the scale up operation, 4 represents the scale down operation, 0 represents the end of the operation (input is considered to end whenever 0 is encountered).

Output format:
The number of output lines of the program is determined by the number of operations on the lever/dial, and one row of data is output for each operation on the lever/dial.The format is: operation type/control lever current gear/dial current scale/wiper current speed Of which there are four operation types, Lever up, Lever down, Dial up, DialDown; Control lever current gear displays Chinese content, such as stop, intermittent, low speed, high speed, super high speed (Table 2); dial current scale displays numeric values, such as 1, 2, 3, 4, 5 (4, 5 see Table 2); wiper current speed displays integer values.

Input sample:
Here is a set of inputs.For example:

1
1 1 1 2 3 2 4 3 3 1 2 0
Output sample:
The corresponding output is given here.For example:

Lever up/Intermittent/1/4
Lever up/Low speed/1/30
Lever up/High speed/1/60
Lever down/low speed/1/30
Dial up/Low speed/2/30
Lever down/Intermittent/2/6
Dial down/Intermittent/1/4
Dial up/Intermittent/2/6
Dial up/Intermittent/3/12
Lever up/Low speed/3/30
Lever down/Intermittent/3/12

The reference code is as follows:

import java.util.Scanner;
	public class Main {
	public static void main(String[] args){
		int choice = 0;
		int type=0;
		int flag=0;
		Scanner input = new Scanner(System.in);
		type=input.nextInt();
		if(type!=1&&type!=2)
			{System.out.println("Wrong Format");
			System.exit(0);
			}
		choice = input.nextInt();
		Lever lever = new Lever(1);
		Dial dial = new Dial(1);
		if(type==1)
			{
			dial.setN(4);
			lever.setN(4);
			}
		else
			{
			dial.setN(5);
			lever.setN(5);
			}
		
		Brush brush = new Brush(0);
		Agent agent = new Agent(lever,dial,brush);
		while(choice != 0){
			flag=0;
			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);
			default :
				flag=1;
				choice = input.nextInt();
				break;
			}
			
			if(flag==0)
			if(type==1)
			{agent.dealSpeed();//Get brush's speed
			System.out.println(agent.getBrush().getSpeed());
			choice = input.nextInt();
			}
			else if(type==2) {
			agent.dealSpeed1();//Get brush's speed
			System.out.println(agent.getBrush().getSpeed());
			choice = input.nextInt();
			}
			
		}
		}
	}
class Lever {
	private int pos;//Gear
	int n;
	public Lever(){
	}
	public Lever(int pos){
	this.pos = pos;
	}
	public int getPos() {
	return pos;
	}
	public void setN(int n)
	{
	this.n=n;	
	}
	//Upgrade
	public void leverUp() {
	if(this.pos < n){
	this.pos ++;
	}
	}
	//Downgrade
	public void leverDown(){
	if(this.pos > 1){
	this.pos --;
	}
	}
}
class Dial {
	private int pos;//scale
	int n;
	public Dial(){
	}
	public Dial(int pos){
	this.pos = pos;
	}
	public int getPos() {
	return pos;
	}
	public void setN(int n)
	{
	this.n=n;	
	}
	//Ascending scale
	public void dialUp() {
	if(this.pos < n){
	this.pos ++;
	}
	}
	//Downscaling
	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;
	}
}
	//To reduce entity class coupling, an Agent proxy class is designed using a mediation mode
class Agent {
	//Aggregate class design
	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;
	}
	//Main business logic: Calculate wiper swing speed based on control lever gear, dial scale
	public void dealSpeed(){
	int speed = 0;
	switch(this.lever.getPos()){
	case 1://Stop it
		System.out.print("Stop it"+"/"+this.dial.getPos()+"/");
	speed = 0;break;
	case 2://intermission
		System.out.print("intermission"+"/"+this.dial.getPos()+"/");
	switch(this.dial.getPos()){
	
	case 1://scale1
	speed = 4;break;
	case 2://scale2
	speed = 6;break;
	case 3://scale3
	speed = 12;break;
	}
	break;
	case 3://low speed
		System.out.print("low speed"+"/"+this.dial.getPos()+"/");
	speed = 30;break;
	case 4://high speed
		System.out.print("high speed"+"/"+this.dial.getPos()+"/");
	speed = 60;break;
	}
	this.brush.setSpeed(speed);
	}
	public void dealSpeed1() {
		int speed = 0;
		switch(this.lever.getPos()){
		case 1://Stop it
			System.out.print("Stop it"+"/"+this.dial.getPos()+"/");
		speed = 0;break;
		case 2://intermission
			System.out.print("intermission"+"/"+this.dial.getPos()+"/");
		switch(this.dial.getPos()){
		case 1://scale1
		speed = 4;break;
		case 2://scale2
		speed = 6;break;
		case 3://scale3
		speed = 12;break;
		case 4://
		speed=15;break;
		case 5:
		speed=20;break;
		}
		break;
		case 3://low speed
			System.out.print("low speed"+"/"+this.dial.getPos()+"/");
		speed = 30;break;
		case 4://high speed
			System.out.print("high speed"+"/"+this.dial.getPos()+"/");
		speed = 60;break;
		case 5://high speed
			System.out.print("Ultra High Speed"+"/"+this.dial.getPos()+"/");
		speed = 90;break;
		}
		this.brush.setSpeed(speed);
	}
}

Tags: Java

Posted on Sat, 06 Jun 2020 22:22:16 -0400 by dougp23