Wuhan University of science and technology - Java object oriented and multithreaded comprehensive experiment - encapsulation, inheritance and polymorphism

Experimental target

A preliminary model of user management module of archives management system is realized. Functions include: login interface of password mechanism; query and modification of ordinary users' own information; addition, deletion and modification of other users' information by administrator users.

Module parsing

Users are divided into: Administrator file Administrator, who is responsible for managing the information of all users; Browser file Browser, who is responsible for uploading and downloading files; Operator file entry, who can download and browse files. The corresponding initial users of the three are kate, rose, jack, and their passwords are 123.

Module structure:

Module implementation:
1) It completes the encapsulation of User properties, and constructs setter() and getter() methods to access the properties;
2) Different role classes Administrator, Operator and Browser inherit from User class;
3) The system automatically calls the showMenu() method of the corresponding role according to the different roles of users;
4) The database is not involved in this experiment, so users of DataProcessing class are used to store user information (data type is Hashtable hash table), and there are a series of methods inside the class to add, delete, query and modify users. Every time the role class needs to operate on the user, the method in the DataProcessing class is called to implement the operation.
5) In this experiment, I/O stream is not involved, so only print statements when operating files.

source code

·DataProcessing

import java.util.*;

public class DataProcessing {

	// Hashtable
	// The storage mode is similar to python's dictionary, which is stored in key value pairs
	static Hashtable<String, User> users;
	// Three initialization users
	static {
		users = new Hashtable<String, User>();
		users.put("rose", new Browser("rose", "123", "browser"));
		users.put("jack", new Operator("jack", "123", "operator"));
		users.put("kate", new Administrator("kate", "123", "administrator"));
	}

	// Find user
	public static User searchUser(String name) {
		if (users.containsKey(name)) {
			return users.get(name);
		}
		return null;
	}

	// Password lookup user
	public static User search(String name, String password) {
		if (users.containsKey(name)) {
			User temp = users.get(name);
			if ((temp.getPassword()).equals(password))
				return temp;
		}
		return null;
	}

	// Get all users
	public static Enumeration<User> getAllUser() {
		Enumeration<User> e = users.elements();
		return e;
	}

	// Update user information
	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
				user = new Browser(name, password, role);
			users.put(name, user);
			return true;
		} else
			return false;
	}

	// Add new users
	public static boolean insert(String name, String password, String role) {
		User user;
		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;
		}
	}

	// delete user
	public static boolean delete(String name) {
		if (users.containsKey(name)) {
			users.remove(name);
			return true;
		} else
			return false;
	}

}

The DataProcessing class is responsible for the management of user information. It should be noted that the usage of the Hashtable structure: for example, put() adds a new key value pair, get() returns the value of the corresponding key, containskey(), remove(), etc., which needs to be familiar with.

·User

public abstract class User {

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

	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 boolean downloadFile(String fileID) {
		System.out.println("Download File... ...");
		System.out.println("Download succeeded!");
		return true;
	}

	public void showFileList() {
		System.out.println("list... ...");
	}

	public boolean changeSelfInfo(String password) {
		if (DataProcessing.update(name, password, role)) {
			this.password = password;
			return true;
		} else
			return false;
	}

	public void exitSystem() {
		System.out.println("System exit, Thank you for using. ! ");
		System.exit(0);
	}

}

·Administrator

import java.util.Map.Entry;
import java.util.Scanner;

public class Administrator extends User {

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

	// delete user
	public void delUser(String input_name) {
		if (DataProcessing.delete(input_name)) {
			System.out.println("Delete successfully!");
		} else {
			System.out.println("Delete failed! User name not found!");
		}
	}

	// Add user
	public void addUser(String input_name, String input_password, String input_role) {
		if (DataProcessing.insert(input_name, input_password, input_role)) {
			System.out.println("Added successfully!");
		} else {
			System.out.println("Delete failed! User name already exists!");
		}
	}

	// List users
	public void listUser() {
		for (Entry<String, User> u : DataProcessing.users.entrySet()) {
			String print_name = u.getValue().getName();
			String print_password = u.getValue().getPassword();
			String print_role = u.getValue().getRole();
			System.out.println("Name:" + print_name + " Password:" + print_password + " Role:" + print_role);
		}
	}

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

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

		// Switch of control interface
		boolean administrator_isopen = true;
		// Record user selection
		String administrator_choice;

		while (administrator_isopen) {

			// Interface display
			System.out.println("=======Welcome to the archivist menu=======");
			System.out.println("           1.Modify user");
			System.out.println("           2.delete user");
			System.out.println("           3.New users");
			System.out.println("           4.List users");
			System.out.println("           5.Download File");
			System.out.println("           6.File list");
			System.out.println("           7.Change Password");
			System.out.println("           8.retreat    Out");
			System.out.println("====================================");
			System.out.print("Please enter options:");
			administrator_choice = scan1.next();

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

				// Enter user information
				System.out.print("Please enter the user name:");
				String input_name = scan2.next();
				System.out.print("Please input a password:");
				String input_password = scan3.next();

				// Password lookup user
				if (DataProcessing.search(input_name, input_password) != null) {

					System.out.print("Please enter your identity:");
					String input_role = scan4.next();

					// Modify user
					if (DataProcessing.update(input_name, input_password, input_role)) {
						System.out.println("Modification succeeded!");
					} else {
						System.out.println("Modification failed! User name not found!");
					}

				} else {
					System.out.println("User name and password do not match!");
				}

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

				System.out.print("Please enter the user name:");
				String input_name = scan2.next();

				// delete user
				this.delUser(input_name);

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

				System.out.print("Please enter the user name:");
				String input_name = scan2.next();
				System.out.print("Please input a password:");
				String input_password = scan3.next();
				System.out.print("Please enter your identity:");
				String input_role = scan4.next();

				// Add user
				this.addUser(input_name, input_password, input_role);

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

				// List users
				System.out.println("User list");
				this.listUser();

			} else if (administrator_choice.equals("5")) {

				System.out.print("Please enter a file name:");
				String filename = scan2.next();

				// Download File
				super.downloadFile(filename);

			} else if (administrator_choice.equals("6")) {

				// List files
				System.out.println("File list");
				super.showFileList();

			} else if (administrator_choice.equals("7")) {

				System.out.print("Please enter a new password:");
				String newpassword = scan2.next();

				// Change Password
				if (this.changeSelfInfo(newpassword)) {
					System.out.println("Modified success!");
				} else {
					System.out.println("Modification failed");
				}

			} else if (administrator_choice.equals("8")) {
				// Interface closure
				administrator_isopen = false;
			} else {
				System.out.println("Wrong input format! Please re-enter!");
			}
		}
	}
}

