import com.nttdocomo.ui.*;

public class SonBunki extends IApplication
					implements ComponentListener {

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

	private void dataIn() {
		pnlIn = new Panel();
		btnCalc = new Button("計　算");
		btnCls = new Button("クリア");
		
		pnlIn.setTitle("損益分岐点計算");
		
		lbl = new Label[4];
		Label lblDummy;
		txt = new TextBox[4];
		
		for (int i=0;i<4;i++){
			if (suti[i] !=0){
				txt[i] = new TextBox(Integer.toString((int)suti[i]),9,1,TextBox.NUMBER);
			} else {
				txt[i] = new TextBox("",9,1,TextBox.NUMBER);
			}
			
			txt[i].setInputMode(TextBox.NUMBER);
			txt[i].setFont(pFont);
		}
		
		int haba = pnlIn.getWidth()-txt[0].getWidth()-10;
		
		pnlIn.add(btnCls);
		lblDummy = new Label("",Label.RIGHT);
		lblDummy.setSize(haba*3/4,15);
		pnlIn.add(lblDummy);
		
		Ticker tyui = new Ticker("(目標利益を入力すれば、必要売上を算出します)");
		tyui.setSize(pnlIn.getWidth(),15);
		pnlIn.add(tyui);
		
		for (int i=0;i<4;i++){
			
			switch (i){
			  case 0:
				  	lbl[i] = new Label("売上高",Label.CENTER);
				  	break;
			  case 1:
					lbl[i] = new Label("変動費",Label.CENTER);
					break;			
			  case 2:
					lbl[i] = new Label("固定費",Label.CENTER);
					break;
			  case 3:
					lbl[i] = new Label("目標利益",Label.CENTER);
					break;
			}
			
			lbl[i].setFont(pFont);
			lbl[i].setSize(haba,20);
			
			pnlIn.add(lbl[i]);
			pnlIn.add(txt[i]);
			
		}
		
		pnlIn.add(btnCalc);

		pnlIn.setComponentListener(this);
		
		Display.setCurrent(pnlIn);
	}

	private void dataOut() {
		pnlIn = new Panel();
		btnRe = new Button("戻る");
		btnRe2 = new Button("戻る");
		pnlIn.setTitle("計算結果");
		
		lbl = new Label[14];
		String [] lblTxt = new String[7];
		boolean gyo;
		
		//必要売上高を計算する場合
		if (suti[3] != 0){
			gyo = false;
		} else {
			gyo = true;
		}
		int haba = pnlIn.getWidth()/2-5;		
		
		if (gyo){
			lblTxt[0] = "変動費率: ";
			lblTxt[1] = "固定費率: ";
			lblTxt[2] = "限界利率: ";
			lblTxt[3] = "限界利益: ";
			lblTxt[4] = "利  益: ";
			lblTxt[5] = "損益分岐: ";
			lblTxt[6] = "分岐比率: ";
		} else {
			lblTxt[0] = "必要売上: ";
			lblTxt[1] = "変動費率: ";
			lblTxt[2] = "固定費率: ";
			lblTxt[3] = "変動費: ";
			lblTxt[4] = "固定費: ";
			lblTxt[5] = "限界利益: ";
			lblTxt[6] = "利　益: ";
		}
			
		int [] intSu = new int[7];
		
		for (int i=0;i<7;i++){
			intSu[i]=0;
		}
		
		try {	
			if (gyo){
				
				if (suti[0] != 0){
					intSu[0] = ((suti[1]*10000)/suti[0]+5)/10;//変動比率
					intSu[1] = ((suti[2]*10000)/suti[0]+5)/10;//固定比率
					intSu[2] = (((suti[0]-suti[1])*10000)/suti[0]+5)/10;//限界利益率
				}
					
				intSu[3] = (suti[0]-suti[1]);//限界利益
				intSu[4] = (suti[0]-suti[1]-suti[2]);//利益
				
				if (intSu[2] != 0){
					intSu[5] = ((suti[2]*1000)/intSu[2]);//損益分岐点
				}
				if (suti[0] != 0){
					intSu[6] = ((intSu[5]*10000)/suti[0]+5)/10;//損益分岐点比率
				}
				
			} else {
				if (suti[0] != 0){
					intSu[1] = ((suti[1]*10000)/suti[0]+5)/10;//変動比率
				}
				if (intSu[1] != 1000){
					intSu[0] = (((suti[2]+suti[3])*10000)/(1000-intSu[1])+5)/10;//必要売上高
				}
				if (intSu[0] != 0){
					intSu[2] = ((suti[2]*10000)/intSu[0]+5)/10;//固定比率
				}
				intSu[3] = intSu[0]-suti[2]-suti[3];//変動費
				intSu[4] = suti[2];//固定費				
				intSu[5] = intSu[0]-intSu[3];//限界利益
				intSu[6] = suti[3];//利益
			}
				
		} catch (Exception e){
		}
		
		pnlIn.add(btnRe);
		Label lblDummy = new Label("",Label.RIGHT);
		lblDummy.setSize(haba,15);
		pnlIn.add(lblDummy);

		
		for (int i=0;i<14;i++){
			if ((i%2) == 0 ){
				lbl[i] = new Label(lblTxt[i/2],Label.CENTER);
			} else {
				
				if (gyo){//損益分岐点
					if (i<7 || i == 13){
						lbl[i] = new Label(syoTen(Integer.toString(intSu[(i-1)/2])),Label.CENTER);								
					} else {
						lbl[i] = new Label(inComma(Integer.toString(intSu[(i-1)/2])),Label.CENTER);															
					}
				} else {//必要売上
					if (i == 3 || i == 5){
						lbl[i] = new Label(syoTen(Integer.toString(intSu[(i-1)/2])),Label.CENTER);								
					} else {
						lbl[i] = new Label(inComma(Integer.toString(intSu[(i-1)/2])),Label.CENTER);															
					}					
				}
				
			}
			
			lbl[i].setFont(pFont);
			lbl[i].setSize(haba,20);
			
			pnlIn.add(lbl[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<4;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<4;i++){
				suti[i]=0;
			}
			dataIn();
		} else if (c == btnRe || c == btnRe2){
			dataIn();
		}
		
	}
	
	//3桁カンマ
	String inComma(String num) {
		
		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 ();
		return out;
	}

	//小数点
	String syoTen(String num) {
		
		StringBuffer sb = new StringBuffer (num);
		String out;
		int len = num.length ();
		
		if (len > 1){
			sb.insert(len-1,".");
			out = sb.toString();
		} else {
			out = num;
		}
		out = out + "%";
		return out;
	}
}
