init
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
|
||||
|
||||
|
||||
package org.example;
|
||||
|
||||
import org.example.model.Satellite;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
private static ArrayList<Satellite> satellites = new ArrayList<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 初始化五个卫星对象
|
||||
satellites.add(new Satellite("1999-025A", "卫星一号", 7000, true));
|
||||
satellites.add(new Satellite("2000-036A", "卫星二号", 8000, false));
|
||||
satellites.add(new Satellite("2001-047A", "卫星三号", 9000, true));
|
||||
satellites.add(new Satellite("2002-058A", "卫星四号", 10000, false));
|
||||
satellites.add(new Satellite("2003-069A", "卫星五号", 11000, true));
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
displayMenu();
|
||||
do {
|
||||
String choose = scanner.next();
|
||||
switch (choose) {
|
||||
//1-----显示目前活动卫星列表。
|
||||
case "1" -> showSatelliteList();
|
||||
//2-----注册新卫星。
|
||||
case "2" -> addNewSatellite(scanner);
|
||||
//3-----删除旧卫星。
|
||||
case "3" -> delOldSatellite(scanner);
|
||||
//4-----激活卫星。
|
||||
case "4" -> activeSatellite(scanner);
|
||||
//5-----封锁(失活)卫星。
|
||||
case "5" -> blockSatellite(scanner);
|
||||
//6-----显示失活卫星列表。
|
||||
case "6" -> showBlockSatelliteList();
|
||||
//7-----按名称模糊查找卫星。
|
||||
case "7" -> querySatelliteByNameLike(scanner);
|
||||
//8-----修改卫星信息。
|
||||
case "8" -> modStatelliteInfo(scanner);
|
||||
//9-----退出!
|
||||
case "9" -> {
|
||||
System.out.println("退出");
|
||||
System.exit(0);
|
||||
}
|
||||
default -> System.out.println("没有这个选项");
|
||||
}
|
||||
|
||||
displayMenu();
|
||||
} while (scanner.hasNext());
|
||||
}
|
||||
|
||||
//1-----显示目前活动卫星列表。
|
||||
private static void showSatelliteList() {
|
||||
for (Satellite satellite : satellites) {
|
||||
if (satellite.isActivated()) {
|
||||
System.out.println(satellite);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//2-----注册新卫星。
|
||||
private static void addNewSatellite(Scanner scanner) {
|
||||
String cosparId = readString(scanner, "请输入卫星的COSPARID: ");
|
||||
String name = readString(scanner, "请输入卫星的名称: ");
|
||||
double r = readDouble(scanner, "请输入卫星的半径: ");
|
||||
boolean activated = readBoolean(scanner, "请输入卫星的状态 (true/false): ");
|
||||
|
||||
Satellite newSatellite = new Satellite(cosparId, name, r, activated);
|
||||
satellites.add(newSatellite);
|
||||
System.out.println("卫星已添加: " + newSatellite);
|
||||
}
|
||||
|
||||
//3-----删除旧卫星。
|
||||
private static void delOldSatellite(Scanner scanner) {
|
||||
String cosparId = readString(scanner, "请输入要删除的卫星COSPARID: ");
|
||||
boolean flag = false;
|
||||
for (Satellite se : satellites) {
|
||||
if (se.getCOSPARID().equals(cosparId)) {
|
||||
satellites.remove(se);
|
||||
System.out.println("卫星已删除: " + se);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!flag) {
|
||||
System.out.println("无效的COSPARID");
|
||||
}
|
||||
}
|
||||
|
||||
//4-----激活卫星。
|
||||
private static void activeSatellite(Scanner scanner) {
|
||||
String cosparId = readString(scanner, "请输入要激活的卫星COSPARID: ");
|
||||
boolean flag = false;
|
||||
for (Satellite se : satellites) {
|
||||
if (se.getCOSPARID().equals(cosparId)) {
|
||||
if (se.isActivated()) {
|
||||
System.out.println("卫星已处于激活状态,无需重新激活: " + se);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
se.setActivated(true);
|
||||
System.out.println("卫星已激活: " + se);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!flag) {
|
||||
System.out.println("无效的COSPARID");
|
||||
}
|
||||
}
|
||||
|
||||
//5-----封锁(失活)卫星。
|
||||
private static void blockSatellite(Scanner scanner) {
|
||||
String cosparId = readString(scanner, "请输入要封锁的卫星COSPARID: ");
|
||||
boolean flag = false;
|
||||
for (Satellite se : satellites) {
|
||||
if (se.getCOSPARID().equals(cosparId)) {
|
||||
if (!se.isActivated()) {
|
||||
System.out.println("卫星已处于失活状态,无需重新封锁: " + se);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
se.setActivated(false);
|
||||
System.out.println("卫星已封锁: " + se);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!flag) {
|
||||
System.out.println("无效的COSPARID");
|
||||
}
|
||||
}
|
||||
|
||||
//6-----显示失活卫星列表。
|
||||
private static void showBlockSatelliteList() {
|
||||
for (Satellite satellite : satellites) {
|
||||
if (!satellite.isActivated()) {
|
||||
System.out.println(satellite);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//7-----按名称模糊查找卫星。
|
||||
private static void querySatelliteByNameLike(Scanner scanner) {
|
||||
String keyword = readString(scanner, "请输入要查找的卫星名称关键字: ");
|
||||
for (Satellite satellite : satellites) {
|
||||
if (satellite.getName().contains(keyword)) {
|
||||
System.out.println(satellite);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//8-----修改卫星信息。
|
||||
private static void modStatelliteInfo(Scanner scanner) {
|
||||
String cosparId = readString(scanner, "请输入要修改的卫星COSPARID: ");
|
||||
boolean flag = false;
|
||||
for (Satellite se : satellites) {
|
||||
if (se.getCOSPARID().equals(cosparId)) {
|
||||
String newCosparId = readString(scanner, "请输入卫星的COSPARID: ");
|
||||
se.setCOSPARID(newCosparId);
|
||||
String newName = readString(scanner, "请输入卫星的名称: ");
|
||||
se.setName(newName);
|
||||
double newR = readDouble(scanner, "请输入卫星的半径: ");
|
||||
se.setR(newR);
|
||||
boolean newActivated = readBoolean(scanner, "请输入卫星的状态 (true/false): ");
|
||||
se.setActivated(newActivated);
|
||||
System.out.println("卫星信息已修改: " + se);
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!flag) {
|
||||
System.out.println("无效的COSPARID");
|
||||
}
|
||||
}
|
||||
|
||||
//显示菜单
|
||||
public static void displayMenu() {
|
||||
System.out.println("**************************");
|
||||
System.out.println("\t1-----显示目前活动卫星列表。");
|
||||
System.out.println("\t2-----注册新卫星。");
|
||||
System.out.println("\t3-----删除旧卫星。");
|
||||
System.out.println("\t4-----激活卫星。");
|
||||
System.out.println("\t5-----封锁(失活)卫星。");
|
||||
System.out.println("\t6-----显示失活卫星列表。");
|
||||
System.out.println("\t7-----按名称模糊查找卫星。");
|
||||
System.out.println("\t8-----修改卫星信息。");
|
||||
System.out.println("\t9-----退出!");
|
||||
System.out.println("**************************");
|
||||
System.out.print("选择:");
|
||||
}
|
||||
|
||||
// 读取字符串输入并校验
|
||||
private static String readString(Scanner scanner, String prompt) {
|
||||
String input;
|
||||
do {
|
||||
System.out.print(prompt);
|
||||
input = scanner.next();
|
||||
if (input.trim().isEmpty()) {
|
||||
System.out.println("输入不能为空,请重新输入。");
|
||||
}
|
||||
} while (input.trim().isEmpty());
|
||||
return input;
|
||||
}
|
||||
|
||||
// 读取双精度浮点数输入并校验
|
||||
private static double readDouble(Scanner scanner, String prompt) {
|
||||
double input;
|
||||
while (true) {
|
||||
System.out.print(prompt);
|
||||
if (scanner.hasNextDouble()) {
|
||||
input = scanner.nextDouble();
|
||||
if (input <= 0) {
|
||||
System.out.println("半径必须大于0,请重新输入。");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
System.out.println("输入无效,请输入一个数字。");
|
||||
scanner.next(); // 清除无效输入
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
// 读取布尔值输入并校验
|
||||
private static boolean readBoolean(Scanner scanner, String prompt) {
|
||||
boolean input;
|
||||
while (true) {
|
||||
System.out.print(prompt);
|
||||
if (scanner.hasNextBoolean()) {
|
||||
input = scanner.nextBoolean();
|
||||
break;
|
||||
} else {
|
||||
System.out.println("输入无效,请输入 true 或 false。");
|
||||
scanner.next(); // 清除无效输入
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.example.model;
|
||||
|
||||
/**
|
||||
* @author : [chd]
|
||||
* @version : [v1.0]
|
||||
* @description : [一句话描述该类的功能]
|
||||
* @createTime : [2024/12/22 17:27]
|
||||
* @updateUser : [chd]
|
||||
* @updateTime : [2024/12/22 17:27]
|
||||
* @updateRemark : [说明本次修改内容]
|
||||
*/
|
||||
public class Satellite {
|
||||
//卫星id
|
||||
private String COSPARID;
|
||||
//卫星名称
|
||||
private String name;
|
||||
//半径
|
||||
private double r;
|
||||
//状态
|
||||
private boolean activated=false;
|
||||
|
||||
public Satellite(String COSPARID, String name, double r, boolean activated) {
|
||||
this.COSPARID = COSPARID;
|
||||
this.name = name;
|
||||
this.r = r;
|
||||
this.activated = activated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Satellite{" +
|
||||
"COSPARID='" + COSPARID + '\'' +
|
||||
", name='" + name + '\'' +
|
||||
", r=" + r +
|
||||
", activated=" + activated +
|
||||
'}';
|
||||
}
|
||||
|
||||
public String getCOSPARID() {
|
||||
return COSPARID;
|
||||
}
|
||||
|
||||
public void setCOSPARID(String COSPARID) {
|
||||
this.COSPARID = COSPARID;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getR() {
|
||||
return r;
|
||||
}
|
||||
|
||||
public void setR(double r) {
|
||||
this.r = r;
|
||||
}
|
||||
|
||||
public boolean isActivated() {
|
||||
return activated;
|
||||
}
|
||||
|
||||
public void setActivated(boolean activated) {
|
||||
this.activated = activated;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user