用java写一个医生信息管理系统
时间: 2024-05-17 19:15:45 浏览: 129
基于java语言开发的医院信息管理系统
好的,我可以为您提供一个简单的医生信息管理系统的Java代码示例。该系统可以实现添加、删除、修改和查找医生信息的功能。
首先,我们需要创建一个Doctor类来定义医生的属性和方法,如下所示:
```java
public class Doctor {
private int id;
private String name;
private String department;
private String title;
private String phone;
public Doctor(int id, String name, String department, String title, String phone) {
this.id = id;
this.name = name;
this.department = department;
this.title = title;
this.phone = phone;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDepartment() {
return department;
}
public String getTitle() {
return title;
}
public String getPhone() {
return phone;
}
public void setName(String name) {
this.name = name;
}
public void setDepartment(String department) {
this.department = department;
}
public void setTitle(String title) {
this.title = title;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Doctor{" +
"id=" + id +
", name='" + name + '\'' +
", department='" + department + '\'' +
", title='" + title + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
```
然后,我们需要创建一个DoctorManager类来管理医生信息,如下所示:
```java
import java.util.ArrayList;
public class DoctorManager {
private ArrayList<Doctor> doctors;
public DoctorManager() {
doctors = new ArrayList<>();
}
public void addDoctor(Doctor doctor) {
doctors.add(doctor);
}
public void removeDoctor(int id) {
for (Doctor doctor : doctors) {
if (doctor.getId() == id) {
doctors.remove(doctor);
break;
}
}
}
public Doctor findDoctor(int id) {
for (Doctor doctor : doctors) {
if (doctor.getId() == id) {
return doctor;
}
}
return null;
}
public void updateDoctor(Doctor doctor) {
int index = doctors.indexOf(doctor);
if (index != -1) {
doctors.set(index, doctor);
}
}
public ArrayList<Doctor> getAllDoctors() {
return doctors;
}
}
```
最后,我们可以创建一个DoctorTest类来测试DoctorManager类,如下所示:
```java
public class DoctorTest {
public static void main(String[] args) {
DoctorManager manager = new DoctorManager();
Doctor doctor1 = new Doctor(1, "张三", "内科", "主治医师", "13812345678");
Doctor doctor2 = new Doctor(2, "李四", "外科", "副主任医师", "13912345678");
Doctor doctor3 = new Doctor(3, "王五", "口腔科", "医师", "13612345678");
manager.addDoctor(doctor1);
manager.addDoctor(doctor2);
manager.addDoctor(doctor3);
System.out.println("所有医生信息:");
for (Doctor doctor : manager.getAllDoctors()) {
System.out.println(doctor);
}
System.out.println("查找医生信息:");
System.out.println(manager.findDoctor(2));
System.out.println("修改医生信息:");
Doctor doctor4 = new Doctor(2, "赵六", "外科", "主任医师", "13987654321");
manager.updateDoctor(doctor4);
System.out.println(manager.findDoctor(2));
System.out.println("删除医生信息:");
manager.removeDoctor(3);
for (Doctor doctor : manager.getAllDoctors()) {
System.out.println(doctor);
}
}
}
```
以上就是一个简单的医生信息管理系统的Java代码示例。您可以根据需要进行修改和扩展。
阅读全文