Java面向对象与多线程综合实验(控制台输出)

1.实验要求

实现一个档案管理系统的用户管理模块的初步模型。

功能包括:密码机制的登录界面;包含三个用户:普通用户(Browser),管理员用户(Administrator),操作员用户(Operator)

普通用户对自身信息的查询、修改,浏览文件列表,并下载需要的文件;

管理员用户对其他用户信息的增添、删除、修改,以及可以浏览文件列表,下载文件。

操作员用户对文件的上传以及下载,修改密码。

Administrator 档案管理员,负责管理所有用户的信息;

Browser 档案浏览员,负责文件的上传与下载

Operator 档案录入员,可进行文件的下载与浏览。

三者对应初始用户分别为 Tom、Rose、Jack,初始密码均为123。

2.代码模块结构

 3.各类结构参考

 

 

 

4.模块具体实现方法

1.完成了用户类 User 各属性的封装,并构造 setter()、getter() 方法实现属性访问;
2.不同的角色类 Administrator、Operator、Browser 继承于用户类 User;
3.通过多态实现不同菜单的展示,根据用户角色的不同,系统自动调用对应角色的 showMenu() 方法;
4.本实验未涉及数据库,故使用 DataProcessing 类的成员 users 存储用户信息 (数据类型为 Hashtable 哈希表),且类内部有一系列方法可对 users 进行增删查改。每次角色类需要对用户进行操作时,调用 DataProcessing 类中的方法实现操作。
5.本实验涉及I/O流,故对文件操作时需要熟悉文件流的输入以及输出。

5.代码实现 

User

package Student2;

import java.sql.Timestamp;
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.util.Scanner;

public abstract class User {

	private String name;//用户姓名
	private String password;//用户密码
	private String role;//用户角色
	
	String serverPath="src/s/";
	String clientPath="D:/";
	
	User()
	{
	
	}

	User(String name, String password, String role) {
		this.name = name;
		this.password = password;
		this.role = role;
	}

	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;
	}

	public abstract void showMenu();


	public void showFileList() {
		System.out.println("... ...文件列表... ...");
		Enumeration<Doc> e=DataProcessing.getAllDoc();
		Doc doc;
		while(e.hasMoreElements())
		{
			doc=e.nextElement();
			System.out.println("ID:"+ ""+doc.getID()+"\t Creator:"+doc.getCreator()+"\t Time:"+(new Timestamp(doc.getTimestamp()))+"\t Filename:"+doc.getFilename());
		    System.out.println("Description:"+doc.getDescription());
		}
	}
	

	public boolean changeSelfInfo(String password) {
		//写用户信息到存储
		if (DataProcessing.update(name, password, role)) {
			this.password = password;
			System.out.println("修改成功!");
			return true;
		} else
			return false;
	}
	
	
	public boolean downloadFile(String filename)
   {
		System. out. println("请输入档案号:");
		String fileid = DataProcessing. scanner. nextLine(). trim();
		
		Doc doc= DataProcessing.searchDoc(fileid);
		if (doc== null)
		{
		 System. out. println("档案号【" + fileid + "】没找到! ");
		 System. out. println("文件下载失败! ");
		 //return;
		}
		
		byte[] buffer = new byte[1024];
		File tempFile = new File(serverPath + doc.getFilename());
		
		String tfilename = tempFile. getName();//暂时名字
		BufferedInputStream infile = null;
		BufferedOutputStream targetfile = null;
		try
	   {
		  infile= new BufferedInputStream(new FileInputStream(tempFile));
		  targetfile = new BufferedOutputStream(new FileOutputStream(clientPath + tfilename));
		  
			  while (true)
			 {
			  int byteRead = infile. read(buffer); // 从文件读数据给字节数组
			  if (byteRead == -1) //在文件尾,无数据可读
			  break;// 退出循环
			  targetfile. write(buffer, 0, byteRead); // 将读到的数据写入目标文件
			 }
			  
			infile. close();
			targetfile. close();
			System.out.println("文件下载成功!");
		   }
			// 向系统数据库中添加文件的基本信息
			
	//		String creator = this. getName();
	//		long timestamp = System. currentTimeMillis();
	//		
	//		upSuccessed = DataProcessing. insertDoc(ID, creator, timestamp, description, filename);
	//	   
		catch (FileNotFoundException e)
	   {
			System. out. println("文件: 【" + doc.getFilename() + "】不存在! ");
			System. out. println("文件下载失败! ");
		 //e. printStackTrace();
		 //System. out. println("文件没有找到! ");
	   }
		catch (IOException e)
		{
		  System. out. println("文件下载过程中数据错误!");
		  System.out.println("文件下载失败!");
		}
//		finally
//		{
//		  if (upSuccessed)
//		     System. out. println("文件上传成功! ");
//		  else
//		     System. out. println("文件上传失败! ");
//		}
		return true;
	}

	//修改用户密码
	public void changeUserPass() //throws IOException
	{
		String password;
		System.out.println("请输入新密码: ");
		password= ((Scanner) DataProcessing.scanner).nextLine();
		@SuppressWarnings("unused")
		boolean chgSuccessed=false;
		try
		{
		    chgSuccessed=DataProcessing.update(name,password,role);
		
		}catch(Exception e)
		{
			System.out.println("修改密码过程中出现系统错误!");
		}
	
		
	}
