All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspire.nm.component.commonUtil.commonMenu.Menu Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package com.aspire.nm.component.commonUtil.commonMenu;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.aspire.nm.component.commonUtil.commonMenu.base.IfTarget;
import com.aspire.nm.component.commonUtil.commonMenu.base.IfTitle;



public class Menu implements IfTarget{
	
	private static Logger logger = Logger.getLogger(Menu.class);
	
	private ArrayList items = new ArrayList();
	private Menu parentMenu ;
	public Menu(Menu parentMenu){
		this.parentMenu = parentMenu;
	}
	
	
	
	public ArrayList getItems() {
		return items;
	}



	@Override
	public void process() {
		for(IfTitle item:items){
			System.out.println(item.getInput()+" : " + item.getTitle());
		}
		if(parentMenu != null){
			System.out.println("9 : back");
		}
		if(parentMenu == null){
			System.out.println("0 : exit");
		}
		System.out.print("select menu:");
		
		String input=userInput();
	    
	    if(parentMenu == null && input.trim().equals("0")){
	    	return;
	    }else if(parentMenu != null && input.trim().equals("9")){
	    	parentMenu.process();
	    }else if(getSelectItem(input) != null){
	    	IfTarget target = getSelectItem(input).getTarget();
	    	if(target != null){
	    		target.process();
	    	}else{
	    		System.out.println("target is null!");
	    		process();
	    	}
	    }else{
	    	System.out.println("input error!");
	    	process();
	    }
	}

	
	private IfTitle getSelectItem(String input){
		for(IfTitle item:items){
	    	if((input != null && input.trim().equals(item.getInput()))){
	    		return item;
	    	}
	    }
		return null;
	}
	private String userInput(){
		byte buffer[]=new byte[1024];
        int count;
		try {
			count = System.in.read(buffer);
		} catch (IOException e) {
			logger.error(e);
			return null;
		}
		//最后两位为结束符,删去不要
		char[] ch=new char[count-2];
	    for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy