[英]How to match arraylist data in another class in java
我開發了一個學生班,可以添加學生,在主班里,我使用arraylist來顯示所有已添加的學生。 現在,我參加了一個入門班,我想將輸入的ID與注冊班級相匹配,是否與已添加的學生相匹配。 我對此有疑問,有人可以告訴我該怎么做嗎?
這是面類
import java.util.*;
public class StudentRecordSystem {
static ArrayList<Student> list = new ArrayList<Student>();
public static void main(String[] args) {
// TODO Auto-generated method stub
String option;
String option1 = "a";//initializing the option a from the main menu
String option2 = "b";//initializing the option b from the main menu
String option3 = "c";//initializing the option c from the main menu
String option4 = "d";//initializing the option d from the main menu
String option5 = "e";//initializing the option e from the main menu
String option6 = "f";//initializing the option f from the main menu
String option7 = "g";//initializing the option g from the main menu
for(int i=1; i<2; i++)
{
System.out.println("========== Main Menu ==========");
System.out.println("a)Student Admin \nb)Course Admin \nc)Staff Admin \nd)Exit");//printing the main menu options
System.out.println("Your option: ");//asking the user for option to choose
Scanner inputOption = new Scanner(System.in);
option = inputOption.nextLine();//Taking user option
if(option.equals(option1))
{
for(int l=1; l<2; l++){
System.out.println("========== Student Admin Menu ==========");
System.out.println("a)Add new Student \nb)List Enrolment Details \nc)Enrol Student \nd)Assign Exemption To a Student \ne)Process Payment \nf)Generate Report \ng)Go Back");//printing the main menu options
System.out.println("Your option: ");//asking the user for option to choose
option = inputOption.nextLine();//Taking user option
if(option.equals(option1))
{
Student st = new Student();
list.add(st);
l--;//continues the loop
}
if(option.equals(option2))
{
for (Student st : list) {
System.out.println(st.toString());
}
l--;//continues the loop
}
if(option.equals(option3))
{
String[] courseName = {"Java Programming","Object Oriented Design","Software Testing","J2EE","Software Architecture","Design Patterns"};
Course c = new Course(courseName);
//c.getCourseName();
System.out.println(Arrays.toString(c.getCourseName()));
}
if(option.equals(option4))
{
System.out.println("Will assign exemption to a student");
}
if(option.equals(option5))
{
System.out.println("Will process payment for the student");
}
if(option.equals(option6))
{
System.out.println("Will Generate Report");
}
}
if(option.equals(option7))
{
i--;//continues the loop
}
}
else if(option.equals(option2))
{
System.out.println("Will go to course class");
i--;//continues the loop
}
else if(option.equals(option3))
{
System.out.println("Will go to staff class");
i--;//continues the loop
}
else if(option.equals(option4))
{
System.out.println("Thank You For Using This Program");
System.exit(0);
}
else
{
System.out.println("Error: The selected option is not Recognised. Try Again...");//Error message if wrong option has been entered
i--;//continues the loop
}
}
}
}
這是學生班
import java.util.ArrayList;
import java.util.Scanner;
public class Student {
private String studentName;
private String studentId;
private String dob;
private Scanner scan;
//Default Constructor
public Student()
{
scan = new Scanner(System.in);
System.out.print("Student Name:");
studentName = scan.next();//Taking Student Name
System.out.print("Student ID:");
studentId = scan.next();//Taking Student ID
System.out.print("Student Date Of Birth(DD/MM/YY):");
dob = scan.next();//Taking Student Date of Birth
}
//Constructor
public Student(String newStudentName, String newStudentId, String newDob)
{
this.studentName = newStudentName;
this.studentId = newStudentId;
this.dob = newDob;
}
//Setter Methods
public void setStudentName(String newStudentName)
{
studentName = newStudentName;
}
public void setStudentId(String newStudentId)
{
studentId = newStudentId;
}
public void setDob(String newDob)
{
dob = newDob;
}
//Getter Methods
public String getStudentName()
{
return studentName;
}
public String getStudentId()
{
return studentId;
}
public String getDob()
{
return dob;
}
public String toString() {
return getStudentName() + "\t" + getStudentId() + "\t" + getDob();
}
}
這是報名課程-
import java.util.ArrayList;
import java.util.Scanner;
public class Enrollment {
private String studentId;
private Scanner scan;
public Enrollment()
{
scan = new Scanner(System.in);
System.out.println("Please Enter The Student ID:");
studentId = scan.next();
}
}
在Enrollment
課程中為studentId
添加getter
,
public String getStudentId(){
return studentId;
}
在主類中,初始化list
后創建Enrollment
對象
Enrollment enroll = new Enrollment();
遍歷ArrayList
以找到匹配項:
boolean isfound = false;
for(int i = 0; i < list.size(); i++){
if(list.get(i).getStudentId().equals(enroll.getStudentId()){
isfound = true;
}
}
if(isfound){
System.out.println("Student record found!");
}
else{
System.out.println("No match found!");
}
在您的主要創建選項中,用戶要按ID搜索學生。 並創建一個EnrollmentClass對象,並將Arraylist傳遞給構造函數。 在Enrollment類的構造函數中,您需要迭代每個arrayList,直到找到ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.