Skip to Content
CoursesCSC102ระบบจัดการยานพาหนะ

ระบบจัดการยานพาหนะ

โจทย์

You have been hired by a transportation company to develop a simple Vehicle Management System using Object-Oriented Programming (OOP) concepts in Java. The system must manage different types of vehicles, allowing the company to store, access, and modify vehicle information in a controlled manner. In addition to basic functionality, the system must also calculate various costs associated with each vehicle, including depreciation, insurance, and the total cost combining these factors.

Class Descriptions:

  • Create a superclass called Vehicle class that contains:
    • Attributes
      • A private String attribute named brand, representing the brand of the vehicle (e.g., Toyota, Ford, etc.).
      • A private String attribute named model, representing the model of the vehicle (e.g., Corolla, Mustang, etc.).
      • A private integer attribute named year, representing the manufacturing year of the vehicle.
      • A private double attribute named price, representing the price of the vehicle.
    • Encapsulation
      • All attributes should be private.
      • Provide public getters and setters for each attribute to allow controlled access and modification.
    • Methods
      • void displayDetails() that prints the details of the vehicle, including the brand, model, year, and price.
      • calculateDepreciation() that calculates and returns the depreciation of the vehicle based on its age. Assume a fixed depreciation rate of 15% per year.
  • Create a subclass of vehicle called Car class that contains:
    • Attribute
      • A private integer attribute named numberOfDoors, representing the number of doors in the car.
    • Encapsulation
      • The attribute should be private.
      • Provide public getter and setter for the numberOfDoors attribute.
    • Methods
      • Override the displayDetails() method to include the number of doors in addition to the details inherited from the Vehicle class.
      • calculateCostPerDoor() that calculates and returns the cost per door for the car.
      • calculateTotalCost() that calculates and returns the total cost associated with the truck by summing the depreciation cost and the cost per door.
  • Create a subclass of vehicle called Truck class that contains:
    • Attribute
      • A double attribute named loadCapacity, representing the load capacity of the truck in tons.
    • Encapsulation
      • The attribute should be private.
      • Provide public getter and setter for the loadCapacity attribute.
    • Methods
      • Override the displayDetails() method to include the number of doors in addition to the details inherited from the Vehicle class.
      • calculateCostPerTon() that calculates and returns the cost per ton of load capacity for the truck.
      • calculateTotalCost() that calculates and returns the total cost associated with the truck by summing the depreciation cost and the cost per ton of load capacity.
  • Create a subclass of vehicle called Motorcycle class that contains:
    • Attribute
      • A double attribute named hasSidecar, indicates whether the motorcycle has a sidecar or not.
    • Encapsulation
      • The attribute should be private.
      • Provide public getter and setter for the hasSidecar attribute.
    • Methods
      • Override the displayDetails() method to include the number of doors in addition to the details inherited from the Vehicle class.
      • estimateInsuranceCost() that estimate and returns the insurance cost based on whether the motorcycle has a sidecar. The base insurance rate is 500 baht, which applies to all motorcycles, and a sidecar surcharge adds an additional 200 baht if the motorcycle has a sidecar. This surcharge represents the increased risk and coverage requirements associated with insuring the sidecar.
      • calculateTotalCost() that calculates and returns the total cost associated with the motorcycle by summing the depreciation cost and the insurance cost.

โค้ดเริ่มต้น

