posted by 빛그루터기 2012. 4. 16. 13:42

아직 완성이 된건 아니지만 기본기능(4칙연산등)은 모두 됨

 

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;

class Exam12_sub extends Frame implements ActionListener {

 // 메뉴
 private MenuBar mb = new MenuBar(); 
 private Menu edit = new Menu("편집"); 
 private MenuItem fcopy = new MenuItem("편집");
 private MenuItem fpase = new MenuItem("편집");
 
 private Menu view = new Menu("편집");
 private CheckboxMenuItem v1 = new CheckboxMenuItem("편집", true);
 private CheckboxMenuItem v2 = new CheckboxMenuItem("편집", false);
 private CheckboxMenuItem v3 = new CheckboxMenuItem("편집");
 
 private Menu help = new Menu("편집");
 private MenuItem h1 = new MenuItem("편집");
 private MenuItem h2 = new MenuItem("편집");
  
 // 텍스트필드
 private TextField tf = new TextField();
  
 // 버튼
 private Button[] bt = new Button[24];
 private Button[] bt01 = new Button[3];
 
 // 버튼이름
 private String[] bt01name = {"Backspace","CE","C"}; 
 private String[] bt02name = {"MC","7","8","9","/","sqrt","MR","4","5","6","*","%","MS","1","2","3","-","1/x","M+","0","+/-",".","+","="};
 
 // 패널
 private Panel p = new Panel();
 private Panel p01 = new Panel();

 //사칙연산클릭확인
 int calnum = 1;
 
 String[] numarray = new String[3];
 
 // 사칙연산클릭확인
 boolean calclick = false;
 
 //String 문자열 패턴
 public String numpattern(String num) throws ParseException {  
  String pattern = "###,###,###,###,###,###.#######";  //패턴문양
  NumberFormat parser = new DecimalFormat(pattern);  //객체생성
  parser.setParseIntegerOnly(false);  //숫자형만 할것인지(true로 할경우 소수점 않나옴)
  parser.setMinimumFractionDigits(0); //소수점 최소자리
  parser.setMaximumFractionDigits(7);  //소수점 최대자리  
  return parser.format(parser.parse(num)); //패턴형식으로 리턴
 }
 
 //Exam12_sub class
 public Exam12_sub (String title) {
  super(title);
  Color bc = new Color(236, 233, 216);
  super.setBackground(bc);
  this.init();
  super.setSize(300,250);
  super.setLocation(100,100);
  super.setResizable(false);
  super.setVisible(true);
 }
 
 //모양만들기
 public void init() {

  //메뉴만들기
  edit.add(fcopy);
  edit.add(fpase);
  mb.add(edit);
  
  view.add(v1);
  view.add(v2);
  view.addSeparator();
  view.add(v3);
  mb.add(view);
  
  help.add(h1);
  help.addSeparator();
  help.add(h2);
  mb.add(help);
  this.setMenuBar(mb);
  
  //상당
  this.setLayout(new BorderLayout(10,10));
  this.tf.setText("0");  
  this.add("North",tf);
  
  Color red = new Color(255, 0, 0);
  
  //중단
  p01.setLayout(new GridLayout(1,3,10,10));
  for(int i=0; i < bt01.length; i++) {
   String btname = bt01name[i];
   bt01[i] = new Button(btname);
   bt01[i].setForeground(red);
   bt01[i].addActionListener(this);
   p01.add(bt01[i]);
  } 
  this.add("Center",p01);
  
  //하단
  p.setLayout(new GridLayout(4,6,10,10));
  for(int i=0; i < bt.length; i++) {
   String btname = bt02name[i];
   bt[i] = new Button(btname);
   if(i%6 == 0) {
    bt[i].setForeground(red);
   }
   bt[i].addActionListener(this);
   p.add(bt[i]);
  }
  this.add("South",p);  
 }

 //각 버튼 이벤트
 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  String id = e.getActionCommand();
  int hashcode = id.hashCode();
  System.out.println("hashcode===>>>"+id.hashCode());
  
  //1~9까지
  if(hashcode >= 48 && hashcode <= 57 || hashcode == 46) {
   if(!this.calclick) {
    if(tf.getText().equals("0") || this.numarray[0] == null) {
     this.tf.setText(id);
    } else {
     this.tf.setText(tf.getText()+""+id);
    }    
   } else {
    this.tf.setText(id);
   }
   if(this.calnum == 1) {    
    numarray[0] =  this.tf.getText();
   } else if(this.calnum == 2) {
    numarray[2] = this.tf.getText();
   }
   this.calclick = false;

  //C, CE
  } else if(hashcode == 67 || hashcode == 2146 ) {
   this.tf.setText("0");
   
   //CE인경우 바로앞취소
   if(hashcode == 2146 ) {
    this.numarray[2] = null;
   
   //C인경우 초기화
   } else {
    this.numarray = new String[3];
    this.calnum = 1;
   }
  //backspace
  } else if(hashcode == -937491361) {
   if(!tf.getText().equals("0")) {
    if(this.tf.getText().length() == 1 ){
     this.tf.setText("0");
    } else {
     this.tf.setText(this.tf.getText().substring(0,this.tf.getText().length()-1));
    }
   }
  
  //사칙연산
  } else if(hashcode == 43 || hashcode == 47 || hashcode == 42 || hashcode == 45) {   
   
   //연산method
   cal(hashcode, e);   
   
   if(id.hashCode() == 43) {
    this.numarray[1] = "+";
   } else  if(id.hashCode() == 47) {
    this.numarray[1] = "/";
   } else  if(id.hashCode() == 42) {
    this.numarray[1] = "*";
   } else  if(id.hashCode() == 45) {
    this.numarray[1] = "-";
   }
   if(this.calnum == 1) this.calnum = 1+1;
   this.calclick = true;
  
  // "="
  } else if(hashcode == 61) {   
   cal(hashcode, e);
   this.calclick = false;
  
  // "+/-"  
  } else if (hashcode == 42825) {   
   try {
    if(this.calnum == 1) {
     if(this.numarray[0] == null || this.numarray[0].equals("0")) {
      this.numarray[0] = "0";    
      this.tf.setText("0");
     } else {
      this.numarray[0] = String.valueOf(Double.parseDouble(this.numarray[0])*-1);     
      this.tf.setText(numpattern(this.numarray[0]));
     }
    } else {
     if(this.numarray[2] == null || this.numarray[2].equals("0")) {
      this.numarray[2] = "0";
      this.tf.setText("0");      
     } else {
      this.numarray[2] = String.valueOf(Double.parseDouble(this.numarray[2])*-1);
      this.tf.setText(numpattern(this.numarray[2]));
     }
    }
   } catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
  }
 }
 
 //연산만 따로 빼놓음
 public void cal(int num, ActionEvent e) {
  try {
   Double num01 = 0.0;
   if(this.numarray[0] != null && this.numarray[1] != null && this.numarray[2] != null) {   
    if(this.numarray[1].equals("+")) {
     num01 = Double.parseDouble(this.numarray[0])+Double.parseDouble(this.numarray[2]);
    } else if(this.numarray[1].equals("/")) {
     num01 = Double.parseDouble(this.numarray[0])/Double.parseDouble(this.numarray[2]);
    } else if(this.numarray[1].equals("*")) {
     num01 = Double.parseDouble(this.numarray[0])*Double.parseDouble(this.numarray[2]);
    } else if(this.numarray[1].equals("-")) {
     num01 = Double.parseDouble(this.numarray[0])-Double.parseDouble(this.numarray[2]);
    }
    numarray[0] = String.valueOf(num01);   
    this.tf.setText(numpattern(String.valueOf(num01)));
    
    if(num == 61) {
     this.calnum = 1;
     this.numarray = new String[3];
    }
   } else if(num == 61 && this.numarray[0] != null && this.numarray[2] == null)  {
    num01 = Double.parseDouble(this.numarray[0]);
    this.tf.setText(numpattern(String.valueOf(num01)));
    this.calnum = 1;
    this.numarray = new String[3];
   }
  } catch (ParseException e1) {
   e1.printStackTrace();
  }
 }
}

public class Exam12 {
 public static void main(String[] args) {
  Exam12_sub eb = new Exam12_sub("계산기");
 }
}

posted by 빛그루터기 2012. 4. 16. 13:41

public String numpattern(String num) throws ParseException {  
    String pattern = "###,###,###,###,###,###.#######";  //패턴문양
    NumberFormat parser = new DecimalFormat(pattern);  //객체생성
    parser.setParseIntegerOnly(false);  //숫자형만 할것인지(true로 할경우 소수점 않나옴)
    parser.setMinimumFractionDigits(0); //소수점 최소자리
    parser.setMaximumFractionDigits(7);  //소수점 최대자리  
    return parser.format(parser.parse(num)); //패턴형식으로 리턴
 }

'프로그램 > java' 카테고리의 다른 글

java로 만든 계산기소스(미완성)  (0) 2012.04.16
posted by 빛그루터기 2012. 3. 28. 13:24

mybatis에서 insert를 할 경우 저장 후 최종 시퀀스 값을 selectKey로 해서 넘겨주는데

이 넘겨준 값을 Spring에서 DAOImpl에서 어떻게 받느냐하면

 

mybatis에서  selectKey로 값을 넘겨 줍니다.

이때 keyProperty로 해당 값의 변수를 지정해 준다 (DTO에 해당 변수명 있어야됨)

 

<insert id="insertBoard" parameterType="newboard">
    INSERT INTO FAQ (
        B_NO, REF, STEP, B_LEVEL, NAME, SUBJ, NEWCONTENTS, HIT, REGIDATE,

        CONTENT, NOTICEYN
    ) VALUES (

        SEQ_FAQBNO.NEXTVAL, #{ref}, '0', '0', #{name}, #{subj}, #{newcontents}, '0', sysdate,

        #{newcontents}, 'N'
    )
    <selectKey resultType="int" keyProperty="b_no" order="AFTER">
        SELECT MAX(B_NO)  AS B_NO FROM FAQ

    </selectKey>
</insert>

 

 

DAOImpl 소스

다른 소스를 보면

int b_no = (int)getSqlSession().insert("newboard.insertBoard", newBoardDTO);

이렇게 되어 있는데 이렇게 하면 무조건 1로 반환되더군요;

 

아래 소스처럼 insert구분은 따로 selectKey값 받아오는것 따로입니다.

selectKey값이  해당 모델(DTO파일)의 keyProperty값과 같은 변수를 찾아 담아서 보내줍니다.

그럼 그 값을 해당 모델에서 꺼내오면 되는군요;

 

 @Override
 public int insertBoard(NewBoardDTO newBoardDTO) throws Exception {
     getSqlSession().insert("newboard.insertBoard", newBoardDTO);
     int b_no = newBoardDTO.getB_no();
     return b_no;
 } 

'프로그램 > mybatis(ibatis)' 카테고리의 다른 글

mybatis 조건 태그모음  (0) 2012.03.28
mybatis에서 procedure 호출하기  (0) 2012.03.28