⒈问题描述
住宿学生信息包括:学号、姓名、性别、用电量、用水量等信息。 教工信
息包括教工号、姓名、性别、用电量、用水量等信息。
⒉功能要求
①添加功能:程序能够添加不同学生和教工的记录,提供选择界面供用户选择所要添
加的类别,要求编号(学生用 12 位数字的学号作为编号,教师用 6 位数字的教工号作为编
号)要唯一,如果添加了重复编号的记录时,则提示数据添加重复并取消添加;
②查询功能:可根据姓名、学号(教工号)信息对已添加的学生或教工记录进行查
询,如果未找到,给出相应的提示信息,如果找到,则显示相应的记录信息;
③显示功能:可显示当前系统中所有学生和教工的记录,每条记录占据一行;
④编辑功能:可根据查询结果对相应的记录进行修改,修改时注意编号的唯一性;
⑤删除功能:主要实现对已添加的学生或教工记录进行删除。如果当前系统中没有相
应的记录,则提示“记录为空!”并返回操作;
⑥统计功能:能根据多种参数进行统计、归类。能统计学生和教工的用水用电量、所
要交纳的电费和水费、未交纳水电费的人员信息;能按照用电量从高到底的顺序对每一条
记录进行排序,并将结果写入副本文本文档中(水费:3.10 元/𝑚3;电费:0.588 元/度)
⑦保存功能:可将当前系统中各类记录存入文本文档(.txt)中,每一条记录占一
行。
⑧读取功能:可将保存在文件中的信息读入到当前系统中,供用户进行使用;
⑨参数校验功能:向系统录入或者修改学生或者教工记录时,应对输入的参数做校
验。例如,学生学号字段值必须位 12 位数字,教工号为 6 为数字,且不与系统中已存在的
记录重复,系统提示录入学号时,输入任何与数字无关的字符,或者不为连续的 12 位数
字,都是非法的!姓名字段值必须为汉字;性别字段值只能为“男”或则“女“;电费与水费合法字段值必须为正数,且是精确到十分位的两位小
数。若输入的参数不合法,系统应该给出提示,引导操作人员输入合法值,若通过校验,才可以将值写入。
整个项目用到的包和文件
datas
包内放入了一个Data类主要是创建一个容器,用于存放数据
package wu.txt.datas;
import wu.txt.human.Human;
import java.util.HashMap;
import java.util.Map;
public class Data {
//加public为了别的类使用
//static很重要
public Map<String, Human> map=new HashMap<String, Human>();
public Human getHuman(String id){
return map.get(id);
}
public void setHuman(String id, Human human){
map.put(id, human);
}
public void delete(String id){
map.remove(id);
}
public Boolean ccontain(String id){
return map.containsKey(id);
}
}
human
包内一个类Human类似于Javabean,主要是给每一条数据进行包装,方便管理
package wu.txt.human;
import java.io.Serializable;
public class Human implements Serializable {
private String name;
private String id;
private String sex;
private int age;
private double water;
private double electricity;
private String fee;
public String getFee() {
return fee;
}
public void setFee(String fee) {
this.fee = fee;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWater() {
return water;
}
public void setWater(double water) {
this.water = water;
}
public double getElectricity() {
return electricity;
}
public void setElectricity(double electricity) {
this.electricity = electricity;
}
public Human(String name, String id, String sex, int age, double water, double electricity, String fee) {
this.name = name;
this.id = id;
this.sex = sex;
this.age = age;
this.water = water;
this.electricity = electricity;
this.fee=fee;
}
@Override
public String toString(){
return name+","+id+","+sex+","+age+","+water+","+electricity+","+fee;
}
}
operation
Operation接口:下面所有类继承这个接口
package wu.txt.operation;
import wu.txt.datas.*;
import java.io.FileNotFoundException;
import java.io.IOException;
public interface Operation {
void work(Data data) throws IOException;
}
Add类:添加功能
package wu.txt.operation;
import wu.txt.datas.Data;
import wu.txt.human.Human;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Add implements Operation {
@Override
public void work(Data data) throws IOException {
Util u=new Util();
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你的姓名");
String name=scanner.nextLine();
System.out.println("请输入你的编号");
String id=scanner.nextLine();
id=u.idUtil(id,scanner);
System.out.println("请输入你的性别");
String sex=scanner.nextLine();
sex=u.sexUtil(sex,scanner);
System.out.println("请输入你的年龄");
int age=scanner.nextInt();
age=u.ageUtil(age,scanner);
System.out.println("请输入你的水量");
double water=scanner.nextDouble();
water=u.waterUtil(water,scanner);
System.out.println("请输入你的电量");
double electricity=scanner.nextDouble();
electricity=u.electricityUtil(electricity,scanner);
System.out.println("是否缴纳水电费");
String e=scanner.nextLine();
String fee=scanner.nextLine();
fee=u.feeUtil(fee,scanner);
//开始添加
if (data.ccontain(id)){
System.out.println("该数据已经存在,添加失败");
}else {
Human human =new Human(name,id,sex,age,water,electricity,fee);
//读入
u.ioObjectRead(data);
data.setHuman(id, human);
//写入
u.ioObjectWrite(data);
u.ioDelectAndChangeAndAdd(data);
System.out.println("添加成功");
}
}
}
All类:显示所有功能
package wu.txt.operation;
import wu.txt.datas.Data;
import wu.txt.operation.Util;
import java.io.IOException;
public class All implements Operation {
public void work(Data data) {
System.out.println("所有信息如下");
Util u=new Util();
try {
u.ioObjectRead(data);
}catch (IOException e) {
}
for (String key: data.map.keySet()){
System.out.println(data.getHuman(key));
}
}
}
Change类:改变信息功能
package wu.txt.operation;
import wu.txt.datas.Data;
import wu.txt.human.Human;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Change implements Operation{
int i=0;
private Boolean flag=true;
@Override
public void work(Data data) throws IOException {
Util u=new Util();
//读入
u.ioObjectRead(data);
Scanner scanner=new Scanner(System.in);
while (flag) {
System.out.println("请输入需要修改的人的编号");
String id = scanner.nextLine();
id=u.idUtil(id,scanner);
if (data.ccontain(id)) {
System.out.println("请输入你的姓名");
String name=scanner.nextLine();
System.out.println("请输入你的性别");
String sex=scanner.nextLine();
sex=u.sexUtil(sex,scanner);
System.out.println("请输入你的年龄");
int age=scanner.nextInt();
age=u.ageUtil(age,scanner);
System.out.println("请输入你的水量");
double water=scanner.nextDouble();
water=u.waterUtil(water,scanner);
System.out.println("请输入你的电量");
double electricity=scanner.nextDouble();
electricity=u.electricityUtil(electricity,scanner);
System.out.println("是否缴纳水电费");
String e=scanner.nextLine();
String fee=scanner.nextLine();
fee=u.feeUtil(fee,scanner);
Human human =new Human(name,id,sex,age,water,electricity,fee);
//读
try {
u.ioObjectRead(data);
} catch (IOException ioException) {
ioException.printStackTrace();
}
data.setHuman(id, human);
//写
try {
u.ioObjectWrite(data);
} catch (IOException ioException) {
ioException.printStackTrace();
}
u.ioDelectAndChangeAndAdd(data);
System.out.println("修改成功");
flag=false;
} else {
System.out.println("输入错误,不存在这个编号,重新输入");
}
}
}
}
Delect类:删除信息功能
package wu.txt.operation;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import wu.txt.datas.Data;
public class Delect implements Operation{
@Override
public void work(Data data) throws FileNotFoundException {
Util u=new Util();
Scanner scanner=new Scanner(System.in);
while (true){
System.out.println("请输入你要删除人的编号");
String id=scanner.nextLine();
id=u.idUtil(id,scanner);
if (data.ccontain(id)){
try {//读
u.ioObjectRead(data);
} catch (IOException e) {
e.printStackTrace();
}
data.delete(id);
//写
try {
u.ioObjectWrite(data);
} catch (IOException e) {
e.printStackTrace();
}
u.ioDelectAndChangeAndAdd(data);
System.out.println("删除成功");
break;
}else {
System.out.println("输入编号有误,该编码不存在,重新输入");
}
}
}
}
Find类:查询信息功能
package wu.txt.operation;
import wu.txt.datas.Data;
import wu.txt.human.Human;
import java.io.IOException;
import java.util.Scanner;
public class Find implements Operation {
@Override
public void work(Data data) throws IOException {
Util u=new Util();
Scanner scanner=new Scanner(System.in);
//读入
u.ioObjectRead(data);
while (true){
System.out.println("输入你要找的人的编号");
String id=scanner.nextLine();
id=u.idUtil(id,scanner);
if (data.ccontain(id)){
Human human =data.getHuman(id);
System.out.println(human);
break;
}else {
System.out.println("输入有误,没有此人,重新输入");
}
}
}
}
WaterAndElectricity类:水电费信息功能
package wu.txt.operation;
import wu.txt.datas.Data;
import wu.txt.human.Human;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class WaterAndElectricity implements Operation {
private double waterFee=3.10;
private double electricity=0.588;
@Override
public void work(Data data) throws IOException {
Util u=new Util();
//读入
u.ioObjectRead(data);
Scanner scanner=new Scanner(System.in);
while (true){
System.out.println("输入你要找的人的编号");
String id=scanner.nextLine();
id=u.idUtil(id,scanner);
if (data.ccontain(id)){
Human human =data.getHuman(id);
DecimalFormat df=new DecimalFormat("#.00");
System.out.println("水费:3.10元/m^3,电费:0.588元/度");
String a=df.format(human.getWater()*3.10);
String b=df.format(human.getElectricity()*0.588);
System.out.println(human.getName()+",水费:"+a+",电费:"+b);
if (human.getFee().equals("是")){
System.out.println("你已经缴费");
}else {
System.out.println("你现在还没有缴费,请及时缴费");
}
break;
}else {
System.out.println("输入有误,没有此人,重新输入");
}
}
}
}
Util类:工具类
package wu.txt.operation;
import wu.txt.datas.Data;
import wu.txt.human.Human;
import java.io.*;
import java.util.Map;
import java.util.Scanner;
public class Util {
public String idUtil(String id, Scanner scanner) {
while (true){
if (((id.length()!=12)&&(id.length()!=6))||id.equals("000000")){
System.out.println("输入有误,学生的编号为12位,老师的编号为6位,000000不能操作,重新输入");
id=scanner.nextLine();
}else {
break;
}
}
return id;
}
public String sexUtil(String sex,Scanner scanner){
while (true){
if (sex.equalsIgnoreCase("男")||sex.equalsIgnoreCase("女")){
break;
}else {
System.out.println("输入有误,个人认为只有男和女,没有变态,重新输入");
sex=scanner.nextLine();
}
}
return sex;
}
public int ageUtil(int age,Scanner scanner){
while (true){
if (age<0||age>120){
System.out.println("输入有误,年龄应该大于0小于120,重新输入");
age=scanner.nextInt();
}else {
break;
}
}
return age;
}
public double waterUtil(double water,Scanner scanner){
while (true){
if (water<0){
System.out.println("输入有误,不能小于0,重新输入");
water=scanner.nextDouble();
}else {
break;
}
}
return water;
}
public double electricityUtil(double electricity,Scanner scanner){
while (true){
if (electricity<0){
System.out.println("输入有误,不能小于0,重新输入");
electricity=scanner.nextDouble();
}else {
break;
}
}
return electricity;
}
public String feeUtil(String fee,Scanner scanner){
while (true){
if (!fee.equals("是")&&!fee.equals("否")){
System.out.println("输入有误,内容必须为是或否,请重新输入");
fee=scanner.nextLine();
}else {
break;
}
}
return fee;
}
public void ioDelectAndChangeAndAdd(Data data) throws FileNotFoundException {
File file1=new File("C:/Users/wu/Desktop/信息.txt");
if (file1.exists()){
file1.delete();
}
File file2=new File("C:/Users/wu/Desktop/老师.txt");
if (file2.exists()){
file2.delete();
}
PrintStream ps1=new PrintStream(new BufferedOutputStream(new FileOutputStream("C:/Users/wu/Desktop/信息.txt")),true);
PrintStream ps2=new PrintStream(new BufferedOutputStream(new FileOutputStream("C:/Users/wu/Desktop/老师.txt")),true);
for (String key: data.map.keySet()){
if (key.length()==12){
ps1.println(data.getHuman(key));
}else {
ps2.println(data.getHuman(key));
}
}
ps1.close();
ps2.close();
}
public void ioDelectAndChangeAndAdd1(Data data) throws FileNotFoundException {
File file=new File("C:/Users/wu/Desktop/老师.txt");
if (file.exists()){
file.delete();
}
PrintStream ps=new PrintStream(new BufferedOutputStream(new FileOutputStream("C:/Users/wu/Desktop/老师.txt")),true);
for (String key: data.map.keySet()){
if (key.length()==6){
ps.println(data.getHuman(key));
}
}
ps.close();
}
public void ioObjectRead(Data data) throws IOException {
ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream("obj3.ser")));
try {
data.map=(Map<String,Human>)ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
ois.close();
}
}
public void ioObjectWrite(Data data) throws IOException {
ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("obj3.ser")));
oos.writeObject(data.map);
oos.flush();
oos.close();
}
}
User
Users抽象类:第一规范让后面的用户去继承
package wu.txt.user;
import wu.txt.datas.Data;
import wu.txt.operation.Operation;
import java.io.FileNotFoundException;
import java.io.IOException;
public abstract class Users {
protected Operation[] operations;
public abstract int menu();
public abstract void doOperation(int choice,Data data) throws IOException;
}
stu类
package wu.txt.user;
import wu.txt.datas.Data;
import wu.txt.operation.*;
import java.io.*;
import java.util.Scanner;
public class stu extends Users {
public stu(){
super();
this.operations=new Operation[]{
new Add(),new Find(),new All(),new Change(),new Delect(),new WaterAndElectricity()
};
}
@Override
public int menu() {
System.out.println("学生菜单");
System.out.print("0.添加");
System.out.print("1.查询");
System.out.print("2.显示全部");
System.out.print("3.修改");
System.out.print("4.删除");
System.out.print("5.统计水电费");
System.out.print("同学请选择需要的功能");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
@Override
public void doOperation(int choice, Data data) throws IOException {
this.operations[choice].work(data);
}
}
tea类
package wu.txt.user;
import wu.txt.datas.Data;
import wu.txt.operation.*;
import java.io.*;
import java.util.Scanner;
public class tea extends Users{
public tea(){
super();
this.operations=new Operation[]{
new Add(),new Find(),new All(),new Change(),new Delect(),new WaterAndElectricity()
};
}
@Override
public int menu() {
System.out.println("老师菜单");
System.out.print("0.添加");
System.out.print("1.查询");
System.out.print("2.显示全部");
System.out.print("3.修改");
System.out.print("4.删除");
System.out.print("5.统计水电费");
System.out.print("老师请选择需要的功能");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
@Override
public void doOperation(int choice, Data data) throws IOException {
this.operations[choice].work(data);
}
}
main
Main类:整个程序的如口
package wu.txt.main;
import wu.txt.datas.Data;
import wu.txt.user.Users;
import wu.txt.user.stu;
import wu.txt.user.tea;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static Users which (){
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的身份");
System.out.print("===0.学生===");
System.out.print("===1.老师===");
int a = scanner.nextInt();
if (a == 0) {
return new stu();
} else if (a == 1) {
return new tea();
} else {
System.out.println("输入有误,重新输入");
}
}
}
public static void main(String[] args) throws IOException {
Users user=which();
Data data=new Data();
while (true){
int choice= user.menu();
user.doOperation(choice,data);
}
}
}
obj3.ser:这个文件是将Map中的数据存入,然后写出来放入文件 老师.txt和学生.txt
开启画面
本人只是刚刚自学完Java基础,有诸多不足,请大家多多包含。