//退出系统菜单
	public void exitSystem() {
		System.out.println("退出菜单! ");
		System.exit(0);
	}

}

Administrator

package Student2;

import java.io.IOException;
import java.util.Enumeration;

public class Administrator extends User{
      Administrator(String name, String password, String role)
      {
         super(name, password, role);
      }

      public boolean changeUserInfo(String name, String password, String role)
      {
    	  return(DataProcessing. update(name, password, role));
      }

      public boolean delUser(String name) throws IOException
      {
    	  return(DataProcessing. deleteUser(name));
      }

      public boolean addUser(String name, String password, String role) throws IOException
      {
    	  return(DataProcessing. insertUser(name, password, role));
      }

      public void listUser()
      {
    	  Enumeration<User> e = DataProcessing. getAllUser();
    	  User user;

    	  while (e. hasMoreElements())
    	  {
    		  user = e. nextElement();
    		  System. out. println(  "Name:" + user. getName() + "\t Password:" + user. getPassword() + "\t Role:" + user. getRole());
    	  }
      }

	
      
      public void showMenu() 
      {
    	  String tip_system = "系统管理员菜单";
    	  String tip_menu = "请选择菜单: ";
    	  String infos ="****欢迎进入" + tip_system +"****\n\t" + "1.修改用户\n\t" +
    				  "2.删除用户\n\t" + "3.新增用户\n\t" + "4.列出用户\n\t" + "5.下载文件\n\t" + "6.文件列表\n\t" + "7.修改密码\n\t" + "8.退   出\n" + "******************************";
    	  String name, password, role;
     
    	  System. out. println(infos);
    	  System. out. print(tip_menu);
    	  String input = null;
      
    	  while (true)
    	  {
     
    		  input = DataProcessing. scanner. nextLine(). trim();
    		  if (!(input). matches("1|2|3|4|5|6|7|8"))
    		  {
    			  System. err. print(tip_menu);
    		  }
     
    		  else
    		  {
    			  int nextInt= Integer. parseInt(input);
    			  switch(nextInt)
    			  {
    			  case 1:// 修改用户信息
      
    				  //System. out. println("修改用户");
    				  System. out. println("请输入用户名: ");
     
    				  name = DataProcessing. scanner. nextLine();
     
    				  System. out. println("请输入口令: ");
     
    				  password = DataProcessing. scanner. nextLine();
     
    				  System. out. println("请输入角色: ");
     
    				  role = DataProcessing. scanner. nextLine();
     
    				  if (changeUserInfo(name, password, role))
      
    					  System. out. println("修改成功");
      
    				  else
     
    					  System. out. println("修改失败");
     
    				  break;
      
    			  case 2:
    				 
    				 // System. out. println("删除用户");
    				  
    				  System. out. println("请输入用户名: ");
    				  name = DataProcessing. scanner. nextLine();
    				 
    				  try {
						if (delUser(name))
							  System. out. println("删除成功");
						  else
						 
							  System. out. println("删除失败");
					} catch (IOException e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
    				  break;
    				  
    			  case 3:
    				  //System. out. println("新增用户");
    				  System. out. println("请输入用户名: ");
    				  
    				  name = DataProcessing. scanner. nextLine(). trim();
    				  
    				  System. out. println("请输入口令: ");
    				  
    				  password = DataProcessing. scanner. nextLine(). trim();
    				  System. out. println("请输入角色: ");
    				  
    				  role = DataProcessing. scanner. nextLine();
    				 
    				  try {
						if (addUser(name, password, role))
						 
							  System. out. println("新增成功");
						  else
						  
							  System. out. println("新增失败");
					} catch (IOException e) {
						// TODO 自动生成的 catch 块
						//e.printStackTrace();
					}
    				  break;
    				 
    			  case 4:
    				  //System. out. println("列出用户");
    				  listUser();
    				  break;


    			  case 5:
    				  //System. out. println("下载文件");
    				  //System. out. println("请输入档案号: ");
    				 // String docID = DataProcessing. scanner. nextLine(). trim();
    				  
    				  if (downloadFile(input))
    					  System. out. println("下载成功! ");
    				  else
    				  
    					  System. out. println("下载失败! ");
    				  break;
    				 
    			  case 6:
    				 // System. out. println("文件列表");
    				  showFileList();
    				  break;
    				 
    			  case 7:
    				 // System. out. println("修改本人密码");
    				  System. out. println("请输入新口令: ");
    				  
    				  password = DataProcessing. scanner. nextLine();
    				  changeSelfInfo(password);
    				  
    				  break;
    				  
    			  case 8:
    				  return;
    				 
    			  }
    				  
    			  System. out. println(infos);	 
    			  System. out. print(tip_menu);
    				  
    		  }
    				  
    	  }
    				 
      }
    				 
}

Browser 

package Student2;

import java.util.Scanner;

public class Browser extends User {

