A collection of Java Fundamentals

Keywords: Java Mobile

The ultimate weapon system written by list set

User operation

package Demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class crud {

	static List<Map<String, Object>> list  = new  ArrayList<Map<String,Object>>();
   
	public List<Map<String, Object>> add( String name, String password) {
		Map<String, Object> map  = new  HashMap<String, Object>();
		int  id ;
		if(list.size()==0){
			id=1;
		}
		else{
			id= new Random().nextInt(1000);
		}
		map.put("id", id);  
		map.put("name", name); 
		map.put("password", password); 
		list.add(map);
		return  list;
	}

	public int delete(String id) {
		for (int i = 0; i < list.size(); i++) {
		   Map<String, Object> map = list.get(i);	
		   if(id.equals(map.get("id").toString())){
			   list.remove(i);
			  return 0;
		   }
		}
		 return 1;
	}

	public List<Map<String, Object>> show() {
		return list;
	}

	public void update(String updateid, String str) {
	     Map<String, Object> newmap = new HashMap<String, Object>();
	     newmap.put("id", updateid);
	   //Use split('-') method to divide the string with "-" to form a string array, and then use index [0] to get the value of the first element in the array.
	     newmap.put("name", str.split("-")[0]);   
	     newmap.put("password", str.split("-")[1]); 
			
		  for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			if(updateid.equals(map.get("id").toString())){
				list.set(i, newmap);
			}
		}
		
	}

	public void em() {
		for (int i = 0; i < list.size(); i++) {
			list.remove(i);
			  
			}
		
	}
		
	 
	
	
}

Login operation

package Demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Login {
	   static  int sum =0;
	   public static Scanner  sc   = new Scanner(System.in);
	   static int i =0;
	   static  crud crud = new crud();
	   static  weapon weapon = new weapon();
	public static void main(String[] args) {
		 int sum =0;
		System.out.println("----------Welcome to the ultimate weapon system------------");
		System.out.println("    ------1.Sign in---------2.register------");
		
		int index = sc.nextInt();
		if(index==1){
			//Sign in
			while (true) {
				System.out.println("Please enter your user name");
				String name = sc.next();
				System.out.println("Please input your password.");
				String pwd = sc.next();
				if(name.equals("admin")&&pwd.equals("admin")){
					System.out.println("Hello, general! You have obtained the highest authority. The system has a self explosive program. Please use it carefully!");
					loginAdmin();
					return;
				}
				List<Map<String, Object>> list = crud.show();
				if(list.size()==0){
					System.out.println("No users");
					main(null);
					return;
				}
				for (int i = 0; i < list.size(); i++) {
					if(name.equals(list.get(i).get("name").toString())){
						if(pwd.equals(list.get(i).get("password").toString())){
							System.out.println("Login successfully");
							loginOk();
						}
						else{
							  System.out.println("Password error");	
							  sum++;
							}
					}
					else{
						 System.out.println("User name error");	
						 sum++;
					}
				}
				if(sum==3){
					System.out.println("You have made three errors in total...");
					main(null);
					return;
				}
			}
			
		}
		if(index==2){
			//The information of successful registration is stored in the array.
		    //  Two-dimensional array
			user_name();
		}
		}
	
	
	private static void loginOk() {
		System.out.println("--1.View weapons and equipment------2.Weapon selection-----3.launch-----4.Recycling weapons-----0.Sign out");
//		500 guns 10000
//		40 antiaircraft gun 500
//		5000 throwing weapons  	
		String weaponindex = sc.next();
		if(weaponindex.equals("1")){
			List<Map<String, Object>> list = weapon.showRm();
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i));
			}
			loginOk();
		}
		
		if(weaponindex.equals("2")){
			boolean t=true;
			while(t) {
			System.out.println("Select weapon type:");
			List<Map<String, Object>> list = weapon.showRm();
			for (int i = 0; i < list.size(); i++) {
				System.out.println(i+1 + ":");
				System.out.println(list.get(i));
			}
			
			
			String type = sc.next();
			if(type.equals("1")) {
				System.out.println("Please select the number of guns:");
				int amount = sc.nextInt();
				if(amount>(int)list.get(0).get("Number")) {
					boolean a=true;
					while(a) {
					System.out.println("There are not so many guns to choose from,Please re-enter!");
					amount = sc.nextInt();
					if(amount<=(int)list.get(0).get("Number")) {
						a=false;
					}
					}
				}
				
				System.out.println("Please select the ammunition quantity:");
				int ammunition = sc.nextInt();
				if(ammunition>(int)list.get(0).get("Ammunition number")) {
					boolean a=true;
					while(a) {
					System.out.println("There is not so much ammunition to choose from,Please re-enter!");
					ammunition = sc.nextInt();
					if(ammunition<=(int)list.get(0).get("Ammunition number")) {
						a=false;
					}
					}
				}
				weapon.add(type,amount,ammunition);
			}
			else if(type.equals("2")) {
				System.out.println("Please select the number of anti-aircraft guns:");
				int amount = sc.nextInt();
				if(amount>(int)list.get(1).get("Number")) {
					boolean a=true;
					while(a) {
					System.out.println("There are not so many antiaircraft guns to choose from,Please re-enter!");
					amount = sc.nextInt();
					if(amount<=(int)list.get(1).get("Number")) {
						a=false;
					}
					}
				}
				System.out.println("Please select the number of missiles:");
				int ammunition = sc.nextInt();
				if(ammunition>(int)list.get(1).get("Missile number")) {
					boolean a=true;
					while(a) {
					System.out.println("There are not so many missiles to choose from,Please re-enter!");
					ammunition = sc.nextInt();
					if(ammunition<=(int)list.get(1).get("Missile number")) {
						a=false;
					}
					}
				}
				weapon.add(type,amount,ammunition);
			}
			else if(type.equals("3")) {
				System.out.println("Please select the number of weapons to throw:");
				int amount = sc.nextInt();
				if(amount>(int)list.get(2).get("Number")) {
					boolean a=true;
					while(a) {
					System.out.println("There are not so many throwing weapons to choose from,Please re-enter!");
					amount = sc.nextInt();
					if(amount<=(int)list.get(2).get("Number")) {
						a=false;
					}
					}
				}
				int ammunition = 0;
				weapon.add(type,amount,ammunition);
			}
		    System.out.println("Continue selection?[y/n]");
		    String yn = sc.next();
		    if(yn.equals("y")||yn.equals("Y")) {
		    	t=true;
		    }
		    else if(yn.equals("n")||yn.equals("N")) {
		    	t=false;
		    }
			}
			List<Map<String, Object>> listUser = weapon.showRm1();
			for (int i = 0; i < listUser.size(); i++) {
				System.out.println("Weapons of choice:");
				System.out.println(listUser.get(i));
			}
		    loginOk();
		}
		if(weaponindex.equals("3")){
			
			System.out.println("Please choose to use weapons:");
			List<Map<String, Object>> listUsers = weapon.showRm1();
			if(listUsers.size()==0) {
				System.out.println("No weapons, please choose weapons to fight!");
				loginOk();
			}
			for (int i = 0; i < listUsers.size(); i++) {
				System.out.println(i+1 + ":");
				System.out.println(listUsers.get(i));
			}
			int type1 = sc.nextInt();
			
				weapon.div(type1);
			
			loginOk();
		}
		if(weaponindex.equals("4")){
			List<Map<String, Object>> listUsers = weapon.showRm1();
			if(listUsers.size()==0) {
				System.out.println("No weapons to recycle!");
				loginOk();
			}
			weapon.recycle();
			System.out.println("Successful recovery");
			loginOk();
		}
		if(weaponindex.equals("0")){
			
			main(null);
		}
	}


	private static void loginAdmin() {
		System.out.println("--1.See------2.Add to-----3.remove------4.change----5 Self detonate----Go back to the previous level/P ");
	    String logindex = sc.next();
	    if(logindex.equals("1")){
	    	List<Map<String, Object>> list = crud.show();
	    	if(list.size()==0){
	    		System.out.println("No data yet, please add");
	    		loginAdmin();
	    	}
	    	else{
	    		for (int i = 0; i < list.size(); i++) {
					System.out.println(list.get(i));
				}
	    		loginAdmin();
	    	}
	    	
	    }
	    if(logindex.equals("2")){
	    	System.out.println("Please enter a name");
	    	String name = sc.next();
	    	System.out.println("Please input a password");
	    	String password = sc.next();
	    	crud.add(name, password);
	    	System.out.println("Add success");
	    	loginAdmin();
	    }
	    
	    if(logindex.equals("3")){
	    	List<Map<String, Object>> list = crud.show();
	    	for (int i = 0; i < list.size(); i++) {
	    		int a=i+1;
				System.out.println("number:"+a+list.get(i));
			}
	    	if(list.size()==0) {
	    		System.out.println("No users!");
	    		loginAdmin();
	    	}
	    	System.out.println("Please enter the mobile number");
	    	String  id= sc.next();
	    	int deletesum =crud.delete(id);
	    	
	    	if(deletesum==0){
	    		System.out.println("Delete successful");
	    		loginAdmin();
	    	}
	    	else{
	    		System.out.println("Delete failed");
	    		loginAdmin();
	    	}
	    	
	    	
	    }
	    
	    if(logindex.equals("4")){
	    	int  sum=0;
	    	 
	    	List<Map<String, Object>> list = crud.show();
	    	for (int i = 0; i < list.size(); i++) {
	    		int b=i+1;
				System.out.println("Serial number:"+b+list.get(i));
			}
	    	if(list.size()==0) {
	    		System.out.println("No users!");
	    		loginAdmin();
	    	}
	    	System.out.println("Please select the number of the data you want to modify");
	    	String updateid = sc.next();
	    	for (int i = 0; i < list.size(); i++) {
				if(updateid.equals(list.get(i).get("id").toString())){
					
					System.out.println(list.get(i));
					sum++;
				}
			}
	    	if(sum==1){
	    		System.out.println("Please enter the data content (name) you want to modify-Password)");
		    	String str = sc.next();
		    	crud.update(updateid,str);
		    	System.out.println("Modified success");
		    	loginAdmin();
	    	}else{
	    		System.out.println("The number you entered is wrong. There is no such data at present.");
	    		loginAdmin();
	    	}
	    	
	    	
	    }
	    if(logindex.equals("5")) {
    	System.out.println("The self explosion program has been started. The system will explode in 5 seconds. Please evacuate as soon as possible (you should not be able to run)!!");
    	List<Map<String, Object>> list = crud.show();
    	crud.em();
    	int a=5;
    	while(true) {
	    	System.out.println(a--);
	    	try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
	    	if(a==0) {
	    		System.out.println("boom,Bang. boom. . . !!!");
	    		main(null);
	    	}
	    }
	    }
	    if(logindex.equals("p")){
	    	main(null);
	    }
	    
	}


	private static  String user_password( String uname ) {
		System.out.println("Please input a password");
		String upwd = sc.next();
		if(upwd.length()>=4 && upwd.length()<=8 ){
		
			 crud.add(uname, upwd);
			
			System.out.println("login was successful");
			main(null);
		}
		else{
			System.out.println("Please enter the correct 4-8 Bit length password");
			user_password(uname);
		}
		return null;
	}
	private static String user_name() {
		
		System.out.println("enter one user name");
		String uname = sc.next();
		if(uname.length()>=4 && uname.length()<=8 ){
			user_password(uname);
		}
		else{
			System.out.println("Please enter the correct 4-8 Bit user name");
			user_name();
		}
		return null;
	}
}

