Express E stack (console simple version) system (array)

Posted by JayNak on Sat, 19 Feb 2022 07:06:20 +0100

Write before:
1. This article is a review of all projects in the first phase of the fifth part of java learning (express e station part - corresponding to the first phase of the first part of Java System - 04 array task)
2. Overall project series blog (including overall express e-station Series)
3. Complete java system link (more Java content than you think)

Requirements introduction:

Task 1: role switching (courier and ordinary users)


Courier function: save express, delete express, modify express, and view all express
Ordinary user function: express delivery

Task 2: deposit Express

Task 3: delete Express

Task 4: modify Express

Task 5: view all express

Task 6: pick up express

Knowledge points involved:

Java basic syntax (variable, data type, identifier, data type conversion, method, operator), process control (API, Scanner class, execution structure), array

Screenshot of code operation:

Task 1:

Task 2:

Task 3:

Task 4:

Task 5:

Task 6:

Train of thought analysis:

This is relatively simple. Just write it in another class.
Think about it. If you want to complete this function, the first step is to define whether the variable is right or not. To start entering numbers, you need the Scanner method. To enter the pick-up code, you need to define a variable for the pick-up code. There cannot be only one pick-up code (one express delivery code is required). Then the pick-up code is of array type. The pick-up code can not burst out of the stone out of thin air. It must be defined by ourselves, so we need to define random numbers to generate pick-up codes. Express company, express company, what else do you need? Company name. It's funny that there is only one company responsible for express delivery. So this is also an array type. The express order number is not explained. Anyone who has taken the express understands it. Finally, define an array subscript to facilitate the storage and counting of express delivery.
So the first step is clear:

Corresponding flow chart

code:

import java.util.Random;
import java.util.Scanner;

import javax.imageio.stream.ImageInputStream;

public class Task00_ZongHeLianXi {
	static Scanner input = new Scanner(System.in);
	
	public static String[] numberArr = new String[100];// courier number
	public static String[] companyArr = new String[100];// Courier Services Company
	public static int[] codeArr = new int[100];// Pick up code
	public static int index = 0;// Array subscript, how many couriers are there in the express cabinet
	
	public static Random random = new Random();// Define random number
	/**
	 * Express E stack training task (console simple version)
	 * Aggregate demand:
		 * 1. Role switching (courier and ordinary users)
		 * 2. Deposit Express
		 * 3. Delete Express
		 * 4. Modify Express
		 * 5. View all couriers
		 * 6. Pick up express
	 * @param args
	 */
	public static void main(String[] args) {
		demo00_startMenu();
	}
	
	/**
	 * Role switching function, general method
	 */
	public static void demo00_startMenu() {
		TuiChu:do {
			System.out.println("--------Welcome to the new vocational class express cabinet----------");
			System.out.println("Please enter your identity: 1-Courier 2-User 0-sign out");
			int id = input.nextInt();
			
			switch (id) { // It feels better to use switch here
			case 1:
				// Courier - show the courier's menu
				demo01_deliverymanMenu();
				break;
			case 2:
				// Ordinary users
				demo01_userMenu();
				break;
			case 0:
				System.out.println("Thank you for using. The program exits");
				break TuiChu;
			default:
				System.out.println("Please enter as directed");
				break;
			}
			
			
		}while(true);	
	}
	
	/**
	 * User menu
	 */
	public static void demo01_userMenu() {
		System.out.println("Please enter the pickup code");
		int code = input.nextInt();
		int codeIndex = demo02_isExist(code);
		if (codeIndex == -1) {
			System.out.println("Express not found");
		} else {
			del(codeIndex);
			System.out.println("Pick up succeeded");
		}
	}
	
	/**
	 * Courier menu
	 */
	public static void demo01_deliverymanMenu() {
		System.out.println("Please select action: 1-Deposit express 2-Delete express 3-Modify express information 4-View all couriers");
		int id = input.nextInt();
		switch (id) { 
		case 1:
			demo02_saveExpress();
			break;
		case 2:
			demo02_delExpress();
			break;
		case 3:
			demo02_updateExpress();
			break;
		case 4:
			printAll();
			break;
		default:
			System.out.println("Please enter as directed");
			break;
		}
	}
	
	/**
	 * Deposit Express
	 */
	public static void demo02_saveExpress() {
		System.out.println("Please enter the courier number");
		String number = input.next();
		System.out.println("Please enter company name");
		String company = input.next();
		
		// Automatic generation of pick-up code 100-900
		int code = random.nextInt(900)+100;
		// Ensure that the pick-up code is not repeated
		do {
			code = random.nextInt(900)+100;
		}while(demo02_isExist(code)!=-1);
		
		numberArr[index] = number;
		companyArr[index] = company;
		codeArr[index] = code;
		index++;
		System.out.println("Express has been deposited, and the pick-up code is:"+code);
	}
	
	/**
	 * Delete Express
	 */
	public static void demo02_delExpress() {
		System.out.println("Please enter the express order number to delete");
		String number = input.next();
		int delIndex = findByNumber(number);
		if (delIndex == -1) {
			System.out.println("Express not found!");
		} else {
			del(delIndex);
			System.out.println("Deleted successfully");
			
		}
	}
	
	/**
	 * Express modification
	 */
	public static void demo02_updateExpress() {
		System.out.println("Please enter the courier number to be modified");
		String number = input.next();
		int updateIndex = findByNumber(number);
		
		if (updateIndex == -1) {
			System.out.println("Express not found");
		} else {
			System.out.println("Please enter a new courier number");
			number = input.next();
			System.out.println("Please enter a new company name");
			String company = input.next();
			numberArr[updateIndex]=number;
			companyArr[updateIndex]=company;
			System.out.println("Modified successfully");
		}	
	}
	
	/**
	 * View all couriers
	 */
	public static void printAll() {
		System.out.println("-----All express information-------");
		System.out.println("courier number\t corporate name\\t Pick up code");
		for (int i = 0; i < index; i++) {
			System.out.println(numberArr[i]+"\t"+companyArr[i]+"\t"+codeArr[i]);
		}
	}
	
	/**
	 * Delete method
	 * @param delIndex courier number
	 */
	public static void del(int delIndex) {
		if (delIndex!=numberArr.length-1) {
			for (int i = delIndex; i < index; i++) {
				numberArr[i]=numberArr[i+1];
				companyArr[i]=companyArr[i+1];
				codeArr[i]=codeArr[i+1];
			}
		} 
		index--;
	}
	
	
	/**
	 * Search according to the express delivery number
	 * @param number Found subscript, - 1 means not found
	 * @return
	 */
	public static int findByNumber(String number) {
		for (int i = 0; i < index; i++) {
			if (numberArr[i].equals(number)) {
				return i;
			}
		}
		return -1;
	}

	
	/**
	 * Judge whether the pick-up code is repeated
	 * @param code
	 * @return
	 */
	public static int demo02_isExist(int code) {
		for (int i = 0; i < index; i++) {// Cyclic traversal pick-up code
			if (codeArr[i]== code) {// Determine whether to repeat
				return i;
			}
		}
		return -1;
	}
}

Topics: Java