	public Browser(String name, String password, String role) {
		super(name, password, role);
	}
	@SuppressWarnings("resource")
	public void showMenu() {

		Scanner scan1 = new Scanner(System.in);
		Scanner scan2 = new Scanner(System.in);

		// 控制菜单界面的开关
		boolean browser_isopen = true;
		
		// Browser用户的选择
		String browser_choice;

		while (browser_isopen) {

			// Browser页面菜单
			System.out.println("=======欢迎进入档案浏览员菜单=======");
			System.out.println("            1.下载文件 ");
			System.out.println("            2.文件列表");
			System.out.println("            3.修改密码");
			System.out.println("            4.退出菜单");
			System.out.println("==================================");
			System.out.print("请输入选项:");
			browser_choice = scan1.next();

			if (browser_choice.equals("1")) {

				//System.out.print("请输入档案号: ");
				//String fileID = scan2.next();

				// 下载文件
				super.downloadFile(browser_choice);

			} else if (browser_choice.equals("2")) {

				// 列出文件
				System.out.println("文件列表");
				super.showFileList();

			} else if (browser_choice.equals("3")) {

				System.out.print("请输入新密码:  ");
				String newpassword = scan2.next();

				//修改密码
				if (this.changeSelfInfo(newpassword)) {
					System.out.println("修改成功!");
				} else {
					System.out.println("修改失败!");
				}

			} else if (browser_choice.equals("4")) {
				// 关闭页面
				browser_isopen = false;
			} else {
				System.out.println("输入格式有误!请重新输入!");
			}

		}
	}
}

 Operator

package Student2;

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.OutputStream;
import java.util.Scanner;

public class Operator extends User {
	
	String serverPath="src/s/";
    String clientPath="D:/";


	public Operator(String name, String password, String role) {
		super(name, password, role);
	}

