用java代码形式写一个酒店信息管理系统
时间: 2023-06-06 20:08:41 浏览: 130
好的,这是一个技术问题,很高兴可以帮您解答。以下是一个简单的Java代码示例,用于实现基本的酒店信息管理系统:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class HotelManagementSystem {
private static List<Hotel> hotels = new ArrayList<>();
public static void main(String[] args) {
start();
}
public static void start() {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("欢迎使用酒店信息管理系统!");
System.out.println("1. 添加酒店");
System.out.println("2. 查询酒店");
System.out.println("3. 修改酒店");
System.out.println("4. 删除酒店");
System.out.println("0. 退出");
System.out.print("请选择您要进行的操作:");
choice = scanner.nextInt();
switch (choice) {
case 1:
addHotel();
break;
case 2:
searchHotel();
break;
case 3:
updateHotel();
break;
case 4:
deleteHotel();
break;
case 0:
System.out.println("欢迎下次使用酒店信息管理系统!");
break;
default:
System.out.println("输入不合法,请重新输入!");
break;
}
} while (choice != 0);
}
public static void addHotel() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入酒店名称:");
String name = scanner.nextLine();
System.out.println("请输入酒店地址:");
String address = scanner.nextLine();
System.out.println("请输入酒店电话:");
String phone = scanner.nextLine();
hotels.add(new Hotel(name, address, phone));
System.out.println("添加成功!");
}
public static void searchHotel() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入酒店名称(输入空字符串表示不限):");
String name = scanner.nextLine();
System.out.println("请输入酒店地址(输入空字符串表示不限):");
String address = scanner.nextLine();
System.out.println("请输入酒店电话(输入空字符串表示不限):");
String phone = scanner.nextLine();
boolean found = false;
for (Hotel hotel : hotels) {
if ((name.equals("") || hotel.getName().contains(name))
&& (address.equals("") || hotel.getAddress().contains(address))
&& (phone.equals("") || hotel.getPhone().contains(phone))) {
System.out.println(hotel);
found = true;
}
}
if (!found) {
System.out.println("没有找到符合条件的酒店!");
}
}
public static void updateHotel() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的酒店名称:");
String name = scanner.nextLine();
for (int i = 0; i < hotels.size(); i++) {
if (hotels.get(i).getName().equals(name)) {
System.out.println("请输入酒店地址(当前值为" + hotels.get(i).getAddress() + ",不修改请直接回车):");
String address = scanner.nextLine();
if (!address.equals("")) {
hotels.get(i).setAddress(address);
}
System.out.println("请输入酒店电话(当前值为" + hotels.get(i).getPhone() + ",不修改请直接回车):");
String phone = scanner.nextLine();
if (!phone.equals("")) {
hotels.get(i).setPhone(phone);
}
System.out.println("更新成功!");
return;
}
}
System.out.println("没有找到指定的酒店!");
}
public static void deleteHotel() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的酒店名称:");
String name = scanner.nextLine();
for (int i = 0; i < hotels.size(); i++) {
if (hotels.get(i).getName().equals(name)) {
hotels.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到指定的酒店!");
}
}
class Hotel {
private String name;
private String address;
private String phone;
public Hotel(String name, String address, String phone) {
this.name = name;
this.address = address;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "酒店名称:" + name + ",地址:" + address + ",电话:" + phone;
}
}
```
此代码使用了Java中的OOP(面向对象编程)和基本的输入输出流,可实现添加、查询、修改和删除酒店信息的功能。当然,这只是一个简单的示例,您可以根据自己的需求进行适当修改或扩展。
阅读全文