用Java写一个带可视化菜单的汽车管理系统,要求用文件输入输出
时间: 2023-06-24 17:05:25 浏览: 109
好的,我可以为您提供一个简单的汽车管理系统的实现。
首先,我们需要创建一个汽车类。这个类包含汽车的品牌、型号、颜色和价格等基本属性。代码如下:
```java
public class Car {
private String brand;
private String model;
private String color;
private double price;
public Car(String brand, String model, String color, double price) {
this.brand = brand;
this.model = model;
this.color = color;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", model='" + model + '\'' +
", color='" + color + '\'' +
", price=" + price +
'}';
}
}
```
接下来,我们需要创建一个汽车管理类。这个类包含了添加、查找、删除和显示所有汽车的方法。我们还需要使用文件输入输出来保存和读取汽车的信息。代码如下:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CarManager {
private List<Car> cars = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
public void addCar() {
System.out.println("请输入汽车品牌:");
String brand = scanner.next();
System.out.println("请输入汽车型号:");
String model = scanner.next();
System.out.println("请输入汽车颜色:");
String color = scanner.next();
System.out.println("请输入汽车价格:");
double price = scanner.nextDouble();
Car car = new Car(brand, model, color, price);
cars.add(car);
System.out.println("汽车添加成功!");
}
public void findCar() {
System.out.println("请输入要查找的汽车品牌:");
String brand = scanner.next();
boolean found = false;
for (Car car : cars) {
if (car.getBrand().equals(brand)) {
System.out.println(car);
found = true;
}
}
if (!found) {
System.out.println("没有找到该品牌的汽车!");
}
}
public void deleteCar() {
System.out.println("请输入要删除的汽车品牌:");
String brand = scanner.next();
boolean deleted = false;
for (Car car : cars) {
if (car.getBrand().equals(brand)) {
cars.remove(car);
deleted = true;
System.out.println("汽车删除成功!");
break;
}
}
if (!deleted) {
System.out.println("没有找到该品牌的汽车!");
}
}
public void displayAllCars() {
if (cars.size() == 0) {
System.out.println("没有汽车信息!");
return;
}
for (Car car : cars) {
System.out.println(car);
}
}
public void saveToFile(String fileName) {
try {
FileWriter writer = new FileWriter(fileName);
for (Car car : cars) {
writer.write(car.getBrand() + "," + car.getModel() + "," + car.getColor() + "," + car.getPrice() + "\n");
}
writer.close();
System.out.println("汽车信息保存成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadFromFile(String fileName) {
try {
FileReader reader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(",");
String brand = parts[0];
String model = parts[1];
String color = parts[2];
double price = Double.parseDouble(parts[3]);
Car car = new Car(brand, model, color, price);
cars.add(car);
}
bufferedReader.close();
reader.close();
System.out.println("汽车信息加载成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
最后,我们需要创建一个主程序,来调用汽车管理类的方法,并提供用户界面。代码如下:
```java
public class Main {
public static void main(String[] args) {
CarManager carManager = new CarManager();
String fileName = "cars.txt";
carManager.loadFromFile(fileName);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加汽车");
System.out.println("2. 查找汽车");
System.out.println("3. 删除汽车");
System.out.println("4. 显示所有汽车");
System.out.println("5. 保存汽车信息");
System.out.println("6. 退出程序");
int choice = carManager.scanner.nextInt();
switch (choice) {
case 1:
carManager.addCar();
break;
case 2:
carManager.findCar();
break;
case 3:
carManager.deleteCar();
break;
case 4:
carManager.displayAllCars();
break;
case 5:
carManager.saveToFile(fileName);
break;
case 6:
carManager.saveToFile(fileName);
System.exit(0);
break;
default:
System.out.println("无效的选择!");
break;
}
}
}
}
```
这样,一个带可视化菜单的汽车管理系统就完成了。用户可以通过选择菜单来实现添加、查找、删除和显示所有汽车的操作,并且可以将汽车信息保存到文件中。
阅读全文