banner



How To Create A Address Book Using Java

It has been a month since I studied Java. I have simply built a student information management system with my knowledge. Because I haven't started to learn database yet, I use ArrayList set to store student information data and HashSet to store user information.

Note: Student id must be int data. It is added from 1, otherwise, an error will be reported when deleting. This is a small bug, which needs to be improved.

The code is as follows:

Student.java

package com.briup.login;  public class Student { 	private int id; 	private String name; 	private int age; 	private double source;  	public Student() { 		super(); 	}  	public Student(int id, String name, int age, double source) { 		super(); 		this.id = id; 		this.name = name; 		this.age = age; 		this.source = source; 	}  	public int getId() { 		return id; 	}  	public void setId(int id) { 		this.id = id; 	}  	public String getName() { 		return name; 	}  	public void setName(String name) { 		this.name = name; 	}  	public int getAge() { 		return age; 	}  	public void setAge(int age) { 		this.age = age; 	}  	public double getSource() { 		return source; 	}  	public void setSource(double source) { 		this.source = source; 	}  	@Override 	public String toString() { 		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", source=" + source + "]"; 	}  }

Customer.java

package com.briup.login;  public class Customer { 	private String username; 	private String password;  	public Customer() { 		super(); 	}  	public Customer(String username, String password) { 		super(); 		this.username = username; 		this.password = password; 	}  	public String getUsername() { 		return username; 	}  	public void setUsername(String username) { 		this.username = username; 	}  	public String getPassword() { 		return password; 	}  	public void setPassword(String password) { 		this.password = password; 	}  	@Override 	public String toString() { 		return "Customer [username=" + username + ", password=" + password + "]"; 	}  }

Login.java

