java Simulated Student Course Selection: Map Interface

Keywords: Java

Addition, deletion and modification: code acceptance connection interface
Notice the keySet method and the ValueSet method: traverse the output

Map interface:
* Map provides a mapping relationship in which elements are stored in the form of key-value pairs and can quickly find value according to key.
* Key-value pairs in Map s exist as Entry-type objects
* Keys (key values) are not repeatable, value values can be
* Each key can be mapped to at most one value, and each value corresponds to many key values.
* The Map interface provides a way to return the key value set, the value value set, and the Entry (key value pair) set, respectively.
* Map supports generics in the form of Map < K, V >

Adding method: put (K key, V value > to add key-value pairs to Map
Delete method: remove (object key) by remove method. If there is a mapping relationship for a key, remove it from the mapping.
Modify method: Use put method, Key value unchanged, modify value value value

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;



public class MapTest {
	
	public Map<String,Student> students;
	
	public MapTest() {
		this.students = new HashMap<String, Student>();
	}
	
	/*
	 * Test Add: Enter ID to determine whether it is occupied
	 * If not occupied, enter a name, create a new student object, and add it to the students
	 */
	public void testPut() {
		
		Scanner console = new Scanner(System.in);
		int i = 0;
		while(i < 3) {
			System.out.println("Please enter the student's ID: ");
			String ID = console.next();
			//Determine whether the ID is occupied, return value if the key has the same value as the ID, otherwise return null
			Student st = students.get(ID);//get Method in Map
			if(st == null) {
				System.out.println("Please enter the name of the student:");
				String name = console.next();
				
				//Creating New Student Objects
				Student newStudent = new Student(ID,name);
				//Add ID-student mapping by calling the put method of students
				students.put(ID, newStudent);
				System.out.println("Successful addition of students:"+students.get(ID).name);
				i++;
			}else {
				System.out.println("The student has been occupied!!!");
				continue;
			}
			}
		
	}
	/*
	Test deletion
	*/
	public void testRemove() {
		Scanner console = new Scanner(System.in);
		while(true) {
		System.out.println("Please enter delete student's ID:");
		String ID = console.next();
		Student st = students.get(ID);
		if(st == null) {
			System.out.println("this ID Non-existent");
			continue;
		}
		students.remove(ID);
		System.out.println("Deleted students"+st.name);
		break;
		}
	}
	/*
	Test Modification
	*/
	public void textModify() {
		Scanner console = new Scanner(System.in);
		while(true) {
			System.out.println("Please enter the revised student's ID: ");
			String stuID = console.next();
			Student st = students.get(stuID);
			if(st == null) {
				System.out.println("this ID No, please re-enter:");
				continue;
			}
			System.out.println("Please enter the name of the student:");
			String name = console.next();
			
			Student newStudent = new Student(stuID, name);
			students.put(stuID, newStudent);
			System.out.println("Successful revision");
			break;
		}
	}
	/*
	Testing EntrySet Method in Map
	*/
	public void testEntrySet() {
		//Returns all key-value pairs in Map by entrySet method
		Set<Map.Entry<String, Student>> entrySet = students.entrySet();
		for(Map.Entry<String, Student> entry : entrySet) {
			System.out.println("Acquisition key:"+entry.getKey());
			System.out.println("The corresponding value is:"+entry.getValue().name);
		}
	}
	
	/*
	 * Testing the keySet method of Map
	 */
	public void testKeySet(){
		//Returns all Set sets of "keys" in the Map through the KeySet method
		Set<String> keySet = students.keySet();
		//Traverse the ketSet, get each key, and then call the get method to get the corresponding value of each key.
		for(String stuId : keySet) {
			Student st = students.get(stuId);
			if(st != null)
				System.out.println("Student"+st.name);
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MapTest mt = new MapTest();
		mt.testPut();
		mt.testKeySet();
		//mt.testRemove();
		//mt.testEntrySet();
		mt.textModify();
		mt.testKeySet();
	}

}


Operation results:

Please enter the ID of the student:
12
 Please enter the name of the student:
ocean
 Successful Added Students: Ocean
 Please enter the ID of the student:
13
 Please enter the name of the student:
lake
 Successful Addition of Students: Lakes
 Please enter the ID of the student:
14
 Please enter the name of the student:
vivi
 Successful Addition of Students: Vivid
 Student Ocean
 Student Lake
 Student dimension
 Please enter the ID of the modified student:
12
 Please enter the name of the student:
Sea
 Successful revision
 Student Sea
 Student Lake
 Student dimension

Posted by scott212 on Sat, 24 Aug 2019 03:57:41 -0700