创建java代码:大众4S店管理信息系统 汽车信息包括:汽车类别、汽车ID、汽车名字、汽车上市日期、购买价格、出售价格、颜色、厂商、发动机、车身结构(4门5座三厢车)。试用面向对象程序设计思想,设计一水禽管理管理信息系统,使之能提供以下功能: 1、汽车信息录入功能 2、汽车信息文件读取功能 3、汽车信息文件保存功能 4、汽车信息浏览功能 5、查询功能: 1)按汽车名字查询 2)按汽车类别查询 6、修改功能: 根据汽车ID修改相应信息; 7、删除汽车功能: 删除指定ID的汽车。 8、排序功能(由用户指定按照名称或ID;指定升序或降序) 9、退出
时间: 2023-07-10 14:35:15 浏览: 139
Java项目之4S店车辆管理系统(源码 + 说明文档)
好的,以下是一个简单的大众4S店管理信息系统的Java代码示例:
Car.java
```java
public class Car {
private String category;
private String id;
private String name;
private String launchDate;
private double purchasePrice;
private double sellingPrice;
private String color;
private String manufacturer;
private String engine;
private String bodyStructure;
public Car(String category, String id, String name, String launchDate, double purchasePrice, double sellingPrice, String color, String manufacturer, String engine, String bodyStructure) {
this.category = category;
this.id = id;
this.name = name;
this.launchDate = launchDate;
this.purchasePrice = purchasePrice;
this.sellingPrice = sellingPrice;
this.color = color;
this.manufacturer = manufacturer;
this.engine = engine;
this.bodyStructure = bodyStructure;
}
// Getters and setters
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLaunchDate() {
return launchDate;
}
public void setLaunchDate(String launchDate) {
this.launchDate = launchDate;
}
public double getPurchasePrice() {
return purchasePrice;
}
public void setPurchasePrice(double purchasePrice) {
this.purchasePrice = purchasePrice;
}
public double getSellingPrice() {
return sellingPrice;
}
public void setSellingPrice(double sellingPrice) {
this.sellingPrice = sellingPrice;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
public String getBodyStructure() {
return bodyStructure;
}
public void setBodyStructure(String bodyStructure) {
this.bodyStructure = bodyStructure;
}
@Override
public String toString() {
return "Car{" +
"category='" + category + '\'' +
", id='" + id + '\'' +
", name='" + name + '\'' +
", launchDate='" + launchDate + '\'' +
", purchasePrice=" + purchasePrice +
", sellingPrice=" + sellingPrice +
", color='" + color + '\'' +
", manufacturer='" + manufacturer + '\'' +
", engine='" + engine + '\'' +
", bodyStructure='" + bodyStructure + '\'' +
'}';
}
}
```
CarManagementSystem.java
```java
import java.io.*;
import java.util.*;
public class CarManagementSystem {
private List<Car> carList;
public CarManagementSystem() {
carList = new ArrayList<>();
}
// Add a new car
public void addCar(Car car) {
carList.add(car);
}
// Read car information from file
public void readFromFile(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
String[] info = line.split(",");
Car car = new Car(info[0], info[1], info[2], info[3], Double.parseDouble(info[4]), Double.parseDouble(info[5]), info[6], info[7], info[8], info[9]);
carList.add(car);
}
reader.close();
}
// Save car information to file
public void saveToFile(String fileName) throws IOException {
File file = new File(fileName);
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (Car car : carList) {
writer.write(car.getCategory() + "," + car.getId() + "," + car.getName() + "," + car.getLaunchDate() + "," + car.getPurchasePrice() + "," + car.getSellingPrice() + "," + car.getColor() + "," + car.getManufacturer() + "," + car.getEngine() + "," + car.getBodyStructure());
writer.newLine();
}
writer.close();
}
// Display all car information
public void displayAllCars() {
for (Car car : carList) {
System.out.println(car);
}
}
// Search car information by name
public void searchCarByName(String name) {
for (Car car : carList) {
if (car.getName().equals(name)) {
System.out.println(car);
}
}
}
// Search car information by category
public void searchCarByCategory(String category) {
for (Car car : carList) {
if (car.getCategory().equals(category)) {
System.out.println(car);
}
}
}
// Modify car information by id
public void modifyCarById(String id, Car newCar) {
for (int i = 0; i < carList.size(); i++) {
if (carList.get(i).getId().equals(id)) {
carList.set(i, newCar);
return;
}
}
System.out.println("Car with ID " + id + " not found.");
}
// Delete car by id
public void deleteCarById(String id) {
for (int i = 0; i < carList.size(); i++) {
if (carList.get(i).getId().equals(id)) {
carList.remove(i);
return;
}
}
System.out.println("Car with ID " + id + " not found.");
}
// Sort cars by name or id
public void sortCars(boolean byName, boolean ascending) {
Comparator<Car> comparator;
if (byName) {
comparator = Comparator.comparing(Car::getName);
} else {
comparator = Comparator.comparing(Car::getId);
}
if (!ascending) {
comparator = comparator.reversed();
}
Collections.sort(carList, comparator);
}
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
CarManagementSystem system = new CarManagementSystem();
boolean exit = false;
while (!exit) {
System.out.println("1. Add car");
System.out.println("2. Read car information from file");
System.out.println("3. Save car information to file");
System.out.println("4. Display all cars");
System.out.println("5. Search car by name");
System.out.println("6. Search car by category");
System.out.println("7. Modify car by ID");
System.out.println("8. Delete car by ID");
System.out.println("9. Sort cars");
System.out.println("10. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter category: ");
String category = scanner.nextLine();
System.out.print("Enter ID: ");
String id = scanner.nextLine();
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter launch date: ");
String launchDate = scanner.nextLine();
System.out.print("Enter purchase price: ");
double purchasePrice = scanner.nextDouble();
System.out.print("Enter selling price: ");
double sellingPrice = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter color: ");
String color = scanner.nextLine();
System.out.print("Enter manufacturer: ");
String manufacturer = scanner.nextLine();
System.out.print("Enter engine: ");
String engine = scanner.nextLine();
System.out.print("Enter body structure: ");
String bodyStructure = scanner.nextLine();
Car newCar = new Car(category, id, name, launchDate, purchasePrice, sellingPrice, color, manufacturer, engine, bodyStructure);
system.addCar(newCar);
System.out.println("Car added successfully.");
break;
case 2:
System.out.print("Enter file name: ");
String readFileName = scanner.nextLine();
system.readFromFile(readFileName);
System.out.println("Car information read successfully.");
break;
case 3:
System.out.print("Enter file name: ");
String saveFileName = scanner.nextLine();
system.saveToFile(saveFileName);
System.out.println("Car information saved successfully.");
break;
case 4:
system.displayAllCars();
break;
case 5:
System.out.print("Enter car name: ");
String searchName = scanner.nextLine();
system.searchCarByName(searchName);
break;
case 6:
System.out.print("Enter car category: ");
String searchCategory = scanner.nextLine();
system.searchCarByCategory(searchCategory);
break;
case 7:
System.out.print("Enter car ID to modify: ");
String modifyId = scanner.nextLine();
System.out.print("Enter new category: ");
String modifyCategory = scanner.nextLine();
System.out.print("Enter new name: ");
String modifyName = scanner.nextLine();
System.out.print("Enter new launch date: ");
String modifyLaunchDate = scanner.nextLine();
System.out.print("Enter new purchase price: ");
double modifyPurchasePrice = scanner.nextDouble();
System.out.print("Enter new selling price: ");
double modifySellingPrice = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter new color: ");
String modifyColor = scanner.nextLine();
System.out.print("Enter new manufacturer: ");
String modifyManufacturer = scanner.nextLine();
System.out.print("Enter new engine: ");
String modifyEngine = scanner.nextLine();
System.out.print("Enter new body structure: ");
String modifyBodyStructure = scanner.nextLine();
Car modifiedCar = new Car(modifyCategory, modifyId, modifyName, modifyLaunchDate, modifyPurchasePrice, modifySellingPrice, modifyColor, modifyManufacturer, modifyEngine, modifyBodyStructure);
system.modifyCarById(modifyId, modifiedCar);
break;
case 8:
System.out.print("Enter car ID to delete: ");
String deleteId = scanner.nextLine();
system.deleteCarById(deleteId);
break;
case 9:
System.out.print("Sort by name or ID (N or I): ");
String sortChoice = scanner.nextLine();
boolean byName;
if (sortChoice.equalsIgnoreCase("N")) {
byName = true;
} else {
byName = false;
}
System.out.print("Sort in ascending or descending order (A or D): ");
sortChoice = scanner.nextLine();
boolean ascending;
if (sortChoice.equalsIgnoreCase("A")) {
ascending = true;
} else {
ascending = false;
}
system.sortCars(byName, ascending);
System.out.println("Cars sorted successfully.");
break;
case 10:
exit = true;
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
System.out.println("Goodbye!");
}
}
```
请注意,这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。此代码还需要添加异常处理和输入验证等功能以提高其健壮性。
阅读全文