Weapon operation

package Demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

public class weapon {

	  static  int   gun = 500;
	  static  int   archibald = 40;
	  static  int   grenade = 5000;
	  static  int  ammunition1 = 10000;
	  static  int  ammunition2 = 500;
	  public static Scanner  sc   = new Scanner(System.in);
	  static List<Map<String, Object>>  list  = new ArrayList<Map<String,Object>>();
	  static List<Map<String, Object>>  listUser  = new ArrayList<Map<String,Object>>();
	static{
		
		Map<String, Object> map1   = new  HashMap<String, Object>();
		map1.put("Number", gun);
		map1.put("type", "firearms");
		map1.put("Ammunition number", ammunition1);
		
		Map<String, Object> map2   = new  HashMap<String, Object>();
		map2.put("Number", archibald);
		map2.put("type", "Antiaircraft gun");
		map2.put("Missile number", ammunition2);
		
		Map<String, Object> map3   = new  HashMap<String, Object>();
		map3.put("Number", grenade);
		map3.put("type", "throw weapon");
		
		
		list.add(map1);
		list.add(map2);
		list.add(map3);
	}
	
	
	public List<Map<String, Object>> showRm() {
		return list;
	}
public List<Map<String, Object>> showRm1() {
		
		return listUser;
	}

	public void add(String type, int amount, int ammunition) {
		
		if(type.equals("1")){
			int a =(int)list.get(0).get("Number")-amount;
			int b =(int)list.get(0).get("Ammunition number")-ammunition;
			Map<String, Object> map   = new  HashMap<String, Object>();
			map.put("Number", a);
			map.put("type", "firearms");
			map.put("Ammunition number", b);
			list.set(0, map);
			
		}
		if(type.equals("2")){
			int a =(int)list.get(1).get("Number")-amount;
			int b =(int)list.get(1).get("Missile number")-ammunition;
			Map<String, Object> map   = new  HashMap<String, Object>();
			map.put("Number", a);
			map.put("type", "Antiaircraft gun");
			map.put("Missile number", b);
			list.set(1, map);
		}
	
		if(type.equals("3")){
			int a =(int)list.get(2).get("Number")-amount;
			Map<String, Object> map   = new  HashMap<String, Object>();
			map.put("Number", a);
			map.put("type", "throw weapon");
			list.set(2, map);
		}
	
		Map<String, Object> map = new HashMap<String, Object>();
		if(type.equals("1")) {
		map.put("Choose weapon type", "firearms");
		map.put("Number", amount);
		map.put("Ammunition number", ammunition);}
		else if(type.equals("2")) {
			map.put("Choose weapon type", "Antiaircraft gun");
			map.put("Number", amount);
			map.put("Missile number", ammunition);}
		else if(type.equals("3")) {
			map.put("Choose weapon type", "throw weapon");
			map.put("Number", amount);
			}
		
		listUser.add(map);
	}
	public void div(int type1) {
		
			if("firearms".equals(listUser.get(type1-1).get("Choose weapon type").toString())) {
				System.out.println("Please select the number of guns to use:");
				int n = sc.nextInt();
				if(n>(int)listUser.get(type1-1).get("Number")) {
					boolean a=true;
					while(a) {
					System.out.println("Guns are not enough,Please re-enter!");
					n = sc.nextInt();
					if(n<=(int)listUser.get(type1-1).get("Number")) {
						a=false;
					}
					}
				}
				System.out.println("Please select the quantity of ammunition to use:");
				int d = sc.nextInt();
				if(d>(int)listUser.get(type1-1).get("Ammunition number")) {
					boolean a=true;
					while(a) {
					System.out.println("Ammunition is not enough.,Please re-enter!");
					d = sc.nextInt();
					if(d<=(int)listUser.get(type1-1).get("Ammunition number")) {
						a=false;
					}
					}
				}
				 Map<String, Object> newmap = new HashMap<String, Object>();
			     newmap.put("Choose weapon type", "firearms");
			     newmap.put("Number",(int)listUser.get(type1-1).get("Number")-n); 
			     newmap.put("Ammunition number",(int)listUser.get(type1-1).get("Ammunition number")-d); 
			     listUser.set(type1-1, newmap);
				System.out.println("Launch:");
				boolean t=true;
				int j=0;
				while(t) {
					System.out.print("Da");
					n--;
					int a=(int) (Math.random()*10);
					if(a==6) {
						j++;
					}
					
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(n==0) {
						System.out.println("  Launch completed,Common hit"+j+"Enemy!");
						t=false;
					}
				}
				
			}
			else if("Antiaircraft gun".equals(listUser.get(type1-1).get("Choose weapon type").toString())) {
				System.out.println("Please select the number of antiaircraft guns to use:");
				int n = sc.nextInt();
				if(n>(int)listUser.get(type1-1).get("Number")) {
					boolean a=true;
					while(a) {
					System.out.println("Not enough antiaircraft guns,Please re-enter!");
					n = sc.nextInt();
					if(n<=(int)listUser.get(type1-1).get("Number")) {
						a=false;
					}
					}
				}
				System.out.println("Please select the number of missiles to use:");
				int d = sc.nextInt();
				if(d>(int)listUser.get(type1-1).get("Missile number")) {
					boolean a=true;
					while(a) {
					System.out.println("Missile is not enough,Please re-enter!");
					d = sc.nextInt();
					if(d<=(int)listUser.get(type1-1).get("Missile number")) {
						a=false;
					}
					}
				}
				 Map<String, Object> newmap = new HashMap<String, Object>();
			     newmap.put("Choose weapon type", "Antiaircraft gun");
			     newmap.put("Number",(int)listUser.get(type1-1).get("Number")-n); 
			     newmap.put("Missile number",(int)listUser.get(type1-1).get("Missile number")-d); 
			     listUser.set(type1-1, newmap);
				System.out.println("Launch:");
				boolean t=true;
				int j = 0;
				while(t) {
					System.out.print("Bombardment");
					n--;
					int a=(int) (Math.random()*10);
					if(a==6) {
						j++;
					}
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(n==0) {
						System.out.println("Launch completed,Common hit"+j+"Enemy!");
						t=false;
					}
				}
				
			}
			else if("throw weapon".equals(listUser.get(type1-1).get("Choose weapon type").toString())) {
				System.out.println("Please select the number of throwing weapons to use:");
				int n = sc.nextInt();
				if(n>(int)listUser.get(type1-1).get("Number")) {
					boolean a=true;
					while(a) {
					System.out.println("Not enough throwing weapons,Please re-enter!");
					n = sc.nextInt();
					if(n<=(int)listUser.get(type1-1).get("Number")) {
						a=false;
					}
					}
				}
				 Map<String, Object> newmap = new HashMap<String, Object>();
			     newmap.put("Choose weapon type", "throw weapon");
			     newmap.put("Number",(int)listUser.get(type1-1).get("Number")-n); 
			  
			     listUser.set(type1-1, newmap);
				System.out.println("Throw:");
				boolean t=true;
				int j = 0;
				while(t) {
					System.out.print("boom ");
					n--;
					int a=(int) (Math.random()*10);
					if(a==6) {
						j++;
					}
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(n==0) {
						System.out.println("Throw up,Common hit"+j+"Enemy!");
						t=false;
					}
				}
				
			}
	}
	public void recycle() {
		
		for (int i = 0; i < listUser.size(); i++) {
			
			if("firearms".equals(listUser.get(i).get("Choose weapon type").toString()))
			{
			Map<String, Object> newmap = new HashMap<String, Object>();
		     newmap.put("type", "firearms");
		     newmap.put("Number",(int)listUser.get(i).get("Number")+(int)list.get(0).get("Number")); 
		     newmap.put("Ammunition number",(int)listUser.get(i).get("Ammunition number")+(int)list.get(0).get("Ammunition number")); 
		    
		     list.set(0, newmap);
			}
			else if("Antiaircraft gun".equals(listUser.get(i).get("Choose weapon type").toString()))
			{
				Map<String, Object> newmap = new HashMap<String, Object>();
		     newmap.put("type", "Antiaircraft gun");
		     newmap.put("Number",(int)listUser.get(i).get("Number")+(int)list.get(1).get("Number")); 
		     newmap.put("Missile number",(int)listUser.get(i).get("Missile number")+(int)list.get(1).get("Missile number")); 
		     
		  list.set(1, newmap);
			}
			else if("throw weapon".equals(listUser.get(i).get("Choose weapon type").toString()))
			{
			
			Map<String, Object> newmap = new HashMap<String, Object>();
		     newmap.put("type", "throw weapon");
		     newmap.put("Number",(int)listUser.get(i).get("Number")+(int)list.get(2).get("Number")); 
		     
		  list.set(2, newmap);
		  
			}
		}
		listUser.clear();
	}
	
	

}

