Files
OOPS-lab-codes/MyOwnAutoShop.java
T
2025-12-17 16:39:23 +05:30

135 lines
3.5 KiB
Java

import java.util.Scanner;
// ---------- SUPER CLASS ----------
class Car {
int speed;
double regularPrice;
String color;
Car(int speed, double regularPrice, String color) {
this.speed = speed;
this.regularPrice = regularPrice;
this.color = color;
}
double getSalePrice() {
return regularPrice;
}
}
// ---------- SUB CLASS : TRUCK ----------
class Truck extends Car {
int weight;
Truck(int speed, double regularPrice, String color, int weight) {
super(speed, regularPrice, color);
this.weight = weight;
}
double getSalePrice() {
if (weight > 2000) return regularPrice * 0.90;
// 10% discount
else return regularPrice * 0.80; // 20% discount
}
}
// ---------- SUB CLASS : FORD ----------
class Ford extends Car {
int year;
int manufacturerDiscount;
Ford(
int speed,
double regularPrice,
String color,
int year,
int manufacturerDiscount
) {
super(speed, regularPrice, color);
this.year = year;
this.manufacturerDiscount = manufacturerDiscount;
}
double getSalePrice() {
return super.getSalePrice() - manufacturerDiscount;
}
}
// ---------- SUB CLASS : SEDAN ----------
class Sedan extends Car {
int length;
Sedan(int speed, double regularPrice, String color, int length) {
super(speed, regularPrice, color);
this.length = length;
}
double getSalePrice() {
if (length > 20) return regularPrice * 0.95;
// 5% discount
else return regularPrice * 0.90; // 10% discount
}
}
// ---------- MAIN CLASS ----------
public class MyOwnAutoShop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// ---- Sedan Object ----
System.out.println("Enter Sedan details:");
System.out.print("Speed: ");
int sSpeed = sc.nextInt();
System.out.print("Regular Price: ");
double sPrice = sc.nextDouble();
sc.nextLine();
System.out.print("Color: ");
String sColor = sc.nextLine();
System.out.print("Length: ");
int sLength = sc.nextInt();
Sedan sedan = new Sedan(sSpeed, sPrice, sColor, sLength);
// ---- Ford Object ----
System.out.println("\nEnter Ford details:");
System.out.print("Speed: ");
int fSpeed = sc.nextInt();
System.out.print("Regular Price: ");
double fPrice = sc.nextDouble();
sc.nextLine();
System.out.print("Color: ");
String fColor = sc.nextLine();
System.out.print("Year: ");
int fYear = sc.nextInt();
System.out.print("Manufacturer Discount: ");
int fDiscount = sc.nextInt();
Ford ford = new Ford(fSpeed, fPrice, fColor, fYear, fDiscount);
// ---- Car Object ----
System.out.println("\nEnter Car details:");
System.out.print("Speed: ");
int cSpeed = sc.nextInt();
System.out.print("Regular Price: ");
double cPrice = sc.nextDouble();
sc.nextLine();
System.out.print("Color: ");
String cColor = sc.nextLine();
Car car = new Car(cSpeed, cPrice, cColor);
// ---- Display Sale Prices ----
System.out.println("\n--- Sale Prices ---");
System.out.println("Sedan Sale Price: " + sedan.getSalePrice());
System.out.println("Ford Sale Price: " + ford.getSalePrice());
System.out.println("Car Sale Price: " + car.getSalePrice());
sc.close();
}
}