diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..d2bcfe5 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index b3aa0a1..be08307 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -3,45 +3,42 @@ 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 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)); - + Satellites sa=new Satellites(); + // 获取卫星集合 + ArrayList satellites = sa.getSatellites(); + // 开启控制台输入 Scanner scanner = new Scanner(System.in); + // 创建调用方法对象实例 + ManageSatellites manage=new ManageSatellites(); + // 第一次显示菜单 displayMenu(); do { String choose = scanner.next(); switch (choose) { //1-----显示目前活动卫星列表。 - case "1" -> showSatelliteList(); + case "1" -> manage.showSatelliteList(satellites); //2-----注册新卫星。 - case "2" -> addNewSatellite(scanner); + case "2" -> manage.addNewSatellite(scanner,satellites); //3-----删除旧卫星。 - case "3" -> delOldSatellite(scanner); + case "3" -> manage.delOldSatellite(scanner,satellites); //4-----激活卫星。 - case "4" -> activeSatellite(scanner); + case "4" -> manage.activeSatellite(scanner,satellites); //5-----封锁(失活)卫星。 - case "5" -> blockSatellite(scanner); + case "5" -> manage.blockSatellite(scanner,satellites); //6-----显示失活卫星列表。 - case "6" -> showBlockSatelliteList(); + case "6" -> manage.showBlockSatelliteList(satellites); //7-----按名称模糊查找卫星。 - case "7" -> querySatelliteByNameLike(scanner); + case "7" -> manage.querySatelliteByNameLike(scanner,satellites); //8-----修改卫星信息。 - case "8" -> modStatelliteInfo(scanner); + case "8" -> manage.modifySatellite(scanner,satellites); //9-----退出! case "9" -> { System.out.println("退出"); @@ -49,135 +46,11 @@ public class Main { } 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() { @@ -195,52 +68,5 @@ public class Main { 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; - } + } diff --git a/src/main/java/org/example/ManageSatellites.java b/src/main/java/org/example/ManageSatellites.java new file mode 100644 index 0000000..d41f748 --- /dev/null +++ b/src/main/java/org/example/ManageSatellites.java @@ -0,0 +1,181 @@ +package org.example; + +import java.util.List; +import java.util.Scanner; + +public class ManageSatellites { + //进一步设计每一个卫星操作方法的细节流程(显示、验证信息) + //课堂演示其中一个功能方法 + + //1-----显示目前活动卫星列表。 + public void showSatelliteList(List satellites) { + for (Satellite satellite : satellites) { + if (satellite.isActivated()) { + System.out.println(satellite); + } + } + } + + //2-----注册新卫星。 + public void addNewSatellite(Scanner scanner, List satellites) { + 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-----删除旧卫星。 + public void delOldSatellite(Scanner scanner, List satellites) { + 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-----激活卫星。 + public void activeSatellite(Scanner scanner, List satellites) { + 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-----封锁(失活)卫星。 + public void blockSatellite(Scanner scanner, List satellites) { + 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-----显示失活卫星列表。 + public void showBlockSatelliteList(List satellites) { + for (Satellite satellite : satellites) { + if (!satellite.isActivated()) { + System.out.println(satellite); + } + } + } + //7-----按名称模糊查找卫星。 + public void querySatelliteByNameLike(Scanner scanner, List satellites) { + String keyword = readString(scanner, "请输入要查找的卫星名称关键字: "); + for (Satellite satellite : satellites) { + if (satellite.getName().contains(keyword)) { + System.out.println(satellite); + } + } + } + + //8-----修改卫星信息。 + public void modifySatellite(Scanner scanner, List satellites) { + + 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"); + } + } + + + // 读取字符串输入并校验 + 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; + } +} diff --git a/src/main/java/org/example/model/Satellite.java b/src/main/java/org/example/Satellite.java similarity index 69% rename from src/main/java/org/example/model/Satellite.java rename to src/main/java/org/example/Satellite.java index 13f72e3..de37748 100644 --- a/src/main/java/org/example/model/Satellite.java +++ b/src/main/java/org/example/Satellite.java @@ -1,4 +1,6 @@ -package org.example.model; +package org.example; + +import java.util.Objects; /** * @author : [chd] @@ -19,6 +21,7 @@ public class Satellite { //状态 private boolean activated=false; + //构造方法 public Satellite(String COSPARID, String name, double r, boolean activated) { this.COSPARID = COSPARID; this.name = name; @@ -26,6 +29,7 @@ public class Satellite { this.activated = activated; } + //重写tostring方法 @Override public String toString() { return "Satellite{" + @@ -36,6 +40,22 @@ public class Satellite { '}'; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Satellite satellite = (Satellite) o; + return Double.compare(r, satellite.r) == 0 && + activated == satellite.activated && + Objects.equals(COSPARID, satellite.COSPARID) && + Objects.equals(name, satellite.name); + } + + @Override + public int hashCode() { + return Objects.hash(COSPARID, name, r, activated); + } + public String getCOSPARID() { return COSPARID; } diff --git a/src/main/java/org/example/Satellites.java b/src/main/java/org/example/Satellites.java new file mode 100644 index 0000000..1256361 --- /dev/null +++ b/src/main/java/org/example/Satellites.java @@ -0,0 +1,68 @@ +package org.example; + +import java.util.ArrayList; + +public class Satellites { + + private ArrayList satellites=new ArrayList(10); + + public Satellites() { + init(); + } + + //初始化 + public void init(){ + 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)); + + } + + public void setSatellites(ArrayList satellites) { + this.satellites = satellites; + } + + + + + //获取卫星集合 + public ArrayList getSatellites() { + return satellites; + } + /*//新增一个卫星 + public void addSatellite(Satellite satellite) { + satellites.add(satellite); + } + //修改卫星对象 + public void modifySatellite(String cosparid,Satellite satellite) { + this.removeSatelliteByCosparid(cosparid); + addSatellite(satellite); + } + //删除一个卫星 + public void removeSatelliteByCosparid(String cosparid){ + int index=-1; + for (Satellite satellite : satellites) { + if(cosparid.equals(satellite.getCOSPARID())){ + index=satellites.indexOf(satellite); + } + } + if(index!=-1){ + System.out.println("未找到对应卫星,请重新操作!"); + }else{ + satellites.remove(index); + } + + + } + //根据名字模糊查询一个卫星 + public Satellite findSatelliteByName(String name) { + for (Satellite satellite : satellites) { + if (satellite.getName().contains(name)) { + return satellite; + } + } + return null; + }*/ +} diff --git a/target/classes/org/example/Main.class b/target/classes/org/example/Main.class index 73f7d5e..e7493a4 100644 Binary files a/target/classes/org/example/Main.class and b/target/classes/org/example/Main.class differ diff --git a/target/classes/org/example/ManageSatellites.class b/target/classes/org/example/ManageSatellites.class new file mode 100644 index 0000000..b12af55 Binary files /dev/null and b/target/classes/org/example/ManageSatellites.class differ diff --git a/target/classes/org/example/Satellite.class b/target/classes/org/example/Satellite.class new file mode 100644 index 0000000..7bf98da Binary files /dev/null and b/target/classes/org/example/Satellite.class differ diff --git a/target/classes/org/example/Satellites.class b/target/classes/org/example/Satellites.class new file mode 100644 index 0000000..fd30426 Binary files /dev/null and b/target/classes/org/example/Satellites.class differ diff --git a/target/classes/org/example/model/Satellite.class b/target/classes/org/example/model/Satellite.class deleted file mode 100644 index 394cf30..0000000 Binary files a/target/classes/org/example/model/Satellite.class and /dev/null differ