	public void uploadFile()
{
	  System. out. println("请输入源文件名(包含路径) : ");
	  String sourcefile = DataProcessing. scanner. nextLine(). trim();
	  
	  System. out. println("请输入档案号: ");
	  String ID = DataProcessing. scanner. nextLine(). trim();
	  
	  System.out. println("请输入档案描述: ");
	  String description = DataProcessing. scanner. nextLine(). trim();
	  
	  boolean upSuccessed = false;
	  
	  File tempFile = new File(sourcefile. trim());
	  String filename = tempFile. getName();
	  
	  int lastIndex0f = filename. lastIndexOf(".");
	  
	  if (lastIndex0f == -1) 
	  {
	    filename = DataProcessing. generateRandomFilename(); // empty extension
	  }
	  else
	    filename = DataProcessing. generateRandomFilename() + filename. substring(lastIndex0f);
	  
	  try
	 {
	   // 将文件内容上传
	   byte[] buffer = new byte[1024];
	   
	   BufferedInputStream infile= new BufferedInputStream(new FileInputStream(tempFile));
	   BufferedOutputStream targetfile = new BufferedOutputStream(new FileOutputStream(serverPath + filename));
	   
	   while (true)
	   {
		int byteRead = infile. read(buffer); // 从文件读数据给字节数组
		if (byteRead ==-1) // 在文件尾,无数据可读
		    break; // 退出循环
		
		targetfile. write(buffer, 0, byteRead); // 将读到的数据写入目标文件
	   }
	   
		    infile. close();
		    targetfile. close();
		
		// 向系统数据库中添加文件的基本信息
		String creator = this. getName();
		long timestamp = System. currentTimeMillis();
		
		upSuccessed = DataProcessing.insertDoc(ID, creator, timestamp, description, filename);
	}
		catch (FileNotFoundException e)
	{
		    //e. printStackTrace();
		    System. out. println("文件没有找到! ");
	}
		catch (IOException e)
	{
		   System. out. println("文件上传过程中出错! ");
	}
		finally
	{
		if (upSuccessed)
		   System. out. println("文件上传成功! ");
		else
		   System. out. println("文件上传失败! ");
	}
}


	// 上传文件
	//@SuppressWarnings({ "resource", "unused" })
//	public void uploadFile() {
//
//		Scanner scan1 = new Scanner(System.in);
//		Scanner scan2 = new Scanner(System.in);
//		Scanner scan3 = new Scanner(System.in);
//
//		System.out.println("上传文件");
//
//		System.out.print("请输入文件名:");
//		String filename = scan1.next();
//		System.out.print("请输入档案号:");
//		String fileID = scan2.next();
//		System.out.print("请输入档案描述:");
//		String fileDescrption = scan3.next();
//		System.out.println("上传成功!");
//
//	}

	//@SuppressWarnings({ "resource" })
	@SuppressWarnings("resource")
	public void showMenu() {

		Scanner scan1 = new Scanner(System.in);
		Scanner scan2 = new Scanner(System.in);

		// 控制页面的开关
		boolean operator_isopen = true;
		// 记录用户的选择
		String operator_choice;

		while (operator_isopen) {

			// 显示页面
			System.out.println("=======欢迎进入档案录入员菜单=======");
			System.out.println("            1.上传文件");
			System.out.println("            2.下载文件");
			System.out.println("            3.文件列表");
			System.out.println("            4.修改密码");
			System.out.println("            5.退    出");
			System.out.println("====================================");
			System.out.print("请输入选项:");
			operator_choice = scan1.next();

			if (operator_choice.equals("1")) {

				// 上传文件
				this.uploadFile();

			} else if (operator_choice.equals("2")) {

				System.out.print("请输入文件名:");
				String filename = scan2.next();

				// 下载文件
				super.downloadFile(filename);

			} else if (operator_choice.equals("3")) {

				// 列出文件
				System.out.println("文件列表");
				super.showFileList();

			} else if (operator_choice.equals("4")) {

				System.out.print("请输入新密码:");
				String newpassword = scan2.next();

				// 修改密码
				if (this.changeSelfInfo(newpassword)) {
					System.out.println("修改成功!");
				} else {
					System.out.println("修改失败");
				}

			} else if (operator_choice.equals("5")) {
				// 关闭界面
				operator_isopen = false;
			} else {
				System.out.println("输入格式有误!请重新输入!");
			}
		}
	}
}


 

DataProcessing

package Student2;

import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;

//import sun.java2d.opengl.WGLSurfaceData.WGLVSyncOffScreenSurfaceData;


public class DataProcessing {
	static final String USERFILE="src/user.txt";
	static final String DOCFILE="src/file.txt";
	//private static final String DataProcessing = null;
	