package com.briup.login;  import java.util.HashSet; import java.util.Scanner; import com.briup.login2.Customer;  public class Login { 	public static HashSet<Customer> set = new HashSet<>();;  	public Login() { 		super(); 	}  	// Judge whether the login is successful 	public static void login(String username, String password) { 		if (set.size() == 0) { 			System.out.println("*Login failed, maybe not registered, please try again!*"); 			return; 		} 		for (Customer cust : set) { 			if (cust.getUsername().equals(username)) { 				if (cust.getPassword().equals(password)) { 					System.out.println("*Login succeeded!*"); 					StudentMessages sm = new StudentMessages(); 					sm.index(); 				} else { 					System.out.println("*Login failed! Wrong password!*"); 					break; 				} 			} else { 				System.out.println("*Login failed, user name does not exist!*"); 				break; 			} 		} 	}  	// Judge whether the registration is successful 	public static boolean register(String username, String password) { 		if (set.size() == 0) { 			System.out.println("*Registration succeeded!!*"); 		} 		for (Customer cust : set) { 			if (cust.getUsername().equals(username)) { 				System.out.println("*User name already exists!!*"); 				return false; 			} else { 				System.out.println("*Registration succeeded!!*"); 				return true; 			} 		} 		return true; 	}  	// Registration main interface 	public void loginindex() { 		while (true) { 			System.out.println("******Main interface******"); 			System.out.println("*     1.Sign in          *"); 			System.out.println("*     2.register          *"); 			System.out.println("*     0.Sign out          *"); 			System.out.println("*Please choose:"); 			@SuppressWarnings("resource") 			int num = new Scanner(System.in).nextInt(); 			while (true) { 				if (num == 1) { 					System.out.println("*enter one user name:"); 					@SuppressWarnings("resource") 					String name = new Scanner(System.in).nextLine(); 					System.out.println("*Please input a password:"); 					@SuppressWarnings("resource") 					String password = new Scanner(System.in).nextLine(); 					login(name, password); 					break; 				} else if (num == 2) { 					System.out.println("*enter one user name:"); 					@SuppressWarnings("resource") 					String name = new Scanner(System.in).nextLine(); 					System.out.println("Please input a password:"); 					@SuppressWarnings("resource") 					String password = new Scanner(System.in).nextLine(); 					if (register(name, password) == false) { 						break; 					} 					Customer cust = new Customer(name, password); 					set.add(cust); 					break; 				} else if (num == 0) { 					System.out.println("***System exited***"); 					System.exit(0); 					break; 				} else { 					System.out.println("The format entered is incorrect, please try again!!"); 				} 			} 		} 	} }

StudentMessages.java

package com.briup.login;  import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; import com.briup.login2.Student;  public class StudentMessages { 	public static ArrayList<Student> list = new ArrayList<>();  	public StudentMessages() { 		super(); 	}  	// Query all student information 	public void findAll() { 		if (list.size() == 0) { 			System.out.println("*No student information!!*"); 			return; 		} else { 			Iterator<Student> iter = list.iterator(); 			while (iter.hasNext()) { 				System.out.println(iter.next()); 			} 		} 	}  	// Add students to judge whether the student number exists 	public boolean add(int id) { 		if (list.size() == 0) { 			return true; 		} 		for (Student stu : list) { 			if (stu.getId() == id) { 				System.out.println("*Student ID already exists*"); 				return false; 			} 		} 		return true; 	}  	// Modify student information to determine whether student number exists 	public boolean update(int id, String name, int age, double source) { 		if (list.size() == 0) { 			return false; 		} 		for (Student stu : list) { 			if (stu.getId() == id) { 				stu.setName(name); 				stu.setAge(age); 				stu.setSource(source); 				list.set(id - 1, stu); 				return true; 			} 		} 		if (true) { 			System.out.println("There is no information about the student in the system!!"); 		} 		return false; 	}  	// Delete student information with specified id 	public boolean delete(int id) { 		if (list.size() == 0) { 			return false; 		} 		for (Student stu : list) { 			if (stu.getId() == id) { 				list.remove(id - 1); 				System.out.println("Delete successful"); 				return true; 			} 		} 		System.out.println("This student number does not exist!!"); 		return true; 	}  	// Design the main interface of student address book management system 	public void index() {  		StudentMessages sm = new StudentMessages(); 		s: while (true) { 			System.out.println("*********Student information management system*********"); 			System.out.println("*        1.Query student information                   *"); 			System.out.println("*        2.Add student information                   *"); 			System.out.println("*        3.Modify student information                   *"); 			System.out.println("*        4.Delete student information                   *"); 			System.out.println("*        0.Exit management system                   *"); 			System.out.println("*Please input:"); 			Scanner scanner = new Scanner(System.in); 			int num = scanner.nextInt(); 			s2: while (true) { 				if (num == 1) { 					sm.findAll(); 					break; 				} else if (num == 2) { 					System.out.println("*Please input the student's id: "); 					@SuppressWarnings("resource") 					int id = new Scanner(System.in).nextInt(); 					if (sm.add(id) == false) { 						break s2; 					} 					System.out.println("*Please enter the student's name:"); 					@SuppressWarnings("resource") 					String name = new Scanner(System.in).nextLine(); 					System.out.println("*Please enter the age of the student:"); 					@SuppressWarnings("resource") 					int age = new Scanner(System.in).nextInt(); 					System.out.println("*Please enter the student's score:"); 					@SuppressWarnings("resource") 					double source = new Scanner(System.in).nextDouble(); 					Student stu = new Student(id, name, age, source); 					list.add(stu); 					System.out.println("Added successfully!!"); 					break; 				} else if (num == 3) { 					System.out.println("*Please enter the student's id: "); 					@SuppressWarnings("resource") 					int id = new Scanner(System.in).nextInt(); 					System.out.println("*Please enter the student's name:"); 					@SuppressWarnings("resource") 					String name = new Scanner(System.in).nextLine(); 					System.out.println("*Please enter the age of the student:"); 					@SuppressWarnings("resource") 					int age = new Scanner(System.in).nextInt(); 					System.out.println("*Please enter the student's score:"); 					@SuppressWarnings("resource") 					double source = new Scanner(System.in).nextDouble(); 					if (sm.update(id, name, age, source) == false) { 						break s2; 					} 					System.out.println("Modification succeeded!!"); 					break; 				} else if (num == 4) { 					System.out.println("*Please input the student's id: "); 					@SuppressWarnings("resource") 					int id = new Scanner(System.in).nextInt(); 					if (sm.delete(id) == false) { 						break s2; 					} 					break; 				} else if (num == 0) { 					System.out.println("****System exited!****"); 					System.exit(0); 					break s; 				} else { 					System.out.println("Input error, please input again!!"); 				} 			} 		} 	} }

Logintest.java (test)

package com.briup.login;  public class loginTest { 	public static void main(String[] args) { 		Login l = new Login(); 		l.loginindex(); 	} }          

Operation result:

How To Create A Address Book Using Java

Source: https://programming.vip/docs/simple-student-address-book-management-system-java-user-student-information.html

Posted by: visserlicedle.blogspot.com

0 Response to "How To Create A Address Book Using Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel