用Java编写一个卫星管理系统的代码,需求可以添加卫星,可以删除卫星,可以查找卫星,可以修改卫星,主菜单应该提供一个操作选项列表,包括查看卫星列表、注册添加新卫星、编辑修改现有卫星信息、删除卫星、按名称搜索卫星、根据ID封锁卫星以及激活已经封锁了的卫星重新启用。
时间: 2023-04-03 09:03:42 浏览: 267
以下是一个简单的Java代码示例,用于实现卫星管理系统的基本功能:
import java.util.ArrayList;
import java.util.Scanner;
public class SatelliteManagementSystem {
private ArrayList<Satellite> satellites = new ArrayList<Satellite>();
private Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
SatelliteManagementSystem system = new SatelliteManagementSystem();
system.run();
}
private 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.println("6. 封锁卫星");
System.out.println("7. 激活已封锁的卫星");
System.out.println("0. 退出程序");
int choice = scanner.nextInt();
switch (choice) {
case 1:
listSatellites();
break;
case 2:
addSatellite();
break;
case 3:
editSatellite();
break;
case 4:
deleteSatellite();
break;
case 5:
searchSatelliteByName();
break;
case 6:
blockSatellite();
break;
case 7:
unblockSatellite();
break;
case 0:
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新输入。");
break;
}
}
}
private void listSatellites() {
if (satellites.isEmpty()) {
System.out.println("卫星列表为空。");
} else {
System.out.println("卫星列表:");
for (Satellite satellite : satellites) {
System.out.println(satellite);
}
}
}
private void addSatellite() {
System.out.println("请输入卫星名称:");
String name = scanner.next();
System.out.println("请输入卫星ID:");
int id = scanner.nextInt();
Satellite satellite = new Satellite(name, id);
satellites.add(satellite);
System.out.println("卫星添加成功。");
}
private void editSatellite() {
System.out.println("请输入要修改的卫星ID:");
int id = scanner.nextInt();
Satellite satellite = findSatelliteById(id);
if (satellite == null) {
System.out.println("找不到指定的卫星。");
} else {
System.out.println("请输入新的卫星名称:");
String name = scanner.next();
satellite.setName(name);
System.out.println("卫星信息修改成功。");
}
}
private void deleteSatellite() {
System.out.println("请输入要删除的卫星ID:");
int id = scanner.nextInt();
Satellite satellite = findSatelliteById(id);
if (satellite == null) {
System.out.println("找不到指定的卫星。");
} else {
satellites.remove(satellite);
System.out.println("卫星删除成功。");
}
}
private void searchSatelliteByName() {
System.out.println("请输入要搜索的卫星名称:");
String name = scanner.next();
Satellite satellite = findSatelliteByName(name);
if (satellite == null) {
System.out.println("找不到指定的卫星。");
} else {
System.out.println("卫星信息:" + satellite);
}
}
private void blockSatellite() {
System.out.println("请输入要封锁的卫星ID:");
int id = scanner.nextInt();
Satellite satellite = findSatelliteById(id);
if (satellite == null) {
System.out.println("找不到指定的卫星。");
} else {
satellite.setBlocked(true);
System.out.println("卫星已封锁。");
}
}
private void unblockSatellite() {
System.out.println("请输入要激活的卫星ID:");
int id = scanner.nextInt();
Satellite satellite = findSatelliteById(id);
if (satellite == null) {
System.out.println("找不到指定的卫星。");
} else {
satellite.setBlocked(false);
System.out.println("卫星已激活。");
}
}
private Satellite findSatelliteById(int id) {
for (Satellite satellite : satellites) {
if (satellite.getId() == id) {
return satellite;
}
}
return null;
}
private Satellite findSatelliteByName(String name) {
for (Satellite satellite : satellites) {
if (satellite.getName().equals(name)) {
return satellite;
}
}
return null;
}
}
class Satellite {
private String name;
private int id;
private boolean blocked;
public Satellite(String name, int id) {
this.name = name;
this.id = id;
this.blocked = false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public boolean isBlocked() {
return blocked;
}
public void setBlocked(boolean blocked) {
this.blocked = blocked;
}
@Override
public String toString() {
return "卫星名称:" + name + ",卫星ID:" + id + ",是否封锁:" + blocked;
}
}
请注意,这只是一个简单的示例代码,仅实现了卫星管理系统的基本功能。在实际开发中,您可能需要添加更多的功能和错误处理机制。
阅读全文