	//public
	// 哈希表
	static Hashtable<String,User> users;
	static Hashtable<String,Doc> docs;
	//private static Hashtable<String, Doc> docs;
 	// 三个初始化用户
	
	static Scanner scanner=new Scanner(System.in);
	
//	static {
//		users = new Hashtable<String, User>();
//		users.put("kate", new Browser("kate", "123", "browser"));
//		users.put("jack", new Operator("jack", "123", "operator"));
//		users.put("rose", new Administrator("rose", "123", "administrator"));
//	}
	
//	// 自定义异常
//class DataException extends Exception{
//		private static final long serialVersionUID = 1L;
//	    public DataException(String msg)
//		{
//		  super(msg);
//		}
//	 }

	public static void Init() throws IOException,DataException
	{
		
	   users = new Hashtable<String, User>();//初始化,从user.txt文本文件中提取user信息
	   String name, password, role;
	 
	   BufferedReader brUser = new BufferedReader(new FileReader(USERFILE));
	   
	   while ((name = brUser. readLine()) != null)
	   {
	       password = brUser. readLine();
	       role = brUser. readLine();
	     if (password == null || role == null)//检查用户数据文件是否完整
	     {
	        brUser. close();
	        throw new DataException("数据错误!");
	        
	     }
	     if (role. equals("Operator"))
	          users. put(name, new Operator(name, password, role));
	     else if (role. equals("Browser"))
	          users. put(name, new Browser(name, password, role));
	     else if (role. equals("Administrator"))
	          users. put(name, new Administrator(name, password, role));
	     else
	     {
	          brUser. close();
	          throw  new DataException("数据错误!");
	     }
     }
	brUser. close();
//	
	
	//从文件中提取已经上传的文件
	 docs =new Hashtable<String,Doc>();
	 String idFile,idUser,timestamp,fileDisc,fileName;
	
	 BufferedReader brFile = new BufferedReader(new FileReader(DOCFILE));
	 while ((idFile = brFile. readLine()) != null)
	   {
	       idUser = brFile. readLine();
	       timestamp = brFile. readLine();
	       fileDisc=brFile.readLine();
	       fileName=brFile.readLine();
	    		   
	    		   
	       if (idUser == null || timestamp == null||fileDisc==null||fileName==null)
	       {
	            brFile. close();
	            throw new DataException("系统中档案数据格式错误!");
	       }
	       
	       long ts=Long.parseLong(timestamp);
	       docs.put(idFile, new Doc(idFile,idUser,ts,fileDisc,fileName));
	}
	 brFile.close();
}





	// 查找用户
//	public static User searchUser(String name) {
//		if (users.containsKey(name)) {
//			return users.get(name);
//		}
//		return null;
//	}

	// 密码查找用户
	public static User searchUser(String name, String password) {
		//name =name.toLowerCase();
		if (users.containsKey(name)) {
			User temp = users.get(name);
			if ((temp.getPassword()).equals(password))
			return temp;
		}
		return null;
	}

	public static Doc searchDoc(String ID) {
		
		if (docs.containsKey(ID)) {
			Doc temp = docs.get(ID);
			return temp;
		}
		return null;
	}
//	// 获取所有用户
//	public static Enumeration<User> getAllUser() {
//		Enumeration<User> e = users.elements();
//		return e;
//	}

	// 更新用户信息
	public static boolean update(String name, String password, String role) {
		User user;
		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 if (role.equalsIgnoreCase("browser"))
				user = new Browser(name, password, role);
			else
			{
				System.out.println("用户角色类型输入错误");
				return false;
			}
			users.put(name, user);
			return true;
		} else
			return false;
	}

	// 增添新用户
	public static boolean insertUser(String name, String password, String role) throws IOException {
		User user;
		//name =name.toLowerCase();
		
		if (users.containsKey(name))
		{
			System.out.println("用户同名!");
			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);
			updateUserFile();
			return true;
		}
	}

	public static boolean insertDoc(String ID, String creator, long timestamp,String description ,String filename) throws IOException 
	{
		Doc doc;
		//name =name.toLowerCase();
		
		if (docs.containsKey(ID))
			return false;
		else
		{
			doc=new Doc(ID,creator, timestamp, description , filename);
			docs.put(ID, doc);
			updateDocFlie();
			return true;
		}
	}
	
	//更新用户信息
  public static void updateUserFile() throws IOException
	{
		PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(USERFILE)));
	     	Enumeration<User> e = DataProcessing.getAllUser();
		User user;
		while(e.hasMoreElements())
		{
			user=e.nextElement();
			out.println(user.getName());
			out.println(user.getPassword());
			out.println(user.getRole());
		}
		out.close();
		
	}
  //更新文件信息
	public static void updateDocFlie() throws IOException
	{
		PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(DOCFILE)));
     	Enumeration<Doc> e = DataProcessing.getAllDoc();
	    Doc doc;
	    
	    while(e.hasMoreElements())
	  {
		doc=e.nextElement();
		out.println(doc.getID());
		out.println(doc.getCreator());
		out.println(doc.getTimestamp());
		out.println(doc.getDescription());
		out.println(doc.getFilename());
	  }
	out.close();
		//return false;
		
	}
	// 删除用户
	public static boolean deleteUser(String name) throws IOException {
		//name =name.toLowerCase();
		
		if (users.containsKey(name)) {
			users.remove(name);
			updateUserFile();
			return true;
		} else
			return false;
	}


	
	public static String generateRandomFilename() 
	{
	  String RandomFilename = "";
	  Random rand = new Random();// 生成随机数
	  
	  @SuppressWarnings("unused")
	int random = rand. nextInt();
	  Calendar calCurrent = Calendar. getInstance();
	  
	  int intDay = calCurrent. get(Calendar. DATE);
	  int intMonth = calCurrent. get(Calendar. MONTH) + 1;
	  int intYear = calCurrent. get(Calendar. YEAR);
	  int intHour=calCurrent.get(Calendar.HOUR)+12;
	  int intMinute = calCurrent. get(Calendar. MINUTE);
	  int intSecond=calCurrent.get(Calendar.SECOND);
	  String now = String. valueOf(intYear) + "_" + String. valueOf(intMonth) + "_" + String. valueOf(intDay)+ "_"+String.valueOf(intHour)+"_"+String.valueOf(intMinute)+"_"+String.valueOf(intSecond);
//	  RandomFilename = now + String.valueOf(random > 0 ? random : (-1) * random);
	  RandomFilename=now;
	  return RandomFilename;
	}
	
	
	
// 自定义异常
// class DataException extends Exception{
//	private static final long serialVersionUID = 1L;
//    public DataException(String msg)
//	{
//	  super(msg);
//	}
// }
//获取所有文件
	public static Enumeration<Doc> getAllDoc() {
		Enumeration<Doc> e=docs.elements();
		return e;
	}
//获取所有用户
	public static Enumeration<User> getAllUser() {
		Enumeration<User> e=users.elements();
		return e;
	}

	
}
// 自定义异常
class DataException extends Exception{
	private static final long serialVersionUID = 1L;
    public DataException(String msg)
	{
	  super(msg);
	}
 }

 

Doc

package Student2;

public class Doc {
	private String ID;
	private String creator;
	private long timestamp;
	private String description;
	private String filename;

	public Doc(String ID, String creator, long ts, String description, String filename) {
		this.setID(ID);
		this.setCreator(creator);
		this.setTimestamp(ts);
		this.setDescription(description);
		this.setFilename(filename);
	}

	public String getID() {
		return ID;
	}

	public void setID(String ID) {
		this.ID = ID;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long ts) {
		this.timestamp = ts;
	}

	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;
	}

	public String toString() {
		return "ID:" + this.ID + " Creator:" + this.creator + " Time:" + this.timestamp + " Filename:" + this.filename
				+ " Description: " + this.description;
	}

}

Main

package Student2;

import java.io.IOException;
//import java.util.Scanner;

//import Student.DataProcessing.DataException;

