目录
首先定义一个student类
建立一个Map集合用于存放学生学号以及信息。
?存放学生信息
?添加学生信息
?删除学生信息
改动学生信息
查找学生信息
打印获得的所有学生信息
界面处理(加了个密码)
?主函数
源代码
运行结果?
我们分块来看:
// 学生的信息,学号,姓名,班级
class Student {
private String id;
private String name;
private String age;
private String sex;
private String clazz;
private String sub;
public Student() {
super();
}
public Student(String id, String name, String age, String sex, String clazz, String sub) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.clazz = clazz;
this.sub = sub;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSub() {
return sub;
}
public void setSub(String sub) {
this.sub = sub;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", clazz=" + clazz + ", sub="
+ sub + "]";
}
}
首先定义一个student类
存放学生的基本信息,get方法与set方法以及tostring不予解释?。
建立一个Map集合用于存放学生学号以及信息。
在这里需要提及Map集合知识点:
?Map集合没有继承collection接口,其提供的是key到value的映射。Map集合中不能包含相同的key,每个key只能映射一个value。key还决定了存储对象映射中的存储位置,但不是由key对象本身决定的,而是通过一种散列计数进行处理,产生一个散列码的整数值。散列码通常用作一个偏移量,该偏移量对应分配给映射的内存区域的起始位置,从而确定存储对象在映射中的存储位置。Map结合包括Map接口以及Map接口的所有实现类。
private Map<String, Student> map = new HashMap<String, Student>();
?存放学生信息
public StudentManager() {
this.map.put("01", new Student("01", "张三", "18", "男", "08", "c++"));
this.map.put("02", new Student("02", "李四", "19", "男", "09", "java"));
this.map.put("03", new Student("03", "王五", "18", "男", "10", "python3"));
}
?添加学生信息
判断
public int add(Student student) {
String id = student.getId();// 将获得的学号放进临时变量id中
if (!this.map.containsKey(id)) {// 如果map集合中不包含此学生的id
this.map.put(id, student);// 则将此学生的信息放入
return 1;// 返回true
}
return 0;
}
添加?
public void addByStr(String info) {
String[] infos = info.split("-");
if (infos != null && infos.length == 6) {
String id = infos[0];
String name = infos[1];
String age = infos[2];
String sex = infos[3];
String clazz = infos[4];
String sub = infos[5];
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setSex(sex);
student.setClazz(clazz);
student.setSub(sub);
if (this.add(student) == 1) {
System.out.println("添加学生信息成功");
} else {
System.out.println("学号已存在[" + id + "],添加学生信息失败");
}
} else {
System.out.println("输入信息格式不正确[" + info + "],添加学生信息失败");
}
}
?删除学生信息
判断
public int remove(String id) {
if (this.map.containsKey(id)) {
this.map.remove(id);
return 1;
}
return 0;
}
?删除
public void removeByStr(String id) {
if (this.remove(id) == 1) {
System.out.println("删除学生信息成功");
} else {
System.out.println("学号不存在[" + id + "],删除学生信息失败");
}
}
改动学生信息
判断
public int edit(Student student) {
String id = student.getId();
if (!this.map.containsKey(id)) {
this.map.put(id, student);
return 1;
}
return 0;
}
改动
public void editByStr(String info) {
String[] infos = info.split("-");
if (infos != null && infos.length == 6) {
String id = infos[0];
String name = infos[1];
String age = infos[2];
String sex = infos[3];
String clazz = infos[4];
String sub = infos[5];
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setSex(sex);
student.setClazz(clazz);
student.setSub(sub);
if (this.edit(student) == 1) {
System.out.println("修改学生信息成功");
} else {
System.out.println("学号已存在[" + id + "],修改学生信息失败");
}
} else {
System.out.println("输入信息格式不正确[" + info + "],修改学生信息失败");
}
}
查找学生信息
public List<Student> getAll() {
List<Student> list = new ArrayList<Student>();// 将已经排好序的list放入列表中
Set<String> set = this.map.keySet();
List<String> keyList = new ArrayList<String>();
keyList.addAll(set);
Collections.sort(keyList);
for (String key : keyList) {
// System.out.println(this.map.get(key));
list.add(this.map.get(key));
}
return list;
}
打印获得的所有学生信息
public void printAll() {
List<Student> all = this.getAll();
for (Student student : all) {
System.out.println(student);
}
}
界面处理(加了个密码)
private static final int COMMAND_SHOW = 1;// 用一个变量
private static final int COMMAND_ADD = 2;
private static final int COMMAND_EDIT = 3;
private static final int COMMAND_REMOVE = 4;
private static final int COMMAND_EXIT = 9;
public void printCommand() {
System.out.println(COMMAND_SHOW + "查看所有学生信息");
System.out.println(COMMAND_ADD + "添加学生信息");
System.out.println(COMMAND_EDIT + "修改学生信息");
System.out.println(COMMAND_REMOVE + "删除学生信息");
System.out.println(COMMAND_EXIT + "退出系统");
}
public void start(InputStream in) {
System.out.println("欢迎登陆学生信息管理系统");
Scanner scanner = new Scanner(in);
int command = COMMAND_SHOW;
do {
printCommand();
command = scanner.nextInt();
switch (command) {
case COMMAND_SHOW: {
System.out.println("显示所有学生信息:");
this.printAll();
break;
}
case COMMAND_ADD: {
String info = scanner.next();
this.addByStr(info);
break;
}
case COMMAND_EDIT: {
String info = scanner.next();
this.editByStr(info);
break;
}
case COMMAND_REMOVE: {
String id = scanner.next();
this.removeByStr(id);
break;
}
case COMMAND_EXIT:
break;
default:
break;
}
// scanner.close();
} while (command != COMMAND_EXIT);
}
public static boolean login(String username, String password) {
for (int i = 0; i < 3; i++) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = scanner.nextLine();
System.out.println("请输入密码:");
String pwd = scanner.nextLine();
// 登录判断
if (name.equals(username) && pwd.equals(password)) {
System.out.println("登录成功,欢迎您:" + username);
return true;
} else {
if (2 - i == 0) {
System.out.println("你的登录次数已经用完,如有问题请与管理员联系! 电话 : *************");
} else {
System.out.println("登录失败,你今天还有" + (2 - i) + "次登录的机会!");
}
}
}
return false;
}
?主函数
public static void main(String[] args) {
String username = "god";
String password = "123456";
if (login(username, password) == true) {
StudentManager manager = new StudentManager();
manager.start(System.in);
} else
System.exit(0);
}
源代码
/*collection 接口是层次结构中的根接口,构成collection的单位称为元素。collection接口通常不能直接使用,但该接口提供了增删改查的方法。由于list接口和set接口都继承了collection接口,
* 因此这些方法对list集合和set集合都是通用的.
*/
package ccut.edu.c10;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class StudentManager {
private static final int COMMAND_SHOW = 1;// 用一个变量
private static final int COMMAND_ADD = 2;
private static final int COMMAND_EDIT = 3;
private static final int COMMAND_REMOVE = 4;
private static final int COMMAND_EXIT = 9;
private Map<String, Student> map = new HashMap<String, Student>();
// Map集合没有继承collection接口,其提供的是key到value的映射。Map集合中不能包含相同的key,每个key只能映射一个value。key还决定了存储对象映射中的存储位置,但不是由key对象本身决定的,
// 而是通过一种散列计数进行处理,产生一个散列码的整数值。散列码通常用作一个偏移量,该偏移量对应分配给映射的内存区域的起始位置,从而确定存储对象在映射中的存储位置。Map结合包括Map接口
// 以及Map接口的所有实现类。
public StudentManager() {
this.map.put("01", new Student("01", "张三", "18", "男", "08", "c++"));
this.map.put("02", new Student("02", "李四", "19", "男", "09", "java"));
this.map.put("03", new Student("03", "王五", "18", "男", "10", "python3"));
}
// 增
public int add(Student student) {
String id = student.getId();// 将获得的学号放进临时变量id中
if (!this.map.containsKey(id)) {// 如果map集合中不包含此学生的id
this.map.put(id, student);// 则将此学生的信息放入
return 1;// 返回true
}
return 0;
}
// 删
public int remove(String id) {
if (this.map.containsKey(id)) {
this.map.remove(id);
return 1;
}
return 0;
}
// 改
public int edit(Student student) {
String id = student.getId();
if (!this.map.containsKey(id)) {
this.map.put(id, student);
return 1;
}
return 0;
}
// 获得所有学生的信息
public List<Student> getAll() {
List<Student> list = new ArrayList<Student>();// 将已经排好序的list放入列表中
Set<String> set = this.map.keySet();
List<String> keyList = new ArrayList<String>();
keyList.addAll(set);
Collections.sort(keyList);
for (String key : keyList) {
// System.out.println(this.map.get(key));
list.add(this.map.get(key));
}
return list;
}
public void printAll() {
List<Student> all = this.getAll();
for (Student student : all) {
System.out.println(student);
}
}
public void addByStr(String info) {
String[] infos = info.split("-");
if (infos != null && infos.length == 6) {
String id = infos[0];
String name = infos[1];
String age = infos[2];
String sex = infos[3];
String clazz = infos[4];
String sub = infos[5];
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setSex(sex);
student.setClazz(clazz);
student.setSub(sub);
if (this.add(student) == 1) {
System.out.println("添加学生信息成功");
} else {
System.out.println("学号已存在[" + id + "],添加学生信息失败");
}
} else {
System.out.println("输入信息格式不正确[" + info + "],添加学生信息失败");
}
}
public void editByStr(String info) {
String[] infos = info.split("-");
if (infos != null && infos.length == 6) {
String id = infos[0];
String name = infos[1];
String age = infos[2];
String sex = infos[3];
String clazz = infos[4];
String sub = infos[5];
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setSex(sex);
student.setClazz(clazz);
student.setSub(sub);
if (this.edit(student) == 1) {
System.out.println("修改学生信息成功");
} else {
System.out.println("学号已存在[" + id + "],修改学生信息失败");
}
} else {
System.out.println("输入信息格式不正确[" + info + "],修改学生信息失败");
}
}
public void removeByStr(String id) {
if (this.remove(id) == 1) {
System.out.println("删除学生信息成功");
} else {
System.out.println("学号不存在[" + id + "],删除学生信息失败");
}
}
public void printCommand() {
System.out.println(COMMAND_SHOW + "查看所有学生信息");
System.out.println(COMMAND_ADD + "添加学生信息");
System.out.println(COMMAND_EDIT + "修改学生信息");
System.out.println(COMMAND_REMOVE + "删除学生信息");
System.out.println(COMMAND_EXIT + "退出系统");
}
public void start(InputStream in) {
System.out.println("欢迎登陆学生信息管理系统");
Scanner scanner = new Scanner(in);
int command = COMMAND_SHOW;
do {
printCommand();
command = scanner.nextInt();
switch (command) {
case COMMAND_SHOW: {
System.out.println("显示所有学生信息:");
this.printAll();
break;
}
case COMMAND_ADD: {
String info = scanner.next();
this.addByStr(info);
break;
}
case COMMAND_EDIT: {
String info = scanner.next();
this.editByStr(info);
break;
}
case COMMAND_REMOVE: {
String id = scanner.next();
this.removeByStr(id);
break;
}
case COMMAND_EXIT:
break;
default:
break;
}
// scanner.close();
} while (command != COMMAND_EXIT);
}
public static boolean login(String username, String password) {
for (int i = 0; i < 3; i++) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = scanner.nextLine();
System.out.println("请输入密码:");
String pwd = scanner.nextLine();
// 登录判断
if (name.equals(username) && pwd.equals(password)) {
System.out.println("登录成功,欢迎您:" + username);
return true;
} else {
if (2 - i == 0) {
System.out.println("你的登录次数已经用完,如有问题请与管理员联系! 电话 : *************");
} else {
System.out.println("登录失败,你今天还有" + (2 - i) + "次登录的机会!");
}
}
}
return false;
}
public static void main(String[] args) {
String username = "god";
String password = "123456";
if (login(username, password) == true) {
StudentManager manager = new StudentManager();
manager.start(System.in);
} else
System.exit(0);
}
}
// 学生的信息,学号,姓名,班级
class Student {
private String id;
private String name;
private String age;
private String sex;
private String clazz;
private String sub;
public Student() {
super();
}
public Student(String id, String name, String age, String sex, String clazz, String sub) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.clazz = clazz;
this.sub = sub;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSub() {
return sub;
}
public void setSub(String sub) {
this.sub = sub;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", clazz=" + clazz + ", sub="
+ sub + "]";
}
}
运行结果?
?
?
|