public class VehicleDriver { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input for Car String carBrand = scanner.next(); String carModel = scanner.next(); int carYear = scanner.nextInt(); double carPrice = scanner.nextDouble(); int numberOfDoors = scanner.nextInt(); Car car = new Car(carBrand, carModel, carYear, carPrice, numberOfDoors); // Input for Truck String truckBrand = scanner.next(); String truckModel = scanner.next(); int truckYear = scanner.nextInt(); double truckPrice = scanner.nextDouble(); double loadCapacity = scanner.nextDouble(); Truck truck = new Truck(truckBrand, truckModel, truckYear, truckPrice, loadCapacity); // Input for Motorcycle String motorcycleBrand = scanner.next(); String motorcycleModel = scanner.next(); int motorcycleYear = scanner.nextInt(); double motorcyclePrice = scanner.nextDouble(); boolean hasSidecar = scanner.nextBoolean(); Motorcycle motorcycle = new Motorcycle(motorcycleBrand, motorcycleModel, motorcycleYear, motorcyclePrice, hasSidecar); // Display details and perform actions System.out.println("Car Details:"); car.displayDetails(); System.out.println("Depreciation: " + car.calculateDepreciation()); System.out.println("Cost per Door: " + car.calculateCostPerDoor()); System.out.println("Total Cost: " + car.calculateTotalCost()); System.out.println("\nTruck Details:"); truck.displayDetails(); System.out.println("Depreciation: " + truck.calculateDepreciation()); System.out.println("Cost per Ton: " + truck.calculateCostPerTon()); System.out.println("Total Cost: " + truck.calculateTotalCost()); System.out.println("\nMotorcycle Details:"); motorcycle.displayDetails(); System.out.println("Depreciation: " + motorcycle.calculateDepreciation()); System.out.println("Estimated Insurance Cost: " + motorcycle.estimateInsuranceCost()); System.out.println("Total Cost: " + motorcycle.calculateTotalCost()); } } class Vehicle{ // Write your code here } class Car{ // Write your code here } class Truck{ // Write your code here } class Motorcycle{ // Write your code here }

โค้ด

import java.util.Scanner; public class VehicleDriver { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input for Car String carBrand = scanner.next(); String carModel = scanner.next(); int carYear = scanner.nextInt(); double carPrice = scanner.nextDouble(); int numberOfDoors = scanner.nextInt(); Car car = new Car(carBrand, carModel, carYear, carPrice, numberOfDoors); // Input for Truck String truckBrand = scanner.next(); String truckModel = scanner.next(); int truckYear = scanner.nextInt(); double truckPrice = scanner.nextDouble(); double loadCapacity = scanner.nextDouble(); Truck truck = new Truck(truckBrand, truckModel, truckYear, truckPrice, loadCapacity); // Input for Motorcycle String motorcycleBrand = scanner.next(); String motorcycleModel = scanner.next(); int motorcycleYear = scanner.nextInt(); double motorcyclePrice = scanner.nextDouble(); boolean hasSidecar = scanner.nextBoolean(); Motorcycle motorcycle = new Motorcycle(motorcycleBrand, motorcycleModel, motorcycleYear, motorcyclePrice, hasSidecar); // Display details and perform actions System.out.println("Car Details:"); car.displayDetails(); System.out.println("Depreciation: " + car.calculateDepreciation()); System.out.println("Cost per Door: " + car.calculateCostPerDoor()); System.out.println("Total Cost: " + car.calculateTotalCost()); System.out.println("\nTruck Details:"); truck.displayDetails(); System.out.println("Depreciation: " + truck.calculateDepreciation()); System.out.println("Cost per Ton: " + truck.calculateCostPerTon()); System.out.println("Total Cost: " + truck.calculateTotalCost()); System.out.println("\nMotorcycle Details:"); motorcycle.displayDetails(); System.out.println("Depreciation: " + motorcycle.calculateDepreciation()); System.out.println("Estimated Insurance Cost: " + motorcycle.estimateInsuranceCost()); System.out.println("Total Cost: " + motorcycle.calculateTotalCost()); } } class Vehicle { private String brand; private String model; private int year; private double price; public Vehicle(String brand, String model, int year, double price) { this.brand = brand; this.model = model; this.year = year; this.price = price; } public String getBrand() { return this.brand; } public String getModel() { return this.model; } public int getYear() { return this.year; } public double getPrice() { return this.price; } public void setBrand(String brand) { this.brand = brand; } public void setModel(String model) { this.model = model; } public void setYear(int year) { this.year = year; } public void setPrice(double price) { this.price = price; } public void start() { } public void stop() { } public void displayDetails() { System.out.printf("Brand: %s, Model: %s, Year: %d, Price: %s, ", brand, model, year, price); } public double calculateDepreciation() { int ageOfVehicle = 2024 - year; return price * (1 - Math.pow(1 - 0.15, ageOfVehicle)); } } class Car extends Vehicle { private int numberOfDoors; public Car(String brand, String model, int year, double price, int numberOfDoors) { super(brand, model, year, price); this.numberOfDoors = numberOfDoors; } public int getNumberOfDoors() { return this.numberOfDoors; } public void setNumberOfDoors(int numberOfDoors) { this.numberOfDoors = numberOfDoors; } public void displayDetails() { super.displayDetails(); System.out.println("Number of Doors: " + numberOfDoors); } public double calculateCostPerDoor() { return getPrice() / getNumberOfDoors(); } public double calculateTotalCost() { return calculateDepreciation() + calculateCostPerDoor(); } } class Truck extends Vehicle { private double loadCapacity; public Truck(String brand, String model, int year, double price, double loadCapacity) { super(brand, model, year, price); this.loadCapacity = loadCapacity; } public double getLoadCapacity() { return this.loadCapacity; } public void setLoadCapacity(double loadCapacity) { this.loadCapacity = loadCapacity; } public void displayDetails() { super.displayDetails(); System.out.println("Load Capacity: " + loadCapacity + " tons"); } public double calculateCostPerTon() { return getPrice() / loadCapacity; } public double calculateTotalCost() { return calculateDepreciation() + calculateCostPerTon(); } } class Motorcycle extends Vehicle { private boolean hasSidebar; public Motorcycle(String brand, String model, int year, double price, boolean hasSidebar) { super(brand, model, year, price); this.hasSidebar = hasSidebar; } public boolean isHasSidecar() { return this.hasSidebar; } public void setHasSidecar(boolean hasSidecar) { this.hasSidebar = hasSidebar; } public void displayDetails() { super.displayDetails(); System.out.println("Has Sidecar: " + hasSidebar); } public double estimateInsuranceCost() { int rate = 500; return hasSidebar ? rate + 200 : rate; } public double calculateTotalCost() { return calculateDepreciation() + estimateInsuranceCost(); } }

คำอธิบาย

โครงสร้างของระบบ

ระบบประกอบด้วยคลาสหลัก 4 คลาส:

คลาส Vehicle (คลาสแม่)

คลาส Vehicle เป็นคลาสหลักที่เก็บแอตทริบิวต์พื้นฐานของยานพาหนะทุกประเภท

แอตทริบิวต์ (Attributes)

private String brand; // ยี่ห้อรถ private String model; // รุ่นรถ private int year; // ปีที่ผลิต private double price; // ราคา

เมธอดสำคัญ (Methods)

  1. Constructor
public Vehicle(String brand, String model, int year, double price) { this.brand = brand; this.model = model; this.year = year; this.price = price; }

คำอธิบาย: Constructor จะถูกเรียกเมื่อสร้างวัตถุใหม่ โดยรับค่าเริ่มต้นของยานพาหนะ

  1. คำนวณค่าเสื่อมราคา
public double calculateDepreciation() { int ageOfVehicle = 2024 - year; return price * (1 - Math.pow(1 - 0.15, ageOfVehicle)); }

คำอธิบาย: คำนวณค่าเสื่อมราคาโดยใช้อัตรา 15% ต่อปี

  • ageOfVehicle คือ อายุของรถ (ปีปัจจุบัน - ปีที่ผลิต)
  • ใช้สูตร: ราคา × (1 - (1 - 0.15)^อายุรถ)

คลาส Car

คลาส Car สืบทอดจากคลาส Vehicle และเพิ่มความสามารถเฉพาะของรถยนต์

แอตทริบิวต์พิเศษ

private int numberOfDoors; // จำนวนประตู

เมธอดพิเศษ

  1. คำนวณราคาต่อประตู
public double calculateCostPerDoor() { return getPrice() / getNumberOfDoors(); }

คำอธิบาย: คำนวณราคาต่อประตูโดยนำราคารถหารด้วยจำนวนประตู

  1. คำนวณราคารวม
public double calculateTotalCost() { return calculateDepreciation() + calculateCostPerDoor(); }

คำอธิบาย: คำนวณราคารวมโดยรวมค่าเสื่อมราคาและราคาต่อประตู

คลาส Truck

คลาส Truck สืบทอดจากคลาส Vehicle และเพิ่มความสามารถเฉพาะของรถบรรทุก

แอตทริบิวต์พิเศษ

private double loadCapacity; // ความสามารถในการบรรทุก (ตัน)

เมธอดพิเศษ

  1. คำนวณราคาต่อตัน
public double calculateCostPerTon() { return getPrice() / loadCapacity; }

คำอธิบาย: คำนวณราคาต่อตันโดยนำราคารถหารด้วยความสามารถในการบรรทุก

คลาส Motorcycle

คลาส Motorcycle สืบทอดจากคลาส Vehicle และเพิ่มความสามารถเฉพาะของรถจักรยานยนต์

แอตทริบิวต์พิเศษ

private boolean hasSidecar; // มีรถพ่วงข้างหรือไม่

เมธอดพิเศษ

  1. คำนวณค่าประกัน
public double estimateInsuranceCost() { int rate = 500; // ค่าประกันพื้นฐาน return hasSidecar ? rate + 200 : rate; // เพิ่ม 200 บาทถ้ามีรถพ่วง }

คำอธิบาย:

  • ค่าประกันพื้นฐาน = 500 บาท
  • ถ้ามีรถพ่วงข้าง เพิ่มอีก 200 บาท

ตัวอย่างการทำงาน

การใช้งานระบบผ่าน VehicleDriver:

  1. การรับข้อมูลรถยนต์
String carBrand = scanner.next(); // รับยี่ห้อรถ String carModel = scanner.next(); // รับรุ่นรถ int carYear = scanner.nextInt(); // รับปีผลิต double carPrice = scanner.nextDouble(); // รับราคา int numberOfDoors = scanner.nextInt(); // รับจำนวนประตู
  1. การแสดงผลข้อมูล
System.out.println("Car Details:"); car.displayDetails(); System.out.println("Depreciation: " + car.calculateDepreciation()); System.out.println("Cost per Door: " + car.calculateCostPerDoor()); System.out.println("Total Cost: " + car.calculateTotalCost());
ปรับปรุงล่าสุด