实现view层
时间: 2023-07-20 09:09:39 浏览: 40
好的,以下是一个简单的船舶管理系统的用户界面实现:
```java
import java.util.Scanner;
public class ShipManagementSystem {
private static ShipDao dao = new ShipDaoImpl();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
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("===============");
System.out.println("请输入操作编号:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
showAllShips();
break;
case 2:
addShip();
break;
case 3:
deleteShip();
break;
case 4:
updateShip();
break;
case 5:
getShipById();
break;
case 6:
System.out.println("谢谢使用,再见!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
}
}
private static void showAllShips() {
System.out.println("所有船舶信息如下:");
for (Ship ship : dao.getAllShips()) {
System.out.println(ship);
}
}
private static void addShip() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入船舶编号:");
String shipId = scanner.nextLine();
System.out.println("请输入船舶名称:");
String shipName = scanner.nextLine();
System.out.println("请输入船长姓名:");
String captain = scanner.nextLine();
System.out.println("请输入载重量:");
double capacity = scanner.nextDouble();
System.out.println("请输入泊位编号:");
String berthId = scanner.nextLine();
System.out.println("请输入船舶状态(0表示空闲,1表示已入港,2表示已离港):");
int status = scanner.nextInt();
dao.addShip(new Ship(shipId, shipName, captain, capacity, berthId, status));
System.out.println("添加成功!");
}
private static void deleteShip() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的船舶编号:");
String shipId = scanner.nextLine();
dao.deleteShip(shipId);
System.out.println("删除成功!");
}
private static void updateShip() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的船舶编号:");
String shipId = scanner.nextLine();
Ship ship = dao.getShipById(shipId);
if (ship == null) {
System.out.println("未找到该船舶!");
return;
}
System.out.println("请输入新的船舶名称(原名称:" + ship.getShipName() + "):");
String shipName = scanner.nextLine();
System.out.println("请输入新的船长姓名(原姓名:" + ship.getCaptain() + "):");
String captain = scanner.nextLine();
System.out.println("请输入新的载重量(原载重量:" + ship.getCapacity() + "):");
double capacity = scanner.nextDouble();
System.out.println("请输入新的泊位编号(原泊位编号:" + ship.getBerthId() + "):");
String berthId = scanner.nextLine();
System.out.println("请输入新的船舶状态(0表示空闲,1表示已入港,2表示已离港,原状态:" + ship.getStatus() + "):");
int status = scanner.nextInt();
ship.setShipName(shipName);
ship.setCaptain(captain);
ship.setCapacity(capacity);
ship.setBerthId(berthId);
ship.setStatus(status);
dao.updateShip(ship);
System.out.println("修改成功!");
}
private static void getShipById() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的船舶编号:");
String shipId = scanner.nextLine();
Ship ship = dao.getShipById(shipId);
if (ship == null) {
System.out.println("未找到该船舶!");
} else {
System.out.println("船舶信息如下:");
System.out.println(ship);
}
}
}
```
在该用户界面中,通过Scanner类实现用户输入的读取,然后根据用户的选择调用对应的方法来实现相应的操作。