Skip to content
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
7 changes: 7 additions & 0 deletions _1_basics/src/main/java/code/_3_in_class/ClasaBunaZiua.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package code._3_in_class;

public class ClasaBunaZiua {
public static void main(String[] args) {
System.out.println("Buna ziua!");
}
}
47 changes: 47 additions & 0 deletions _2_oo/src/main/java/code/_3_in_class/Boxor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package code._3_in_class;

import java.util.Random;

public class Boxor implements IBoxer{

String name;
int health;
int damagePerAttack;

public Boxor(String name, int health, int damagePerAttack)
{
this.name = name;
this.health = health;
this.damagePerAttack = damagePerAttack;
}

public Boxor(String nume)
{
this.name = nume;
}
public void attack(IBoxer opponent)
{
int defendValue = (this.damagePerAttack * this.defend()) / 100;
opponent.receiveAttack(this.damagePerAttack - defendValue);
}
public int defend()
{
Random random = new Random();
int defendPercentage = random.nextInt(101);

return defendPercentage;
}

public boolean isAlive()
{
return this.health > 0;
}

@Override
public void receiveAttack(int damage) {

this.health = this.health - damage;
System.out.println("defendValue: " + damage);
System.out.println("newHealth: " + this.health);
}
}
9 changes: 9 additions & 0 deletions _2_oo/src/main/java/code/_3_in_class/IBoxer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package code._3_in_class;

public interface IBoxer {

public void attack(IBoxer opponent);
public int defend();
public boolean isAlive();
public void receiveAttack(int damage);
}
32 changes: 31 additions & 1 deletion _2_oo/src/main/java/code/_3_in_class/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
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 SuperBoxer("ion", 100, 10);
IBoxer vasile = new Boxor( "vasile", 100, 10);


startBoxingMatch(ion, vasile);
endingResults(ion);
}

private static void endingResults(IBoxer ion) {
if(!ion.isAlive())
System.out.println("Vasile won!");
else
System.out.println("Ion won!");
}

//startBoxingMatch_Boxor_Boxor
//startBoxingMatch_IBoxer_IBoxer
private static void startBoxingMatch(IBoxer ion, IBoxer vasile) {
Random random = new Random();

while(ion.isAlive() && vasile.isAlive())
{
int randVar = random.nextInt(2);
if(randVar == 1)
ion.attack(vasile);
else
vasile.attack(ion);
}
}
}
39 changes: 39 additions & 0 deletions _2_oo/src/main/java/code/_3_in_class/SuperBoxer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package code._3_in_class;

public class SuperBoxer implements IBoxer{

String name;
int health;
int damagePerAttack;
public SuperBoxer(String name, int health, int damagePerAttack)
{
this.name = name;
this.health = health;
this.damagePerAttack = damagePerAttack;
}

public SuperBoxer(String nume)
{
this.name = nume;
}

@Override
public void attack(IBoxer opponent) {

}

@Override
public int defend() {
return 0;
}

@Override
public boolean isAlive() {
return this.health > 0;
}

@Override
public void receiveAttack(int damage) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@
public class Main {

public static void main(String[] args) {
//TODO implement your design patterns in this package

//Singleton pattern + Builder pattern

User Damian = new User("damiano992");
User Angela = new User("ur_angel");
User Robin = new User("epicSidekick_12");

//the builder in action
Damian.addSong(new Song.Builder("Torna a casa")
.setArtist("Maneskin")
.setDuration("3:51")
.setGenre("ROCK")
.setUser(Damian.getUserName())
.build());

Damian.showPlaylist();

Angela.showPlaylist();

//the builder in action
Robin.addSong(new Song.Builder("Summertime Sadness")
.setArtist("Lana Del Rey")
.setUser(Robin.getUserName())
.setGenre("POP")
.setDuration("4:25")
.build());

Angela.showPlaylist();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package clean.code.design_patterns.requirements;

import java.util.ArrayList;
import java.util.*;

//Singleton design pattern
public class MusicPlaylist {
//unique instance
private static volatile MusicPlaylist instance;
//identify the playlist
private static String name;
//the list of all the songs
private static List<Song> songs = new ArrayList<Song>();

//we set up the name of the playlist in the beginning
private MusicPlaylist(String name) {
instance.name = name;
}

//builder pattern magic
public static MusicPlaylist getInstance(String name) {
if (instance == null) {
synchronized (MusicPlaylist.class) {
if (instance == null) {
instance = new MusicPlaylist(name);
}
}
}
return instance;
}

//we want to add new songs
public static void addSong(Song newSong)
{
instance.songs.add((newSong));
}

//we want to see the playlist
public static void showPlaylist()
{
for(Song listSong:songs)
listSong.listDetails();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package clean.code.design_patterns.requirements;

public class Settings {

private static volatile Settings instance;
private Short volumeLevel;
private Short musicLevel;
private boolean enhancedGraphicsMode;
private boolean stereoSound;

private Settings() {
this.volumeLevel = 255;
this.musicLevel = 255;
this.enhancedGraphicsMode = true;
this.stereoSound = false;
}

public static Settings getInstance()
{
if(instance == null)
{
synchronized (Settings.class)
{
if(instance == null)
{
instance = new Settings();
}
}
}
return instance;
}

public static void changeVolume(int newValue)
{
if(newValue <= 0)
instance.volumeLevel = 0;
else if(newValue >=255)
instance.volumeLevel = 255;
else
instance.volumeLevel = (short)newValue;
}

public static void changeMusic(int newValue)
{
if(newValue <= 0)
instance.musicLevel = 0;
else if(newValue >=255)
instance.musicLevel = 255;
else
instance.musicLevel = (short)newValue;
}

public static void toggleEG()
{
if(instance.enhancedGraphicsMode)
instance.enhancedGraphicsMode = false;
else
instance.enhancedGraphicsMode = true;
}

public static void toggleEG(boolean val)
{
instance.enhancedGraphicsMode = val;
}

public static void toggleSS()
{
if(instance.stereoSound)
instance.stereoSound = false;
else
instance.stereoSound = true;
}

public static void toggleSS(boolean val)
{
instance.stereoSound = val;
}

public static void showSettings()
{
System.out.println("Volume: " + instance.volumeLevel);
System.out.println("Music: " + instance.musicLevel);
System.out.println("Enhanced Graphics: " + instance.enhancedGraphicsMode);
System.out.println("Stereo sound: " + instance.stereoSound);
System.out.println();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package clean.code.design_patterns.requirements;

//builder design pattern
public class Song {
private String name;
private String artistName;
private String duration;
private String genre;
private String userName;

public void listDetails()
{
System.out.println("Name: " + name);
System.out.println("Artist: " + artistName);
System.out.println("Duration: " + duration);
System.out.println("Genre: " + genre);
System.out.println("Added by: " + userName);
System.out.println("---");
}

//Song constructor
private Song(Builder builder)
{
this.userName = builder.userName;
this.artistName = builder.artistName;
this.duration = builder.duration;
this.genre = builder.genre;
this.name = builder.name;
}

//builder class
public static class Builder {
private String name;
private String artistName;
private String duration;
private String genre;
private String userName;
//the song is required to have a name
//so we use it in the builder constructor
public Builder(String nameVal)
{
this.name = nameVal;
}

public Builder setArtist(String artist)
{
this.artistName = artist;
return this;
}

public Builder setDuration(String dur)
{
this.duration = dur;
return this;
}

public Builder setGenre(String gen)
{
this.genre = gen;
return this;
}

public Builder setUser(String user)
{
this.userName = user;
return this;
}

//build the Song
public Song build()
{
return new Song(this);
}
}
}
Loading