Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

taimin #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,57 @@ public class TicketBooth {
// ==========
private static final int MAX_QUANTITY = 10;
private static final int ONE_DAY_PRICE = 7400; // when 2019/06/15
private static final int TWO_DAY_PRICE = 13200;

// ===================================================================================
// Attribute
// =========
private int quantity = MAX_QUANTITY;
private Integer salesProceeds;
private int price;

// ===================================================================================
// Constructor
// ===========
public TicketBooth() {
}

public void confirmPrice(int choice){
if (choice==1){price=ONE_DAY_PRICE;}
else if (choice==2){price=TWO_DAY_PRICE;}
else { throw new TicketBadChoiceException("Bad Choice");}
}
// ===================================================================================
// Buy Ticket
// ==========
public void buyOneDayPassport(int handedMoney) {
public TicketBuyResult buyPassport(int choice, int handedMoney){
confirmPrice(choice);
if (quantity <= 0) {
throw new TicketSoldOutException("Sold out");
}
--quantity;
if (handedMoney < ONE_DAY_PRICE) {
if (handedMoney < price) {
throw new TicketShortMoneyException("Short money: " + handedMoney);
}
--quantity;
if (salesProceeds != null) {
salesProceeds = salesProceeds + handedMoney;
salesProceeds = salesProceeds + price;
} else {
salesProceeds = handedMoney;
salesProceeds = price;
}
TicketBuyResult PassportResult = new TicketBuyResult(handedMoney,price);
return PassportResult;
}
public Ticket buyOneDayPassport(int handedMoney) {
buyPassport(1,handedMoney);
Ticket x=new Ticket(ONE_DAY_PRICE);
return x;
}


public TicketBuyResult buyTwoDayPassport(int handedMoney){
buyPassport(2,handedMoney);
TicketBuyResult x=new TicketBuyResult(handedMoney,TWO_DAY_PRICE);
return x;
}
public static class TicketSoldOutException extends RuntimeException {

private static final long serialVersionUID = 1L;
Expand All @@ -65,6 +85,15 @@ public TicketSoldOutException(String msg) {
}
}

public static class TicketBadChoiceException extends RuntimeException {

private static final long serialVersionUID = 1L;

public TicketBadChoiceException(String msg) {
super(msg);
}
}

public static class TicketShortMoneyException extends RuntimeException {

private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.docksidestage.bizfw.basic.buyticket;

public class TicketBuyResult {
private final int handedMoney;
private int price;

public TicketBuyResult(int Money,int price) {
this.handedMoney = Money;
this.price = price;
}
public Ticket getTicket() {
Ticket x=new Ticket(price);
return x;
}

public int getChange() {
return handedMoney-price;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void test_variable_initial() {
String piari = null;
String dstore = "mai";
sea = sea + land + piari + ":" + dstore;
log(sea); // your answer? =>
log(sea); // your answer? => mystic8null :mai
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
Expand All @@ -56,7 +56,7 @@ public void test_variable_reassigned_basic() {
String land = "oneman";
sea = land;
land = land + "'s dreams";
log(sea); // your answer? =>
log(sea); // your answer? => oneman
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
Expand All @@ -65,7 +65,7 @@ public void test_variable_reassigned_int() {
int land = 415;
sea = land;
land++;
log(sea); // your answer? =>
log(sea); // your answer? => 415
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
Expand All @@ -75,7 +75,7 @@ public void test_variable_reassigned_BigDecimal() {
sea = land;
sea = land.add(new BigDecimal(1));
sea.add(new BigDecimal(1));
log(sea); // your answer? =>
log(sea); // your answer? => 416
}

// ===================================================================================
Expand All @@ -89,19 +89,19 @@ public void test_variable_reassigned_BigDecimal() {
/** Same as the previous method question. (前のメソッドの質問と同じ) */
public void test_variable_instance_variable_default_String() {
String sea = instanceBroadway;
log(sea); // your answer? =>
log(sea); // your answer? => null
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
public void test_variable_instance_variable_default_int() {
int sea = instanceDockside;
log(sea); // your answer? =>
log(sea); // your answer? => 0
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
public void test_variable_instance_variable_default_Integer() {
Integer sea = instanceHangar;
log(sea); // your answer? =>
log(sea); // your answer? => null
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
Expand All @@ -110,7 +110,7 @@ public void test_variable_instance_variable_via_method() {
instanceMagiclamp = "magician";
helpInstanceVariableViaMethod(instanceMagiclamp);
String sea = instanceBroadway + "|" + instanceDockside + "|" + instanceHangar + "|" + instanceMagiclamp;
log(sea); // your answer? =>
log(sea); // your answer? => bigband|1|null|magician
}

private void helpInstanceVariableViaMethod(String instanceMagiclamp) {
Expand All @@ -130,7 +130,7 @@ public void test_variable_method_argument_immutable_methodcall() {
String sea = "harbor";
int land = 415;
helpMethodArgumentImmutableMethodcall(sea, land);
log(sea); // your answer? =>
log(sea); // your answer? => harbor
}

private void helpMethodArgumentImmutableMethodcall(String sea, int land) {
Expand All @@ -147,7 +147,7 @@ public void test_variable_method_argument_mutable_methodcall() {
StringBuilder sea = new StringBuilder("harbor");
int land = 415;
helpMethodArgumentMethodcall(sea, land);
log(sea); // your answer? =>
log(sea); // your answer? => harbor416
}

private void helpMethodArgumentMethodcall(StringBuilder sea, int land) {
Expand All @@ -163,13 +163,13 @@ public void test_variable_method_argument_variable_assignment() {
StringBuilder sea = new StringBuilder("harbor");
int land = 415;
helpMethodArgumentVariable(sea, land);
log(sea); // your answer? =>
log(sea); // your answer? => harbor
}

private void helpMethodArgumentVariable(StringBuilder sea, int land) {
private void helpMethodArgumentVariable(StringBuilder localsea, int land) {
++land;
String seaStr = sea.toString(); // is "harbor"
sea = new StringBuilder(seaStr).append(land);
String seaStr = localsea.toString(); // is "harbor"
localsea = new StringBuilder(seaStr).append(land);
}

// ===================================================================================
Expand All @@ -191,8 +191,16 @@ private void helpMethodArgumentVariable(StringBuilder sea, int land) {
* o すべての変数をlog()でカンマ区切りの文字列で表示
* </pre>
*/
private int piari;
public void test_variable_writing() {
// define variables here
String sea="mystic";
Integer land=null;
// int piari;
log(sea);
log(land);
log(piari);

}

// ===================================================================================
Expand All @@ -210,5 +218,16 @@ public void test_variable_writing() {
*/
public void test_variable_yourExercise() {
// write your code here
StringBuilder sea=new StringBuilder("Hello");
testmyown(sea);
log(sea);

}

public void testmyown(StringBuilder localsea){
localsea.append(" world!");
StringBuilder land=localsea;
localsea=new StringBuilder("Hello").append("kitty!");
land.append(" I'm Taimin!");
}
}
63 changes: 48 additions & 15 deletions src/test/java/org/docksidestage/javatry/basic/Step02IfForTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import org.docksidestage.unit.PlainTestCase;

Expand Down Expand Up @@ -52,7 +55,7 @@ public void test_if_else_basic() {
} else {
sea = 7;
}
log(sea); // your answer? =>
log(sea); // your answer? => 7
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
Expand All @@ -67,7 +70,7 @@ public void test_if_elseif_basic() {
} else {
sea = 9;
}
log(sea); // your answer? =>
log(sea); // your answer? => 7
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
Expand All @@ -91,7 +94,7 @@ public void test_if_elseif_nested() {
if (land) {
sea = 10;
}
log(sea); // your answer? =>
log(sea); // your answer? => 10
}

// ===================================================================================
Expand All @@ -107,7 +110,7 @@ public void test_for_inti_basic() {
sea = stage;
}
}
log(sea); // your answer? =>
log(sea); // your answer? => dockside
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
Expand All @@ -117,7 +120,7 @@ public void test_for_foreach_basic() {
for (String stage : stageList) {
sea = stage;
}
log(sea); // your answer? =>
log(sea); // your answer? => magiclamp
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
Expand All @@ -133,7 +136,7 @@ public void test_for_foreach_continueBreak() {
break;
}
}
log(sea); // your answer? =>
log(sea); // your answer? => hangar
}

/** Same as the previous method question. (前のメソッドの質問と同じ) */
Expand All @@ -149,7 +152,7 @@ public void test_for_listforeach_basic() {
}
});
String sea = sb.toString();
log(sea); // your answer? =>
log(sea); // your answer? => dockside
}

// ===================================================================================
Expand All @@ -161,6 +164,14 @@ public void test_for_listforeach_basic() {
*/
public void test_iffor_making() {
// write if-for here
List<String> stageList = prepareStageList();
StringBuilder a=new StringBuilder();
stageList.forEach(stage->{
if (stage.contains("a")) {
a.append(stage+',');
}
});
log(a);
}

// ===================================================================================
Expand All @@ -170,17 +181,31 @@ public void test_iffor_making() {
* Change foreach statement to List's forEach() (keep result after fix) <br>
* (foreach文をforEach()メソッドへの置き換えてみましょう (修正前と修正後で実行結果が同じになるように))
*/
// public void test_iffor_refactor_foreach_to_forEach() {
// List<String> stageList = prepareStageList();
// String sea = null;
// for (String stage : stageList) {
// if (stage.startsWith("br")) {
// continue;
// }
// sea = stage;
// if (stage.contains("ga")) {
// break;
// }
// }
// log(sea); // should be same as before-fix
// }

public void test_iffor_refactor_foreach_to_forEach() {
List<String> stageList = prepareStageList();
String sea = null;
for (String stage : stageList) {
if (stage.startsWith("br")) {
continue;
}
sea = stage;
if (stage.contains("ga")) {
break;
StringBuilder sea = new StringBuilder();
stageList.forEach(stage->{
if (!stage.startsWith("br") && stage.contains("ga") && sea.length()==0) {
sea.append(stage);
}
});
if(sea.length()==0){
sea.append(stageList.get(stageList.size()-1));
}
log(sea); // should be same as before-fix
}
Expand All @@ -197,6 +222,14 @@ public void test_iffor_refactor_foreach_to_forEach() {
*/
public void test_iffor_yourExercise() {
// write your code here

prepareStageList().stream()
.filter(s -> s.startsWith("h"))
.filter(s -> s.length() > 3)
.findFirst()
.ifPresent(s -> {
log(s);
});
}

// ===================================================================================
Expand Down
Loading