CalculTest.java
public class CalculTest {
public static void main(String args[]){
//calculTest ct= new calculTest();
CalculModel calModel = new CalculModel();
CalculControler calCon = new CalculControler(calModel);
}
}
CalculModel.java
public class CalculModel {
CalculControler calCon;
String num1; //기존의 수
String num2; //새로 받은 수
String oper; //연산자
boolean inputNumberAbleStatus; // 화면에 숫자가 새로 받아지는걸 허용/비허용
boolean selectedOperStatus; // 연산자 버튼을 눌럿는지 안 눌렀는지
boolean pointExist; // .을 눌럿는지 안 눌럿는지
//생성자
CalculModel(){
num1="0";
num2="0";
oper="";
inputNumberAbleStatus=true;
selectedOperStatus=false;
pointExist=false;
}
//숫자 입력 받앗을때
public void inputNumber(String num){
if(!num.equals("Number not divide 0")){
if(num.equals(".")){
if(pointExist) num="";
if(inputNumberAbleStatus){
inputNumberAbleStatus=false;
calCon.setView("0"+num);
}else{
calCon.setView(getView()+num);
}
pointExist=true;
}else{
if(inputNumberAbleStatus){
inputNumberAbleStatus=false;
setView(num);
}else{
setView(getView()+num);
}
}
}
}
//기호 입력 받앗을때
public void inputOper(String oper){
if(selectedOperStatus){
num2=getView();
if(!num2.equals("Number not divide 0")){
num1=operation(this.oper, num1, num2);
this.oper=oper;
setView(num1);
inputNumberAbleStatus=true;
pointExist=false;
}
}else{
this.oper=oper;
num2=getView();
num1=operation(oper, num2, num1);
selectedOperStatus=true;
inputNumberAbleStatus=true;
pointExist=false;
}
}
// Clear버튼 눌럿을때
public void inputClear(){
num1="0";
num2="0";
oper="";
setView("0");
inputNumberAbleStatus=true;
selectedOperStatus=false;
}
// 연산자 처리 부분 로직
public String operation(String oper, String num1, String num2){
switch(oper.charAt(0)){
case '+':
num1=""+(Double.parseDouble(num1)+Double.parseDouble(num2));
return num1;
case '-':
num1=""+(Double.parseDouble(num1)-Double.parseDouble(num2));
return num1;
case '*':
if(selectedOperStatus)
num1=""+(Double.parseDouble(num1)*Double.parseDouble(num2));
return num1;
case '/':
if(selectedOperStatus&&num2.equals("0")){
num1="Number not divide 0";
oper="";
}
if(!num2.equals("0"))
num1=""+(Double.parseDouble(num1)/Double.parseDouble(num2));
return num1;
case '=':
num1=num2;
oper="";
return num1;
default:
return num1;
}
}
//소수점이 .0일때 뒤에 .0을 제거하여 정수형으로 나타냄
public String removePointZero(String numWithPoint){
String newNum="";
int pointIdx=0;
int zeroIdx=0;
if(numWithPoint.equals("-0")) return "0";
if(numWithPoint.length()<3) return numWithPoint;
if(numWithPoint.charAt(numWithPoint.length()-2)
=='.'&&numWithPoint.charAt(numWithPoint.length()-1)=='0'){
for(int i=0;i<numWithPoint.length()-2;i++){
newNum+=numWithPoint.charAt(i);
}
return newNum;
}else
return numWithPoint;
}
//뷰의 필드값 세팅/게팅
public void setView(String s){calCon.setView(removePointZero(s));}
public String getView(){return calCon.getView();}
}
CalculControler.java
public class CalculControler {
CalculView calView;
CalculModel calModel;
public CalculControler(CalculModel calModel) {
this.calModel=calModel;
calModel.calCon=this;
calView = new CalculView(this);
calView.setBounds(600,400,300,200);
calView.setResizable(false);
calView.setVisible(true);
}
// 숫자버튼.기호버튼,Clear버튼을 구분
public void inputNumber(String num){calModel.inputNumber(num);}
public void inputOper(String oper){calModel.inputOper(oper);}
public void inputClear(){calModel.inputClear();}
// 계산기창 필드의 값을 setting / getting
public void setView(String s){calView.setView(s);}
public String getView(){return calView.getView();}
}
CalculView.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculView extends JFrame {
CalculControler calCon;
JPanel pCalView, pCalButton;
JLabel vCalView;
JButton numButton[], operButton[];
JButton bZero, bOne, bTwo, bThree, bFour, bFive, bSix, bSeven, bEight, bNine, bPoint;
JButton bPlus, bMinus, bMultiply, bDivide, bEquals;
JButton bClear;
GridBagConstraints gbc;
GridBagLayout gbl;
CalculView(CalculControler calCon){
this.calCon=calCon;
//this.calModel=calModel;
//타이틀과 배치관리자를 설정
Container con = getContentPane();
this.setTitle("이상한 계산기");
con.setLayout(new BorderLayout());
//뷰 패널 생성 및 배치관리자 설정
pCalView = new JPanel();
pCalView.setLayout(new BorderLayout());
//계산 뷰 텍스트창 생성
vCalView = new JLabel("0");
vCalView.setHorizontalAlignment(JLabel.RIGHT);
vCalView.setBackground(Color.WHITE);
vCalView.setFocusable(false);
//뷰 패널에 계산 텍스트 창 합치기
pCalView.add(vCalView);
//프레임에 부착
this.add(pCalView, "North");
//계산 패널 생성
pCalButton = new JPanel();
//숫자 버튼 생성
bZero = new JButton("0");
bOne = new JButton("1");
bTwo = new JButton("2");
bThree = new JButton("3");
bFour = new JButton("4");
bFive = new JButton("5");
bSix = new JButton("6");
bSeven = new JButton("7");
bEight = new JButton("8");
bNine = new JButton("9");
bPoint = new JButton(".");
//리스너 부착
setNumberButton(bZero, bOne, bTwo, bThree, bFour, bFive, bSix, bSeven, bEight, bNine, bPoint);
//연산자 버튼 생성
bPlus = new JButton("+");
bMinus = new JButton("-");
bMultiply = new JButton("*");
bDivide = new JButton("/");
bEquals = new JButton("=");
//리스너 부착
setOperButton(bPlus, bMinus, bMultiply, bDivide, bEquals);
//Clear버튼 생성
bClear = new JButton("C");
setClearButton(bClear);
//리스너부착
setPanelButton();
//배치관리자 버튼 부착
this.add(pCalButton, "Center");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
dispose();
System.exit(0);
}
});
}
//리스너 부착 메소드
public void setNumberButton(JButton ...numButtonArr){
numButton=numButtonArr;
for(JButton numB : numButton)
numB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
calCon.inputNumber(ae.getActionCommand());
}
});
}
// 리스너 부착 메소드
public void setOperButton(JButton ...operButtonArr){
operButton=operButtonArr;
for(JButton operB : operButton)
operB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
calCon.inputOper(ae.getActionCommand());
}
});
}
// 리스너 부착 메소드
public void setClearButton(JButton clearButton){
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calCon.inputClear();
}
});
}
// 레이아웃 메소드
public void setPanelButton(){
gbl = new GridBagLayout();
pCalButton.setLayout(gbl);
gbc=new GridBagConstraints();
gbc.weightx=1.0;
gbc.weighty=1.0;
gbc.fill=GridBagConstraints.BOTH;
layout(bClear, 0, 0, 1, 1);
layout(bDivide, 1, 0, 1, 1);
layout(bMultiply, 2, 0, 1, 1);
layout(bMinus, 3, 0, 1, 1);
layout(bSeven, 0, 1, 1, 1);
layout(bEight, 1, 1, 1, 1);
layout(bNine, 2, 1, 1, 1);
layout(bPlus, 3, 1, 1, 2);
layout(bFour, 0, 2, 1, 1);
layout(bFive, 1, 2, 1, 1);
layout(bSix, 2, 2, 1, 1);
layout(bOne, 0, 3, 1, 1);
layout(bTwo, 1, 3, 1, 1);
layout(bThree, 2, 3, 1, 1);
layout(bEquals, 3, 3, 1, 2);
layout(bZero, 0, 4, 2, 1);
layout(bPoint, 2, 4, 1, 1);
}
public void layout(Component obj, int x, int y, int width, int height){
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
pCalButton.add(obj, gbc);
}
//필드값을 세팅,게팅
public void setView(String s){vCalView.setText(s);}
public String getView(){return vCalView.getText();}
}
//분석하자!!
public class CalculTest {
public static void main(String args[]){
//calculTest ct= new calculTest();
CalculModel calModel = new CalculModel();
CalculControler calCon = new CalculControler(calModel);
}
}
CalculModel.java
public class CalculModel {
CalculControler calCon;
String num1; //기존의 수
String num2; //새로 받은 수
String oper; //연산자
boolean inputNumberAbleStatus; // 화면에 숫자가 새로 받아지는걸 허용/비허용
boolean selectedOperStatus; // 연산자 버튼을 눌럿는지 안 눌렀는지
boolean pointExist; // .을 눌럿는지 안 눌럿는지
//생성자
CalculModel(){
num1="0";
num2="0";
oper="";
inputNumberAbleStatus=true;
selectedOperStatus=false;
pointExist=false;
}
//숫자 입력 받앗을때
public void inputNumber(String num){
if(!num.equals("Number not divide 0")){
if(num.equals(".")){
if(pointExist) num="";
if(inputNumberAbleStatus){
inputNumberAbleStatus=false;
calCon.setView("0"+num);
}else{
calCon.setView(getView()+num);
}
pointExist=true;
}else{
if(inputNumberAbleStatus){
inputNumberAbleStatus=false;
setView(num);
}else{
setView(getView()+num);
}
}
}
}
//기호 입력 받앗을때
public void inputOper(String oper){
if(selectedOperStatus){
num2=getView();
if(!num2.equals("Number not divide 0")){
num1=operation(this.oper, num1, num2);
this.oper=oper;
setView(num1);
inputNumberAbleStatus=true;
pointExist=false;
}
}else{
this.oper=oper;
num2=getView();
num1=operation(oper, num2, num1);
selectedOperStatus=true;
inputNumberAbleStatus=true;
pointExist=false;
}
}
// Clear버튼 눌럿을때
public void inputClear(){
num1="0";
num2="0";
oper="";
setView("0");
inputNumberAbleStatus=true;
selectedOperStatus=false;
}
// 연산자 처리 부분 로직
public String operation(String oper, String num1, String num2){
switch(oper.charAt(0)){
case '+':
num1=""+(Double.parseDouble(num1)+Double.parseDouble(num2));
return num1;
case '-':
num1=""+(Double.parseDouble(num1)-Double.parseDouble(num2));
return num1;
case '*':
if(selectedOperStatus)
num1=""+(Double.parseDouble(num1)*Double.parseDouble(num2));
return num1;
case '/':
if(selectedOperStatus&&num2.equals("0")){
num1="Number not divide 0";
oper="";
}
if(!num2.equals("0"))
num1=""+(Double.parseDouble(num1)/Double.parseDouble(num2));
return num1;
case '=':
num1=num2;
oper="";
return num1;
default:
return num1;
}
}
//소수점이 .0일때 뒤에 .0을 제거하여 정수형으로 나타냄
public String removePointZero(String numWithPoint){
String newNum="";
int pointIdx=0;
int zeroIdx=0;
if(numWithPoint.equals("-0")) return "0";
if(numWithPoint.length()<3) return numWithPoint;
if(numWithPoint.charAt(numWithPoint.length()-2)
=='.'&&numWithPoint.charAt(numWithPoint.length()-1)=='0'){
for(int i=0;i<numWithPoint.length()-2;i++){
newNum+=numWithPoint.charAt(i);
}
return newNum;
}else
return numWithPoint;
}
//뷰의 필드값 세팅/게팅
public void setView(String s){calCon.setView(removePointZero(s));}
public String getView(){return calCon.getView();}
}
CalculControler.java
public class CalculControler {
CalculView calView;
CalculModel calModel;
public CalculControler(CalculModel calModel) {
this.calModel=calModel;
calModel.calCon=this;
calView = new CalculView(this);
calView.setBounds(600,400,300,200);
calView.setResizable(false);
calView.setVisible(true);
}
// 숫자버튼.기호버튼,Clear버튼을 구분
public void inputNumber(String num){calModel.inputNumber(num);}
public void inputOper(String oper){calModel.inputOper(oper);}
public void inputClear(){calModel.inputClear();}
// 계산기창 필드의 값을 setting / getting
public void setView(String s){calView.setView(s);}
public String getView(){return calView.getView();}
}
CalculView.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculView extends JFrame {
CalculControler calCon;
JPanel pCalView, pCalButton;
JLabel vCalView;
JButton numButton[], operButton[];
JButton bZero, bOne, bTwo, bThree, bFour, bFive, bSix, bSeven, bEight, bNine, bPoint;
JButton bPlus, bMinus, bMultiply, bDivide, bEquals;
JButton bClear;
GridBagConstraints gbc;
GridBagLayout gbl;
CalculView(CalculControler calCon){
this.calCon=calCon;
//this.calModel=calModel;
//타이틀과 배치관리자를 설정
Container con = getContentPane();
this.setTitle("이상한 계산기");
con.setLayout(new BorderLayout());
//뷰 패널 생성 및 배치관리자 설정
pCalView = new JPanel();
pCalView.setLayout(new BorderLayout());
//계산 뷰 텍스트창 생성
vCalView = new JLabel("0");
vCalView.setHorizontalAlignment(JLabel.RIGHT);
vCalView.setBackground(Color.WHITE);
vCalView.setFocusable(false);
//뷰 패널에 계산 텍스트 창 합치기
pCalView.add(vCalView);
//프레임에 부착
this.add(pCalView, "North");
//계산 패널 생성
pCalButton = new JPanel();
//숫자 버튼 생성
bZero = new JButton("0");
bOne = new JButton("1");
bTwo = new JButton("2");
bThree = new JButton("3");
bFour = new JButton("4");
bFive = new JButton("5");
bSix = new JButton("6");
bSeven = new JButton("7");
bEight = new JButton("8");
bNine = new JButton("9");
bPoint = new JButton(".");
//리스너 부착
setNumberButton(bZero, bOne, bTwo, bThree, bFour, bFive, bSix, bSeven, bEight, bNine, bPoint);
//연산자 버튼 생성
bPlus = new JButton("+");
bMinus = new JButton("-");
bMultiply = new JButton("*");
bDivide = new JButton("/");
bEquals = new JButton("=");
//리스너 부착
setOperButton(bPlus, bMinus, bMultiply, bDivide, bEquals);
//Clear버튼 생성
bClear = new JButton("C");
setClearButton(bClear);
//리스너부착
setPanelButton();
//배치관리자 버튼 부착
this.add(pCalButton, "Center");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
dispose();
System.exit(0);
}
});
}
//리스너 부착 메소드
public void setNumberButton(JButton ...numButtonArr){
numButton=numButtonArr;
for(JButton numB : numButton)
numB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
calCon.inputNumber(ae.getActionCommand());
}
});
}
// 리스너 부착 메소드
public void setOperButton(JButton ...operButtonArr){
operButton=operButtonArr;
for(JButton operB : operButton)
operB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
calCon.inputOper(ae.getActionCommand());
}
});
}
// 리스너 부착 메소드
public void setClearButton(JButton clearButton){
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calCon.inputClear();
}
});
}
// 레이아웃 메소드
public void setPanelButton(){
gbl = new GridBagLayout();
pCalButton.setLayout(gbl);
gbc=new GridBagConstraints();
gbc.weightx=1.0;
gbc.weighty=1.0;
gbc.fill=GridBagConstraints.BOTH;
layout(bClear, 0, 0, 1, 1);
layout(bDivide, 1, 0, 1, 1);
layout(bMultiply, 2, 0, 1, 1);
layout(bMinus, 3, 0, 1, 1);
layout(bSeven, 0, 1, 1, 1);
layout(bEight, 1, 1, 1, 1);
layout(bNine, 2, 1, 1, 1);
layout(bPlus, 3, 1, 1, 2);
layout(bFour, 0, 2, 1, 1);
layout(bFive, 1, 2, 1, 1);
layout(bSix, 2, 2, 1, 1);
layout(bOne, 0, 3, 1, 1);
layout(bTwo, 1, 3, 1, 1);
layout(bThree, 2, 3, 1, 1);
layout(bEquals, 3, 3, 1, 2);
layout(bZero, 0, 4, 2, 1);
layout(bPoint, 2, 4, 1, 1);
}
public void layout(Component obj, int x, int y, int width, int height){
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
pCalButton.add(obj, gbc);
}
//필드값을 세팅,게팅
public void setView(String s){vCalView.setText(s);}
public String getView(){return vCalView.getText();}
}
//분석하자!!