public class Main 
{
  public static void main(String[] args)   {
	try
	{
	    DataProcessing. Init();
	}
	catch (IOException e)
	{
	    System. out. println(e. getMessage());
	    System. out. println("系统非正常退出! ");
	    DataProcessing. scanner. close();
	    System. exit(0);
	}
	catch (DataException e)
	{
	    System. out. println(e. getMessage());
	    System. out. println("系统非正常退出! ");
	    DataProcessing. scanner. close();
	    System. exit(0);
	}
	
	String tip_menu ="请选择菜单: ";
	String tip_exit ="系统退出,谢谢使用! ";
	String infos = "**** 欢迎进入档案管理系统 ****\n\t " + "1.登录\n \t 2.退出\n" +"**************************";
	String name, password;
	System. out. println(infos);
	System. out. print(tip_menu);
	
	String input= null;
	while (true)
	{
	    input = DataProcessing. scanner. nextLine(). trim();
	    if (!(input). matches("1|2"))
	   {
	    System. out. println("选择菜单错误! ");
	    System. out. println(tip_menu);
	   }
	    else
	   {
	       int nextInt = Integer. parseInt(input);
	       switch (nextInt)
	      {
	      case 1:// 登录
	             System. out. print("请输入用户名: ");
	             name = DataProcessing. scanner. nextLine(). trim();
	             
	             System. out. print("请输入口令: ");
	             password = DataProcessing. scanner. nextLine().trim();
	             
	             User user = DataProcessing. searchUser(name, password);
	             
	             if (user == null)
	                System. out. println("用户名口令错误");
	             else
	                user. showMenu();
	             break;
	       case 2:// 退出
	             System. out. println(tip_exit);
	             DataProcessing. scanner. close();
	             System. exit(0);
	      }
	  System. out. println(infos);
 	  System. out. print(tip_menu);
      } 
	}
}
}

	    



6.创建文本文件与用户文件

1.在src中创建一个s文件夹,用于存放来自D盘上传的文件。

2.在src中创建user文本文档,用于存放用户信息,并可以随时更新用户信息。

3. 在src中创建file文本文档,用于存放文件信息,并可以随时更新文件信.

 

7.文件的上传与下载

1.当操作员在D盘创建一个文档(例如st001),其完整名字为st001.txt,只不过电脑省略了.txt,所以操作员上传文件时,必须输入正确名字,例如输入D:\st001.txt,才可以正确运行程序,负责会出错。

2.当操作员上传文件后,文件名字会发生改变,这里我采用时间记名字方法,比如你操作员上传名字为st001.txt的文件,我服务器中src中s文件夹就会接受到这个文件,不过这时名字将取操作员将文件上传的时间,类似于我们拍照片时,照片名字会取拍摄时间一样。

这里我给出这个时间取名字代码

public static String generateRandomFilename() //时间取名字代码实现
	{
	  String RandomFilename = "";
	  Random rand = new Random();// 生成随机数
	  
	  @SuppressWarnings("unused")
	int random = rand. nextInt();
	  Calendar calCurrent = Calendar. getInstance();
	  
	  int intDay = calCurrent. get(Calendar. DATE);
	  int intMonth = calCurrent. get(Calendar. MONTH) + 1;
	  int intYear = calCurrent. get(Calendar. YEAR);
	  int intHour=calCurrent.get(Calendar.HOUR)+12;
	  int intMinute = calCurrent. get(Calendar. MINUTE);
	  int intSecond=calCurrent.get(Calendar.SECOND);
	  String now = String. valueOf(intYear) + "_" + String. valueOf(intMonth) + "_" + String. valueOf(intDay)+ "_"+String.valueOf(intHour)+"_"+String.valueOf(intMinute)+"_"+String.valueOf(intSecond);
//	  RandomFilename = now + String.valueOf(random > 0 ? random : (-1) * random);
	  RandomFilename=now;
	  return RandomFilename;
	}

3.类似的我们要下载文件时,输入正确的档案号后,文件将从服务器serverpath(src\s)下载到客户端clientpath(D:\) 中了,这时文件名字就是你以时间取名字的名字。

8.user和file的内容

 user:

file:其中1701846586254为时间戳,意味从格林尼治开始时间到现在的秒数。

 

武汉理工大学计算机专业 制作不易 请点赞并关注我呀 

 

 

 

  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值