Collection summary

1. list set

Create: List < generic > List = new ArrayList < generic > ();
Add method: add()
list list= new ArrayList();
list.add("hello");
Update method: set(index, element)
String str = "Hello Java!"
list.set(0,str)
Remove method: remove(index);
list.add("2");
list.remove(0);
Get method: get(index);
list.get(0);
Advanced: List < map < string, Object > > List = new ArrayList < map < string, Object > > ();
Add: Map < string, Object > MAP1 = new HashMap < string, Object > ();
map1.put("quantity", gun);
map1.put("category", "gun");
map1.put("ammunition number", ammunition1);
list.add(map1);
list.get(0).get("quantity"); / / the value gun corresponding to the first quantity in the list collection array;

2. map set

Create: Map < K, V > map = new HashMap < K, V > ();
k is the key value, v is the corresponding value, and the corresponding v value can be found according to k value.
Such as:

List<Map<String, Object>>  list  = new ArrayList<Map<String,Object>>();
Map<String, Object> map1   = new  HashMap<String, Object>();
		map1.put("Number", gun);
		map1.put("type", "firearms");
		map1.put("Ammunition number", ammunition1);
		
Map<String, Object> map2   = new  HashMap<String, Object>();
		map2.put("Number", archibald);
		map2.put("type", "Antiaircraft gun");
		map2.put("Missile number", ammunition2);
		
Map<String, Object> map3   = new  HashMap<String, Object>();
		map3.put("Number", grenade);
		map3.put("type", "throw weapon");
		
		
		list.add(map1);
		list.add(map2);
		list.add(map3);
		list.get(0).get("Number");//The value gun corresponding to the number of the first in the list set array;

Add method: put();
Map<String,Object> map = new HashMap<String,Object>();
map.put();

Posted by billybathgate on Sat, 02 Nov 2019 16:19:01 -0700