Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. class Bike {
  2. constructor(model, price) {
  3. if(typeof model !== 'string' || typeof price !== 'number'){
  4. throw new Error("Model must be a string, and number must be a number");
  5. } else {
  6. this.model = model;
  7. this.price = price;
  8.  
  9. if(Bike.totalPrice) {
  10. Bike.totalPrice += price;
  11. } else {
  12. Bike.totalPrice = price;
  13. }
  14.  
  15. if(Bike.bikeCount) {
  16. Bike.bikeCount++;
  17. } else {
  18. Bike.bikeCount = 1;
  19. }
  20. }
  21. }
  22.  
  23. toString(){
  24. return "Model: " + this.model + " Price: " + this.price + ".kr";
  25. };
  26.  
  27. static avgPricePerBike(){
  28. return "The average price per bike is " + (Bike.totalPrice/Bike.bikeCount) + ".kr";
  29. };
  30.  
  31. static totalNumberOfBikes(){
  32. return "There is " + Bike.bikeCount + " bikes in storage";
  33. };
  34.  
  35. };
  36.  
  37. class ElBike extends Bike {
  38. constructor(model, price, battery) {
  39. super(model, price);
  40. this.batteri = battery;
  41. }
  42. toString() {
  43. return super.toString() + "kr. Battery: " + this.batteri;
  44. }
  45. };
  46.  
  47. // Test
  48. try {
  49. let b = new Bike("M1", 23000);
  50. let eb = new ElBike("Electrical", 200, "Lithium");
  51. console.log(b.toString());
  52. console.log(eb.toString());
  53. console.log(Bike.totalNumberOfBikes());
  54. console.log(Bike.avgPricePerBike());
  55. /*
  56. let b2 = new Bike(2, 20);
  57. let eb2 = new ElBike(2, 20, "Lithium");
  58. */
  59. } catch (e) {
  60. console.log("Error: " + e.message);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement