设计并实现一个新冠疫苗接种信息管理系统(假设该系统面向需要接种两剂的疫苗)。要求定义一个包含接种者的身份证号、姓名、已接种了几剂疫苗、第一剂接种时间、第二剂接种时间等信息的顺序表,系统至少包含以下功能:
(1)逐个显示信息表中疫苗接种的信息;
(2)两剂疫苗接种需要间隔14~28天,输出目前满足接种第二剂疫苗的接种者信息;
(3)给定一个新增接种者的信息,插入到表中指定的位置;
(4)分别删除指定位置和给定接种者身份证号的图书记录;
(5)利用直接插入排序或者折半插入排序按照身份证号进行排序;
(6)分别利用快速排序和堆排序按照第一剂接种的时间进行排序;
(7)根据身份证号进行折半查找,若查找成功,则返回此接种者的信息;
(9)为提高检索效率,要求利用利用接种者的姓氏为关键字建立哈希表,并利用链地址法处理冲突。给定接种者的身份证号或姓名,查找疫苗接种信息,并输出冲突次数和平均查找长度;
(10)提供用户菜单,方便选择执行功能。可以设计成一级或多级菜单。所有功能都可重复执行。
该项目实现代码有些多,有些功能我实现后并没有进行多次测试,如有问题请在下边留言,大家互相讨论学习
下边是主方法代码:
package work.Yiqing;
import java.util.Arrays;
import java.util.Scanner;
public class TestPersonArrView {
private static PersonindexInfo personindexInfo = new PersonindexInfo(10);
public TestPersonArrView() {
// PersonindexInfo personindexInfo = new PersonindexInfo();
//默认创建n位人员信息
PersonInfo person1 = new PersonInfo(1,410222101,"常白",2,new Time(2022,12,5),new Time(2022,12,29));
PersonInfo person2 = new PersonInfo(2,410222106,"张一山",1,new Time(2022,12,12));
PersonInfo person3 = new PersonInfo(3,410222105,"张三",1,new Time(2022,12,11));
PersonInfo person4 = new PersonInfo(4,410222102,"李四",1,new Time(2022,12,13));
PersonInfo person5 = new PersonInfo(5,410222103,"周杰伦",1,new Time(2022,12,14));
personindexInfo.addPerson(person1);
personindexInfo.addPerson(person2);
personindexInfo.addPerson(person3);
personindexInfo.addPerson(person4);
personindexInfo.addPerson(person5);
// personindexInfo.Search();//1查询所有接种人员信息
// personindexInfo.okTwo();//2查询满足第二针人员
// System.out.println(personOne);
}
//按对应序号执行
public void enterView () {
boolean isFlag = true;
while (isFlag) {
System.out.println("------------------新冠疫苗接种信息管理系统--------------------");
System.out.println(" 1.查询所有接种人员信息");
System.out.println(" 2.查询满足第二针人员");
System.out.println(" 3.信息插入到表中指定位置");
System.out.println(" 4.按照序号指定位置删除");
System.out.println(" 5.按照IdCard指定位置删除");
System.out.println(" 6.按照IdCard进行插入排序");
System.out.println(" 7.按照第一剂接种的时间快速排序");
System.out.println(" 8.按照第一次接种时间进行堆排序");
System.out.println(" 9.按照身份证折半查找人员信息(查找前请先按6进行排序)");
System.out.println(" 10.按照名字为索引创建哈希表并搜索到该成员信息");
System.out.println(" 11.显示按照名字为索引创建哈希表");
System.out.println(" 12.退出系统、、、");
System.out.println("------------------新冠疫苗接种信息管理系统--------------------");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
switch (num){
case 1:
System.out.println("查询所有接种人员信");
searchinfor();
break;
case 2:
// System.out.println();
System.out.println("查询满足第二针人员");
//查询满足第二针条件的人员信息
serchTwo();
break;
case 3:
System.out.println("信息插入到表中指定位置");
insertinfo();
break;
case 4:
System.out.println("按照序号指定位置删除");
delinfor();
break;
case 5:
System.out.println("按照IdCar指定位置删除");
IdCardelinfor();
break;
case 6:
System.out.println("按照IdCard排序");
IdSort();
break;
case 7:
System.out.println("按照第一次接种时间快速排序");
Quick();
break;
case 8:
System.out.println("按照第一次接种时间进行堆排序");
Heapsort();
break;
case 9:
System.out.println("按照身份证折半查找人员信息");
idCarinsert();
break;
case 10:
System.out.println("按照名字为索引创建哈希表");
creathash();
break;
case 11:
personindexInfo.CreatHash();
break;
case 12:
isFlag = false ;
System.out.println("退出成功");
}
}
}
//逐步查询疫苗接种的信息
public void searchinfor(){
personindexInfo.Search();
}
//查询满足第二针条件的人员信息
public void serchTwo(){
personindexInfo.okTwo();
}
public void insertinfo(){
Scanner input = new Scanner(System.in);
System.out.println("输入你要插入的位置:");
int index = input.nextInt();
System.out.println("---------------------新冠疫苗接种信息管理系统---------------------");
System.out.println("身份证id:");
long idCar = input.nextInt();
// input.nextLine();
System.out.println("姓名:");
input.nextLine();
String name1 = input.nextLine();
System.out.println("接种次数:");
int count = input.nextInt();
if (count < 2) {
System.out.println("第一针接种年份:");
int year = input.nextInt();
System.out.println("第一针接种月份:");
int month = input.nextInt();
System.out.println("第一针接种日:");
int day = input.nextInt();
PersonInfo person = new PersonInfo(index,idCar,name1,count,new Time(year,month,day));
if (personindexInfo.insertindex(person,index-1)){
System.out.println("插入成功");
// personindexInfo.Search();
}else {
System.out.println("插入失败");
}
}else {
System.out.println("第一针接种年份:");
int year = input.nextInt();
System.out.println("第一针接种月份:");
int month = input.nextInt();
System.out.println("第一针接种日:");
int day = input.nextInt();
System.out.println("第二针接种年份:");
int year1 = input.nextInt();
System.out.println("第二针接种月份:");
int month1 = input.nextInt();
System.out.println("第二针接种日:");
int day1 = input.nextInt();
PersonInfo person = new PersonInfo(index,idCar,name1,count,new Time(year,month,day),new Time(year1,month1,day1));
if (personindexInfo.insertindex(person,index-1)){
System.out.println("插入成功");
// personindexInfo.Search();
}else {
System.out.println("插入失败");
}
}
}
//按照序号删除
public void delinfor () {
System.out.println("请输入你要删除的位置");
Scanner input = new Scanner(System.in);
int index = input.nextInt();
boolean judge = personindexInfo.deletePerson(index-1);
if (judge==true){
System.out.println("删除成功");
personindexInfo.Search();
}else {
System.out.println("删除失败!可能是因为您的输入不合法");
}
}
//按照身份证id删除
public void IdCardelinfor(){
System.out.println("请输入你要删除的IdCar");
Scanner input = new Scanner(System.in);
int index = input.nextInt();
System.out.println(index);
boolean judge = personindexInfo.IdCardelePerson(index);
if (judge==true){
System.out.println("删除成功");
personindexInfo.Search();
}else {
System.out.println("删除失败!可能是因为您的输入不合法");
}
}
//按照身份证Id插入排序
public void IdSort(){
if (personindexInfo.InsertInto()){
System.out.println("排序成功");
searchinfor();
}else {
System.out.println("排序失败");
}
}
public void Quick(){
personindexInfo.Quicksort();
System.out.println("排序成功");
searchinfor();
}
public void Heapsort(){
personindexInfo.heapsort();
System.out.println("排序成功");
searchinfor();
}
public void idCarinsert(){
System.out.println("请输入你要查找人员的IdCard:");
Scanner input = new Scanner(System.in);
int IdCard = input.nextInt();
personindexInfo.binaryinfo(IdCard);
}
public void creathash(){
Scanner input = new Scanner(System.in);
System.out.println("输入你要查询的名字");
String name = input.nextLine();
System.out.println("哈希表:");
personindexInfo.CreatHash();
System.out.println("搜索到的成员信息为:");
try {
personindexInfo.FindHash(name);
}catch (Exception e){
System.out.println("没有找到该成员信息");
}
}
public static void main(String[] args) {
TestPersonArrView main = new TestPersonArrView();
// main.insertinfo();
// personindexInfo.Search();
// main.delinfor();
main.enterView();
}
}
创建一个PersonInfo类
package work.Yiqing;
public class PersonInfo {
private int id;
private long idCar;
private String name;
private char xing ;
private int count; //接种疫苗次数
private Time time1;//第一次疫苗接种时间
private Time time2 ;//第二次疫苗接种时间
public PersonInfo() {
xing = getName().charAt(0);
}
public PersonInfo(int id, long idCar, String name, int count, Time time1, Time time2) {
this.id = id;
this.idCar = idCar;
this.name = name;
this.count = count;
this.time1 = time1;
this.time2 = time2;
}
public PersonInfo(int id, long idCar, String name, int count, Time time1) {
this.id = id;
this.idCar = idCar;
this.name = name;
this.count = count;
this.time1 = time1;
}
public void setId(int id) {
this.id = id;
}
public void setIdCar(long idCar) {
this.idCar = idCar;
}
public void setName(String name) {
this.name = name;
}
public void setCount(int count) {
this.count = count;
}
public void setTime1(Time time1) {
this.time1 = time1;
}
public void setTime2(Time time2) {
this.time2 = time2;
}
public long getId() {
return id;
}
public long getIdCar() {
return idCar;
}
public String getName() {
return name;
}
public int getCount() {
return count;
}
public char getXing() {
return name.charAt(0);
}
public Time getTime1() {
return time1;
}
public Time getTime2() {
return time2;
}
@Override
public String toString() {
return "疫苗接种人员"+id+"{" +
" idCar:" + idCar +
", name:'" + name + '\'' +
", 已接种" + count +
"针, 第一针接种时间:" + time1 +
", 第二针接种时间:" + time2 +
'}';
}
}
在创建一个Time类,来存储时间
package work.Yiqing;
public class Time {
int year;
int month;
int day;
// int total;
// int total = year+month+day;
public Time(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return year+"年"+month+"月"+day+"日";
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
// public int getTotal() {
// return this.getYear()+this.getMonth()+this.getDay();
// }
}
各个功能实现的代码在PersonIndexInfo类中
各个方法有注释,如果不懂可以评论留言互相讨论
package work.Yiqing;
import java.util.Arrays;
import java.util.Calendar;
public class PersonindexInfo {
private static PersonInfo[] ArrayPersonInfo;//记录注射疫苗人员信息数组
private static HashTab[] hashTab = new HashTab[4]; //创建哈希表数组
private static int total = 0;//记录已保存人员的总数
private PersonInfo p;
public PersonindexInfo() {
}
public PersonindexInfo(int totalPerson) {
ArrayPersonInfo = new PersonInfo[totalPerson];
}
//添加新增接种者的信息
public boolean addPerson(PersonInfo person){
if (total >= ArrayPersonInfo.length){
return false;
}
ArrayPersonInfo[total++] = person;
return true;
}
public void Search(){
for (int i = 0; i < total;i++){
System.out.println(ArrayPersonInfo[i]);
}
}
// 查询满足接种第二剂疫苗的接种者信息
public void okTwo(){
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_MONTH);//获取今天是这个月的第几天
System.out.println("两剂疫苗接种需要间隔14~28天,以下是目前满足接种第二剂疫苗的接种者信息:");
for (int i = 0; i < total;i++){
if (ArrayPersonInfo[i].getCount()<2 && day - ArrayPersonInfo[i].getTime1().getDay()>=14 && day - ArrayPersonInfo[i].getTime1().getDay()<=28){
System.out.println(ArrayPersonInfo[i]);
}
}
}
//给定一个新增接种者的信息,插入到表中指定的位置
public boolean insertindex(PersonInfo Person,int index){
if (index < 0 || index > total) {
return false;
}
int a = 0;
for (int i = 0; i < total; i++) {
if (i==index) {
a=i;
break;
}
}
//将要插入的数字位置的后面数字后移
for (int i = total-1; i>=a; i--) {
ArrayPersonInfo[i+1]=ArrayPersonInfo[i]; //后移的操作
ArrayPersonInfo[i].setId(i+2);
}
//插入数字
ArrayPersonInfo[a]=Person;
total++;
return true;
}
//分别删除指定位置和给定接种者身份证号的图书记录
public boolean deletePerson(int index) {
if (index < 0 || index >= total) {
return false;
}
for (int i = index; i < total - 1; i++) {
ArrayPersonInfo[i] = ArrayPersonInfo[i + 1];
ArrayPersonInfo[i].setId(i+1);
}
//最后的元素需要为空
ArrayPersonInfo[total - 1] = null;
total--;
return true;
}
public boolean IdCardelePerson(int idCar){
int a = 0;
for (int i = 0; i < total; i++) {
if (ArrayPersonInfo[i].getIdCar()==idCar) {
a=i;
break;
}
}
//将a位置以后的元素向前移动一位,并将最后一位赋值为空
for (int i = a+1; i<=total-1; i++) {
ArrayPersonInfo[i-1]=ArrayPersonInfo[i]; //后移的操作
ArrayPersonInfo[i].setId(i);
}
ArrayPersonInfo[total - 1] = null;
total--;
return true;
}
//按照身份证号进行插入排序
//插入排序:把所有元素分为两组 已经排序 和 未排序的
//找到未排序中数组的第一个元素 和已排序进行比对插入
//倒序遍历已经排序的元素 依次比对 直到找到一个元素小于等于待插入的元素 那么把待插入元素放入这个位置 其他元素向后移动一位
public boolean InsertInto(){
for (int i = 1; i < total; i++) {
for (int j = i; j >= 0; j--) {
if (ArrayPersonInfo[j-1].getIdCar()>ArrayPersonInfo[j].getIdCar()){
PersonInfo temp;
temp = ArrayPersonInfo[j-1];
ArrayPersonInfo[j-1] = ArrayPersonInfo[j];
ArrayPersonInfo[j] = temp;
}else {
break;
}
}
}
return true;
}
//
public static void Quicksort(){
int lo = 0;
int hi = total-1;
sort(ArrayPersonInfo,lo,hi);
}
private static void sort(PersonInfo[] a,int lo,int hi){
//递归出口
if (hi <= lo){
return;
}
int partition = partition(a, lo, hi);//得到分界值 (左子组 右子组)
//创建左子组
sort(a,lo,partition);
//创建右子组
sort(a,partition+1,hi);
}
private static int partition(PersonInfo[] a,int lo,int hi){
// System.out.println("1");
//创建双向指针 和基准值
int key = a[lo].getTime1().getDay();
int left = lo;
int right = hi+1;
while (true){
//右子组 右指针找到比基准值小的数得到索引
while (ArrayPersonInfo[--right].getTime1().getDay() > key){
if (right==lo){
break;
}
}
//左子组 左指针找到比基准值大的数得到索引
while (ArrayPersonInfo[++left].getTime1().getDay() < key){
if (left==hi){
break;
}
}
//判断条件结束
if (left>=right){
break;
}else {
exch(a,left,right); //不稳定 分组后 两个相等的5元素不在一组 如果左子组的靠前的元素的大于那么后边的5就会交换位置 破坏了稳定性
}
//交换左右指针位置上的数据
}
exch(a,lo,right);
return right;
}
private static void exch(PersonInfo[] a,int i,int j){
PersonInfo temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
//利用堆排序对注射第一针疫苗人员信息进行排序
public static boolean less(PersonInfo[] heap,int i, int j){
return ((heap[i].getTime1().getDay())-(heap[j].getTime1().getDay()))<0;
}
private static void exch1(PersonInfo[] heap,int i,int j){
PersonInfo temp;
temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
private static void createHeap( PersonInfo[] heap){
System.arraycopy(ArrayPersonInfo,0,heap,1,total);
for (int i = (heap.length)/2; i > 0 ; i--) {
sink(heap,i,heap.length-1);
}
}
public static void heapsort(){
PersonInfo[] heap = new PersonInfo[total+1];
createHeap(heap);
int N = heap.length-1;
while (N!=1){
exch1(heap,1,N);
N--;
sink(heap,1,N);
}
System.arraycopy(heap,1,ArrayPersonInfo,0,total);
}
private static void sink(PersonInfo[] heap,int target,int range){
while (2*target<=range){
int max;
if (2*target+1<=range) {
if (less(heap, 2 * target, 2 * target + 1)) {
max = 2 * target + 1;
} else {
max = 2 * target;
}
}else {
max = 2 * target;
}
if (!less(heap,target,max)){
break;
}
exch1(heap,target,max);
target = max;
}
}
//对身份证号进行折半查找(查找前先进行排序)
public void binaryinfo(int idCar){
int index = binarySearch(ArrayPersonInfo,idCar,0,total-1);
if (index==-1){
System.out.println("结果不存在");
}else {
System.out.println(ArrayPersonInfo[index].toString());
}
}
public static int binarySearch(PersonInfo[] arr,int key, int low,int high){
if (low>high){
return -1;
}
int mid = (low+high)/2;
int val = (int) arr[mid].getIdCar();
//递归
if (val>key){
return binarySearch(arr,key,low,mid-1);
} else if (val<key) {
return binarySearch(arr,key,mid+1,high);
}else {
return mid;
}
}
//根据名字创建hash表 在查找
public void CreatHash(){
// PersonindexInfo[] hashtab = new PersonindexInfo[total];
// HashTab personhash = new HashTab(4);
hashTab[0] = new HashTab(5);
hashTab[1] = new HashTab(5);
hashTab[2] = new HashTab(5);
hashTab[3] = new HashTab(5);
hashTab[0].setName('常');
// System.out.println(hashTab[0].getPersonhash());
hashTab[1].setName('李');
hashTab[2].setName('张');
hashTab[3].setName('周');
for (int i = 0; i < 4; i++) {
int index = 0;
for (int j = 0; j < total; j++) {
if (hashTab[i].getName()==ArrayPersonInfo[j].getXing()){
hashTab[i].Personhash[index++] = ArrayPersonInfo[j];
}
}
System.out.println(hashTab[i].toString());
}
}
//根据名字在哈希表中查找你想要找的人员信息 && !hashTab[i].Personhash[j].equals(null)
public void FindHash(String name){
for (int i = 0; i < 4; i++) {
if (name.charAt(0)==hashTab[i].getName()){
for (int j = 0; j < 5; j++) {
if (name.equals(hashTab[i].Personhash[j].getName())){
System.out.println(hashTab[i].Personhash[j].toString());
break;
}
}
}
}
}
}
创建Hash表 来实现功能11
package work.Yiqing;
import java.util.Arrays;
public class HashTab {
char name;
PersonInfo [] Personhash;
public HashTab(int size) {
Personhash = new PersonInfo[size];
}
public void setPersonhash(PersonInfo[] personhash) {
Personhash = personhash;
}
public char getName() {
return name;
}
public PersonInfo[] getPersonhash() {
return Personhash;
}
public void setName(char name) {
this.name = name;
}
@Override
public String toString() {
return "HashTab{" +
"姓:" + name +
", 姓"+ name+"的成员信息:" + Arrays.toString(Personhash) +
'}';
}
}