/*
* any user can try to buy a gun
* the gun can be sell according to law rules
* * for Poland, min age 18
* * for USA, min age 21
*/
class Countries {
constructor() {
this.counties = []
}
add(country) {
this.counties.push(country)
}
}
class Country {
constructor(name, minimumAge) {
this.name = name
this.minimumAge = minimumAge
}
checkRequirements(self) {
if (userAge() > this.minimumAge) {
return true
} else {
throw new Error(`Required age is ${this.minimumAge}`)
}
function userAge() {
return new Date(Date.now()).getFullYear() - self.age.getFullYear()
}
}
}
let poland = new Country('Poland', 18)
let usa = new Country('USA', 21)
let countries = new Countries()
countries.add(poland)
countries.add(usa)
class User {
constructor(country, age) {
this.country = country
this.age = age
}
buyAGun() {
return this.country.checkRequirements(this)
}
}
let userOne = new User(poland, new Date(2000, 2, 3))
let userTwo = new User(poland, new Date(1998, 2, 3))
let userThree = new User(usa, new Date(1997, 2, 3))
let userFourth = new User(usa, new Date(1995, 2, 3))
~[userOne, userTwo, userThree, userFourth].forEach(user => {
try {
console.log('user bought a gun', user.buyAGun())
} catch (e) {
console.log('we have got issue', e.message)
}
})
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.