Skip to content

Instantly share code, notes, and snippets.

@rohanjai777
Created November 8, 2022 17:59
Show Gist options
  • Save rohanjai777/a83c6e6b81482af721317e4f526996fe to your computer and use it in GitHub Desktop.
Save rohanjai777/a83c6e6b81482af721317e4f526996fe to your computer and use it in GitHub Desktop.
import java.util.*;
enum BulletType { //type of bullets available
FIVE_MM,
SEVEN_MM,
NINE_MM,
}
//-------------
class Bullet{ //intrinsic properties
int weight;
int radius;
BulletType bulletType;
Bullet(BulletType bulletType, int radius, int weight){
this.bulletType = bulletType;
this.radius = radius;
this.weight = weight;
}
}
//-------------
class FlyingBullet{ //exterinsic properties
int speed;
String direction;
Bullet bullet;
FlyingBullet(Bullet bullet, String direction, int speed){
this.bullet = bullet;
this.direction = direction;
this.speed = speed;
}
}
//--------------
class BulletRegistry { //register few types of bullets
private Map<BulletType, Bullet> bullets = new HashMap<>();
public void registerBullet(BulletType type, Bullet bullet) { //register bullet
bullets.put(type, bullet);
}
public Bullet getBullet(BulletType bulletType) { //return bullet based on type
return bullets.get(bulletType);
}
}
//---------------
public class Game{
public static void main(String[] args){
BulletRegistry bulletRegistory = new BulletRegistry();
//create bullets
Bullet fivemm = new Bullet(BulletType.FIVE_MM, 5,50);
Bullet sevenmm = new Bullet(BulletType.SEVEN_MM, 7,70);
Bullet ninemm = new Bullet(BulletType.NINE_MM, 9,90);
//add them in registry
bulletRegistory.registerBullet(BulletType.FIVE_MM, fivemm);
bulletRegistory.registerBullet(BulletType.FIVE_MM, fivemm);
bulletRegistory.registerBullet(BulletType.FIVE_MM, fivemm);
//create list of Flying Bullets
List<FlyingBullet> flyingBullets = new ArrayList<>();
//make 2000 five mm bullets
for(int i=0;i<2000;i++){
FlyingBullet fb = new FlyingBullet(bulletRegistory.getBullet(BulletType.FIVE_MM),"North",100);
flyingBullets.add(fb);
}
//make 100 nine mm bullets
for(int i=0;i<100;i++){
FlyingBullet fb = new FlyingBullet(bulletRegistory.getBullet(BulletType.NINE_MM),"South",100);
flyingBullets.add(fb);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment