Skip to Content
CoursesCSC122ระบบบริหารยานพาหนะ

ระบบบริหารยานพาหนะ

โจทย์

Write Java program reflecting relationship between owner and vehicle according to the following class diagram.

The Owner class represents a vehicle owner, storing their name and a list of owned vehicles. The addVehicle method adds a vehicle to the list if it is not already present. The move method moves a specified vehicle using the given speed and distance via the move method of the Vehicle class.

The Vehicle class represents any type of vehicle, storing its brand, type, model, color, and total accumulated distance (initialized to 0). The printInfo method displays vehicle details. The move method is abstract and must be implemented by concrete subclasses.

The Car class is a concrete subclass of Vehicle with additional attributes: id, consumption rate, and power consumption (initialized to 0). Upon creation, it sets the type as “Car.” The move method updates the total distance and calculates power consumption as:

Power Consumption = Distance / Consumption Rate

If speed is ≤ 0, no update occurs. The printInfo method extends Vehicle’s printInfo by also displaying total power consumption.

The Bicycle class is another concrete subclass of Vehicle, setting its type as “Bicycle” upon creation. Its move method updates the total distance unless the speed is ≤ 0, in which case no changes occur.

The provided outline includes a structural guide. Do not modify the VehicleTester class, as changes may cause errors.

InputOutput
Mikey JJ
Toyota 1001 Alphard White 15
Mercedes-benz 1002 S-Class Black 11
BMW 1003 M3 Blue 12
TWITTER STORM Red
CAMP WHIZZ Gray
----------Mikey----------
-----1-----
Brand: Toyota
Type: Car
Model: Alphard
Color: White
Accumulate distance: 600
Total power consumption: 39
-----2-----
Brand: BMW
Type: Car
Model: M3
Color: Blue
Accumulate distance: 650
Total power consumption: 54
-----3-----
Brand: TWITTER
Type: Bicycle
Model: STORM
Color: Red
Accumulate distance: 15
----------JJ----------
-----2-----
Brand: Mercedes-benz
Type: Car
Model: S-Class
Color: Black
Accumulate distance: 450
Total power consumption: 40
-----3-----
Brand: CAMP
Type: Bicycle
Model: WHIZZ
Color: Gray
Accumulate distance: 15

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

import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; public class VehicleTester { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Owner mikey = new Owner(sc.next()); Owner jj = new Owner(sc.next()); Car car1 = new Car(sc.next(), sc.nextInt(), sc.next(), sc.next(), sc.nextInt()); Car car2 = new Car(sc.next(), sc.nextInt(), sc.next(), sc.next(), sc.nextInt()); Car car3 = new Car(sc.next(), sc.nextInt(), sc.next(), sc.next(), sc.nextInt()); Bicycle bike1 = new Bicycle(sc.next(), sc.next(), sc.next()); Bicycle bike2 = new Bicycle(sc.next(), sc.next(), sc.next()); mikey.addVehicle(car1); mikey.addVehicle(car3); mikey.addVehicle(bike1); jj.addVehicle(car2); jj.addVehicle(bike2); mikey.move(car1, 60, 100); mikey.move(car1, 100, 500); mikey.move(car1, 0, 100); mikey.move(car2, 80, 200); mikey.move(car3, 120, 600); mikey.move(car3, 90, 50); mikey.move(bike1, 20, 10); mikey.move(bike2, 10, 15); mikey.move(bike1, 10, 5); jj.move(car2, 50, 100); jj.move(car2, 100, 350); jj.move(bike2, 20, 10); jj.move(bike2, 5, 5); Iterator<Vehicle> Mikey_it = mikey.getVehicles().iterator(); int mikey_count = 1; Iterator<Vehicle> JJ_it = jj.getVehicles().iterator(); int jj_count = 2; System.out.println("----------"+mikey.getName()+"----------"); while(Mikey_it.hasNext()){ System.out.println("-----"+mikey_count+"-----"); Vehicle v = Mikey_it.next(); v.printInfo(); mikey_count++; } System.out.println("----------"+jj.getName()+"----------"); while(JJ_it.hasNext()){ System.out.println("-----"+jj_count+"-----"); Vehicle v = JJ_it.next(); v.printInfo(); jj_count++; } } } class Owner{ } class Vehicle{ public void printInfo(){ System.out.println("Brand: "+this.getBrand()); System.out.println("Type: "+this.getType()); System.out.println("Model: "+this.getModel()); System.out.println("Color: "+this.getColor()); System.out.println("Accumulate distance: "+this.accumulative_distance); } } class Car{ } class Bicycle{ }

