第一次验收:面向对象与多线程综合实验之控制台输出版本
面向对象与多线程综合实验是一次大项目,总共分为4个部分进行验收,我将分成四个部分展示4个版本的项目工程。希望看到本文章的你,对你有所收获。
文章目录
档案管理系统简介
档案管理系统:用于规范档案文件管理,构建档案资源信息共享服务平台,为用户提供完整的档案管理和网络查询功能。
• 系统是一个基于C/S的GUI应用程序
• 使用Java SE开发
• 综合运用面向对象编程的相关知识
系统环境
系统环境
系统开发环境:JavaSE-12
集成开发工具:Eclipse Java 2019-06
系统功能
以下是本篇文章正文内容,下面案例可供参考
具体实现
MSystem包
Administrator.java
代码如下(示例):
package MSystem;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Scanner;
public class Administrator extends User {
/**
*
*/
private static final long serialVersionUID = 1L;
Administrator(String name, String password, String role) {
super(name,password,role);
// TODO Auto-generated constructor stub
}
public boolean changeUserInfo(String name,String password,String role) throws SQLException{
//写用户信息到存储
if (DataProcessing.updateUser(name, password, role)){
setName(name);
setPassword(password);
setRole(role);
System.out.println("修改成功");
return true;
}else
return false;
}
public boolean delUser(String name) throws SQLException {
if(DataProcessing.deleteUser(name)) {
System.out.println("删除成功");
return true;
}else
return false;
}
public boolean addUser(String name,String password,String role) throws SQLException {
if(DataProcessing.insertUser(name, password, role)) {
System.out.println("新增用户成功");
return true;
}else {
return false;
}
}
public void listUser() {
Enumeration<User> a = null;
try {
a=DataProcessing.getAllUser();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("error!");
}
User user = null;
while(a.hasMoreElements()) {
user=a.nextElement();
System.out.println("用户名为: "+user.getName()+" 密码为: "+user.getPassword()+" 身份为: "+user.getRole());
}
}
transient Scanner scan =new Scanner(System.in);
public void showMenu() {
// TODO Auto-generated method stub
while(true) {
System.out.println("****欢迎来到系统管理员菜单****\t");
System.out.println("\t 1.修改用户");
System.out.println("\t 2.删除用户");
System.out.println("\t 3.新增用户");
System.out.println("\t 4.列出用户");
System.out.println("\t 5.下载文件");
System.out.println("\t 6.文件列表");
System.out.println("\t 7.修改(文件)密码");
System.out.println("\t 8.退 出");
System.out.println("*****************************************");
System.out.println("请选择菜单:");
int a=scan.nextInt();
switch(a) {
case 1:
System.out.println("请输入用户名:");
String name1=scan.next();
System.out.println("请输入密码");
String password=scan.next();
System.out.println("请输入身份");
String role=scan.next();
try {
changeUserInfo(name1,password,role);
DataProcessing.xuliehua();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 2:
System.out.println("请输入用户名:");
String name2=scan.next();
try {
delUser(name2);
DataProcessing.xuliehua();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 3:
System.out.println("请输入用户名");
String name3=scan.next();
System.out.println("请输入密码");
String password3=scan.next();
System.out.println("请输入身份");
String role3=scan.next();
try {
addUser(name3,password3,role3);
DataProcessing.xuliehua();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 4:
listUser();
break;
case 5:
System.out.println("请输入ID:");
String ID=scan.next();
try {
downloadFile(ID);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
break;
case 6:
try {
showFileList();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
break;
case 7:
System.out.println("请输入新密码:");
String password5=scan.next();
try {
changeSelfInfo(password5);
DataProcessing.xuliehua();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}break;
case 8:
exitSystem();
break;
default:
System.out.println("输入的数字有误!");
break;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Browser.java
代码如下(示例):
package MSystem;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
public class Browser extends User {
/**
*
*/
private static final long serialVersionUID = 1L;
Browser(String name,String password,String role) {
super(name, password, role);
// TODO Auto-generated constructor stub
}
transient Scanner scan =new Scanner(System.in);
public void showMenu() {
// TODO Auto-generated method stub
while(true) {
System.out.println("****欢迎进入档案浏览员菜单****");
System.out.println("\t 1.下载文件");
System.out.println("\t 2.文件列表");
System.out.println("\t 3.修改密码");
System.out.println("\t 4.退 出");
System.out.println("*************************************");
System.out.println("请选择菜单:");
int a=scan.nextInt();
switch(a) {
case 1:
System.out.println("请输入ID:");
String ID=scan.next();
try {
downloadFile(ID);
} catch (IOException e1) {
// TODO Auto-generated catch block
System.out.println(e1.getMessage());
} catch (SQLException e1) {
// TODO Auto-generated catch block
System.out.println(e1.getMessage());
}
break;
case 2:
try {
showFileList();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("数据库错误!"+e.getMessage());
}
break;
case 3:
System.out.println("请输入新密码:");
String password5=scan.next();
try {
DataProcessing.xuliehua();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
changeSelfInfo(password5);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}break;
case 4:
exitSystem();
break;
default:
System.out.println("输入的数字有误!");
break;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
DataProcessing.java
package MSystem;
import java.util.Enumeration;
import java.util.Hashtable;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.*;
public class DataProcessing {
public static Hashtable<String, User> users;
public static Hashtable<String, Doc> docs;
static {
users = new Hashtable<String, User>();
users.put("jack", new Operator("jack","123","operator"));
users.put("rose", new Browser("rose","123","browser"));
users.put("kate", new Administrator("kate","123","administrator"));
Init();
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
docs = new Hashtable<String,Doc>();
docs.put("0001",new Doc("0001","jack",timestamp,"Doc Source Java","Doc.java"));
}
public static void Init(){
// connect to database
// update database connection status
// if (Math.random()>0.2)
// connectToDB = true;
// else
// connectToDB = false;
}
public static Doc searchDoc(String ID) throws SQLException {
if (docs.containsKey(ID)) {
Doc temp =docs.get(ID);
return temp;
}
return null;
}
public static Enumeration<Doc> getAllDocs() throws SQLException{
Enumeration<Doc> e = docs.elements();
return e;
}
public static boolean insertDoc(String ID, String creator, Timestamp timestamp, String description, String filename) throws SQLException{
Doc doc;
if (docs.containsKey(ID))
return false;
else{
doc = new Doc(ID,creator,timestamp,description,filename);
docs.put(ID, doc);
return true;
}
}
public static User searchUser(String name) throws SQLException{
// if ( !connectToDB )
// throw new SQLException( "Not Connected to Database" );
// double ranValue=Math.random();
// if (ranValue>0.5)
// throw new SQLException( "Error in excecuting Query" );
if (users.containsKey(name))
return users.get(name);
return null;
}
public static User search(String name, String password) throws SQLException, IOException {
// if ( !connectToDB )
// throw new SQLException( "Not Connected to Database" );
// double ranValue=Math.random();
// if (ranValue>0.5)
// throw new SQLException( "Error in excecuting Query" );
if (users.containsKey(name)) {
User temp =users.get(name);
if ((temp.getPassword()).equals(password))
return temp;
}
return null;
}
public static Enumeration<User> getAllUser() throws SQLException{
// if ( !connectToDB )
// throw new SQLException( "Not Connected to Database" );
//
// double ranValue=Math.random();
// if (ranValue>0.5)
// throw new SQLException( "Error in excecuting Query" );
Enumeration<User> e = users.elements();
return e;
}
public static boolean updateUser(String name, String password, String role) throws SQLException{
User user;
// if ( !connectToDB )
// throw new SQLException( "Not Connected to Database" );
//
// double ranValue=Math.random();
// if (ranValue>0.5)
// throw new SQLException( "Error in excecuting Update" );
if (users.containsKey(name)) {
if (role.equalsIgnoreCase("administrator"))
user = new Administrator(name,password, role);
else if (role.equalsIgnoreCase("operator"))
user = new Operator(name,password, role);
else
user = new Browser(name,password, role);
users.put(name, user);
return true;
}else
return false;
}
public static boolean insertUser(String name, String password, String role) throws SQLException{
User user;
// if ( !connectToDB )
// throw new SQLException( "Not Connected to Database" );
//
// double ranValue=Math.random();
// if (ranValue>0.5)
// throw new SQLException( "Error in excecuting Insert" );
if (users.containsKey(name))
return false;
else{
if (role.equalsIgnoreCase("administrator"))
user = new Administrator(name,password, role);
else if (role.equalsIgnoreCase("operator"))
user = new Operator(name,password, role);
else
user = new Browser(name,password, role);
users.put(name, user);
return true;
}
}
public static boolean deleteUser(String name) throws SQLException{
// if ( !connectToDB )
// throw new SQLException( "Not Connected to Database" );
//
// double ranValue=Math.random();
// if (ranValue>0.5)
// throw new SQLException( "Error in excecuting Delete" );
if (users.containsKey(name)){
users.remove(name);
return true;
}else
return false;
}
public static String getrole(String name) throws SQLException{
if (users.containsKey(name))
{
User role = users.get(name);
return role.getRole();
}
return null;
}
public static void xuliehua() throws IOException {
FileOutputStream user = new FileOutputStream("..\\ManagementSystem-v1\\xuliehua\\user.bin\\");
FileOutputStream doc = new FileOutputStream("..\\ManagementSystem-v1\\xuliehua\\doc.bin\\");
ObjectOutputStream s1 = new ObjectOutputStream(user);
ObjectOutputStream s2 = new ObjectOutputStream(doc);
s1.writeObject(users);
s2.writeObject(docs);
s1.close();
s2.close();
}
public static void main(String[] args) {
}
}
Doc.java
package MSystem;
import java.io.Serializable;
import java.sql.Timestamp;
public class Doc implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String ID;
private String creator;
private Timestamp timestamp;
private String description;
private String filename;
public Doc(String ID, String creator, Timestamp timestamp, String description, String filename) {
super();
this.ID = ID;
this.creator = creator;
this.timestamp = timestamp;
this.description = description;
this.filename=filename;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
}
Main.java
package MSystem;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.SQLException;
import java.util.Scanner;
import MSystem.DataProcessing;
import MSystem.User;
import java.util.Hashtable;
public class Main {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
//反序列化:
FileInputStream fi = new FileInputStream("..\\ManagementSystem-v1\\xuliehua\\user.bin\\");
ObjectInputStream si = new ObjectInputStream(fi);
FileInputStream fi2 = new FileInputStream("..\\ManagementSystem-v1\\xuliehua\\doc.bin\\");
ObjectInputStream si2 = new ObjectInputStream(fi2);
try
{
DataProcessing.users = (Hashtable<String, User>)si.readObject();
} catch (ClassNotFoundException e1)
{
// TODO Auto-generated catch block
System.out.println(e1.getMessage());
}
try
{
DataProcessing.docs = (Hashtable<String, Doc>)si2.readObject();
} catch (ClassNotFoundException e1)
{
// TODO Auto-generated catch block
System.out.println(e1.getMessage());
}
si.close();
si2.close();
// TODO Auto-generated catch block*/
@SuppressWarnings("resource") Scanner scan =new Scanner(System.in);
String tip_system="档案系统";
String tip_memu="请选择菜单:";
String tip_exit="系统退出,谢谢使用!";
String infos="**********欢迎进入"+tip_system+"**********\n\t"+
"\t1.登录\n\t 2.退出\n"+
"************************************";
while(true) {
System.out.println(infos);
System.out.println(tip_memu);
int a=scan.nextInt();
switch(a) {
case 1:
System.out.println("请输入用户名:");
String name = scan.next();
User ad=null;
User br=null;
User op=null;
try {
if(DataProcessing.searchUser(name)!=null) {
System.out.println("请输入密码:");
String password = scan.next();
if(DataProcessing.search(name, password)!=null) {
String role=null;
role=DataProcessing.getrole(name);
if(role.equals("administrator")) {
ad=new Administrator(name, password, role);
ad.showMenu();
}
else if(role.equals("browser")) {
br=new Browser(name,password,role);
br.showMenu();
}
else if(role.equals("operator")) {
op=new Operator(name,password,role);
op.showMenu();
}
}
}
else
System.out.println("请重新输入:");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
case 0:
System.out.println(tip_exit);
System.exit(0);
break;
default:
System.out.println("输入的数字有误!");
break;
}
}
}
}
Operator.java
package MSystem;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Scanner;
public class Operator extends User{
/**
*
*/
private static final long serialVersionUID = 1L;
Operator(String name, String password, String role) {
super(name, password, role);
// TODO Auto-generated constructor stub
}
public boolean uploadFile(String ID,String filename,String description,String creator) throws IOException, SQLException{
//System.out.println("上传成功!");
File file = new File(filename);
String filename1=file.getName();
File file1=new File(uploadpath+filename1);
file1.createNewFile();
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
DataProcessing.insertDoc(ID,creator, timestamp, description, filename1);
BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out= new BufferedOutputStream(new FileOutputStream(file1));
byte[] a=new byte[1024];
while(true) {
int l=in.read(a);
if(l==-1)
break;
out.write(a,0,l);
}
in.close();
out.close();
return true;
}
transient Scanner scan =new Scanner(System.in);
public void showMenu() {
// TODO Auto-generated method stub
while(true) {
System.out.println("****欢迎进入档案录入员菜单****");
System.out.println("\t 1.上传文件");
System.out.println("\t 2.下载文件");
System.out.println("\t 3.文件列表");
System.out.println("\t 4.修改密码");
System.out.println("\t 5.退 出");
System.out.println("********************************************");
System.out.println("请选择菜单:");
int a=scan.nextInt();
switch(a) {
case 1:
System.out.println("请输入文件名:");
String name=scan.next();
System.out.println("请输入ID:");
String ID=scan.next();
System.out.println("请输入文件描述:");
String description=scan.next();
System.out.println("请输入作者名:");
String creator=scan.next();
try {
uploadFile(ID,name, description, creator);
DataProcessing.xuliehua();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
case 2:
System.out.println("请输入ID:");
String ID1=scan.next();
try {
downloadFile(ID1);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage()); }
catch(SQLException e){
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
break;
case 3:
System.out.println("文件列表:");
try {
showFileList();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
break;
case 4:
System.out.println("请输入新密码:");
String password5=scan.next();
try {
DataProcessing.xuliehua();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
changeSelfInfo(password5);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}break;
case 5:
exitSystem();
break;
default:
System.out.println("输入的数字有误!");
break;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
User.java
package MSystem;
import java.sql.SQLException;
import java.util.Enumeration;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
public abstract class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String password;
private String role;
User(String name,String password,String role){
this.name=name;
this.password=password;
this.role=role;
}
String uploadpath="..\\ManagementSystem-v1\\uploadfile";
String downloadpath="..\\ManagementSystem-v1\\downloadfile";
public boolean changeSelfInfo(String password) throws SQLException, FileNotFoundException, IOException{
//写用户信息到存储
if (DataProcessing.updateUser(name, password, role)){
this.password=password;
System.out.println("修改成功");
return true;
}else
return false;
}
public boolean downloadFile(String ID) throws IOException, SQLException{
// double ranValue=Math.random();
// if (ranValue>0.5)
// throw new IOException( "Error in accessing file" );
// System.out.println("下载文件... ...");
Doc doc=DataProcessing.searchDoc(ID);
if(doc==null)
return false;
File file=new File(uploadpath+doc.getFilename());
BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(downloadpath+doc.getFilename()));
byte[] a=new byte[1024];
while(true) {
int l=in.read(a);
if(l==-1)
break;
out.write(a, 0, l);
} in.close();
out.close();
return true;
}
public void showFileList() throws SQLException{
//double ranValue=Math.random();
//if (ranValue>0.5)
//throw new SQLException( "Error in accessing file DB" );
//System.out.println("列表... ...");
Enumeration<Doc> e=DataProcessing.getAllDocs();
Doc doc;
while(e.hasMoreElements()) {
doc=e.nextElement();
System.out.println("name: "+doc.getFilename()+"\t ID: "+doc.getID()+"\t Timestamp: "+doc.getTimestamp()+"\t description: "+doc.getDescription()+"\t creator: "+doc.getCreator());
}
}
public abstract void showMenu();
public void exitSystem(){
System.out.println("系统退出, 谢谢使用 ! ");
System.exit(0);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
总结
本版本采用文件序列化来对数据进行存储,程序结构比较简单明了,后续版本会不断补足完整。
感谢支持。