The option number is of String string type instead of int shape, which is to prevent the type mismatch exception of the input letters.
The method of showMenu() is to display the interface continuously through the while loop + Boolean isopen variable. When the exit option is selected, isopen is set to false to exit the interface.

·Browser

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

		// Switch of control interface
		boolean browser_isopen = true;
		// Record user selection
		String browser_choice;

		while (browser_isopen) {

			// Interface display
			System.out.println("=======Welcome to the file explorer menu=======");
			System.out.println("            1.Download File");
			System.out.println("            2.File list");
			System.out.println("            3.Change Password");
			System.out.println("            4.retreat    Out");
			System.out.println("====================================");
			System.out.print("Please enter options:");
			browser_choice = scan1.next();

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

				System.out.print("Please enter the file number:");
				String fileID = scan2.next();

				// Download File
				super.downloadFile(fileID);

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

				// List files
				System.out.println("File list");
				super.showFileList();

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

				System.out.print("Please enter a new password:");
				String newpassword = scan2.next();

				// Change Password
				if (this.changeSelfInfo(newpassword)) {
					System.out.println("Modified success!");
				} else {
					System.out.println("Modification failed");
				}

			} else if (browser_choice.equals("4")) {
				// Close page
				browser_isopen = false;
			} else {
				System.out.println("Wrong input format! Please re-enter!");
			}

		}
	}
}

· Operator

import java.util.Scanner;

public class Operator extends User {

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

	// Upload files
	@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("Upload files");

		System.out.print("Please enter a file name:");
		String filename = scan1.next();
		System.out.print("Please enter the file number:");
		String fileID = scan2.next();
		System.out.print("Please enter a profile description:");
		String fileDescrption = scan3.next();
		System.out.println("Upload succeeded!");

	}

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

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

		// Switch to control the page
		boolean operator_isopen = true;
		// Record user selection
		String operator_choice;

		while (operator_isopen) {

			// Display page
			System.out.println("=======Welcome to the file entry clerk menu=======");
			System.out.println("            1.Upload files");
			System.out.println("            2.Download File");
			System.out.println("            3.File list");
			System.out.println("            4.Change Password");
			System.out.println("            5.retreat    Out");
			System.out.println("====================================");
			System.out.print("Please enter options:");
			operator_choice = scan1.next();

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

				// Upload files
				this.uploadFile();

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

				System.out.print("Please enter a file name:");
				String filename = scan2.next();

				// Download File
				super.downloadFile(filename);

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

				// List files
				System.out.println("File list");
				super.showFileList();

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

				System.out.print("Please enter a new password:");
				String newpassword = scan2.next();

				// Change Password
				if (this.changeSelfInfo(newpassword)) {
					System.out.println("Modified success!");
				} else {
					System.out.println("Modification failed");
				}

			} else if (operator_choice.equals("5")) {
				// Closing interface
				operator_isopen = false;
			} else {
				System.out.println("Wrong input format! Please re-enter!");
			}
		}
	}
}

·Main

import java.util.Scanner;

public class Main {

	@SuppressWarnings({ "resource" })
	public static void main(String[] args) {

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

		// Opening and closing of control interface
		boolean main_isopen = true;
		// Used to record user selection
		String main_choice;

		while (main_isopen) {

			// Interface display
			System.out.println("=======Welcome to the filing system=======");
			System.out.println("           1.Sign in ");
			System.out.println("           2.Sign out ");
			System.out.println("==============================");
			System.out.print("Please enter options:");
			main_choice = scan1.next();

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

				// Enter user information
				System.out.print("Please enter the user name:");
				String input_name = scan2.next();
				System.out.print("Please input a password:");
				String input_password = scan3.next();

				// Check the correctness of password
				User user = DataProcessing.search(input_name, input_password);
				if (user == null) {
					System.out.println("User name and password do not match!");
				} else {
					// Open the corresponding identity interface
					user.showMenu();
				}

			} else if (main_choice.equals("2")) {
				// Exit situation, close the interface
				main_isopen = false;
			} else {
				// Format input error
				System.out.println("Wrong input format! Please re-enter!");
			}

		}
		System.out.println("System exit, thanks for using!");
	}

}

Written in the end

Statement: the content of this paper comes from the Java programming experiment of Wuhan University of technology in 2019-2020 academic year, which is only for learning reference. If there are any shortcomings and mistakes, please also point out.
Don't copy the code without brains. Many details are not explained in detail and need to be understood by the readers themselves. Programming is practiced. I wish readers to make progress on the way of programming!

Published 1 original article, praised 0, visited 18
Private letter follow

Tags: Java Programming Database Python

Posted on Sun, 19 Jan 2020 06:15:33 -0500 by darkninja_com