โค้ด

import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; public class VehicleTester { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Owner mikey = new Owner(sc.next()); Owner jj = new Owner(sc.next()); Car car1 = new Car(sc.next(), sc.nextInt(), sc.next(), sc.next(), sc.nextInt()); Car car2 = new Car(sc.next(), sc.nextInt(), sc.next(), sc.next(), sc.nextInt()); Car car3 = new Car(sc.next(), sc.nextInt(), sc.next(), sc.next(), sc.nextInt()); Bicycle bike1 = new Bicycle(sc.next(), sc.next(), sc.next()); Bicycle bike2 = new Bicycle(sc.next(), sc.next(), sc.next()); mikey.addVehicle(car1); mikey.addVehicle(car3); mikey.addVehicle(bike1); jj.addVehicle(car2); jj.addVehicle(bike2); mikey.move(car1, 60, 100); mikey.move(car1, 100, 500); mikey.move(car1, 0, 100); mikey.move(car2, 80, 200); mikey.move(car3, 120, 600); mikey.move(car3, 90, 50); mikey.move(bike1, 20, 10); mikey.move(bike2, 10, 15); mikey.move(bike1, 10, 5); jj.move(car2, 50, 100); jj.move(car2, 100, 350); jj.move(bike2, 20, 10); jj.move(bike2, 5, 5); Iterator<Vehicle> Mikey_it = mikey.getVehicles().iterator(); int mikey_count = 1; Iterator<Vehicle> JJ_it = jj.getVehicles().iterator(); int jj_count = 2; System.out.println("----------"+mikey.getName()+"----------"); while(Mikey_it.hasNext()){ System.out.println("-----"+mikey_count+"-----"); Vehicle v = Mikey_it.next(); v.printInfo(); mikey_count++; } System.out.println("----------"+jj.getName()+"----------"); while(JJ_it.hasNext()){ System.out.println("-----"+jj_count+"-----"); Vehicle v = JJ_it.next(); v.printInfo(); jj_count++; } sc.close(); } } class Owner { private String name; private ArrayList<Vehicle> vehicles; public Owner(String name) { this.name = name; this.vehicles = new ArrayList<>(); } public String getName() { return name; } public ArrayList<Vehicle> getVehicles() { return vehicles; } public void addVehicle(Vehicle vehicle) { if (!vehicles.contains(vehicle)) { vehicles.add(vehicle); } } public void move(Vehicle vehicle, int speed, int distance) { if (vehicles.contains(vehicle)) { vehicle.move(speed, distance); } } } abstract class Vehicle { protected String brand; protected String type; protected String model; protected String color; protected int accumulative_distance; public Vehicle(String brand, String type, String model, String color) { this.brand = brand; this.type = type; this.model = model; this.color = color; this.accumulative_distance = 0; } public String getBrand() { return brand; } public String getType() { return type; } public String getModel() { return model; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getAccumulative_distance() { return accumulative_distance; } public void setAccumulative_distance(int accumulative_distance) { this.accumulative_distance = accumulative_distance; } public void printInfo() { System.out.println("Brand: " + brand); System.out.println("Type: " + type); System.out.println("Model: " + model); System.out.println("Color: " + color); System.out.println("Accumulate distance: " + accumulative_distance); } public abstract void move(int speed, int distance); } class Car extends Vehicle { private int id; private int consumption_rate; private int power_consumption; public Car(String brand, int id, String model, String color, int consumption_rate) { super(brand, "Car", model, color); this.id = id; this.consumption_rate = consumption_rate; this.power_consumption = 0; } public int getId() { return id; } public int getConsumption_rate() { return consumption_rate; } public void setConsumption_rate(int consumption_rate) { this.consumption_rate = consumption_rate; } public int getPower_consumption() { return power_consumption; } @Override public void move(int speed, int distance) { if (speed > 0) { accumulative_distance += distance; power_consumption += distance / consumption_rate; } } @Override public void printInfo() { super.printInfo(); System.out.println("Total power consumption: " + power_consumption); } } class Bicycle extends Vehicle { public Bicycle(String brand, String model, String color) { super(brand, "Bicycle", model, color); } @Override public void move(int speed, int distance) { if (speed > 0) { accumulative_distance += distance; } } }

คำอธิบาย

ปรับปรุงล่าสุด