`
raymond.chen
  • 浏览: 1437436 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

解析器模式范例:阿拉伯数字金额转为中文数字金额

 
阅读更多

表达式抽象类:

public abstract class Expression {
	protected String[] chineseFigures = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; //中文数字
	
	public void interpret(Context ctx){
		if(ctx.hasNextFigure()){
			int d = Integer.parseInt(ctx.nextFigure()); //阿拉伯数字
			String chineseFigure = chineseFigures[d]; //中文数字
			
			ctx.beforeAppend(chineseFigure + getSuffix());
			
			if(ctx.hasNextFigure()){
				parseNext(ctx);
			}
		}
	}
	
	protected abstract void parseNext(Context ctx);
	
	public abstract String getSuffix();
}

 

个位数解析类:

public class OneExpression extends Expression {
	private Expression next = null;
	
	@Override
	protected void parseNext(Context ctx) {
		next = new ShiExpression();
		next.interpret(ctx);
	}

	@Override
	public String getSuffix() {
		return "";
	}
}

 

十位数解析类:

public class ShiExpression extends Expression {
	private Expression next = null;
	
	@Override
	protected void parseNext(Context ctx) {
		next = new BaiExpression();
		next.interpret(ctx);
	}

	@Override
	public String getSuffix() {
		return "拾";
	}
}

 

其它位数的解析类请看附件!

 

Context类:

public class Context {
	private String input;
	private String output = "";
	private int inputLength = 0;
	private int figurePosition = 0;
	private int limitLength = 12;
	
	public Context(String s){
		this.input = (s==null) ? "" : s.trim();
		this.inputLength = (input==null) ? 0 : input.length();
		
		if(inputLength > limitLength) throw new RuntimeException("只支持到"+limitLength+"位长度的数值!");
	}
	
	public void beforeAppend(String s){
		output = s + output;
	}
	
	public boolean hasNextFigure(){
		return figurePosition<inputLength;
	}
	
	public String nextFigure(){
		figurePosition++;
		String currentFigure = String.valueOf(input.charAt(inputLength - figurePosition));
		return currentFigure;
	}
	
	private void optimize(){
		if(output.length()>0 && output.equals("零")==false){
			while(output.endsWith("零")){
				output = output.substring(0, output.length()-1);
			}
			
			output = output.replaceAll("零拾", "零");
			output = output.replaceAll("零佰", "零");
			output = output.replaceAll("零仟", "零");
			
			output = output.replaceAll("零萬", "萬");
			output = output.replaceAll("零亿", "亿");
			
			while(output.indexOf("零零")>0){
				output = output.replaceAll("零零", "零");
			}
			
			output = output.replaceAll("零萬", "萬");
			output = output.replaceAll("零亿", "亿");
			output = output.replaceAll("亿萬", "亿");
			
			while(output.endsWith("零")){
				output = output.substring(0, output.length()-1);
			}
			
			if(output.equals("壹拾")) output = "拾";
			
		}
		output += "元";
	}
	
	public String getOutput(){
		optimize();
		return output;
	}
}

 

测试类:

public class Test {
	public static void main(String[] args) {
		Context ctx = new Context("9876543210");
		Expression root = new OneExpression();
		root.interpret(ctx);
		System.out.println(ctx.getOutput());
	}
}

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics