湯先生にJavaノートを習う:練習3:List総合


湯先生にJavaノートを習う:練習3:List総合
完了:1回目
1.練習3:List総合
学生クラスStudentクラスのプロパティを定義します:id、name、age
1.ユーザに学生の情報の循環入力を促す、ArrayList集合に格納する.すべての学生の情報を出力する.指定生徒情報4を学号に従って変更する.学号に基づいて学生を削除する
/**
 *        Student 
 *   :id、name、age
 * 
 * 1.             ,   ArrayList   
 * 2.         
 * 3.            
 * 4.        
 */
package season13;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;


public class TestListTest {
	//    
	 static Scanner input = new Scanner(System.in);		
     static ArrayList<Student> arrayList = new ArrayList<Student>();
	public static void main(String[] args) {
		//         
		inputStu();
		//           
		printStu();
		//               
		System.out.print("         id: ");
		int id = input.nextInt();
		modifyById(id);
		//        
		System.out.print("         id: ");
		int removeId = input.nextInt();
		removeById(removeId);

	}
	
	public static void inputStu(){
			
		//     0   
		while(true){
			System.out.println("======       ======");
			
			Student student = new Student();
			System.out.println("     : ");
			int id=input.nextInt();
			if(0==id){
				break;
			}
			student.setId(id);
			System.out.println("     : ");
			student.setName(input.next());
			System.out.println("     : ");
			student.setAge(input.nextInt());			
			arrayList.add(student);			
		}	
	}
	
	public static void printStu(){
		System.out.println("-------      ------");
		System.out.println("  \t  \t  ");
		for(Student s:arrayList){
			System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge());
		}
	}
	
	public static void modifyById(int id){
		//1.    id       
		Student student = findStuById(id);
		if(null==student){
			System.out.println("  id      ");
			return;
		}
		//2.      
		System.out.println("         ");
		student.setName(input.next());
		System.out.println("         : ");
		student.setAge(input.nextInt());	
		System.out.println("    ");
		printStu();
	}
	// :  id   
	public static Student findStuById(int id){
		Iterator<Student> iterator = arrayList.iterator();
		while(iterator.hasNext()){
			Student student = iterator.next();
			if(student.getId()==id){
				return student;
			}
		}
		return null;
	}
	
	public static void removeById(int id){
		//1.    id       
		Student student = findStuById(id);
		if(null==student){
			System.out.println("  id      ");
			return;
		}
		arrayList.remove(student);
		System.out.println("    ");
		printStu();
	}
	
}

class Student{
	
	private int id;
	private String name;
	private int age;

	public Student() {
		super();
	}
	public Student(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", age=" + age + ", name=" + name + "]";
	}
	
	
}