diff --git a/_1_basics/src/main/java/code/_3_in_class/bunaZiua.java b/_1_basics/src/main/java/code/_3_in_class/bunaZiua.java new file mode 100644 index 000000000..d524bde4b --- /dev/null +++ b/_1_basics/src/main/java/code/_3_in_class/bunaZiua.java @@ -0,0 +1,8 @@ +package code._3_in_class; + +public class bunaZiua { + public static void main(String[] args) { + System.out.println("buna ziua"); + } +} + diff --git a/_2_oo/src/main/java/code/_3_in_class/Boxer.java b/_2_oo/src/main/java/code/_3_in_class/Boxer.java new file mode 100644 index 000000000..f99c2f9b9 --- /dev/null +++ b/_2_oo/src/main/java/code/_3_in_class/Boxer.java @@ -0,0 +1,30 @@ +package code._3_in_class; +import java.util.Random; + +public class Boxer { + String nume; + int health=100; + int damagePerAttack=10; + + public Boxer(String nume, int health, int damagePerAttack){ + this.nume=nume; + this.health=health; + this.damagePerAttack=damagePerAttack; + } + public Boxer(String nume){ + this.nume=nume; + } + + public void attak(Boxer opponent){ + int defendValue=(this.damagePerAttack*this.defend())/100; + opponent.health=opponent.health-(this.damagePerAttack+this.defend())/100; + System.out.println(("defendValue: "+defendValue)); + System.out.println(("newHealth: "+opponent.health)); + } + + public void defend() { + Random random=new Random(); + int defendPerc = random.nextInt(101); + return defendPerc; + } +} diff --git a/_2_oo/src/main/java/code/_3_in_class/BruceLee.java b/_2_oo/src/main/java/code/_3_in_class/BruceLee.java new file mode 100644 index 000000000..63979a116 --- /dev/null +++ b/_2_oo/src/main/java/code/_3_in_class/BruceLee.java @@ -0,0 +1,22 @@ +package code._3_in_class; + +public class BruceLee { + String nume; + int health=100; + int damagePerAttack=10; + + public Boxer(String nume, int health, int damagePerAttack){ + this.nume=nume; + this.health=health; + this.damagePerAttack=damagePerAttack; + } + public Boxer(String nume){ + this.nume=nume; + } + + void attak(Boxer opponent){ + opponent.health=0; + } + void defend(){} +} + diff --git a/_2_oo/src/main/java/code/_3_in_class/IBoxer.java b/_2_oo/src/main/java/code/_3_in_class/IBoxer.java new file mode 100644 index 000000000..b8e28a877 --- /dev/null +++ b/_2_oo/src/main/java/code/_3_in_class/IBoxer.java @@ -0,0 +1,7 @@ +package code._3_in_class; + +public interface IBoxer { + public void attack(Boxer opponent); + public int defent(); + +} diff --git a/_2_oo/src/main/java/code/_3_in_class/Main.java b/_2_oo/src/main/java/code/_3_in_class/Main.java index 3b87b4563..516b128bc 100644 --- a/_2_oo/src/main/java/code/_3_in_class/Main.java +++ b/_2_oo/src/main/java/code/_3_in_class/Main.java @@ -1,8 +1,33 @@ package code._3_in_class; - +import java.util.Random; public class Main { public static void main(String[] args) { //TODO put your code changes in here + IBoxer ion=new Boxer("ion", 100, 10); + IBoxer vasile=new Boxer("vasile"); + + startBoxingMatch(ion, vasile); + announceVictory(ion); + } + private static void announceVictory(Boxer ion) { + if(ion.health<=0) + System.out.println("vasile goooo"); + else + System.out.println("ion gooooo"); + } + + //startingBoxingMatch + + + private static void startBoxingMatch(Boxer ion, Boxer vasile){ + Random random= new Random(); + while (ion.health>0 && vasile.health>0){ + int zeroOrOne= random.nextInt(2); + if(zeroOrOne==0) + ion.attak((vasile)); + else + vasile.attak((ion)); + } } } \ No newline at end of file diff --git a/_2_oo/src/main/java/code/_3_in_class/interface01.java b/_2_oo/src/main/java/code/_3_in_class/interface01.java new file mode 100644 index 000000000..960ed2a3c --- /dev/null +++ b/_2_oo/src/main/java/code/_3_in_class/interface01.java @@ -0,0 +1,8 @@ +package code._3_in_class; + +public interface interface01 { + default void m(){ + System.out.println("helllo from m()"); + } +} + diff --git a/_2_oo/src/main/java/code/_3_in_class/interface02.java b/_2_oo/src/main/java/code/_3_in_class/interface02.java new file mode 100644 index 000000000..1501b8f6a --- /dev/null +++ b/_2_oo/src/main/java/code/_3_in_class/interface02.java @@ -0,0 +1,5 @@ +package code._3_in_class; + +public interface interface02 { + default void m2 +} diff --git a/_2_oo/src/main/java/code/_3_in_class/someRandomClass.java b/_2_oo/src/main/java/code/_3_in_class/someRandomClass.java new file mode 100644 index 000000000..6e1f5de80 --- /dev/null +++ b/_2_oo/src/main/java/code/_3_in_class/someRandomClass.java @@ -0,0 +1,13 @@ +package code._3_in_class; + +public class someRandomClass implements interface01, interface02{ + public static void main(){ + someRandomClass c= new someRandomClass(); + c.m(); + } + + @Override + public void m(){ + interface02.super.m(); + } +} diff --git a/_2_oo/src/main/java/code/_3_in_class/superBoxer.java b/_2_oo/src/main/java/code/_3_in_class/superBoxer.java new file mode 100644 index 000000000..a1bc0b255 --- /dev/null +++ b/_2_oo/src/main/java/code/_3_in_class/superBoxer.java @@ -0,0 +1,23 @@ +package code._3_in_class; + +public class superBoxer implements IBoxer { + String nume; + int health=100; + int damagePerAttack=10; + + public Boxer(String nume, int health, int damagePerAttack){ + this.nume=nume; + this.health=health; + this.damagePerAttack=damagePerAttack; + } + public superBoxer(String nume){ + this.nume=nume; + } + @Override + public void attack(Boxer opponent){ + + } + + @Override + public void defent(); +} diff --git a/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/ChessBoard.java b/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/ChessBoard.java index 1e40d959f..e50b49cd9 100644 --- a/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/ChessBoard.java +++ b/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/ChessBoard.java @@ -2,21 +2,33 @@ public class ChessBoard { - public static int MAX_BOARD_WIDTH = 7; - public static int MAX_BOARD_HEIGHT = 7; + public static int MAX_BOARD_WIDTH = 8; + public static int MAX_BOARD_HEIGHT = 8; private Pawn[][] pieces; public ChessBoard() { pieces = new Pawn[MAX_BOARD_WIDTH][MAX_BOARD_HEIGHT]; + } + public void add(Pawn piece, int xCoordinate, int yCoordinate, PieceColor pieceColor) { + if (isLegalBoardPosition(xCoordinate, yCoordinate)) { + piece.setXCoordinate(xCoordinate); + piece.setYCoordinate(yCoordinate); + piece.setPieceColor(pieceColor); + pieces[xCoordinate][yCoordinate] = piece; + } } - public void Add(Pawn pawn, int xCoordinate, int yCoordinate, PieceColor pieceColor) { - throw new UnsupportedOperationException("Need to implement ChessBoard.add()"); + public boolean isLegalBoardPosition(int xCoordinate, int yCoordinate) { + return xCoordinate >= 0 && xCoordinate < MAX_BOARD_WIDTH && + yCoordinate >= 0 && yCoordinate < MAX_BOARD_HEIGHT; } - public boolean IsLegalBoardPosition(int xCoordinate, int yCoordinate) { - throw new UnsupportedOperationException("Need to implement ChessBoard.IsLegalBoardPosition()"); + public Pawn getPiece(int xCoordinate, int yCoordinate) { + if (isLegalBoardPosition(xCoordinate, yCoordinate)) { + return pieces[xCoordinate][yCoordinate]; + } + return null; } } diff --git a/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/MovementType.java b/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/MovementType.java index 326e0721f..8c7579157 100644 --- a/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/MovementType.java +++ b/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/MovementType.java @@ -2,5 +2,5 @@ public enum MovementType { - MOVE, CAPTURE; + MOVE, FORWARD, CAPTURE; } diff --git a/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/Pawn.java b/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/Pawn.java index e589abeb7..658ab1bd9 100644 --- a/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/Pawn.java +++ b/clean_code_projects/_1_project_requirements_chess/src/main/java/clean/code/chess/requirements/Pawn.java @@ -11,10 +11,6 @@ public Pawn(PieceColor pieceColor) { this.pieceColor = pieceColor; } - public ChessBoard getChesssBoard() { - return chessBoard; - } - public void setChessBoard(ChessBoard chessBoard) { this.chessBoard = chessBoard; } @@ -39,21 +35,55 @@ public PieceColor getPieceColor() { return this.pieceColor; } - private void setPieceColor(PieceColor value) { + void setPieceColor(PieceColor value) { pieceColor = value; } - public void Move(MovementType movementType, int newX, int newY) { - throw new UnsupportedOperationException("Need to implement Pawn.Move()"); + private boolean isValidForwardMove(int newX, int newY) { + int direction = (pieceColor == PieceColor.WHITE) ? 1 : -1; + if (newX == xCoordinate + direction && newY == yCoordinate) { + return chessBoard.isLegalBoardPosition(newX, newY) && chessBoard.getPiece(newX, newY) == null; + } + return false; + } + + + public void move(MovementType movementType, int newX, int newY) { + if (movementType == MovementType.FORWARD) { + if (isValidForwardMove(newX, newY)) { + chessBoard.getPiece(xCoordinate, yCoordinate).setXCoordinate(newX); + chessBoard.getPiece(xCoordinate, yCoordinate).setYCoordinate(newY); + chessBoard.add(chessBoard.getPiece(xCoordinate, yCoordinate), newX, newY, pieceColor); + chessBoard.add(null, xCoordinate, yCoordinate, null); + } + } else if (movementType == MovementType.CAPTURE) { + if (isValidCaptureMove(newX, newY)) { + chessBoard.add(null, newX, newY, null); + chessBoard.getPiece(xCoordinate, yCoordinate).setXCoordinate(newX); + chessBoard.getPiece(xCoordinate, yCoordinate).setYCoordinate(newY); + chessBoard.add(chessBoard.getPiece(xCoordinate, yCoordinate), newX, newY, pieceColor); + chessBoard.add(null, xCoordinate, yCoordinate, null); + } + } + } + + private boolean isValidCaptureMove(int newX, int newY) { + int direction = (pieceColor == PieceColor.WHITE) ? 1 : -1; + if (newX == xCoordinate + direction && Math.abs(newY - yCoordinate) == 1) { + return chessBoard.isLegalBoardPosition(newX, newY) && chessBoard.getPiece(newX, newY) != null && + chessBoard.getPiece(newX, newY).getPieceColor() != pieceColor; + } + return false; } + @Override public String toString() { - return CurrentPositionAsString(); + return currentPositionAsString(); } - protected String CurrentPositionAsString() { + protected String currentPositionAsString() { String eol = System.lineSeparator(); - return String.format("Current X: {1}{0}Current Y: {2}{0}Piece Color: {3}", eol, xCoordinate, yCoordinate, pieceColor); + return String.format("Current X: %d%sCurrent Y: %d%sPiece Color: %s", xCoordinate, eol, yCoordinate, eol, pieceColor); } } diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/DiscJockey.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/DiscJockey.class new file mode 100644 index 000000000..73abfabec Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/DiscJockey.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/RadioStation.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/RadioStation.class new file mode 100644 index 000000000..a06376387 Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/RadioStation.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongInfo.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongInfo.class new file mode 100644 index 000000000..836382acd Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongInfo.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongIterator.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongIterator.class new file mode 100644 index 000000000..5c3947e3d Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongIterator.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongsOfThe70s.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongsOfThe70s.class new file mode 100644 index 000000000..46d82238c Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongsOfThe70s.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongsOfThe80s.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongsOfThe80s.class new file mode 100644 index 000000000..4b6a8b0c6 Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongsOfThe80s.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongsOfThe90s.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongsOfThe90s.class new file mode 100644 index 000000000..9d46f2cd7 Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/out/production/Iterator/SongsOfThe90s.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/DiscJockey.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/DiscJockey.java new file mode 100644 index 000000000..1f9aa22af --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/DiscJockey.java @@ -0,0 +1,57 @@ +package clean.code.design_patterns.requirements.Iterator.src; + +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Iterator; + +public class DiscJockey { + + SongsOfThe70s songs70s; + SongsOfThe80s songs80s; + SongsOfThe90s songs90s; + + SongIterator iter70sSongs; + SongIterator iter80sSongs; + SongIterator iter90sSongs; + + + public DiscJockey(SongIterator newSongs70s, SongIterator newSongs80s, SongIterator newSongs90s) { + + iter70sSongs = newSongs70s; + iter80sSongs = newSongs80s; + iter90sSongs = newSongs90s; + + } + public void showTheSongs(){ + + Iterator Songs70s = iter70sSongs.createIterator(); + Iterator Songs80s = iter80sSongs.createIterator(); + Iterator Songs90s = iter90sSongs.createIterator(); + + System.out.println("Songs of the 70s\n"); + printTheSongs(Songs70s); + // :) + System.out.println("Songs of the 80s\n"); + printTheSongs(Songs80s); + + System.out.println("Songs of the 90s\n"); + printTheSongs(Songs90s); + + } + + public void printTheSongs(Iterator iterator){ + + while(iterator.hasNext()){ + + SongInfo songInfo = (SongInfo) iterator.next(); + + System.out.println(songInfo.getSongName()); + System.out.println(songInfo.getBandName()); + System.out.println(songInfo.getYearReleased() + "\n"); + + } + + } + +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/RadioStation.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/RadioStation.java new file mode 100644 index 000000000..5a20782dc --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/RadioStation.java @@ -0,0 +1,19 @@ +package clean.code.design_patterns.requirements.Iterator.src; + +public class RadioStation { + + public static void main(String[] args){ + + SongsOfThe70s songs70s = new SongsOfThe70s(); + SongsOfThe80s songs80s = new SongsOfThe80s(); + SongsOfThe90s songs90s = new SongsOfThe90s(); + + DiscJockey album = new DiscJockey(songs70s, songs80s, songs90s); + + // madMike.showTheSongs(); + + album.showTheSongs(); + + } + +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongInfo.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongInfo.java new file mode 100644 index 000000000..a9ee63c1b --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongInfo.java @@ -0,0 +1,24 @@ +package clean.code.design_patterns.requirements.Iterator.src; + +public class SongInfo{ + + String songName; + String bandName; + int yearReleased; + + public SongInfo(String newSongName, String newBandName, int newYearReleased){ + songName = newSongName; + bandName = newBandName; + yearReleased = newYearReleased; + } + + public String getSongName(){ + return songName; + } + public String getBandName(){ + return bandName; + } + public int getYearReleased(){ + return yearReleased; + } +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongIterator.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongIterator.java new file mode 100644 index 000000000..05fa65733 --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongIterator.java @@ -0,0 +1,9 @@ +package clean.code.design_patterns.requirements.Iterator.src; + +import java.util.Iterator; + +public interface SongIterator { + + public Iterator createIterator(); + +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongsOfThe70s.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongsOfThe70s.java new file mode 100644 index 000000000..a7fefe82e --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongsOfThe70s.java @@ -0,0 +1,39 @@ +package clean.code.design_patterns.requirements.Iterator.src; + +import java.util.ArrayList; +import java.util.Iterator; + +public class SongsOfThe70s implements SongIterator{ + + // ArrayList holds SongInfo objects + ArrayList bestSongs; + + public SongsOfThe70s() { + + bestSongs = new ArrayList(); + + addSong("Imagine", "John Lennon", 1971); + addSong("American Pie", "Don McLean", 1971); + addSong("I Will Survive", "Gloria Gaynor", 1979); + + } + + // Add a SongInfo object to the end of the ArrayList + public void addSong(String songName, String bandName, int yearReleased){ + + SongInfo songInfo = new SongInfo(songName, bandName, yearReleased); + + bestSongs.add(songInfo); + + } + + public ArrayList getBestSongs(){ + return bestSongs; + + } + + public Iterator createIterator() { + return bestSongs.iterator(); + } + +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongsOfThe80s.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongsOfThe80s.java new file mode 100644 index 000000000..3d17fe2c6 --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongsOfThe80s.java @@ -0,0 +1,49 @@ +package clean.code.design_patterns.requirements.Iterator.src; + +import java.util.Arrays; +import java.util.Iterator; + +public class SongsOfThe80s implements SongIterator{ + + // Create an array of SongInfo Objects + SongInfo[] bestSongs; + + // Used to increment to the next position in the array + int arrayValue = 0; + + public SongsOfThe80s() { + + bestSongs = new SongInfo[3]; + + addSong("Roam", "B 52s", 1989); + addSong("Cruel Summer", "Bananarama", 1984); + addSong("Head Over Heels", "Tears For Fears", 1985); + + } + + // Add a SongInfo Object to the array and increment to the next position + public void addSong(String songName, String bandName, int yearReleased){ + + SongInfo song = new SongInfo(songName, bandName, yearReleased); + + bestSongs[arrayValue] = song; + + arrayValue++; + + } + + // This is replaced by the Iterator + public SongInfo[] getBestSongs(){ + + return bestSongs; + + } + + // NEW By adding this method I'll be able to treat all + // collections the same + @Override + public Iterator createIterator() { + return Arrays.asList(bestSongs).iterator(); + } + +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongsOfThe90s.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongsOfThe90s.java new file mode 100644 index 000000000..1849030ac --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Iterator/src/SongsOfThe90s.java @@ -0,0 +1,54 @@ +package clean.code.design_patterns.requirements.Iterator.src; + +import java.util.Hashtable; +import java.util.Iterator; + +public class SongsOfThe90s implements SongIterator{ + + // Create a Hashtable with an int as a key and SongInfo + // Objects + + Hashtable bestSongs = new Hashtable(); + + // Will increment the Hashtable key + + int hashKey = 0; + + public SongsOfThe90s() { + + addSong("Losing My Religion", "REM", 1991); + addSong("Creep", "Radiohead", 1993); + addSong("Walk on the Ocean", "Toad The Wet Sprocket", 1991); + + } + + // Add a new SongInfo Object to the Hashtable and then increment + // the Hashtable key + + public void addSong(String songName, String bandName, int yearReleased){ + + SongInfo songInfo = new SongInfo(songName, bandName, yearReleased); + + bestSongs.put(hashKey, songInfo); + + hashKey++; + + } + + // This is replaced by the Iterator + // Return a Hashtable full of SongInfo Objects + + public Hashtable getBestSongs(){ + + return bestSongs; + + } + + // NEW By adding this method I'll be able to treat all + // collections the same + + public Iterator createIterator() { + return bestSongs.values().iterator(); + } + +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/GetTheStock.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/GetTheStock.class new file mode 100644 index 000000000..e18ff9e61 Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/GetTheStock.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/GrabStocks.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/GrabStocks.class new file mode 100644 index 000000000..5f5820eb3 Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/GrabStocks.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/Observer.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/Observer.class new file mode 100644 index 000000000..f43ad0f5c Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/Observer.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/StockGrabber.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/StockGrabber.class new file mode 100644 index 000000000..90acc1bc6 Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/StockGrabber.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/StockObserver.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/StockObserver.class new file mode 100644 index 000000000..dc92fa9a9 Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/StockObserver.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/Subject.class b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/Subject.class new file mode 100644 index 000000000..7253aaf3e Binary files /dev/null and b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/out/production/Observer/Subject.class differ diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/GetTheStock.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/GetTheStock.java new file mode 100644 index 000000000..23e5cd5ad --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/GetTheStock.java @@ -0,0 +1,52 @@ +package clean.code.design_patterns.requirements.Observer.src; + +import java.text.DecimalFormat; + +public class GetTheStock implements Runnable{ + + // Could be used to set how many seconds to wait in Thread.sleep() below + + // private int startTime; + private String stock; + private double price; + + // Will hold reference to the StockGrabber object + + private Subject stockGrabber; + + public GetTheStock(Subject stockGrabber, int newStartTime, String newStock, double newPrice){ + + // Store the reference to the stockGrabber object, so I can make calls to its methods + this.stockGrabber = stockGrabber; + stock = newStock; + price = newPrice; + + } + + public void run(){ + + for(int i = 1; i <= 20; i++){ + + try{ + // Sleep for 2 seconds + Thread.sleep(2000); + } + catch(InterruptedException e) + {} + + double randNum = (Math.random() * (.06)) - .03; + DecimalFormat df = new DecimalFormat("#.##"); + price = Double.valueOf(df.format((price + randNum))); + + if(stock == "IBM") ((StockGrabber) stockGrabber).setIBMPrice(price); + if(stock == "AAPL") ((StockGrabber) stockGrabber).setAAPLPrice(price); + if(stock == "GOOG") ((StockGrabber) stockGrabber).setGOOGPrice(price); + + System.out.println(stock + ": " + df.format((price + randNum)) + + " " + df.format(randNum)); + System.out.println(); + + } + } + +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/GrabStocks.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/GrabStocks.java new file mode 100644 index 000000000..ee3205670 --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/GrabStocks.java @@ -0,0 +1,51 @@ +package clean.code.design_patterns.requirements.Observer.src; + +public class GrabStocks{ + + public static void main(String[] args){ + + // Create the Subject object + // It will handle updating all observers + // as well as deleting and adding them + + StockGrabber stockGrabber = new StockGrabber(); + + // Create an Observer that will be sent updates from Subject + + StockObserver observer1 = new StockObserver(stockGrabber); + + stockGrabber.setIBMPrice(90); + stockGrabber.setAAPLPrice(100); + stockGrabber.setGOOGPrice(123); + + StockObserver observer2 = new StockObserver(stockGrabber); + + stockGrabber.setIBMPrice(90); + stockGrabber.setAAPLPrice(100); + stockGrabber.setGOOGPrice(123); + + // Delete one of the observers + //stockGrabber.unregister(observer2); + + stockGrabber.setIBMPrice(90); + stockGrabber.setAAPLPrice(100); + stockGrabber.setGOOGPrice(123); + + // Create 3 threads using the Runnable interface + // GetTheStock implements Runnable, so it doesn't waste + // its one extendable class option + + Runnable getIBM = new GetTheStock(stockGrabber, 2, "IBM", 90); + Runnable getAAPL = new GetTheStock(stockGrabber, 2, "AAPL", 100); + Runnable getGOOG = new GetTheStock(stockGrabber, 2, "GOOG", 123); + + // Call for the code in run to execute + + new Thread(getIBM).start(); + new Thread(getAAPL).start(); + new Thread(getGOOG).start(); + + + } + +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/Observer.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/Observer.java new file mode 100644 index 000000000..a49ddbd6c --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/Observer.java @@ -0,0 +1,5 @@ +package clean.code.design_patterns.requirements.Observer.src;// The Observers update method is called when the Subject changes + +public interface Observer { + public void update(double ibmPrice, double aaplPrice, double googPrice); +} diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/StockGrabber.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/StockGrabber.java new file mode 100644 index 000000000..204c2b435 --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/StockGrabber.java @@ -0,0 +1,61 @@ +package clean.code.design_patterns.requirements.Observer.src; + +import java.util.ArrayList; + +// Uses the Subject interface to update all Observers +public class StockGrabber implements Subject{ + + private ArrayList observers; + private double ibmPrice; + private double aaplPrice; + private double googPrice; + + public StockGrabber(){ + observers = new ArrayList(); + } + + public void register(Observer newObserver) { + observers.add(newObserver); + + } + + public void unregister(Observer deleteObserver) { + int observerIndex = observers.indexOf(deleteObserver); + System.out.println("Observer " + (observerIndex+1) + " deleted"); + observers.remove(observerIndex); + + } + + public void notifyObserver() { + + // Cycle through all observers and notifies them of + // price changes + for(Observer observer : observers){ + observer.update(ibmPrice, aaplPrice, googPrice); + } + } + + // Change prices for all stocks and notifies observers of changes + public void setIBMPrice(double newIBMPrice){ + this.ibmPrice = newIBMPrice; + notifyObserver(); + + } + + public void setAAPLPrice(double newAAPLPrice){ + + this.aaplPrice = newAAPLPrice; + + notifyObserver(); + + } + + public void setGOOGPrice(double newGOOGPrice){ + + this.googPrice = newGOOGPrice; + + notifyObserver(); + + } + +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/StockObserver.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/StockObserver.java new file mode 100644 index 000000000..f775f9839 --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/StockObserver.java @@ -0,0 +1,41 @@ +package clean.code.design_patterns.requirements.Observer.src;// Represents each Observer that is monitoring changes in the subject + +public class StockObserver implements Observer { + + private double ibmPrice; + private double aaplPrice; + private double googPrice; + + private static int observerIDTracker = 0; + private int observerID; + private Subject stockGrabber; + + public StockObserver(Subject stockGrabber){ + // Store the reference to the stockGrabber object so + // I can make calls to its methods + this.stockGrabber = stockGrabber; + this.observerID = ++observerIDTracker; + System.out.println("New Observer " + this.observerID); + stockGrabber.register(this); + + } + + // Called to update all observers + public void update(double ibmPrice, double aaplPrice, double googPrice) { + + this.ibmPrice = ibmPrice; + this.aaplPrice = aaplPrice; + this.googPrice = googPrice; + + printThePrices(); + + } + + public void printThePrices(){ + + System.out.println(observerID + "\nIBM: " + ibmPrice + "\nAAPL: " + + aaplPrice + "\nGOOG: " + googPrice + "\n"); + + } + +} \ No newline at end of file diff --git a/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/Subject.java b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/Subject.java new file mode 100644 index 000000000..138a63d8f --- /dev/null +++ b/clean_code_projects/_2_project_requirements_design_patterns/src/main/java/clean/code/design_patterns/requirements/Observer/src/Subject.java @@ -0,0 +1,11 @@ +package clean.code.design_patterns.requirements.Observer.src; + +// This interface handles adding, deleting and updating +// all observers +public interface Subject { + + public void register(Observer o); + public void unregister(Observer o); + public void notifyObserver(); + +}