Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
みなとRuby会議01 ソーシャルコーディング あみだくじ
package org.yokohama.rb.kaigi01;
import org.yokohama.rb.kaigi01.result.ResultViewModel;
public class CreateAmidakuji {
public CreateAmidakuji() {
ResultViewModel model = new ResultViewModel(5);
System.out.println(model.toString());
}
}
package org.yokohama.rb.kaigi01.result;
public interface ResultColumn {
public String toString();
}
package org.yokohama.rb.kaigi01.result;
import java.util.Random;
public class ResultViewFooter implements ResultColumn {
private String footer = "";
private int rowNumber;
public ResultViewFooter(int rowNumber) {
this.rowNumber = rowNumber;
initialize();
}
private void initialize() {
int rnd = getRandom();
for (int i = 0; i < this.rowNumber; i++) {
if (i == rnd) {
this.footer += "!!! ";
} else {
this.footer += "  ";
}
}
}
private int getRandom() {
return new Random().nextInt(this.rowNumber-1);
}
@Override
public String toString() {
return this.footer;
}
}
package org.yokohama.rb.kaigi01.result;
public class ResultViewHeader implements ResultColumn {
private String header = " A B C D E";
public ResultViewHeader(){
}
@Override
public String toString() {
return this.header;
}
}
package org.yokohama.rb.kaigi01.result;
import java.util.ArrayList;
import java.util.List;
public class ResultViewModel {
private final int columnNumber = 8;
private final int rowNumber;
private List<ResultColumn> view;
public ResultViewModel(int rowNumber) {
this.view = new ArrayList<ResultColumn>();;
this.rowNumber = rowNumber;
initializeView();
}
private void initializeView() {
view.add(new ResultViewHeader());
for(int i = 0; i < this.columnNumber-2; i++){
view.add(new ResultViewElement(this.rowNumber));
}
view.add(new ResultViewFooter(this.rowNumber));
}
public String toString(){
String result = "";
for (ResultColumn column : this.view) {
result += column.toString() + "¥n";
}
return result;
}
}
package org.yokohama.rb.kaigi01.result;
import java.util.Random;
public class ResultViewElement implements ResultColumn {
private String column = " ";
private int rowNumber;
private final String TAB = " ";
public ResultViewElement(int rowNumber){
this.rowNumber = rowNumber;
this.fill();
}
protected void fill() {
// 結果を初期化
initializeColumn();
}
private int getRandom() {
return new Random().nextInt(this.rowNumber-1);
}
public int getRowNumber() {
return rowNumber;
}
public void setRowNumber(int rowNumber) {
this.rowNumber = rowNumber;
}
public void initializeColumn() {
int rnd = getRandom();
for(int i=0; i<rowNumber; i++){
if(i == rnd){
this.column += "|---";
}else{
this.column += "" + TAB ;
}
}
}
@Override
public String toString() {
return this.column;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.