Skip to content
Merged
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
18 changes: 9 additions & 9 deletions typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
int givenTime = 50; //50ms
int toWin = 500; //points
int pointsWon = 0;
int numOfRows = 3;
long start = System.currentTimeMillis();
long end = System.currentTimeMillis();
int round = 0;
var givenTime = 50; //50ms
var toWin = 500; //points
var pointsWon = 0;
var numOfRows = 3;
var start = System.currentTimeMillis();
var end = System.currentTimeMillis();
var round = 0;
while (pointsWon < toWin && end - start < givenTime) {
round++;
CellPool pool = new CellPool(numOfRows * numOfRows + 5);
CandyGame cg = new CandyGame(numOfRows, pool);
var pool = new CellPool(numOfRows * numOfRows + 5);
var cg = new CandyGame(numOfRows, pool);
if (round > 1) {
LOGGER.info("Refreshing..");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class CandyGame {
this.cells = new Cell[num][num];
this.pool = pool;
this.totalPoints = 0;
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
for (var i = 0; i < num; i++) {
for (var j = 0; j < num; j++) {
this.cells[i][j] = this.pool.getNewCell();
this.cells[i][j].xIndex = j;
this.cells[i][j].yIndex = i;
Expand All @@ -55,19 +55,19 @@ public class CandyGame {

static String numOfSpaces(int num) {
String result = "";
for (int i = 0; i < num; i++) {
for (var i = 0; i < num; i++) {
result += " ";
}
return result;
}

void printGameStatus() {
LOGGER.info("");
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells.length; j++) {
String candyName = cells[i][j].candy.name;
for (var i = 0; i < cells.length; i++) {
for (var j = 0; j < cells.length; j++) {
var candyName = cells[i][j].candy.name;
if (candyName.length() < 20) {
int totalSpaces = 20 - candyName.length();
var totalSpaces = 20 - candyName.length();
LOGGER.info(numOfSpaces(totalSpaces / 2) + cells[i][j].candy.name
+ numOfSpaces(totalSpaces - totalSpaces / 2) + "|");
} else {
Expand Down Expand Up @@ -105,16 +105,16 @@ ArrayList<Cell> adjacentCells(int yIndex, int xIndex) {
}

boolean continueRound() {
for (int i = 0; i < this.cells.length; i++) {
for (var i = 0; i < this.cells.length; i++) {
if (this.cells[cells.length - 1][i].candy.getType().equals(Type.rewardFruit)) {
return true;
}
}
for (int i = 0; i < this.cells.length; i++) {
for (int j = 0; j < this.cells.length; j++) {
for (var i = 0; i < this.cells.length; i++) {
for (var j = 0; j < this.cells.length; j++) {
if (!this.cells[i][j].candy.getType().equals(Type.rewardFruit)) {
ArrayList<Cell> adj = adjacentCells(i,j);
for (int a = 0; a < adj.size(); a++) {
var adj = adjacentCells(i,j);
for (var a = 0; a < adj.size(); a++) {
if (this.cells[i][j].candy.name.equals(adj.get(a).candy.name)) {
return true;
}
Expand All @@ -132,21 +132,21 @@ void handleChange(int points) {
}

void round(int timeSoFar, int totalTime) {
long start = System.currentTimeMillis();
long end = System.currentTimeMillis();
var start = System.currentTimeMillis();
var end = System.currentTimeMillis();
while (end - start + timeSoFar < totalTime && continueRound()) {
for (int i = 0; i < this.cells.length; i++) {
int points = 0;
int j = this.cells.length - 1;
for (var i = 0; i < this.cells.length; i++) {
var points = 0;
var j = this.cells.length - 1;
while (this.cells[j][i].candy.getType().equals(Type.rewardFruit)) {
points = this.cells[j][i].candy.getPoints();
this.cells[j][i].crush(pool, this.cells);
handleChange(points);
}
}
for (int i = 0; i < this.cells.length; i++) {
int j = cells.length - 1;
int points = 0;
for (var i = 0; i < this.cells.length; i++) {
var j = cells.length - 1;
var points = 0;
while (j > 0) {
points = this.cells[j][i].interact(this.cells[j - 1][i], this.pool, this.cells);
if (points != 0) {
Expand All @@ -156,9 +156,9 @@ void round(int timeSoFar, int totalTime) {
}
}
}
for (int i = 0; i < this.cells.length; i++) {
int j = 0;
int points = 0;
for (var i = 0; i < this.cells.length; i++) {
var j = 0;
var points = 0;
while (j < cells.length - 1) {
points = this.cells[i][j].interact(this.cells[i][j + 1], this.pool, this.cells);
if (points != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ void crush(CellPool pool, Cell[][] cellMatrix) {
}

void fillThisSpace(CellPool pool, Cell[][] cellMatrix) {
for (int y = this.yIndex; y > 0; y--) {
for (var y = this.yIndex; y > 0; y--) {
cellMatrix[y][this.xIndex] = cellMatrix[y - 1][this.xIndex];
cellMatrix[y][this.xIndex].yIndex = y;
}
Cell newC = pool.getNewCell();
var newC = pool.getNewCell();
cellMatrix[0][this.xIndex] = newC;
cellMatrix[0][this.xIndex].xIndex = this.xIndex;
cellMatrix[0][this.xIndex].yIndex = 0;
Expand All @@ -78,7 +78,7 @@ int interact(Cell c, CellPool pool, Cell[][] cellMatrix) {
return 0;
} else {
if (this.candy.name.equals(c.candy.name)) {
int pointsWon = this.candy.getPoints() + c.candy.getPoints();
var pointsWon = this.candy.getPoints() + c.candy.getPoints();
handleCrush(c,pool,cellMatrix);
return pointsWon;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ public class CellPool {
randomCode[4] = new Candy("orange gum", "candy", Type.crushableCandy, 10);
}
for (int i = 0; i < num; i++) {
Cell c = new Cell();
var c = new Cell();
c.candy = randomCode[RANDOM.nextInt(randomCode.length)];
this.pool.add(c);
}
this.pointer = num - 1;
}

Cell getNewCell() {
Cell newCell = this.pool.remove(pointer);
var newCell = this.pool.remove(pointer);
pointer--;
return newCell;
}
Expand All @@ -77,12 +77,12 @@ void addNewCell(Cell c) {
}

Candy[] assignRandomCandytypes() throws FileNotFoundException, IOException, ParseException {
JsonParser jp = new JsonParser();
var jp = new JsonParser();
jp.parse();
Candy[] randomCode = new Candy[jp.candies.size() - 2]; //exclude generic types 'fruit' and 'candy'
int i = 0;
for (Enumeration<String> e = jp.candies.keys(); e.hasMoreElements();) {
String s = e.nextElement();
var randomCode = new Candy[jp.candies.size() - 2]; //exclude generic types 'fruit' and 'candy'
var i = 0;
for (var e = jp.candies.keys(); e.hasMoreElements();) {
var s = e.nextElement();
if (!s.equals("fruit") && !s.equals("candy")) {
//not generic
randomCode[i] = jp.candies.get(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ public class JsonParser {
}

void parse() throws FileNotFoundException, IOException, ParseException {
JSONParser parser = new JSONParser();
JSONObject jo = (JSONObject) parser.parse(new FileReader(new File("").getAbsolutePath()
var parser = new JSONParser();
var jo = (JSONObject) parser.parse(new FileReader(new File("").getAbsolutePath()
+ "\\src\\main\\java\\com\\iluwatar\\typeobject\\candy.json"));
JSONArray a = (JSONArray) jo.get("candies");
for (Object o : a) {
JSONObject candy = (JSONObject) o;
String name = (String) candy.get("name");
String parentName = (String) candy.get("parent");
String t = (String) candy.get("type");
var a = (JSONArray) jo.get("candies");
for (var o : a) {
var candy = (JSONObject) o;
var name = (String) candy.get("name");
var parentName = (String) candy.get("parent");
var t = (String) candy.get("type");
Type type = null;
if (t.equals("rewardFruit")) {
type = Type.rewardFruit;
} else {
type = Type.crushableCandy;
}
int points = Integer.parseInt((String) candy.get("points"));
Candy c = new Candy(name, parentName, type, points);
var points = Integer.parseInt((String) candy.get("points"));
var c = new Candy(name, parentName, type, points);
this.candies.put(name, c);
}
setParentAndPoints();
}

void setParentAndPoints() {
for (Enumeration<String> e = this.candies.keys(); e.hasMoreElements();) {
Candy c = this.candies.get(e.nextElement());
var c = this.candies.get(e.nextElement());
if (c.parentName == null) {
c.parent = null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,33 @@ class CandyGameTest {

@Test
void adjacentCellsTest() {
CandyGame cg = new CandyGame(3,new CellPool(9));
ArrayList<Cell> arr1 = cg.adjacentCells(0, 0);
ArrayList<Cell> arr2 = cg.adjacentCells(1, 2);
ArrayList<Cell> arr3 = cg.adjacentCells(1, 1);
var cg = new CandyGame(3,new CellPool(9));
var arr1 = cg.adjacentCells(0, 0);
var arr2 = cg.adjacentCells(1, 2);
var arr3 = cg.adjacentCells(1, 1);
assertTrue(arr1.size() == 2 && arr2.size() == 3 && arr3.size() == 4);
}

@Test
void continueRoundTest() {
Cell[][] matrix = new Cell[2][2];
Candy c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
Candy c2 = new Candy("purple jelly", "jelly", Type.crushableCandy, 5);
Candy c3 = new Candy("green apple", "apple", Type.rewardFruit, 10);
var matrix = new Cell[2][2];
var c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
var c2 = new Candy("purple jelly", "jelly", Type.crushableCandy, 5);
var c3 = new Candy("green apple", "apple", Type.rewardFruit, 10);
matrix[0][0] = new Cell(c1,0,0);;
matrix[0][1] = new Cell(c2,1,0);
matrix[1][0] = new Cell(c3,0,1);
matrix[1][1] = new Cell(c2,1,1);
CellPool p = new CellPool(4);
CandyGame cg = new CandyGame(2,p);
var p = new CellPool(4);
var cg = new CandyGame(2,p);
cg.cells = matrix;
boolean fruitInLastRow = cg.continueRound();
var fruitInLastRow = cg.continueRound();
matrix[1][0].crush(p, matrix);
matrix[0][0] = new Cell(c3,0,0);
boolean matchingCandy = cg.continueRound();
var matchingCandy = cg.continueRound();
matrix[0][1].crush(p,matrix);
matrix[0][1] = new Cell(c3,1,0);
boolean noneLeft = cg.continueRound();
var noneLeft = cg.continueRound();
assertTrue(fruitInLastRow && matchingCandy && !noneLeft);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class CellPoolTest {

@Test
void assignRandomCandyTypesTest() {
CellPool cp = new CellPool(10);
Hashtable<String, Boolean> ht = new Hashtable<String, Boolean>();
int parentTypes = 0;
for (int i = 0; i < cp.randomCode.length; i++) {
var cp = new CellPool(10);
var ht = new Hashtable<String, Boolean>();
var parentTypes = 0;
for (var i = 0; i < cp.randomCode.length; i++) {
if (ht.get(cp.randomCode[i].name) == null) {
ht.put(cp.randomCode[i].name, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ class CellTest {

@Test
void interactTest() {
Candy c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
Candy c2 = new Candy("green apple", "apple", Type.rewardFruit, 10);
Cell[][] matrix = new Cell[4][4];
var c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
var c2 = new Candy("green apple", "apple", Type.rewardFruit, 10);
var matrix = new Cell[4][4];
matrix[0][0] = new Cell(c1,0,0);
matrix[0][1] = new Cell(c1,1,0);
matrix[0][2] = new Cell(c2,2,0);
matrix[0][3] = new Cell(c1,3,0);
CellPool cp = new CellPool(5);
int points1 = matrix[0][0].interact(matrix[0][1], cp, matrix);
int points2 = matrix[0][2].interact(matrix[0][3], cp, matrix);
var cp = new CellPool(5);
var points1 = matrix[0][0].interact(matrix[0][1], cp, matrix);
var points2 = matrix[0][2].interact(matrix[0][3], cp, matrix);
assertTrue(points1 > 0 && points2 == 0);
}

@Test
void crushTest() {
Candy c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
Candy c2 = new Candy("purple candy", "candy", Type.crushableCandy, 5);
Cell[][] matrix = new Cell[4][4];
var c1 = new Candy("green jelly", "jelly", Type.crushableCandy, 5);
var c2 = new Candy("purple candy", "candy", Type.crushableCandy, 5);
var matrix = new Cell[4][4];
matrix[0][0] = new Cell(c1,0,0);
matrix[1][0] = new Cell(c2,0,1);
matrix[1][0].crush(new CellPool(5), matrix);
Expand Down
10 changes: 5 additions & 5 deletions unit-of-work/src/main/java/com/iluwatar/unitofwork/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public class App {
* @param args no argument sent
*/
public static void main(String[] args) {
Student ram = new Student(1, "Ram", "Street 9, Cupertino");
Student shyam = new Student(2, "Shyam", "Z bridge, Pune");
Student gopi = new Student(3, "Gopi", "Street 10, Mumbai");
var ram = new Student(1, "Ram", "Street 9, Cupertino");
var shyam = new Student(2, "Shyam", "Z bridge, Pune");
var gopi = new Student(3, "Gopi", "Street 10, Mumbai");

HashMap<String, List<Student>> context = new HashMap<>();
StudentDatabase studentDatabase = new StudentDatabase();
StudentRepository studentRepository = new StudentRepository(context, studentDatabase);
var studentDatabase = new StudentDatabase();
var studentRepository = new StudentRepository(context, studentDatabase);

studentRepository.registerNew(ram);
studentRepository.registerModified(shyam);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void registerDeleted(Student student) {
}

private void register(Student student, String operation) {
List<Student> studentsToOperate = context.get(operation);
var studentsToOperate = context.get(operation);
if (studentsToOperate == null) {
studentsToOperate = new ArrayList<>();
}
Expand Down Expand Up @@ -99,24 +99,24 @@ public void commit() {
}

private void commitInsert() {
List<Student> studentsToBeInserted = context.get(IUnitOfWork.INSERT);
for (Student student : studentsToBeInserted) {
var studentsToBeInserted = context.get(IUnitOfWork.INSERT);
for (var student : studentsToBeInserted) {
LOGGER.info("Saving {} to database.", student.getName());
studentDatabase.insert(student);
}
}

private void commitModify() {
List<Student> modifiedStudents = context.get(IUnitOfWork.MODIFY);
for (Student student : modifiedStudents) {
var modifiedStudents = context.get(IUnitOfWork.MODIFY);
for (var student : modifiedStudents) {
LOGGER.info("Modifying {} to database.", student.getName());
studentDatabase.modify(student);
}
}

private void commitDelete() {
List<Student> deletedStudents = context.get(IUnitOfWork.DELETE);
for (Student student : deletedStudents) {
var deletedStudents = context.get(IUnitOfWork.DELETE);
for (var student : deletedStudents) {
LOGGER.info("Deleting {} to database.", student.getName());
studentDatabase.delete(student);
}
Expand Down
Loading