Last active
October 15, 2019 01:48
-
-
Save oliveira-andre/0037635d8a4332f4830bc511ca30cc13 to your computer and use it in GitHub Desktop.
class methods and heritage on JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Spaceship { | |
| constructor(name, crewQuantity) { | |
| this.name = name, | |
| this.crewQuantity = crewQuantity, | |
| this.velocity = 0 | |
| } | |
| speedUp(acceleration) { | |
| return this.velocity += acceleration | |
| } | |
| } | |
| class BattleSpaceship extends Spaceship { | |
| constructor(name, crewQuantity, manyGuns) { | |
| super(name, crewQuantity), | |
| this.manyGuns = manyGuns | |
| } | |
| } | |
| class transportSpaceship extends Spaceship { | |
| constructor(name, crewQuantity, manyPlaces) { | |
| super(name, crewQuantity), | |
| this.manyPlaces = manyPlaces | |
| } | |
| } | |
| let spaceshipName = prompt('Digite o nome da nave') | |
| let spaceshipCrew = prompt('Digite o número de tripulantes') | |
| let spaceshipOption = prompt('Qual nave você escolhe?\n 1- Nave de batalha\n' + | |
| '2- Nave de transporte') | |
| let spaceship | |
| switch (spaceshipOption) { | |
| case '1': | |
| spaceshipGuns = prompt('Digite o número de armas') | |
| spaceship = new BattleSpaceship(spaceshipName, spaceshipCrew, spaceshipGuns) | |
| break | |
| case '2': | |
| spaceshipPlaces = prompt('Digite o número de lugares') | |
| spaceship = new transportSpaceship(spaceshipName, spaceshipCrew, spaceshipPlaces) | |
| break | |
| default: | |
| alert('opção inválida') | |
| } | |
| let choosedOption | |
| while (choosedOption != '3') { | |
| choosedOption = prompt('Escolha uma opção\n' + | |
| '1-Acelerar a nave\n' + | |
| '2-Trocar a nave\n' + | |
| '3-Imprimir e sair') | |
| switch (choosedOption) { | |
| case '1': | |
| acceleration = prompt('Quanto você quer acelerar') | |
| spaceship.speedUp(parseInt(acceleration)) | |
| break | |
| case '2': | |
| switchSpaceship() | |
| break | |
| } | |
| } | |
| if (choosedOption == '3') { | |
| printSpaceship() | |
| } | |
| function printSpaceship() { | |
| let additional | |
| if (spaceship) { | |
| if (spaceship.manyGuns) { | |
| additional = 'Quantidade de armas: ' + spaceship.manyGuns | |
| } else { | |
| additional = 'Quantidade de lugares: ' + spaceship.manyPlaces | |
| } | |
| alert('Nave: ' + spaceship.name + '\nquantidade de pessoas: ' + | |
| spaceship.crewQuantity + '\nvelocidade: ' + spaceship.velocity + '\n' | |
| + additional) | |
| } else { | |
| alert('não há naves') | |
| } | |
| } | |
| function switchSpaceship() { | |
| spaceshipOption = prompt('Qual nave você escolhe?\n 1- Nave de batalha\n' + | |
| '2- Nave de transporte') | |
| switch (spaceshipOption) { | |
| case '1': | |
| spaceshipGuns = prompt('Digite o número de armas') | |
| spaceship = new BattleSpaceship(spaceshipName, spaceshipCrew, spaceshipGuns) | |
| break | |
| case '2': | |
| spaceshipPlaces = prompt('Digite o número de lugares') | |
| spaceship = new transportSpaceship(spaceshipName, spaceshipCrew, spaceshipPlaces) | |
| break | |
| default: | |
| alert('opção inválida') | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment