1.代码Package1(包名)
Demo类(主函数)
package Package1;
public class Demo {
public static void main(String[] args) throws Exception {
new MainManager().mainMenu();
}
}
DataBase类(数据类)
package Package1;
public class DataBase {
public static int sIndex = 0;
public static Student[] slist = new Student[50];
public static Teacher[] tlist = new Teacher[2];
public static int start = 0;
public static Friend[] flist = new Friend[20];static {
tlist[0] = new Teacher("admin", "123456");
tlist[1] = new Teacher("miui", "123");
slist[sIndex++] = new Student("路飞", "1234", "01", "男", "10086");
slist[sIndex++] = new Student("索隆", "1234", "02", "男", "20086");
slist[sIndex++] = new Student("娜美", "1234", "03", "女", "30086");
slist[sIndex++] = new Student("娜娜", "1234", "04", "女", "40086");
slist[sIndex++] = new Student("阿索", "1234", "05", "男", "50086");
slist[sIndex++] = new Student("罗宾", "1234", "06", "女", "60086");
}}
Person类
package Package1;
public class Person {
public String name;
public String pwd;public Person() {
}
public Person(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
public Person(String name) {
this.name = name;
}}
Student类(学生类)
package Package1;
public class Student extends Person {
public String ID;
public String sex;
public String tel;public Student() {
}
public Student(String name, String pwd, String ID, String sex, String tel) {
super(name, pwd);
this.ID = ID;
this.sex = sex;
this.tel = tel;
}
public Student(String name, String ID, String sex) {
super(name);
this.ID = ID;
this.sex = sex;
}}
Teacher类(教师类)
package Package1;
public class Teacher extends Person {
public Teacher() {
}public Teacher(String name, String pwd) {
super(name, pwd);
}}
Friend类(学生朋友类)
package Package1;
public class Friend extends Student {
public String myid;
public String fid;
public String bname;public Friend() { }
public Friend(String myid, String fid, String bname) {
this.myid = myid;
this.fid = fid;
this.bname = bname;
}}
MainManager类(主菜单)
使用了Tools工具类的菜单生成器
package Package1;
import static Package1.Tools.*;
public class MainManager {
public void mainMenu() throws Exception {
int data = -1;
while (true) {
int menu = factoryMenu(“主菜单”, “1、老师登录”, “2、学生登录”);
switch (menu) {
case 1:
new StudentManager().stuJudge();
break;
case 2:
new FriendManager().teaJudge();
break;
default:
data = 1;
break;
}
if (data == 1) break; //输入除选项1,2外的其他数字键键退出程序
if (!getPan(“是否继续进行主菜单y/n”)) break;
}
pl(“程序运行结束,欢迎下次使用!”);
}
}
StudentManager类(教师管理类)
package Package1;
import java.io.FileWriter;
import static Package1.DataBase.;
import static Package1.Tools.;
public class StudentManager {
public void stuJudge() throws Exception {
int data = -1;
for (int i = 0; i < 3; i++) {
String name = getString(“请输入用户名:”);
String pwd = getString(“请输入密码:”);
for (int j = 0; j < 2; j++) {
if (tlist[j].name.equals(name) && tlist[j].pwd.equals(pwd)) {
pl(“登录成功!”);
mainMenu();
data = 1;
}
}
if (data == 1 ) break; //用来跳出返回主菜单时的输入用户密码的for循环
pl(“用户名或者密码错误请重新输入”);
}
}public void mainMenu() throws Exception {
int data = -1;
while (true) {
int menu = factoryMenu("学生管理界面", "1、添加学生信息", "2、修改学生信息", "3、删除学生信息",
"4、查询学生信息", "5、保存学生信息");
switch (menu) {
case 1:
Tadd();
break;
case 2:
Trevise();
break;
case 3:
Tdelete();
break;
case 4:
Tselect();
break;
case 5:
saveStuInfo();
break;
default:
data = 1;
break;
}
if (data == 1) break; //输入除选项1,2,3,4外的其他数字键键退出学生管理界面
if (!getPan("是否要继续进行学生管理界面y/n")) break;
}
}
public static void Tadd() {
while (true) {
Student stu = new Student();
stu.name = getString("姓名");
stu.pwd = getString("密码");
stu.ID = getString("ID");
stu.sex = getString("性别");
stu.tel = getString("电话");
slist[sIndex++] = stu;
if (!getPan("是否要继续输入学生信息y/n")) break;
}
}
public static void Tselect() {
int data = -1;
while (true) {
int menu = factoryMenu("学生信息查询界面", "1、所有学生信息", "2、学号查询信息", "3、姓名查询信息"
, "4、性别查询信息");
switch (menu) {
case 1:
selectAll();
break;
case 2:
selectById();
break;
case 3:
selectByName();
break;
case 4:
selectBySex();
break;
default:
data = 1;
break;
}
if (data == 1) break; //输入除选项1,2,3,4外的其他数字键键退出查询界面
if (!getPan("是否继续查询界面y/n")) break;
}
}
public static void Tdelete() {
selectAll();
String sc = getString("请输入要删除者的姓名");
for (int i = 0; i < sIndex; i++) {
if (slist[i].name.equals(sc)) {
for (int j = i; j < sIndex - 1; j++) slist[j] = slist[j + 1];
sIndex = sIndex - 1;
pl("删除成功!");
selectAll();
break;
}
}
}
public static void Trevise() {
selectAll();
String sc = getString("请输入要修改者的姓名");
for (int i = 0; i < sIndex; i++) {
if (slist[i].name.equals(sc)) {
Student stu = new Student();
stu.name = getString("请输入修改后的姓名:");
stu.pwd = getString("请输入修改后的密码:");
stu.ID = getString("请输入修改后的ID:");
stu.sex = getString("请输入修改后的性别:");
stu.tel = getString("请输入修改后的电话号码");
slist[i] = stu;
pl("修改信息成功!");
selectAll();
break;
}
}
}
public static void selectAll() {
pl("\t姓名\t电话\t性别\tID");
for (int i = 0; i < sIndex; i++) {
pl(1 + i + "\t" + slist[i].name + "\t" + slist[i].tel + "\t" + slist[i].sex + "\t" + slist[i].ID);
}
}
public static void selectById() {
String sc = getString("请输入大概的学号");
pl("学号\t姓名\t密码\t电话\t性别");
for (int i = 0; i < sIndex; i++) {
if (slist[i].ID.indexOf(sc) >= 0)
pl(slist[i].ID + "\t" + slist[i].name + "\t" + slist[i].pwd + "\t" + slist[i].tel + "\t" + slist[i].sex);
}
}
public static void selectByName() {
String sc = getString("请输入大概的姓名");
pl("学号\t姓名\t密码\t电话\t性别");
for (int i = 0; i < sIndex; i++) {
if (slist[i].name.indexOf(sc) >= 0)
pl(slist[i].ID + "\t" + slist[i].name + "\t" + slist[i].pwd + "\t" + slist[i].tel + "\t" + slist[i].sex);
}
}
public static void selectBySex() {
String sc = getString("请输入性别");
pl("学号\t姓名\t密码\t电话\t性别");
for (int i = 0; i < sIndex; i++) {
if (slist[i].sex.indexOf(sc) >= 0)
pl(slist[i].ID + "\t" + slist[i].name + "\t" + slist[i].pwd + "\t" + slist[i].tel + "\t" + slist[i].sex);
}
}
public static void saveStuInfo() throws Exception {
FileWriter fileWriter = new FileWriter("D:\\Javaone\\StudentInfo.txt");
for (int i = 0; i < sIndex; i++) {
fileWriter.write(slist[i].name + "\t" + slist[i].ID + "\t" + slist[i].sex + "\t" + slist[i].tel + "\t" + slist[i].pwd + "\n");
}
fileWriter.close();
}}
FriendManager类(学生管理类)
package Package1;
import static Package1.DataBase.;
import static Package1.StudentManager.selectAll;
import static Package1.Tools.;
public class FriendManager {
public String myid;
int data = -1;
String name;public void teaJudge() {
for (int i = 0; i < 3; i++) {
name = getString("请输入用户名:");
String pwd = getString("请输入密码:");
for (int j = 0; j < sIndex; j++) {
if (slist[j].name.equals(name) && slist[j].pwd.equals(pwd)) {
pl("登录成功!");
mainMenu();
data = 1;
}
}
if (data == 1) break; //用来跳出返回主菜单时的输入用户密码的for循环
pl("用户名或者密码错误请重新输入");
}
}
public void mainMenu() {
int data = -1;
while (true) {
int menu = factoryMenu("学生个人中心界面", "1、我的个人信息", "2、修改个人密码", "3、查询同学信息",
"4、添加好友信息", "5、查询好友信息", "6、删除好友信息");
switch (menu) {
case 1:
stuInformation();
break;
case 2:
sturevise();
break;
case 3:
selectAll();
break;
case 4:
stuadd();
break;
case 5:
friSelect();
break;
case 6:
friDelete();
break;
default:
data = 1;
break;
}
if (data == 1) break; //输入除选项1,2,3,4,5,6外的其他数字键键退出学生个人中心界面
if (!getPan("是否要继续进行学生个人中心界面y/n")) break;
}
}
public void stuInformation() {
for (int i = 0; i < sIndex; i++) {
if (slist[i].name.equals(name)) {
pl("\t姓名\t电话\t性别\tID");
pl(1 + i + "\t" + slist[i].name + "\t" + slist[i].tel + "\t" + slist[i].sex + "\t" + slist[i].ID);
break;
}
}
}
public void sturevise() {
String sc = getString("请输入原来的密码:");
for (int i = 0; i < sIndex; i++) {
if (slist[i].pwd.equals(sc)) {
slist[i].pwd = getString("请输入修改后的密码:");
pl("新密码为" + "\t" + slist[i].pwd);
break;
}
}
}
public void stuadd() {
pl("\t姓名\t电话\t性别\tID");
int scount=0,fcount;
boolean tag=true;
while(scount<sIndex){
for (fcount=0;fcount<start;fcount++){
if(slist[scount].name.equals(flist[fcount].name))
tag=false;
}
if (tag){
pl(slist[scount].name + "\t" + slist[scount].tel + "\t" + slist[scount].sex + "\t" + slist[scount].ID);
}
scount++;
tag=true;
}
String frid = getString("请输入要添加的好友ID:");
int findex = getStudentIndexById(frid);
if(findex >=0){
String bname = getString("请输入好友备注");
Friend info = new Friend();
info.myid = myid;
info.fid = frid;
info.bname= bname;
info.name=slist[findex].name;
info.sex=slist[findex].sex;
info.tel=slist[findex].tel;
flist[start++]=info;
}else {
System.out.println("没有找到");
}
}
public int getStudentIndexById(String id){
int i;
for(i = 0; i < sIndex ; i++){
if(id.equals(slist[i].ID)){
return i;
}
}
return -1;
}
public void friSelect() {
int data = -1;
while (true) {
int menu = factoryMenu("好友查询界面", "1、所有好友信息", "2、学号查询信息", "3、性别查询信息");
switch (menu) {
case 1:
selectFriendAll();
break;
case 2:
selectFriendId();
break;
case 3:
selectFriendSex();
break;
default:
data = 1;
break;
}
if (data == 1) break; //输入除选项1,2,3外的其他数字键键退出好友查询界面
if (!getPan("是否继续进行好友查询界面y/n")) break;
}
}
public void friDelete() {
selectFriendAll();
String sc = getString("请输入要删除好友的ID:");
for (int i = 0; i < start; i++) {
if (flist[i].fid.equals(sc)) {
for (int j = i; j < start - 1; j++) {
flist[j] = flist[j + 1];
start = start - 1;
pl("删除成功");
selectFriendAll();
break;
}
}
}
}
public void selectFriendAll() {
pl("好友id\t好友备注\t好友姓名\t好友电话\t好友性别");
for(int i = 0;i < start; i++) {
if (flist[i].myid==myid) {
pl(flist[i].fid+"\t"+flist[i].bname+"\t"+flist[i].name+"\t"+flist[i].tel+"\t"+flist[i].sex);
}
}
}
public static void selectFriendId() {
String sc = getString("请输入大概的ID:");
pl("ID\t姓名\t备注\t性别");
for (int i = 0; i < start; i++) {
if (flist[i].fid.indexOf(sc) >= 0)
pl(flist[i].fid + "\t" + flist[i].name + "\t" + flist[i].bname + "\t" + flist[i].sex);
}
}
public static void selectFriendSex() {
String sc = getString("请输入性别:");
pl("ID\t姓名\t备注\t性别");
for (int i = 0; i < start; i++) {
if (flist[i].sex.indexOf(sc) >= 0)
pl(flist[i].fid + "\t" + flist[i].name + "\t" + flist[i].bname +"\t" + flist[i].sex);
}
}}
Tools类(工具类)
package Package1;
import java.util.Scanner;
public class Tools {
static Scanner input = new Scanner(System.in);public static void pl(Object msg) {
System.out.println(msg);
}
public static String getString(String msg) {
System.out.println(msg);
return input.next();
}
public static int getInt(String msg) {
System.out.println(msg);
return input.nextInt();
}
public static Boolean getPan(String msg) {
String isC = getString(msg);
return isC.equalsIgnoreCase("y");
}
public static int factoryMenu(String ... strs){
pl("------------------------------------------------");
pl(" | " + strs[0] + " | ");
pl("");
int i=0;
StringBuffer sb=new StringBuffer("请选者功能菜单<");
for (i = 1; i < strs.length; i++) {
pl(" % % % % % % % % " + strs[i] + " % % % % % % % %");
sb.append(i+",");
}
sb.delete(sb.length()-1,sb.length());
sb.append(">:");
pl("------------------------------------------------");
System.out.print(sb);
return new Scanner(System.in).nextInt();
}}
2.运行结果图
2.1主菜单
2.2教师登录(学生管理界面)
2.3教师查询学生信息界面
2.4学生登录(学生管理界面)
2.5学生查询朋友信息界面