知识点:类、接口、Java数组、java.io相关工具类的使用
TeacherTel——教师电话类
public class TeacherTel {// 定义顺序表中的数据元素类型
// 属性定义
private String name;
private int teacherNo;
private String telphone;
private String address;
private String mailbox;
public TeacherTel() {
}
public TeacherTel(int teacherNo,String name,String telphone, String address, String mailbox) {
this.name = name;
this.teacherNo = teacherNo;
this.telphone = telphone;
this.address = address;
this.mailbox = mailbox;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTeacherNo() {
return teacherNo;
}
public void setTeacherNo(int teacherNo) {
this.teacherNo = teacherNo;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMailbox() {
return mailbox;
}
public void setMailbox(String mailbox) {
this.mailbox = mailbox;
}
}
TeacTelOPeration———顺序表操作接口
public interface TeacTelOPeration {// 此接口定义了顺序表的数据操作
int getCounts(); // 获取记录个数
void add(TeacherTel node); // 在顺序表的尾部添加一个教师电话记录
void listAll(); // 遍历顺序表中所有教师电话记录
void search(int index); // 根据教师编号查询记录
void search(String name); // 根据教师姓名查询记录
void delete(int index); // 根据教师编号删除记录
void delete(String name); // 根据教师姓名删除记录
void update(int index); //根据教师编号更改信息
void update(String name) ; //根据教师姓名更改信息
}
ArrTeacTel——顺序表接口实现类(主要)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ArrTeacTel implements TeacTelOPeration {// 实现接口中的操作
private TeacherTel[] teacTel;// 顺序表,存放教师电话信息
private int count = 0;// 记录记数器,当前顺序表中的记录个数
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ArrTeacTel(int initialCapacity) {// 创建指定容量的顺序表
teacTel = new TeacherTel[initialCapacity];
}
public int getCounts() {// 获取教师电话记录个数
return count;
}
// 在顺序表添加一个教师电话信息记录,并且在添加时按教师编号从小到大的顺序插入结点
public void add(TeacherTel node){
if (count == 0) {
//to do..
teacTel[0]=new TeacherTel(node.getTeacherNo(),node.getName(),node.getTelphone(),node.getAddress(),node.getMailbox());
count++;
} else {
int flag = 0;
for (int i = 0; i < count; i++) {
//①如果编号已存在,提示已存在,要求用户重新输入
if (node.getName().equals(teacTel[i].getName()) || node.getTelphone().equals(teacTel[i].getTelphone())
|| node.getTeacherNo()==teacTel[i].getTeacherNo() || node.getMailbox().equals(teacTel[i].getMailbox())){
System.out.println(" ");
System.out.println("对不起,您输入的编号/姓名/电话/邮箱已存在,请重新输入 1 进行添加");
flag=1;
//②编号<原序列中的编号,则插入原序列中,count++
}else if(node.getTeacherNo()<teacTel[i].getTeacherNo()){
flag=1;
for(int j=count;j>i;j--){
teacTel[j]=teacTel[j-1];
}
teacTel[i]=new TeacherTel(node.getTeacherNo(),node.getName(),node.getTelphone(),node.getAddress(),node.getMailbox());
count++;
break;
}
}
//③编号>原~编号,falg=0,直接添加,count++
if (flag == 0) {
teacTel[count]=new TeacherTel(node.getTeacherNo(),node.getName(),node.getTelphone(),node.getAddress(),node.getMailbox());
count++;
}
}
if (count == teacTel.length){System.out.println(" 存储空间已满");}
}
public void listAll() {// 遍历顺序表中所有教师电话记录
if (count == 0)
System.out.println("没有记录!");
for (int i = 0; i < count; i++) {
//to do...
System.out.println("教师编号: "+teacTel[i].getTeacherNo()+" 姓名:"+teacTel[i].getName()+" 电话:"+
teacTel[i].getTelphone()+" 家庭住址:"+teacTel[i].getAddress()+" 邮箱:"+teacTel[i].getMailbox());
}
}
public void search(int teacherNo) { // 根据教师编号查询记录
int flag = 0;
for (int i = 0; i < count; i++) {
//to do...
if(teacTel[i].getTeacherNo()==teacherNo){
flag=1;
System.out.println("电话记录:"+teacTel[i].getTelphone());
}
}
if (flag == 0)
System.out.println("输入的编号无效!");
}
public void search(String name) { // 根据教师姓名查询记录
int flag = 0;
for (int i = 0; i < count; i++) {
//to do...
if(name.equals(teacTel[i].getName())){
flag=1;
System.out.println("电话记录:"+teacTel[i].getTelphone());
}
}
if (flag == 0)
System.out.println("查无此人");
}
public void delete(int teacherNo) { // 根据教师编号删除记录
int flag = 0;
for (int i = 0; i < count; i++) {
if (teacTel[i].getTeacherNo() == teacherNo) {
//to do...
flag=1;
for(int j=i;j<count;j++){
teacTel[j]=teacTel[j+1];
}
count--;
System.out.println("删除成功!");
break;
}
}
if (flag == 0)
System.out.println("查无此人");
}
public void delete(String name) { // 根据教师姓名删除记录
int flag = 0;
for (int i = 0; i < count; i++) {
if (teacTel[i].getName().equals(name)) {
//to do... 将后面的往前移
flag=1;
for(int j=i;j<count;j++){
teacTel[j]=teacTel[j+1];
}
count--;
System.out.println("删除成功!");
break;
}
}
if (flag == 0)
System.out.println("查无此人!");
}
public void update(int teacherNO) {
int flag=0;
for(int i=0;i<count;i++){
if(teacherNO==teacTel[i].getTeacherNo()){
flag=1;
int ff=1;
while(ff==1){
System.out.println("请选择你要修改的内容:");
System.out.println("1 编号\n2 电话号码\n3 家庭住址\n4 邮箱\n0 退出");
int n = 0;
try {
n = Integer.parseInt(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
switch(n){
case 0:
ff=0;
break;
case 1:
System.out.println("原编号为:"+teacTel[i].getTeacherNo()+",请输入修改后的编号:");
int upNum = 0;
try {
upNum = Integer.parseInt(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
teacTel[i].setTeacherNo(upNum);
System.out.println("修改成功");
break;
case 2:
System.out.println("原电话号码为:"+teacTel[i].getTelphone()+",请输入修改后的电话号码");
String upTelPhone= null;
try {
upTelPhone = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
teacTel[i].setTelphone(upTelPhone);
System.out.println("修改成功");
break;
case 3:
System.out.println("原家庭住址为:"+teacTel[i].getAddress()+",请输入修改后的家庭住址");
String upAddress= null;
try {
upAddress = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
teacTel[i].setTelphone(upAddress);
System.out.println("修改成功");
break;
case 4:
System.out.println("原邮箱为:"+teacTel[i].getMailbox()+",请输入修改后的邮箱");
String upMailBox= null;
try {
upMailBox = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
teacTel[i].setTelphone(upMailBox);
System.out.println("修改成功");
break;
}
}
}
}
if(flag==0){
System.out.println("查无此人");
}
}
@Override
public void update(String name) {
int flag=0;
for(int i=0;i<count;i++){
if(name.equals(teacTel[i].getName())){
flag=1;
int ff=1;
while(ff==1){
System.out.println("请选择你要修改的内容:");
System.out.println("1 编号\n2 电话号码\n3 家庭住址\n4 邮箱\n0 退出");
int n = 0;
try {
n = Integer.parseInt(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
switch(n){
case 0:
ff=0;
break;
case 1:
System.out.println("原编号为:"+teacTel[i].getTeacherNo()+",请输入修改后的编号:");
int upNum = 0;
try {
upNum = Integer.parseInt(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
teacTel[i].setTeacherNo(upNum);
System.out.println("修改成功");
break;
case 2:
System.out.println("原电话号码为:"+teacTel[i].getTelphone()+",请输入修改后的电话号码");
String upTelPhone= null;
try {
upTelPhone = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
teacTel[i].setTelphone(upTelPhone);
System.out.println("修改成功");
break;
case 3:
System.out.println("原家庭住址为:"+teacTel[i].getAddress()+",请输入修改后的家庭住址");
String upAddress= null;
try {
upAddress = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
teacTel[i].setTelphone(upAddress);
System.out.println("修改成功");
break;
case 4:
System.out.println("原邮箱为:"+teacTel[i].getMailbox()+",请输入修改后的邮箱");
String upMailBox= null;
try {
upMailBox = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
teacTel[i].setTelphone(upMailBox);
System.out.println("修改成功");
break;
}
}
}
}
if(flag==0){
System.out.println("查无此人");
}
}
}
ArrTeacherTelUser——测试类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ArrTeacherTelUser {// 完成教师电话信息的管理
public static void main(String args[]) throws IOException {
ArrTeacTel teacTel = new ArrTeacTel(20); // 根据实际情况初始化数组的大小
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print("********教师电话管理系统**********\n");
System.out.print("1 增加记录\n");
System.out.print("2 显示所有信息\n");
System.out.print("3 根据教师编号查找记录\n");
System.out.print("4 获取教师电话信息记录个数\n");
System.out.print("5 根据编号删除记录\n");
System.out.print("6 根据教师姓名查找记录\n");
System.out.print("7 根据教师姓名删除记录\n");
// to do...
System.out.println("8 根据教师编号更改信息");
System.out.println("9 根据教师姓名更改信息");
System.out.print("0 退出\n\n");
System.out.print("请输入你的选择:");
int choice = Integer.parseInt(br.readLine());
switch (choice) {
case 0:
System.exit(0);
case 1:
System.out.print("\n请输入教师编号:");
int TeacNum = Integer.parseInt(br.readLine());
System.out.print("请输入姓名:");
String TeacName = br.readLine();
System.out.print("请输入电话号码:");
String TeacTelphone = br.readLine();
System.out.println("请输入家庭住址:");
String TeacAddress = br.readLine();
System.out.println("请输入邮箱:");
String TeacMailBox = br.readLine();
TeacherTel Teacnode = new TeacherTel(TeacNum, TeacName,
TeacTelphone,TeacAddress,TeacMailBox);
teacTel.add(Teacnode);
System.out.println();
break;
case 2:
teacTel.listAll();
System.out.println();
break;
case 3:
System.out.print("\n请输入教师编号:");
int teacherNo = Integer.parseInt(br.readLine());
teacTel.search(teacherNo);
System.out.println();
break;
case 4:
System.out.println("教师电话记录个数为:" + teacTel.getCounts());
break;
case 5:
System.out.println("请输入要删除记录的教师编号");
int teacherId = Integer.parseInt(br.readLine());
teacTel.delete(teacherId);
System.out.println();
break;
case 6:
System.out.println("请输入要查询记录的教师姓名");
String name1 = br.readLine();
teacTel.search(name1);
System.out.println();
break;
case 7:
System.out.println("请输入要删除的教师姓名");
String name = br.readLine();
teacTel.delete(name);
System.out.println();
break;
case 8:
System.out.println("请输入要修改的教师编号");
int upTeacherId = Integer.parseInt(br.readLine());
teacTel.update(upTeacherId);
System.out.println();
break;
case 9:
System.out.println("请输入要修改的教师姓名");
String upName = br.readLine();
teacTel.update(upName);
System.out.println();
break;
}
}
}
}