Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package inheritance;
- public abstract class RegularCar {
- protected double regularPrice;
- private int speed;
- private String color;
- public RegularCar(int speed, double regularPrice, String color) {
- this.speed = speed;
- this.regularPrice = regularPrice;
- this.color = color;
- }
- public abstract double getSalePrice();
- public double getRegularPrice() {
- return regularPrice;
- }
- public abstract void getDescription();
- }
- -------------------------------------------------------------------------------------------
- package inheritance;
- public class Truck extends RegularCar implements Sellable {
- private double weight;
- public Truck(int speed, double regularPrice, String color) {
- super(speed, regularPrice, color);
- this.weight = weight;
- }
- @Override
- public double getSalePrice() {
- return this.weight > 2000 ? super.getRegularPrice() * 0.9 : super.getRegularPrice() * 0.8;
- }
- @Override
- public void getDescription() {
- System.out.println("This is a Track!");
- }
- }
- --------------------------------------------------------------------------------------------------
- package inheritance;
- public class Ford extends RegularCar implements Sellable, Discountable {
- private int year;
- private int manufacturerDiscount;
- public Ford(int speed, double regularPrice, String color) {
- super(speed, regularPrice, color);
- }
- @Override
- public double getSalePrice() {
- return super.getRegularPrice() - this.manufacturerDiscount;
- }
- @Override
- public void getDescription() {
- System.out.println("This is a Ford!");
- }
- }
- ---------------------------------------------------------------------------------------------------------------
- package inheritance;
- public class Sedan extends RegularCar implements Sellable {
- private int length;
- public Sedan(int speed, double regularPrice, String color, int length) {
- super(speed, regularPrice, color);
- this.length = length;
- }
- @Override
- public double getSalePrice() {
- return this.length > 20 ? super.getRegularPrice() * 0.95 : super.getRegularPrice() * 0.9;
- }
- @Override
- public void getDescription() {
- System.out.println("This is a Sedan!");
- }
- }
- --------------------------------------------------------------------------------------------------------------
- package inheritance;
- public interface Sellable {
- public double getSalePrice();
- }
- ----------------------------------------------------------------------------------------------------------------
- package inheritance;
- public interface Discountable extends Sellable {
- @Override
- double getSalePrice();
- }
- -------------------------------------------------------------------------------------------------------------
- package inheritance;
- public class Main {
- public static void main(String[] args) {
- Truck truck = new Truck(150, 10000000, "red");
- truck.getDescription();
- System.out.println(truck.getSalePrice());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement