Advertisement
Flaron

Progmasters_03-oop_Inheritance

Nov 14th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. package inheritance;
  2.  
  3. public abstract class RegularCar {
  4.  
  5. protected double regularPrice;
  6. private int speed;
  7. private String color;
  8.  
  9. public RegularCar(int speed, double regularPrice, String color) {
  10. this.speed = speed;
  11. this.regularPrice = regularPrice;
  12. this.color = color;
  13. }
  14.  
  15. public abstract double getSalePrice();
  16.  
  17. public double getRegularPrice() {
  18. return regularPrice;
  19. }
  20.  
  21. public abstract void getDescription();
  22. }
  23.  
  24. -------------------------------------------------------------------------------------------
  25.  
  26. package inheritance;
  27.  
  28. public class Truck extends RegularCar implements Sellable {
  29.  
  30. private double weight;
  31.  
  32. public Truck(int speed, double regularPrice, String color) {
  33. super(speed, regularPrice, color);
  34. this.weight = weight;
  35. }
  36.  
  37. @Override
  38. public double getSalePrice() {
  39.  
  40. return this.weight > 2000 ? super.getRegularPrice() * 0.9 : super.getRegularPrice() * 0.8;
  41. }
  42.  
  43. @Override
  44. public void getDescription() {
  45. System.out.println("This is a Track!");
  46. }
  47. }
  48.  
  49. --------------------------------------------------------------------------------------------------
  50.  
  51. package inheritance;
  52.  
  53. public class Ford extends RegularCar implements Sellable, Discountable {
  54.  
  55. private int year;
  56. private int manufacturerDiscount;
  57.  
  58.  
  59. public Ford(int speed, double regularPrice, String color) {
  60. super(speed, regularPrice, color);
  61. }
  62.  
  63. @Override
  64. public double getSalePrice() {
  65. return super.getRegularPrice() - this.manufacturerDiscount;
  66. }
  67.  
  68. @Override
  69. public void getDescription() {
  70. System.out.println("This is a Ford!");
  71. }
  72. }
  73.  
  74. ---------------------------------------------------------------------------------------------------------------
  75.  
  76. package inheritance;
  77.  
  78. public class Sedan extends RegularCar implements Sellable {
  79.  
  80. private int length;
  81.  
  82. public Sedan(int speed, double regularPrice, String color, int length) {
  83. super(speed, regularPrice, color);
  84. this.length = length;
  85. }
  86.  
  87. @Override
  88. public double getSalePrice() {
  89. return this.length > 20 ? super.getRegularPrice() * 0.95 : super.getRegularPrice() * 0.9;
  90. }
  91.  
  92. @Override
  93. public void getDescription() {
  94. System.out.println("This is a Sedan!");
  95. }
  96. }
  97.  
  98. --------------------------------------------------------------------------------------------------------------
  99.  
  100. package inheritance;
  101.  
  102. public interface Sellable {
  103.  
  104. public double getSalePrice();
  105. }
  106.  
  107. ----------------------------------------------------------------------------------------------------------------
  108.  
  109. package inheritance;
  110.  
  111. public interface Discountable extends Sellable {
  112. @Override
  113. double getSalePrice();
  114. }
  115.  
  116. -------------------------------------------------------------------------------------------------------------
  117.  
  118. package inheritance;
  119.  
  120. public class Main {
  121. public static void main(String[] args) {
  122. Truck truck = new Truck(150, 10000000, "red");
  123. truck.getDescription();
  124. System.out.println(truck.getSalePrice());
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement