用Java写一个带菜单的汽车管理系统
时间: 2023-06-24 15:06:03 浏览: 76
用java做的一个图书管理系统的源代码.doc
好的,下面是一个简单的汽车管理系统的Java代码,包含菜单功能,可以实现添加、查询、修改和删除汽车信息的基本操作:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class CarManagementSystem {
private ArrayList<Car> carList = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
public void run() {
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.print("请选择操作:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addCar();
break;
case 2:
searchCar();
break;
case 3:
updateCar();
break;
case 4:
deleteCar();
break;
case 5:
System.out.println("谢谢使用!");
System.exit(0);
default:
System.out.println("输入错误,请重新选择!");
}
}
}
private void addCar() {
System.out.print("请输入汽车品牌:");
String brand = scanner.next();
System.out.print("请输入汽车型号:");
String model = scanner.next();
System.out.print("请输入汽车颜色:");
String color = scanner.next();
System.out.print("请输入汽车价格:");
double price = scanner.nextDouble();
Car car = new Car(brand, model, color, price);
carList.add(car);
System.out.println("汽车信息添加成功!");
}
private void searchCar() {
System.out.print("请输入查询的汽车品牌:");
String brand = scanner.next();
for (Car car : carList) {
if (car.getBrand().equals(brand)) {
System.out.println(car);
}
}
}
private void updateCar() {
System.out.print("请输入要修改的汽车品牌:");
String brand = scanner.next();
for (Car car : carList) {
if (car.getBrand().equals(brand)) {
System.out.print("请输入新的汽车品牌:");
String newBrand = scanner.next();
car.setBrand(newBrand);
System.out.print("请输入新的汽车型号:");
String newModel = scanner.next();
car.setModel(newModel);
System.out.print("请输入新的汽车颜色:");
String newColor = scanner.next();
car.setColor(newColor);
System.out.print("请输入新的汽车价格:");
double newPrice = scanner.nextDouble();
car.setPrice(newPrice);
System.out.println("汽车信息修改成功!");
return;
}
}
System.out.println("未找到该汽车品牌!");
}
private void deleteCar() {
System.out.print("请输入要删除的汽车品牌:");
String brand = scanner.next();
for (int i = 0; i < carList.size(); i++) {
if (carList.get(i).getBrand().equals(brand)) {
carList.remove(i);
System.out.println("汽车信息删除成功!");
return;
}
}
System.out.println("未找到该汽车品牌!");
}
public static void main(String[] args) {
CarManagementSystem system = new CarManagementSystem();
system.run();
}
}
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 "汽车品牌:" + brand + ",汽车型号:" + model + ",汽车颜色:" + color + ",汽车价格:" + price;
}
}
```
这个程序使用了ArrayList来存储汽车信息,通过菜单功能来实现添加、查询、修改和删除汽车信息的操作。由于这只是一个简单的示例程序,实际的汽车管理系统可能会更加复杂,需要根据实际需求进行设计和开发。
阅读全文