import com.nttdocomo.ui.*;

public class ei_cf extends IApplication 
						implements ComponentListener{

	Button btnCalc,btnCls,btnRe,btnRe2;
	TextBox [] txt;
	Panel pnlIn;
	int suti[] = new int[26];
	int haba;
	Font pFont=Font.getFont(Font.SIZE_SMALL);
	
	public void start() {
		// TODO 自動生成されたメソッド・スタブ
		dataIn();
	}

	private void dataIn() {
		// TODO 自動生成されたメソッド・スタブ
		pnlIn = new Panel();
		btnCalc = new Button("計　算");
		btnCls = new Button("クリア");
		pnlIn.setTitle("簡易営業CF計算");

		Label [] lbl = new Label[26];
		String [] strLbl = new String[26];
		Label lblSYU = new Label("----【前　期】----",Label.CENTER);
		Label lblMATSU = new Label("----【当　期】----",Label.CENTER);		
		lblSYU.setFont(pFont);
		lblMATSU.setFont(pFont);
		
		strLbl[0] = "税前利益";
		strLbl[1] = "減価償却";
		strLbl[2] = "役員賞与";
		strLbl[3] = "法人税等";
		strLbl[4] = "受取手形";
		strLbl[5] = "売掛金";
		strLbl[6] = "棚卸資産";
		strLbl[7] = "前途金";
		strLbl[8] = "他流資産";
		strLbl[9] = "支払手形";
		strLbl[10] = "買掛金";
		strLbl[11] = "前受金";
		strLbl[12] = "未払税等";		
		strLbl[13] = "他流負債";
		strLbl[14] = "引当金";
		
		strLbl[15] = "受取手形";
		strLbl[16] = "売掛金";
		strLbl[17] = "棚卸資産";
		strLbl[18] = "前途金";
		strLbl[19] = "他流資産";
		strLbl[20] = "支払手形";
		strLbl[21] = "買掛金";
		strLbl[22] = "前受金";
		strLbl[23] = "未払税等";		
		strLbl[24] = "他流負債";
		strLbl[25] = "引当金";
		
		Label lblDummy;
		txt = new TextBox[26];
		
		for (int i=0;i<26;i++){
			if (suti[i] !=0){
				txt[i] = new TextBox(Integer.toString((int)suti[i]),8,1,TextBox.NUMBER);
			} else {
				txt[i] = new TextBox("",8,1,TextBox.NUMBER);
			}
			
			txt[i].setInputMode(TextBox.NUMBER);
			
		}
		
		haba = pnlIn.getWidth()-txt[0].getWidth()-5;
		
		pnlIn.add(btnCls);
		lblDummy = new Label("",Label.RIGHT);
		lblDummy.setSize(haba*3/4,15);
		pnlIn.add(lblDummy);
		
		for (int i=0;i<26;i++){
			
			lbl[i] = new Label(strLbl[i],Label.CENTER);
			lbl[i].setSize(haba-20,20);
			if (i < 4 ){
				  lbl[i].setBackground(Graphics.getColorOfName(Graphics.LIME));
			} else if (i<15){
				  lbl[i].setBackground(Graphics.getColorOfName(Graphics.SILVER));
			} else {
				  lbl[i].setBackground(Graphics.getColorOfName(Graphics.YELLOW));
			}
			
			if (i == 4){
				pnlIn.add(lblSYU);
			}
			if (i == 15){
				pnlIn.add(lblMATSU);
			}
			
			lbl[i].setFont(pFont);
			txt[i].setFont(pFont);
			pnlIn.add(lbl[i]);
			pnlIn.add(txt[i]);
		}
		
		pnlIn.add(btnCalc);
		pnlIn.setComponentListener(this);
		
		Display.setCurrent(pnlIn);
	}
	
	
	private void dataOut() {
		// TODO 自動生成されたメソッド・スタブ
		pnlIn = new Panel();
		btnRe = new Button("戻る");
		btnRe2 = new Button("戻る");
		pnlIn.setTitle("計算結果");
		
		Label [] lbl = new Label[12];
		String [] strLbl = new String[12];
		txt = new TextBox[12];
		long [] lngSu = new long[12];
		Label lblKIRI = new Label("＿＿＿＿＿＿＿",Label.CENTER);
		Label lblKIRI2 = new Label("＿＿＿＿＿＿＿",Label.CENTER);
		
		strLbl[0] = "税前利益";
		strLbl[1] = "減価償却";
		strLbl[2] = "引当金";
		strLbl[3] = "売上債権";
		strLbl[4] = "棚卸資産";
		strLbl[5] = "他流資産";
		strLbl[6] = "買入債務";
		strLbl[7] = "他流負債";
		strLbl[8] = "役員賞与";
		strLbl[9] = "小　計";
		strLbl[10] = "税支払";
		strLbl[11] = "営業CF";
		
		//計算処理
		for (int i=0;i<12;i++){
			lngSu[i]=0;
		}
		
		try {

			lngSu[0]=suti[0];
			lngSu[1]=suti[1];
			lngSu[8]=suti[2] * -1;
			lngSu[10]=(suti[12]+suti[3]-suti[23]) * -1;
			//引当金
			lngSu[2]=suti[25] - suti[14];
			//売上債権
			lngSu[3]=(suti[4] - suti[15])+(suti[5] - suti[16])-(suti[11] - suti[22]);
			//棚卸資産
			lngSu[4]=(suti[6] - suti[17]);
			//その他流動資産
			lngSu[5]=(suti[8] - suti[19]);
			//買入債務
			lngSu[6]=(suti[20] - suti[9])+(suti[21] - suti[10])-(suti[18] - suti[7]);
			//その他流動負債
			lngSu[7]=(suti[24] - suti[13]);
			
			lngSu[9] = lngSu[0]+lngSu[1]+lngSu[2]+lngSu[3]+lngSu[4];
			lngSu[9] = lngSu[9]+lngSu[5]+lngSu[6]+lngSu[7]+lngSu[8];
			lngSu[11] = lngSu[9]+lngSu[10];
				
			
		} catch (Exception e){
		}
		
		TextBox dummy = new TextBox("",4,1,TextBox.DISPLAY_ANY);
		dummy.setEditable(false);
		dummy.setText("結果");
		pnlIn.add(dummy);
		
		pnlIn.add(btnRe);
		
		Label lblDummy;
		lblDummy = new Label("",Label.RIGHT);	
		lblDummy.setSize(haba-10,15);
		pnlIn.add(lblDummy);
		
		//パネルに追加
		for (int i=0;i<12;i++){
			
			if (lngSu[i] !=0){
					txt[i] = new TextBox(inComma(Long.toString(lngSu[i])),8,1,TextBox.DISPLAY_ANY);
			} else {
				txt[i] = new TextBox("---",8,1,TextBox.NUMBER);
			}
			
			txt[i].setEditable(false);			
			lbl[i] = new Label(strLbl[i],Label.CENTER);				
			lbl[i].setSize(haba-20,20);
			
			if (i==9){
				pnlIn.add(lblKIRI);
			}
			if (i==11){
				pnlIn.add(lblKIRI2);
			}
			lbl[i].setFont(pFont);
			txt[i].setFont(pFont);
			pnlIn.add(lbl[i]);
			pnlIn.add(txt[i]);
			
		}
		
		pnlIn.add(btnRe2);
		pnlIn.setComponentListener(this);
		
		Display.setCurrent(pnlIn);
	}
	
	public void componentAction(Component c, int act, int prm) {
		// TODO 自動生成されたメソッド・スタブ
		if (c == btnCalc){
			for (int i=0;i<26;i++){
				suti[i]=0;
				try {
					suti[i] = Integer.parseInt(txt[i].getText());
				} catch (Exception e){
				}
			}
			dataOut();
		} else if (c == btnCls){
			for (int i=0;i<26;i++){
				suti[i]=0;
			}
			dataIn();
		} else if (c == btnRe || c == btnRe2){
			dataIn();
		}
	}
	
	//3桁カンマ
	String inComma(String num) {
		int minus = 0;
		if (Integer.parseInt(num) < 0){
			minus = 1;
			num = num.substring(1,num.length());
		}
		StringBuffer sb = new StringBuffer (num);
		int len = num.length ();
		int comma_num = (int)((len - 1) / 3);  // カンマの個数
		int comma_pos = (int)(len % 3);        // カンマの最左端位置
		if (comma_pos == 0) {
		  comma_pos = 3;
		}
		if (comma_num == 0) {
		  comma_pos = 0;
		}
		for (int i=0; i < comma_num; i++) {
		  sb.insert (comma_pos + 3*i + i, ",");
		}
		String out = sb.toString ();
		if (minus == 1){ 
			out = "▲" + out;
		}	
		return out;
	}	

}

