一个物业管理系统具体Java代码
时间: 2023-09-24 22:13:16 浏览: 31
以下是一个简单的物业管理系统的 Java 代码示例:
```
import java.util.Scanner;
public class PropertyManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Property[] properties = new Property[5];
int count = 0;
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. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
if (count < 5) {
System.out.println("请输入物业名称:");
String name = scanner.next();
System.out.println("请输入物业地址:");
String address = scanner.next();
System.out.println("请输入物业面积:");
double area = scanner.nextDouble();
properties[count] = new Property(name, address, area);
count++;
System.out.println("添加成功!");
} else {
System.out.println("物业数量已达上限!");
}
break;
case 2:
if (count == 0) {
System.out.println("当前没有物业!");
} else {
for (int i = 0; i < count; i++) {
System.out.println(properties[i]);
}
}
break;
case 3:
System.out.println("请输入搜索关键字:");
String keyword = scanner.next();
boolean found = false;
for (int i = 0; i < count; i++) {
if (properties[i].getName().contains(keyword) || properties[i].getAddress().contains(keyword)) {
System.out.println(properties[i]);
found = true;
}
}
if (!found) {
System.out.println("未找到符合条件的物业!");
}
break;
case 4:
System.out.println("请输入要删除的物业名称:");
String name = scanner.next();
boolean deleted = false;
for (int i = 0; i < count; i++) {
if (properties[i].getName().equals(name)) {
for (int j = i; j < count - 1; j++) {
properties[j] = properties[j+1];
}
count--;
deleted = true;
System.out.println("删除成功!");
break;
}
}
if (!deleted) {
System.out.println("未找到要删除的物业!");
}
break;
case 5:
System.out.println("感谢使用物业管理系统!");
System.exit(0);
default:
System.out.println("无效的选择!");
break;
}
}
}
}
class Property {
private String name;
private String address;
private double area;
public Property(String name, String address, double area) {
this.name = name;
this.address = address;
this.area = area;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public double getArea() {
return area;
}
public String toString() {
return "名称:" + name + ",地址:" + address + ",面积:" + area;
}
}
```
这个物业管理系统可以进行添加物业、显示所有物业、搜索物业和删除物业等操作。其中,使用 Property 类来表示一个物业,包含名称、地址和面积三个属性。在主函数中,使用一个 Property 数组来存储所有的物业,并且限制了最多只能添加五个物业。用户可以通过控制台输入来进行各种操作,如添加物业时需要输入物业名称、地址和面积,删除物业时需要输入要删除的物业名称等。