java车辆管理系统代码
Here's a simple example of a Java vehicle management system. This system includes classes for vehicles, a vehicle management system, and a simple user interface for demonstration purposes.
javaimport java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// Vehicle class representing a basic vehicle
class Vehicle {
private String make;
private String model;
private int year;
public Vehicle(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
@Override
public String toString() {
return year + " " + make + " " + model;
}
}
// VehicleManagementSystem class managing vehicles
class VehicleManagementSystem {
private List<Vehicle> vehicles;
public VehicleManagementSystem() {
vehicles = new ArrayList<>();
}
public void addVehicle(Vehicle vehicle) {
vehicles.add(vehicle);
}
public List<Vehicle> getVehicles() {
return vehicles;
}
}
// Simple user interface
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
VehicleManagementSystem managementSystem = new VehicleManagementSystem();
while (true) {
System.out.println("1. Add Vehicle");
System.out.println("2. View Vehicles");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter make: ");
String make = scanner.nextLine();
System.out.print("Enter model: ");
String model = scanner.nextLine();
System.out.print("Enter year: ");
int year = scanner.nextInt();
scanner.nextLine(); // Consume newline
Vehicle vehicle = new Vehicle(make, model, year);
managementSystem.addVehicle(vehicle);
System.out.println("Vehicle added successfully!");
break;
case 2:
List<Vehicle> vehicles = managementSystem.getVehicles();
System.out.println("List of Vehicles:");
for (Vehicle v : vehicles) {
System.out.println(v);
}
break;
case 3:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
This code includes three main classes:
Vehicle
: Represents a basic vehicle with attributes like make, model, and year.VehicleManagementSystem
: Manages a collection of vehicles and provides methods to add and retrieve vehicles.Main
: Provides a simple user interface for interacting with the vehicle management system. It allows users to add vehicles and view the list of existing vehicles.
Sure, let's continue building upon the Java vehicle management system by adding features for editing and deleting vehicles, implementing input validation, and storing data persistently using a simple text file.
First, let's add the functionality to edit and delete vehicles:
java// Inside VehicleManagementSystem class
public void editVehicle(int index, Vehicle newVehicle) {
if (index >= 0 && index < vehicles.size()) {
vehicles.set(index, newVehicle);
System.out.println("Vehicle edited successfully!");
} else {
System.out.println("Invalid index!");
}
}
public void deleteVehicle(int index) {
if (index >= 0 && index < vehicles.size()) {
vehicles.remove(index);
System.out.println("Vehicle deleted successfully!");
} else {
System.out.println("Invalid index!");
}
}
Next, let's implement input validation in the user interface to handle invalid inputs:
java// Inside Main class
while (true) {
System.out.println("1. Add Vehicle");
System.out.println("2. View Vehicles");
System.out.println("3. Edit Vehicle");
System.out.println("4. Delete Vehicle");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
if (!scanner.hasNextInt()) {
System.out.println("Invalid choice. Please enter a number.");
scanner.next(); // Clear invalid input
continue;
}
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
Now, let's add functionality to store and load vehicles from a text file:
javaimport java.io.*;
import java.util.Scanner;
class FileHandler {
private static final String FILE_NAME = "vehicles.txt";
static void saveToFile(List<Vehicle> vehicles) {
try (PrintWriter writer = new PrintWriter(new FileWriter(FILE_NAME))) {
for (Vehicle vehicle : vehicles) {
writer.println(vehicle.getMake() + "," + vehicle.getModel() + "," + vehicle.getYear());
}
System.out.println("Vehicles saved to file successfully!");
} catch (IOException e) {
System.out.println("Error saving vehicles to file: " + e.getMessage());
}
}
static List<Vehicle> loadFromFile() {
List<Vehicle> vehicles = new ArrayList<>();
try (Scanner scanner = new Scanner(new FileReader(FILE_NAME))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
if (parts.length == 3) {
String make = parts[0];
String model = parts[1];
int year = Integer.parseInt(parts[2]);
vehicles.add(new Vehicle(make, model, year));
}
}
System.out.println("Vehicles loaded from file successfully!");
} catch (FileNotFoundException e) {
System.out.println("No saved vehicles found.");
} catch (IOException e) {
System.out.println("Error loading vehicles from file: " + e.getMessage());
}
return vehicles;
}
}
Finally, update the Main
class to load vehicles from file when the program starts and save vehicles to file before exiting:
java// Inside Main class
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
VehicleManagementSystem managementSystem = new VehicleManagementSystem();
// Load vehicles from file
managementSystem.setVehicles(FileHandler.loadFromFile());
// Main menu loop
while (true) {
// Menu options
// ...
case 5:
// Save vehicles to file before exiting
FileHandler.saveToFile(managementSystem.getVehicles());
System.out.println("Exiting...");
System.exit(0);
// ...
}
}
With these enhancements, the vehicle management system now supports editing, deleting, input validation, and data persistence. You can further improve this system by adding more features like searching, sorting, or using a database for data storage.