ระบบจัดการทรัพยากรยานอวกาศ
โจทย์
In a future where humanity has expanded across the galaxy, managing resources on interstellar expeditions is crucial. As a lead software engineer for the Galactic Exploration Agency, you are tasked with developing a system to manage the various resources on space expeditions, such as fuel, food, and oxygen. To ensure that different types of spacecrafts can handle these resources efficiently, you need to design a standardized interface that all spacecraft can implement.
You are required to design a Java interface named ResourceManagement that will define the essential operations for managing resources on any spacecraft. Additionally, you need to create four classes, CargoShip, ResearchVessel, PassengerShuttle, and MiningDrone, which implement the ResourceManagement interface. Each class should provide specific implementations for the operations defined in the interface, including calculations related to resource consumption.
Class Descriptions:
-
Create an interface class called
ResourceManagementclass that defines the essential operations for managing resources on any spacecraft. It contains:- Variables
double FUEL_CONSUMPTION_RATE = 0.5, which represents the fuel consumption rate per unit distance.double FOOD_CONSUMPTION_RATE = 0.2, which represents the food consumption rate per unit distance.double OXYGEN_CONSUMPTION_RATE = 0.3, which represents the oxygen consumption rate per unit distance.double ENERGY_CONSUMPTION_RATE = 0.4, which represents the energy consumption rate per unit distance.
- Methods
- void
addResource(double amount)that adds a specified amount of resource to the spacecraft. - void
consumeResource(double amount)that consumes a specified amount of resource from the spacecraft. - double
calculateRemainingResource()that calculates the remaining amount of the resource. - double
estimateConsumptionForDistance(double distance)that estimates the resource consumption for a given distance.
- void
- Variables
-
Create an abstract class called
Spacecraftclass that provides a base implementation for common resource management operations and serves as a template for specific spacecraft types. It contains:- Attributes
- A protected double attribute named
resource, representing the current amount of the managed resource (fuel, food, oxygen, or energy).
- A protected double attribute named
- Methods
- An abstract method
calculateRemainingResource()that must be implemented by subclasses to return the current amount of the specific resource. - An abstract method
estimateConsumptionForDistance(double distance)that must be implemented by subclasses to calculate consumption based on the specific resource and distance. - Override the concrete method
void addResource(double amount)that adds the specified amount of the current resource. - Override the concrete method
void consumeResource(double amount)that deducts the specified amount of the current resource.
- An abstract method
- Attributes
-
Create a subclass of
SpacecraftcalledCargoShipclass that represents spacecraft that transport goods. Fuel management is critical as it undertakes long-distance missions.- Managed Resource: Fuel
- Initial Resource: 1000 units
- Estimation Formula:
distance * FUEL_CONSUMPTION_RATE
-
Create a subclass of
SpacecraftcalledResearchVesselclass that represents spacecraft that conduct scientific research. Food management is crucial for crew sustenance on research missions.- Managed Resource: Food
- Initial Resource: 500 units
- Estimation Formula:
distance * FOOD_CONSUMPTION_RATE
-
Create a subclass of
SpacecraftcalledPassengerShuttleclass that represents spacecraft that transport passengers. Oxygen management is vital for passenger safety during travel.- Managed Resource: Oxygen
- Initial Resource: 800 units
- Estimation Formula:
distance * OXYGEN_CONSUMPTION_RATE
-
Create a subclass of
SpacecraftcalledMiningDroneclass that represents unmanned drones that extract resources. Energy management is key to completing mining operations efficiently.- Managed Resource: Energy
- Initial Resource: 600 units
- Estimation Formula:
distance * ENERGY_CONSUMPTION_RATE
โค้ดเริ่มต้น
public class SpaceExpeditionDriver {
public static void main(String[] args) {
// Create instances of different spacecraft
CargoShip cargoShip = new CargoShip();
ResearchVessel researchVessel = new ResearchVessel();
PassengerShuttle passengerShuttle = new PassengerShuttle();
MiningDrone miningDrone = new MiningDrone();
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Ask the user for the distance each spacecraft will travel
double cargoShipDistance = scanner.nextDouble();
double researchVesselDistance = scanner.nextDouble();
double passengerShuttleDistance = scanner.nextDouble();
double miningDroneDistance = scanner.nextDouble();
// CargoShip fuel management
cargoShip.consumeResource(cargoShip.estimateConsumptionForDistance(cargoShipDistance));
System.out.println("CargoShip fuel remaining after traveling " + cargoShipDistance + " units: " + cargoShip.calculateRemainingResource() + " units");
// ResearchVessel food management
researchVessel.consumeResource(researchVessel.estimateConsumptionForDistance(researchVesselDistance));
System.out.println("ResearchVessel food supply remaining after traveling " + researchVesselDistance + " units: " + researchVessel.calculateRemainingResource() + " units");
// PassengerShuttle oxygen management
passengerShuttle.consumeResource(passengerShuttle.estimateConsumptionForDistance(passengerShuttleDistance));
System.out.println("PassengerShuttle oxygen supply remaining after traveling " + passengerShuttleDistance + " units: " + passengerShuttle.calculateRemainingResource() + " units");
// MiningDrone energy management
miningDrone.consumeResource(miningDrone.estimateConsumptionForDistance(miningDroneDistance));
System.out.println("MiningDrone energy remaining after traveling " + miningDroneDistance + " units: " + miningDrone.calculateRemainingResource() + " units");
}
}
class ResourceManagement{
// Write your code here
}
class Spacecraft{
// Write your code here
}
class CargoShip{
// Write your code here
}
class ResearchVessel{
// Write your code here
}
class PassengerShuttle{
// Write your code here
}
class MiningDrone{
// Write your code here
}โค้ด
import java.util.Scanner;
public class SpaceExpeditionDriver {
public static void main(String[] args) {
// Create instances of different spacecraft
CargoShip cargoShip = new CargoShip();
ResearchVessel researchVessel = new ResearchVessel();
PassengerShuttle passengerShuttle = new PassengerShuttle();
MiningDrone miningDrone = new MiningDrone();
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Ask the user for the distance each spacecraft will travel
double cargoShipDistance = scanner.nextDouble();
double researchVesselDistance = scanner.nextDouble();
double passengerShuttleDistance = scanner.nextDouble();
double miningDroneDistance = scanner.nextDouble();
// CargoShip fuel management
cargoShip.consumeResource(cargoShip.estimateConsumptionForDistance(cargoShipDistance));
System.out.println("CargoShip fuel remaining after traveling " + cargoShipDistance + " units: " + cargoShip.calculateRemainingResource() + " units");
// ResearchVessel food management
researchVessel.consumeResource(researchVessel.estimateConsumptionForDistance(researchVesselDistance));
System.out.println("ResearchVessel food supply remaining after traveling " + researchVesselDistance + " units: " + researchVessel.calculateRemainingResource() + " units");
// PassengerShuttle oxygen management
passengerShuttle.consumeResource(passengerShuttle.estimateConsumptionForDistance(passengerShuttleDistance));
System.out.println("PassengerShuttle oxygen supply remaining after traveling " + passengerShuttleDistance + " units: " + passengerShuttle.calculateRemainingResource() + " units");
// MiningDrone energy management
miningDrone.consumeResource(miningDrone.estimateConsumptionForDistance(miningDroneDistance));
System.out.println("MiningDrone energy remaining after traveling " + miningDroneDistance + " units: " + miningDrone.calculateRemainingResource() + " units");
}
}
interface ResourceManagement {
public static double FUEL_CONSUMPTION_RATE = 0.5;
public static double FOOD_CONSUMPTION_RATE = 0.2;
public static double OXYGEN_CONSUMPTION_RATE = 0.3;
public static double ENERGY_CONSUMPTION_RATE = 0.4;
public void addResource(double amount);
public void consumeResource(double amount);
public double calculateRemainingResource();
public double estimateConsumptionForDistance(double distance);
}
abstract class Spacecraft implements ResourceManagement {
protected double resource;
public void addResource(double amount) {
this.resource += amount;
}
public void consumeResource(double amount) {
this.resource = Math.max(this.resource - amount, 0);
}
abstract public double calculateRemainingResource();
abstract public double estimateConsumptionForDistance(double distance);
}
class CargoShip extends Spacecraft {
public CargoShip() {
this.resource = 1000;
}
public double calculateRemainingResource() {
return this.resource;
}
public double estimateConsumptionForDistance(double distance) {
return distance * ResourceManagement.FUEL_CONSUMPTION_RATE;
}
}
class ResearchVessel extends Spacecraft {
public ResearchVessel() {
this.resource = 500;
}
public double calculateRemainingResource() {
return this.resource;
}
public double estimateConsumptionForDistance(double distance) {
return distance * ResourceManagement.FOOD_CONSUMPTION_RATE;
}
}
class PassengerShuttle extends Spacecraft {
public PassengerShuttle() {
this.resource = 800;
}
public double calculateRemainingResource() {
return this.resource;
}
public double estimateConsumptionForDistance(double distance) {
return distance * ResourceManagement.OXYGEN_CONSUMPTION_RATE;
}
}
class MiningDrone extends Spacecraft {
public MiningDrone() {
this.resource = 600;
}
public double calculateRemainingResource() {
return this.resource;
}
public double estimateConsumptionForDistance(double distance) {
return distance * ResourceManagement.ENERGY_CONSUMPTION_RATE;
}
}คำอธิบาย
โครงสร้างของระบบ
Interface ResourceManagement
Interface นี้กำหนดมาตรฐานการจัดการทรัพยากรสำหรับยานอวกาศทุกประเภท
ค่าคงที่ (Constants)
public static double FUEL_CONSUMPTION_RATE = 0.5; // อัตราการใช้เชื้อเพลิง
public static double FOOD_CONSUMPTION_RATE = 0.2; // อัตราการใช้อาหาร
public static double OXYGEN_CONSUMPTION_RATE = 0.3; // อัตราการใช้ออกซิเจน
public static double ENERGY_CONSUMPTION_RATE = 0.4; // อัตราการใช้พลังงานเมธอดที่ต้องมี (Methods)
// เพิ่มทรัพยากร
public void addResource(double amount);
// ใช้ทรัพยากร
public void consumeResource(double amount);
// คำนวณทรัพยากรที่เหลือ
public double calculateRemainingResource();
// ประมาณการใช้ทรัพยากรตามระยะทาง
public double estimateConsumptionForDistance(double distance);คลาส Spacecraft (Abstract Class)
คลาสนี้เป็น abstract class ที่ให้การทำงานพื้นฐานสำหรับยานอวกาศทุกประเภท
แอตทริบิวต์
protected double resource; // ปริมาณทรัพยากรปัจจุบันวิธีการทำงาน
// เพิ่มทรัพยากร
public void addResource(double amount) {
this.resource += amount;
}
// ใช้ทรัพยากร
public void consumeResource(double amount) {
this.resource = Math.max(this.resource - amount, 0);
}หมายเหตุ: Math.max ใช้เพื่อป้องกันไม่ให้ทรัพยากรติดลบ
ยานอวกาศประเภทต่าง ๆ
1. CargoShip (ยานขนส่งสินค้า)
public class CargoShip extends Spacecraft {
public CargoShip() {
this.resource = 1000; // เริ่มต้นด้วยเชื้อเพลิง 1000 หน่วย
}
public double estimateConsumptionForDistance(double distance) {
return distance * FUEL_CONSUMPTION_RATE; // ใช้เชื้อเพลิง 0.5 หน่วยต่อระยะทาง 1 หน่วย
}
}2. ResearchVessel (ยานวิจัย)
public class ResearchVessel extends Spacecraft {
public ResearchVessel() {
this.resource = 500; // เริ่มต้นด้วยอาหาร 500 หน่วย
}
public double estimateConsumptionForDistance(double distance) {
return distance * FOOD_CONSUMPTION_RATE; // ใช้อาหาร 0.2 หน่วยต่อระยะทาง 1 หน่วย
}
}3. PassengerShuttle (ยานโดยสาร)
public class PassengerShuttle extends Spacecraft {
public PassengerShuttle() {
this.resource = 800; // เริ่มต้นด้วยออกซิเจน 800 หน่วย
}
public double estimateConsumptionForDistance(double distance) {
return distance * OXYGEN_CONSUMPTION_RATE; // ใช้ออกซิเจน 0.3 หน่วยต่อระยะทาง 1 หน่วย
}
}4. MiningDrone (โดรนขุดเหมือง)
public class MiningDrone extends Spacecraft {
public MiningDrone() {
this.resource = 600; // เริ่มต้นด้วยพลังงาน 600 หน่วย
}
public double estimateConsumptionForDistance(double distance) {
return distance * ENERGY_CONSUMPTION_RATE; // ใช้พลังงาน 0.4 หน่วยต่อระยะทาง 1 หน่วย
}
}การใช้งานระบบ (SpaceExpeditionDriver)
1. การสร้างยานอวกาศ
CargoShip cargoShip = new CargoShip(); // ยานขนส่ง
ResearchVessel researchVessel = new ResearchVessel(); // ยานวิจัย
PassengerShuttle passengerShuttle = new PassengerShuttle(); // ยานโดยสาร
MiningDrone miningDrone = new MiningDrone(); // โดรนขุดเหมือง2. การรับข้อมูลระยะทาง
Scanner scanner = new Scanner(System.in);
double cargoShipDistance = scanner.nextDouble(); // ระยะทางของยานขนส่ง
double researchVesselDistance = scanner.nextDouble(); // ระยะทางของยานวิจัย
// ... รับระยะทางของยานอื่นๆ3. การคำนวณและแสดงผล
// สำหรับยานขนส่ง
cargoShip.consumeResource(
cargoShip.estimateConsumptionForDistance(cargoShipDistance)
);
System.out.println("เชื้อเพลิงที่เหลือ: " +
cargoShip.calculateRemainingResource() + " หน่วย");