- Problem description
- Solution ideas
This problem simulates the pick-up problem of express cabinet in daily life. In the program, there are two people with different identities, administrator and user, who can perform different operations. Therefore, you can use the switch case statement to judge, and then enter different interfaces. There are also multiple administrator operation functions. You can use the switch case statement again to realize nested use. However, in order to understand the program better, the middle note is not messy when looking at the code. It can wrap second switch-case statements into new methods and call them in the first statement.
In the program design, MVC framework (model view controller) is adopted, and v-places all the contents displayed on the page, including welcome page, exit page, identity selection page, function selection page and so on; M-place the methods related to database operation, i.e. add, delete, modify and query methods. There is no logical connection between each method, and the logical connection is realized through the controller; Finally, through C-Call each method in M and V, the display and function of each page are realized.
Benefits of using MVC framework: reduced coupling. The display of each page is not related to each other, and the implementation process of addition, deletion, modification and query is not related to each other. In the later stage of code modification, the addition and deletion of functions and other changes, the amount of code is greatly reduced. It is also easy to understand when reading the code. Each functional module can be understood separately. Finally, it can be understood through the logical connection of the controller. - Program code
View presentation views java
package com.vanessa.express; import java.util.Scanner; public class Views { Scanner input = new Scanner(System.in); /** * Welcome page */ public void welcome(){ System.out.println("Welcome to the express management system of carrot community!"); } /** * end page */ public void byebye(){ System.out.println("Welcome to use next time!"); } /** * Identity selection page * @return 0.Exit 1 Administrator 2 user */ public int menu(){ System.out.println("Please choose your identity:0.Exit 1.Administrator 2.user"); String text = input.nextLine(); int num = -1; try{ num = Integer.parseInt(text); } catch (NumberFormatException e){ } if(0>num || 2<num){ System.out.println("Incorrect input,Please re-enter!"); menu(); } return num; } /** * Administrator function selection page * @return 0.Exit 1 Express entry 2 Delete express 3 Modify express 4 View all couriers */ public int aMenu(){ System.out.println("Please select the action you want to perform:0.Return to parent directory 1.Enter express 2.Delete express 3.Modify express 4.View all couriers"); String text = input.nextLine(); int num = -1; try{ num = Integer.parseInt(text); } catch (NumberFormatException e){ } if(0>num || 4<num){ System.out.println("Incorrect input,Please re-enter!"); aMenu(); } return num; } /** * User access express page */ public int uMenu(){ System.out.print("Please enter your pick-up code for pick-up:"); String text = input.nextLine(); int num = -1; try{ num = Integer.parseInt(text); } catch (NumberFormatException e){ } if(100000>num || 9999999<num){ System.out.println("Incorrect input,Please re-enter!"); uMenu(); } return num; } /** * Enter express page * @return e */ public Express insert(){ System.out.print("Please enter the courier number:"); String code = input.nextLine(); System.out.print("Please enter the express company:"); String company = input.nextLine(); Express e = new Express(); e.setCode(code); e.setCompany(company); return e; } /** * Get express order number * @return courier number */ public String findByCode(){ System.out.print("Please enter the express order number to operate:"); String code = input.nextLine(); return code; } /** * Ask if you want to delete the page * @return 0.Exit 1 Confirm deletion 2 Cancel operation */ public int delete(){ System.out.println("Are you sure to delete? 0.Exit 1.Confirm deletion 2.Cancel operation"); String text = input.nextLine(); int num = -1; try{ num = Integer.parseInt(text); } catch (NumberFormatException e){ } if(0>num || 2<num){ System.out.println("Incorrect input,Please re-enter!"); delete(); } return num; } /** * Modify express delivery order number page * @return Modified Express */ public Express update(Express e){ System.out.print("Please enter a new courier number:"); String newCode = input.nextLine(); System.out.print("Please enter a new express company:"); String company = input.nextLine(); e.setCode(newCode); e.setCompany(company); return e; } /** * Print express information * @param e */ public void printExpress(Express e) { if (e != null) { System.out.println("The express information is as follows,Courier Services Company:" + e.getCompany() + ",courier number:" + e.getCode() + ",Pick up code:" + e.getNumber()); } } /** * Print all courier information * @param es */ public void printAll(Express[][] es){ if (es==null || es.length==0){ System.out.println("There is no express in the express system"); return; } for(int i=0;i<10;i++){ for(int j=0;j<10;j++){ if (es[i][j] != null) { System.out.print("The first" + (i + 1) + "Row" + (j + 1) + "column"); printExpress(es[i][j]); } //printExpress(es[i][j]); } } } /** * Delete express success page */ public void deleteSuccess(){ System.out.println("Express delivery has been successfully deleted!"); } /** * Pick up success page */ public void successPickup(){ System.out.println("Successful pick-up!"); } /** * Duplicate storage of express page */ public void expressExist(){ System.out.println("This order number already exists in the express cabinet,Do not store repeatedly!"); } /** * There is no corresponding page */ public void expressNotExist(){ System.out.println("This order number does not exist in the express cabinet,Please re-enter!"); } }
data access
Express.java represents express delivery, which contains the relevant attributes of express delivery: express order number, express company and pick-up code, and related methods such as get(), set(), toString().
package com.vanessa.express; import java.util.Objects; public class Express { private String company; //Courier Services Company private String code; //courier number private int number; //Pick up code @Override public String toString() { return "Express{" + "company='" + company + '\'' + ", code=" + code + ", number=" + number + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Express)) return false; Express express = (Express) o; return Objects.equals(code, express.code); } @Override public int hashCode() { return Objects.hash(code); } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } }
ExpressDao.java contains the operation methods of and database, that is, add, delete, modify and query (any logic between each method is independent).
package com.vanessa.express; import java.util.Random; public class ExpressDao { private Express[][] data = new Express[10][]; private int size; //Number of couriers currently stored private Random random = new Random(); { for (int i=0;i<10;i++){ data[i] = new Express[10]; } } /** * Access Express * @param e * @return If the express delivery is saved successfully, it will return true; if there is no location, it will return false */ public boolean add(Express e){ if (size == 100){ return false; } int x = -1; int y = -1; //Randomly generate the row and column where the express is located while (true){ x = random.nextInt(10); y = random.nextInt(10); if (data[x][y] == null){//There is no express at this location break; } } int number = randomNumber(); e.setNumber(number); //Set the pick-up code of express delivery data[x][y] = e; size++; return true; } /** * Randomly generated pick-up code * @return Pick up code that does not exist in the express system */ public int randomNumber(){ /*int number = random.nextInt(900000)+100000; Express e = findByNumber(number); if(e != null){ randomCode();//The pickup code already exists. Reassign the pickup code } return number;*/ while (true){ int number = random.nextInt(900000)+100000; Express e = findByNumber(number); if(e == null){ return number;//The pick-up code is not repeated and can be used directly } } } /** * Query express delivery according to pick-up code * @param number Pick up code * @return If the query result is found, the express information will be returned; otherwise, null will be returned */ public Express findByNumber(int number){ for (int i=0;i<10;i++){ for (int j=0;j<10;j++){ if(data[i][j] != null) { if (data[i][j].getNumber() == number) { return data[i][j]; } } } } return null; } /** * Query express delivery according to the order number * @param code Odd Numbers * @return If the query result is found, the express information will be returned; otherwise, null will be returned */ public Express findByCode(String code){ Express e = new Express(); e.setCode(code); for (int i=0;i<10;i++){ for (int j=0;j<10;j++){ if (e.equals(data[i][j])){ return data[i][j]; } } } return null; } /** * Delete Express * @param e Express to be deleted */ public void delete(Express e){ d:for (int i=0;i<10;i++){ for (int j=0;j<10;j++){ if (e.equals(data[i][j])){ data[i][j] =null; size--; break d; } } } } /** * How to modify the express delivery order No.: delete the express delivery to be modified, and then modify the order No. and add it again * @param oldE * @param newE */ public void update(Express oldE,Express newE){ delete(oldE); add(newE); } /** * Query all express information * @return Send each express message */ public Express[][] all(){ return data; } }
Dispatching data main java
Content used to link V and M. Through the user's digital input, jump to different pages and realize the corresponding functions.
package com.vanessa.express; public class Main { private static Views v = new Views(); private static ExpressDao d = new ExpressDao(); public static void main(String[] args) { v.welcome(); w:while (true) { int menu = v.menu(); switch (menu) { case 0: break w; case 1: administrator(); break; case 2: user(); break; } } v.byebye(); } /** * Realization of various functions of users */ private static void user() { boolean b = true; while (b) { int number = v.uMenu(); Express e = d.findByNumber(number); if (e == null) { v.expressNotExist(); b = true; } else { v.printExpress(e); v.successPickup(); d.delete(e);//Delete the express from the express cabinet after successful pick-up b = false; } } } /** * Realization of various functions of administrator */ private static void administrator() { while (true){ int menu = v.aMenu(); switch (menu){ case 0: return;//Return to the previous level case 1: {//Enter Express Express e1 = v.insert(); Express e2 = d.findByCode(e1.getCode());//Judge whether the express already exists through the express order number if (e2 == null){ d.add(e1); v.printExpress(e1); } else {//The express delivery order number already exists in the express cabinet v.expressExist(); } } break; case 2: {//Delete Express boolean b = true; while (b) { String code = v.findByCode();//Get express order number Express e = d.findByCode(code);//Find the express delivery in the express delivery array through the express delivery number if (e == null) {//No such express delivery order exists v.expressNotExist(); b = true; } else { v.printExpress(e); int type = v.delete(); if (type == 1) { d.delete(e); v.deleteSuccess(); } b = false; } } /*Express e = d.findByCode(code); if (e == null){ v.expressNotExist(); } else { v.printExpress(e); int type = v.delete(); if (type == 1) { d.delete(e); v.deleteSuccess(); } }*/ } break; case 3: {//Modify Express boolean b = true; while (b) { String code = v.findByCode(); Express e = d.findByCode(code); if (e == null) { v.expressNotExist(); b = true; } else { v.update(e);//Call this method to modify the express order number and express company v.printExpress(e); b = false; } } } break; case 4: {//View all couriers Express[][] data = d.all(); v.printAll(data); } break; } }
- Problems encountered
(1) Null pointer problem
Problem Description: when the express is empty, an error occurs when calling the get method to obtain data for condition judgment.Solution: add the statement if (data[i][j]! = null) before if to judge, so that the data[i][j] in the judgment statement is not empty.
Problem Description: in 135 lines of code, when the express is empty, calling the get method appears.
Solution: judge first and then output.
(2) Logical problem
Problem Description: when deleting express delivery, if you enter the wrong document number, you enter the operation of modifying the document number.
Reason: break is placed in the whole operation. Judge in brackets that the statement cannot exit until break is executed, and proceed directly to the next case, that is, the operation of modifying the express delivery order number.
Solution: move break outside the brackets and execute it after case executes all statements.
5. Further improvement
When picking up, if the input pick-up code is wrong, it will return to the identity selection page to realize: when the input pick-up code is wrong, output the prompt information and let the user input the pick-up code again.
When deleting express delivery, return to the previous level when the express delivery information does not exist.
When modifying the express operation, return to the previous level when the express information does not exist.
Use the while loop statement above to create a boolean variable to continue to judge whether to carry out the next loop. If the input is wrong, assign the boolean variable to true to enter the next loop; If the input is correct, assign the boolean variable to false and exit the loop.
6. Summary
Through the learning process of this experiment, we have a deeper understanding of Java programming specification and more standardized programming habits. There are still some imperfections in the code. Let's make progress together!