Last active
December 27, 2015 05:49
-
-
Save kawakawa/7276597 to your computer and use it in GitHub Desktop.
2013.11.02 XP寺小屋 シンプル・コーディングのペアプロ結果です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class Calc { | |
public static void main(String Args[]) throws IOException{ | |
InputStreamReader isr = new InputStreamReader(System.in); | |
BufferedReader br = new BufferedReader(isr); | |
String num1 = inputNum(); | |
String ope = inputOperator(); | |
String num2=inputNum(); | |
Calc calc=new Calc(); | |
String resultTotal = calc.operating(num1, ope, num2); | |
System.out.println(num1+ope+num2+"="+resultTotal); | |
} | |
private static String inputOperator() throws IOException { | |
InputStreamReader isr = new InputStreamReader(System.in); | |
BufferedReader br = new BufferedReader(isr); | |
Check check=new Check(); | |
while(true){ | |
String inputOperator=br.readLine(); | |
if(check.isOperator(inputOperator)==true){ | |
return inputOperator; | |
} | |
System.out.println("演算子の入力値が正しくありません。"); | |
} | |
} | |
private static String inputNum() throws IOException { | |
InputStreamReader isr = new InputStreamReader(System.in); | |
BufferedReader br = new BufferedReader(isr); | |
Check check=new Check(); | |
while(true){ | |
String inputNum=br.readLine(); | |
if(check.isNum(inputNum)==true){ | |
return inputNum; | |
} | |
System.out.println("入力値が正しくありません。"); | |
} | |
} | |
public String operating(String num1String,String opeString,String num2String){ | |
int num1 = Integer.parseInt(num1String); | |
int num2= Integer.parseInt(num2String); | |
int total=0; | |
if(opeString.equals("+")){ | |
total= num1+num2; | |
} | |
else if(opeString.equals("-")){ | |
total=num1-num2; | |
} | |
else if(opeString.equals("*")){ | |
total=num1*num2; | |
} | |
else if(opeString.equals("/")){ | |
total=num1/num2; | |
} | |
return Integer.toString(total); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Check { | |
public boolean isNum(String target) { | |
if(target.equals("1")){ | |
return true; | |
} | |
if(target.equals("2")){ | |
return true; | |
} | |
if(target.equals("3")){ | |
return true; | |
} | |
if(target.equals("4")){ | |
return true; | |
} | |
if(target.equals("5")){ | |
return true; | |
} | |
if(target.equals("6")){ | |
return true; | |
} | |
if(target.equals("7")){ | |
return true; | |
} | |
if(target.equals("8")){ | |
return true; | |
} | |
if(target.equals("9")){ | |
return true; | |
} | |
return false; | |
} | |
public boolean isOperator(String target) { | |
if(target.equals("+")){ | |
return true; | |
} | |
if(target.equals("-")){ | |
return true; | |
} | |
if(target.equals("*")){ | |
return true; | |
} | |
if(target.equals("/")){ | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.hamcrest.CoreMatchers.*; | |
import static org.junit.Assert.*; | |
public class Test { | |
@org.junit.Test | |
public void 数値がゼロの場合はFalseとなることをテスト() { | |
Check sut = new Check(); | |
String target="0"; | |
boolean actual= sut.isNum(target); | |
boolean expected=false; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void _1桁の数値の入力はTrueとなることをテスト() { | |
Check sut = new Check(); | |
String target="1"; | |
boolean actual= sut.isNum(target); | |
boolean expected=true; | |
assertThat(expected,is(actual)); | |
target="2"; | |
actual=sut.isNum(target); | |
assertThat(expected,is(actual)); | |
target="2"; | |
actual=sut.isNum(target); | |
assertThat(expected,is(actual)); | |
target="3"; | |
actual=sut.isNum(target); | |
assertThat(expected,is(actual)); | |
target="4"; | |
actual=sut.isNum(target); | |
assertThat(expected,is(actual)); | |
target="5"; | |
actual=sut.isNum(target); | |
assertThat(expected,is(actual)); | |
target="6"; | |
actual=sut.isNum(target); | |
assertThat(expected,is(actual)); | |
target="7"; | |
actual=sut.isNum(target); | |
assertThat(expected,is(actual)); | |
target="8"; | |
actual=sut.isNum(target); | |
assertThat(expected,is(actual)); | |
target="9"; | |
actual=sut.isNum(target); | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void _2ケタの数値の入力はFalseとなることをテスト(){ | |
Check sut = new Check(); | |
String target="10"; | |
boolean actual=sut.isNum(target); | |
boolean expected=false; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 数値以外の入力の場合Falseとなることをテスト(){ | |
Check sut=new Check(); | |
String target="a"; | |
boolean actual=sut.isNum(target); | |
boolean expected=false; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 演算子入力_プラス_の場合はTrueとなるこをテスト(){ | |
Check sut=new Check(); | |
String target="+"; | |
boolean actual=sut.isOperator(target); | |
boolean expected=true; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 演算子入力_マイナス_の場合はTrueとなるこをテスト(){ | |
Check sut=new Check(); | |
String target="-"; | |
boolean actual=sut.isOperator(target); | |
boolean expected=true; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 演算子入力_カケル_の場合はTrueとなるこをテスト(){ | |
Check sut=new Check(); | |
String target="*"; | |
boolean actual=sut.isOperator(target); | |
boolean expected=true; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 演算子入力_割る_の場合はTrueとなるこをテスト(){ | |
Check sut=new Check(); | |
String target="/"; | |
boolean actual=sut.isOperator(target); | |
boolean expected=true; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 演算結果_1足す1が正しいことをテストする() | |
{ | |
Calc sut=new Calc(); | |
String num1="1"; | |
String ope="+"; | |
String num2="1"; | |
String actual=sut.operating(num1,ope,num2); | |
String expected="2"; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 演算結果_9足す9が正しいことをテストする() | |
{ | |
Calc sut=new Calc(); | |
String num1="9"; | |
String ope="+"; | |
String num2="9"; | |
String actual=sut.operating(num1,ope,num2); | |
String expected="18"; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 演算結果_1引く1が正しいことをテストする() | |
{ | |
Calc sut=new Calc(); | |
String num1="1"; | |
String ope="-"; | |
String num2="1"; | |
String actual=sut.operating(num1,ope,num2); | |
String expected="0"; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 演算結果_1引く9が正しいことをテストする() | |
{ | |
Calc sut=new Calc(); | |
String num1="1"; | |
String ope="-"; | |
String num2="9"; | |
String actual=sut.operating(num1,ope,num2); | |
String expected="-8"; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 演算結果_1かける1が正しいことをテストする() | |
{ | |
Calc sut=new Calc(); | |
String num1="1"; | |
String ope="*"; | |
String num2="1"; | |
String actual=sut.operating(num1,ope,num2); | |
String expected="1"; | |
assertThat(expected,is(actual)); | |
} | |
@org.junit.Test | |
public void 演算結果_1ワル1が正しいことをテストする() | |
{ | |
Calc sut=new Calc(); | |
String num1="1"; | |
String ope="/"; | |
String num2="1"; | |
String actual=sut.operating(num1,ope,num2); | |
String expected="1"; | |
assertThat(actual,is(expected)); | |
} | |
@org.junit.Test | |
public void 演算結果_1ワル3が正しいことをテストする() | |
{ | |
Calc sut=new Calc(); | |
String num1="1"; | |
String ope="/"; | |
String num2="3"; | |
String actual=sut.operating(num1,ope,num2); | |
String expected="0"; | |
assertThat(actual,is(expected)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment