Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bike {
- constructor(model, price) {
- if(typeof model !== 'string' || typeof price !== 'number'){
- throw new Error("Model must be a string, and number must be a number");
- } else {
- this.model = model;
- this.price = price;
- if(Bike.totalPrice) {
- Bike.totalPrice += price;
- } else {
- Bike.totalPrice = price;
- }
- if(Bike.bikeCount) {
- Bike.bikeCount++;
- } else {
- Bike.bikeCount = 1;
- }
- }
- }
- toString(){
- return "Model: " + this.model + " Price: " + this.price + ".kr";
- };
- static avgPricePerBike(){
- return "The average price per bike is " + (Bike.totalPrice/Bike.bikeCount) + ".kr";
- };
- static totalNumberOfBikes(){
- return "There is " + Bike.bikeCount + " bikes in storage";
- };
- };
- class ElBike extends Bike {
- constructor(model, price, battery) {
- super(model, price);
- this.batteri = battery;
- }
- toString() {
- return super.toString() + "kr. Battery: " + this.batteri;
- }
- };
- // Test
- try {
- let b = new Bike("M1", 23000);
- let eb = new ElBike("Electrical", 200, "Lithium");
- console.log(b.toString());
- console.log(eb.toString());
- console.log(Bike.totalNumberOfBikes());
- console.log(Bike.avgPricePerBike());
- /*
- let b2 = new Bike(2, 20);
- let eb2 = new ElBike(2, 20, "Lithium");
- */
- } catch (e) {
- console.log("Error: " + e.message);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement