Java SE project closure
Java development program
java se project - development team scheduling software [based on text interface]Look at the effect
Company member status after adding
Team members after adding
Before development
As for why this project is written or implemented, it is because the previous socket chat room project is complex, but to be honest, it is not complex. Most of the code is in interface design. The core is TCP/IP communication. In addition, there is no thread use. You can see it in the simplified version, Only 70 lines can realize the core text chat function
Therefore, both readers and myself think that it is not representative for JAVA SE to end with this project, so next, we will use time to realize the MVC design pattern used by the development team to schedule software and architecture
Development team scheduling software [PROJECT]
Knowledge points involved in the project
Knowledge points involved in this project: Inheritance and polymorphism of classes, value transfer of objects, interfaces, static and final modifiers, use of special classes: wrapper classes, abstract classes, inner classes, exception handling (Development) - not simple try catch, throws
The architecture of the project is mainly based on MVC design mode
The so-called MVC design pattern is to layer the project
Project requirements
-
When the software starts, a list of some members of the company is created according to the given data
-
According to the menu prompt, the component is a development team based on existing company members
-
The component process includes inserting a member into the team, deleting a member from the team, and listing the existing members in the team
-
Developers include architects, designers, and programmers
Project architecture
The project uses MVC design pattern to divide the whole project into three packages
- pers.Cfeng.groupsheduing.view
Obviously, the function of this package is to realize the view function of the organization scheduling software, that is, to display the interface to the user. Here, in order to facilitate the speed of code writing, we will not introduce java web interface or GUI, but use the console for display
- pers.Cfeng.groupsheduing.service
The function of this package is to provide services
- pers.Cfeng.groupsheduing.datafiled
This package contains the objects operated by the software. Here are the employees of the company
Project issues
The project reports an error and throws an exception because "this.ept" is null
After debugging, it is found that the EQUIPMENT is written as EMPLOYEE in the method of selecting EQUIPMENT, which is normal after modification
Project error report is out of range
It is found that the number of device groups and member groups is inconsistent, so the last member corresponds to empty memory, plus a {} solution
The project output results are not standardized
The Programmer column is not aligned when the member list is not, and it is normal after entering the class for modification
Detailed annotation of project source code (part)
view package main interface
package pers.Cfeng.groupsheduing.view; /** * @author Cfeng *This class is the main starting class of the application and realizes the display of the console interface */ import pers.Cfeng.groupsheduing.datafield.*; import pers.Cfeng.groupsheduing.service.*; public class TeamView { private NameListService listService = new NameListService(); private TeamListService teamService = new TeamListService(); //Define the operation corresponding to the selected number private static final char TEAMLIST = '1'; private static final char ADDMEMBER = '2'; private static final char DELMEMBER = '3'; private static final char EXIT = '4'; //Enter the main menu; public void enterMainMenu() { boolean loopFlag = true; //To exit the operation char key = 0; //Input first and then execute, so it is a do while loop do { if(key != '1') { listAllEmployees();//Show all members of the company } System.out.print("1-Team list 2-Add member 3-Delete team member 4-Please select (1) to exit-4): "); key = UtilView.readMenuSelection(); System.out.println(); switch(key){ case TEAMLIST: listTeam(); break; case ADDMEMBER: addMember(); break; case DELMEMBER: delMember(); break; case EXIT: System.out.print("Confirm whether to exit(Y/N)"); char confirm = UtilView.readConfirmSelection(); if(confirm == 'Y') loopFlag = false; break; } }while(loopFlag);//End of statement } //display private void listAllEmployees() { System.out.print("\n***************************Team scheduling software simulation******************************************\n"); System.out.print("--welcome------------------------W city CT Technology company--------welcome--------------------------\n"); Employee[] tempe = listService.getAllEmployees(); if(tempe.length == 0) { System.out.println("No customer record!"); } else System.out.println("ID\t full name\t Age\t wages\t position\t state\t bonus\t shares\t Use equipment"); for(Employee em : tempe) { System.out.println(" " + em); } System.out.print("--welcome-----------------W city CT Technology company--------welcome---------------------------------\n"); } //Team member list private void listTeam() { System.out.print("--hello-----------------CT science and technology CT Project team--------hello---------------\n"); Programmer[] team = teamService.getTeam(); if(team.length == 0) { System.out.println("There are no development members in this project team!"); } else System.out.println("TID/ID\t full name\t Age\t wages\t position\t bonus\t shares"); for(Programmer pr : team) { System.out.println(" " + pr.toString()); } System.out.print("--hello-----------------CT science and technology CT Project team--------hello---------------\n"); } //Add members to team private void addMember() { System.out.println("-----------------------Add member to project group----------------------"); System.out.println("Please enter the employee to add ID : "); int id = UtilView.readInt(); try { Employee e = listService.getEmployee(id); teamService.addMember(e); System.out.println("Added successfully"); }catch(TeamException e) { System.out.println("Failed to add,"+ e.getMessage()); } //Press enter to continue UtilView.readReturn(); } //Delete member private void delMember() { System.out.println("-----------------------Delete project group members----------------------"); System.out.println("Please enter the employee to delete TID: "); int id = UtilView.readInt(); System.out.print("Are you sure to delete(Y/N)"); char confirm = UtilView.readConfirmSelection(); if(confirm == 'N') return; try { teamService.DelMember(id); System.out.println("Delete succeeded"); }catch(TeamException e) { System.out.println("Deletion failed," + e.getMessage()); } UtilView.readReturn();//Enter to continue } //Starting class public static void main(String[] args) { TeamView view = new TeamView(); view.enterMainMenu(); } }
Swevice package
NameList class
package pers.Cfeng.groupsheduing.service; /** * @author Cfeng *Write the user as a list */ import pers.Cfeng.groupsheduing.datafield.*; import static pers.Cfeng.groupsheduing.service.Data.*;//Import data public class NameListService {//Click Add F2 to modify the class name and package name private Employee[] employees;//Imported employee data store //Construction method to import data public NameListService() {//Copy of array //Convert the String type in data to the corresponding type employees = new Employee[EMPLOYEES.length];//Allocate memory for(int i = 0; i < employees.length; i++) { int type = Integer.parseInt(EMPLOYEES[i][0]); int id = Integer.parseInt(EMPLOYEES[i][1]); String name = EMPLOYEES[i][2]; int age = Integer.parseInt(EMPLOYEES[i][3]); double salary = Double.parseDouble(EMPLOYEES[i][4]); Equipment ept; double bonus; int stock; //Choose the appropriate identity according to the type; it is more appropriate to use constants instead of numbers switch(type) { case EMPLOYEE:{ employees[i] = new Employee(id, name, age, salary); break; } case PROGRAMMER:{ ept = createEquiment(i);//Corresponds to the location employees[i] = new Programmer(id, name, age, salary, ept); break; } case DESIGNER:{ ept = createEquiment(i); bonus = Double.parseDouble(EMPLOYEES[i][5]); employees[i] = new Designer(id, name, age, salary, ept, bonus); break; } case ARCHITECT:{ ept = createEquiment(i);//At the beginning, the error is reported to be out of bounds, and it is normal after modification bonus = Double.parseDouble(EMPLOYEES[i][5]); stock = Integer.parseInt(EMPLOYEES[i][6]); employees[i] = new Architect(id, name, age, salary, ept, bonus, stock); break; } } } } private Equipment createEquiment(int index) { int type = Integer.parseInt(EQUIPMENTS[index][0]);//Before, it was written as EMPLOYEE, and it was empty switch(type) { case PC: return new PC(EQUIPMENTS[index][1], EQUIPMENTS[index][2]); case NOTEBOOK: return new NoteBook(EQUIPMENTS[index][1], Integer.parseInt(EQUIPMENTS[index][2])); case PRINTER: return new Printer(EQUIPMENTS[index][1], EQUIPMENTS[index][2]); } return null; } //Defines how to manipulate arrays public Employee[] getAllEmployees() { return employees; } public Employee getEmployee(int id) throws TeamException { for(Employee t: employees) { if(t.getId() == id) { return t; } }//Custom throw exception throw new TeamException("The employee does not exist"); } } /* //Test it public static void main(String[] args) throws TeamException {//Throw exception because "this.ept" is null for(int i = 1;i <= 13;i++) { Employee test = new NameListService().getEmployee(i); System.out.println(test.toString()); } } } */
TeamListService class
package pers.Cfeng.groupsheduing.service; /** * @author Cfeng *This class is an operation to add company employees to the work team *Because it is a development team, it does not include ordinary staff */ import pers.Cfeng.groupsheduing.datafield.*; public class TeamListService { private int total = 0;//Total number of members private final int MAX_MENMBER = 5;//Maximum team size private int counter = 1;//Count team members private Programmer[] team = new Programmer[MAX_MENMBER]; public TeamListService() { // } //get method, array type is also object type; return team array public Programmer[] getTeam() { Programmer[] team = new Programmer[total]; for(int i = 0;i < total;i++) { team[i] = this.team[i];//Copy output } return team; } //Add people to the team public void addMember(Employee per) throws TeamException { if(total >= MAX_MENMBER) throw new TeamException("The member is full and cannot be added"); //Use instanceof to determine whether it is a Programmer object (including subclasses) if(!(per instanceof Programmer)) throw new TeamException("This member is not a developer, please reselect"); //Is Programmer, cast type //Use the unique methods of subclasses to transform Programmer programmer = (Programmer)per; if(isExit(programmer)) throw new TeamException("The member is already in the team"); if(programmer.getStatu().getStatu().equals("BUSY")) throw new TeamException("The member is already on the team"); if(programmer.getStatu().getStatu().equals("VOCATION")) throw new TeamException("The member is on vacation"); //The team consists of up to one architect, two designers and three programmers int numOfArch = 0, numOfDsgn = 0, numOfPrg = 0; for(int i = 0; i < total; i++) { if(team[i] instanceof Architect) { numOfArch ++; } else if(team[i] instanceof Designer) { numOfDsgn++; } else { numOfPrg++; } } //Judge whether the number of people exceeds the requirements if (programmer instanceof Architect) { if (numOfArch >= 1) throw new TeamException("There can be at most one architect on the team"); } else if (programmer instanceof Designer) { if (numOfDsgn >= 2) throw new TeamException("There can be no more than two designers on the team"); } else if (programmer instanceof Programmer) { if (numOfPrg >= 3) throw new TeamException("There can be no more than three programmers on the team"); } //No exception, continue to add normally programmer.setStatu(Status.BUSY);//Set status programmer.setMemberId(counter++); team[total++] = programmer; } private boolean isExit(Programmer p) { for(int i = 0; i < total; i++) { if(team[i].getId() == p.getId()) { return true; } } return false; } //Delete a member from a team public void DelMember(int memberId) throws TeamException { int n = 0; for(; n < total; n++) { if(team[n].getMemberId() == memberId) { team[n].setStatu(Status.FREE); break; } } //To delete, move the following elements if(n == total) { throw new TeamException("The member cannot be found and cannot be deleted"); } for(int i = n + 1; i < total; i++) { team[i - 1] = team[i]; } team[--total] = null; //If it is empty, the garbage collection thread will process it, and the size will change automatically } }
Date class (data source)
package pers.Cfeng.groupsheduing.service; /** * The function of this package is the service layer, in which all service related classes are included * @author Cfeng * Data Class is the data source and specification, which defines the data, such as various employee and equipment numbers [easy to call], and uses a two-dimensional array to define the initial data of various employees and equipment * Attion: The data here cannot be modified when required, so we need to define them as final type * In fact, the source of data can be a file. Let's make it simple and input it directly * * This class is the initial data operation layer of the service operation layer */ public class Data { //Use the number to refer to various employees, including ordinary employees, designers, programmers and architects. Pay attention to the specification of constant definition. Data cannot be modified during operation. The access permission is set to public, and other classes can be used public static final int EMPLOYEE = 101;//101 represents ordinary staff public static final int PROGRAMMER = 102; public static final int DESIGNER = 103; public static final int ARCHITECT = 104; //Use numbers to define various types of equipment public static final int PC = 121; public static final int NOTEBOOK = 122; public static final int PRINTER = 123; /** * The higher the rank, the better the benefits * Employee : 101,id,name,age,salary * Programmer: 102,id,name,age,salary * Designer: 103,id,name,age,salary,bonus * Architect: 104,id,name,age,salary,bonus,stock * Use an initial two-dimensional array EMPLOYEES to write a set of initial data. Note that the String type is used */ public static final String[][] EMPLOYEES = {//The sorting method here is based on the arrival of employees sooner or later, not job level {"101","1","C god","35","3000"}, {"102","2","C cloud","34","3500"}, {"103","3","C strong","30","3800","3000"}, {"102","4","Lee L","31","3100"}, {"103", "5", "thunder J", "28", "10000", "5000"}, {"102", "6", "Zhang Q", "22", "6800"}, {"103", "7", "Willow Y", "29", "10800","5200"}, {"104", "8", "C wind", "30", "19800", "15000", "2500"}, {"103", "9", "C rain", "26", "9800", "5500"}, {"102", "10", "Ding D", "21", "6600"}, {"102", "11", "Zhang C", "25", "7100"}, {"103", "12", "C Yang", "27", "9600", "4800"}, {"104", "13", "Liu Da W", "30", "19500", "17000", "3000"}, };//A two-dimensional array is defined. The code is too long and needs to be separated /**The following EQUIPMENTS array corresponds to the above EMPLOYEES array element one by one * PC :121, model, display * NoteBook:122, model, price * Printer :123, name, type */ public static final String[][] EQUIPMENTS = {//Ordinary employees do not need {}, {"122", "association T4", "6000"}, {"121", "Dale", "NEC17 inch"}, {"121", "Dale", "Samsung 17 inches"}, {"123", "Canon 2900", "laser"}, {"121", "ASUS", "Samsung 17 inches"}, {"121", "ASUS", "Samsung 17 inches"}, {"123", "Epson 20 K", "Needle type"}, {"122", "HP m6", "5800"}, {"121", "Dale", "NEC 17 inch"}, {"121", "ASUS","Samsung 17 inches"}, {"122", "HP m6", "5800"}, {"123", "HP M401d", "Inkjet"} }; }
Datafield package
Here we explain that programmer inherits Employee, Designer inherits programmer, and Architect inherits Designer
Programmer class
package pers.Cfeng.groupsheduing.datafield; /** * @author Cfeng * Programmer It is also an Employee, which is an extension of ordinary employees, so it inherits Employee */ import pers.Cfeng.groupsheduing.service.Status; public class Programmer extends Employee { //The added attributes include status, member ID and collecting equipment private int memberId;//There is no setting here. It will only be set when you want to select private Status statu = Status.FREE; private Equipment ept; public Programmer(int id, String name, int age, double salary, Equipment ept) { super(id, name, age, salary); this.ept = ept; } public int getMemberId() { return memberId; } public void setMemberId(int newId) { this.memberId = newId; } public Status getStatu() { return statu; } public void setStatu(Status newStatu) { this.statu = newStatu; } public Equipment getEquipment() { return ept; } public void setEquipment(Equipment newEpt) { this.ept = newEpt; } //Obtain the list of programmer s and add the title and equipment on the basis of the original method @Override public String toString() { return super.getDetails() + "\t programmer\t" + statu + "\t\t\t" + ept.getInformation(); } //Get the list of development teams with protected access rights protected String getMemberDetails() { return memberId +"/" + super.getDetails(); } //Get a list of the team members selected for this category public String getDetaileForTeam() { return getMemberDetails() + "\t programmer"; } }
This project is very simple, but it is suitable for Java beginners to consolidate the foundation. At the beginning, learning is to practice more. Although it is simple, it does not affect the long code. Not all of them will be posted here. I will upload what I need to resources without points~~