JAVA를 잡아라!...
JAVA_배열리스트_#1_Student 본문
학생부 프로그램
CRUD
1. 학생 추가 [관리자_1]
2. 학생 목록 출력
3. 1명 검색
4. 1등 출력
5. 학생 점수 변경 [관리자_2]
6. 학생 삭제 [관리자_3]
코드_ver0.1
package practice01;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
class Student {
int num;
// PK; private key 정말 그사람인지 구분하는! ex)학번,주민번호,...
// 사용자가 선택 xxx 프로그램에서 설정 O
String name;
int score;
static Random random = new Random();
Student(int num, String name) {
this.num = num;
this.name = name;
this.score = Student.random.nextInt(101);
}
// 자바 최상위 object 클래스
// 예쁘게 나오고싶니? 그럼 나를 오버라이딩해
// 이거 안하고 syso하면 주소 나옴
@Override
public String toString() {
return "[" + this.num + "]" + this.name + "학생:" + this.score + "점";
}
}
public class StudentArrayList {
public static void main(String[] args) {
ArrayList<Student> datas = new ArrayList<Student>();
int PK = 1001;
datas.add(new Student(PK++, "샘플학생01"));
datas.add(new Student(PK++, "샘플학생02"));
System.out.println(datas); // --> toString()안쓰면 주소값 나옴
// System.out.println(datas.get(1).name); //--> get(인덱스번호) 배열 엔댁스
Scanner sc = new Scanner(System.in);
while (true) {// R
System.out.println("1. 목록출력");
System.out.println("2. 학생검색");
System.out.println("3. 1등출력");
System.out.println("0. 프로그램 종료");
System.out.println("입력 >>> ");
int action = sc.nextInt();
if (action == 0) {
break;
}
// 관리자 모드는 특별한 번호
else if (action == 777) {// CUD
while (true) {
System.out.println("[ 관리자 모드 ]");
System.out.println("1. 학생추가");
System.out.println("2. 정보변경"); // U.재고와관련된부분...
System.out.println("3. 학생삭제");
System.out.println("0. 관리자 모드 종료");
System.out.println("입력 >>> ");
action = sc.nextInt();
if (action == 0) {
break;
} else if (action == 1) {
System.out.println(PK + "번 학생을 추가합니다.");
System.out.println("이름입력 >> ");
String name = sc.next();
datas.add(new Student(PK++, name));
System.out.println("학생추가완료!");
} else if (action == 2) {
// 1001번 학생의 점수를 변경할거야
// 1001번 학생이 어디있는지 알아야함 index 파악
// 기존 : 해당 인덱스의 score를 재설정; --> datas[index].score = 재설정
// 배열리스트 : 로 바꾸면--> datas.get(index).score
System.out.println("변경할 학생의 번호를 입력 >> ");
int num = sc.nextInt();
boolean flag = false;
int i; // scope 이슈 해결하기
for (i = 0; i < datas.size(); i++) {
if (num == datas.get(i).num) { // 내가 입력한 번호가 배열.의 학생.의 번호와 같아?
flag = true;
break;
}
}
if (!flag) {
System.out.println("해당 번호의 학생은 없습니다...");
continue;
}
int score; // scope이슈
while (true) {
System.out.println("재설정할 점수를 입력 >>");
score = sc.nextInt();
if (score >= 0 && score <= 100) {
break;
}
System.out.println("0~100점 사이로 입력해주세요!");
}
datas.get(i).score = score;
System.out.println("정보 변경완료!");
} else if (action == 3) {
// 내가 1001번을 삭제할거야~(이름받으면 안됨 학번 받아야함! 이름 중복도리수도있으니까)
// 1001번의 인덱스 위치를 알아야함
// datas.remove(1001번 인덱스 위치);
System.out.print("삭제할 학생의 번호를 입력 >>");
int num = sc.nextInt();
// 1001번 입력
boolean flag = false;
int i; // scope 이슈 해결하기
for (i = 0; i < datas.size(); i++) {
if (num == datas.get(i).num) { // 내가 입력한 번호가 배열.의 학생.의 번호와 같아?
flag = true;
break;
}
}
if (!flag) {
System.out.println("해당 번호의 학생은 없습니다...");
continue; // 여전히 false일땐 찾지 못하니까 continue;
}
datas.remove(i);
System.out.println("학생삭제완료!");
} else {
System.out.println("다시 입력 >>> ");
}
}
} else if (action == 1) {
// 유효성: 데이터가 없는경우
if (datas.size() == 0) {
System.out.println("데이터 없음");
continue;
}
for (Student data : datas) {
System.out.println(data);
}
} else if (action == 2) {
System.out.println("학번입력 >> ");
int num = sc.nextInt();
boolean flag = false;
for (Student data : datas) {
if (data.num == num) {
System.out.println(data);
flag = true;
break;
}
// 없는 학생 입력
if (flag) {
System.out.println("해당 학번은 검색결과가 없습니다.");
}
}
} else if (action == 3) {
int maxIndex = 0;
for (int i = 0; i < datas.size(); i++) {
if (datas.get(maxIndex).score < datas.get(i).score) {
// datas[maxIndex] == 학생데이터
// datas[maxIndex].score == 학생 점수 데이터
maxIndex = i;
}
}
System.out.println("1등은 " + datas.get(maxIndex) + "입니다.");
} else {
System.out.println("다시 입력 >>> ");
}
}
}
}
코드_ver0.2
package practice01;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
class Student {
int num;
String name;
int score;
static Random random = new Random();
Student(int num, String name) {
this.num = num;
this.name = name;
this.score = Student.random.nextInt(101);
}
@Override
public String toString() {
return "[" + this.num + "] " + this.name + "학생 " + this.score + "점";
}
}
public class StudentArrayList {
public static int hasStudent(ArrayList<Student> datas, int num) {
boolean flag = false;
int i;
for (i = 0; i < datas.size(); i++) {
if (num == datas.get(i).num) {
flag = true;
break;
}
}
if (!flag) {
System.out.println("해당 번호의 학생은 없습니다...");
i = -1;
}
return i;
}
public static void main(String[] args) {
ArrayList<Student> datas = new ArrayList<Student>();
int PK = 1001;
datas.add(new Student(PK++, "샘플01"));
datas.add(new Student(PK++, "샘플02"));
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. 목록출력");
System.out.println("2. 학생검색");
System.out.println("3. 1등출력");
System.out.println("0. 프로그램 종료");
System.out.print("입력 >> ");
int action = sc.nextInt();
if (action == 0) {
break;
}
else if (action == 777) {
while (true) {
System.out.println("=== 관리자 모드 ===");
System.out.println("1. 학생추가");
System.out.println("2. 정보변경");
System.out.println("3. 학생삭제");
System.out.println("0. 관리자 모드 종료");
System.out.print("입력 >> ");
action = sc.nextInt();
if (action == 0) {
break;
}
else if (action == 1) {
System.out.println(PK + "번 학생을 추가합니다.");
System.out.print("이름입력 >> ");
String name = sc.next();
datas.add(new Student(PK++, name));
System.out.println("학생추가완료!");
}
else if (action == 2) {
System.out.print("변경할 학생의 번호를 입력 >> ");
int num = sc.nextInt();
int i = hasStudent(datas, num);
if (i < 0) {
continue;
}
int score;
while (true) {
System.out.print("재설정할 점수를 입력 >> ");
score = sc.nextInt();
if (0 <= score && score <= 100) {
break;
}
System.out.println("0~100점 사이로 입력해주세요!");
}
datas.get(i).score = score;
System.out.println("정보변경완료!");
}
else if (action == 3) {
System.out.print("삭제할 학생의 번호를 입력 >> ");
int num = sc.nextInt();
int i = hasStudent(datas, num);
if (i < 0) {
continue;
}
datas.remove(i);
System.out.println("학생삭제완료!");
}
else {
System.out.println("다시!");
}
}
}
else if (action == 1) {
if (datas.size() == 0) {
System.out.println("데이터없음");
continue;
}
for (Student data : datas) {
System.out.println(data);
}
}
else if (action == 2) {
System.out.print("학번입력 >> ");
int num = sc.nextInt();
boolean flag = false;
for (Student data : datas) {
if (data.num == num) {
System.out.println(data);
flag = true;
break;
}
}
if (flag) {
System.out.println("해당 학번은 검색결과가 없습니다...");
}
}
else if (action == 3) {
int maxIndex = 0;
for (int i = 1; i < datas.size(); i++) {
if (datas.get(maxIndex).score < datas.get(i).score) {
maxIndex = i;
}
}
System.out.println("1등은 " + datas.get(maxIndex) + "입니다.");
}
else {
System.out.println("다시!");
}
}
}
}
코드_ver0.3
package practice01;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
class Student {
// 자바에서 모든 멤버변수는 private 처리하자!
// 접근제어자 private, public
// public : 사용할 코드(호출되어야하는 코드, 메소드!!, 생성자) == 메소드에 붙임
// private : 멤버변수에 붙임
private int num; // PK : 사용자가 선택 xxx 프로그램에서 설정 O
private String name;
private int score;
private static Random random = new Random();
Student(int num, String name) {
this.num = num; // 내 코드 안에서는 멤버접근연산자(.)으로 private 접근 가능
this.name = name;
this.score = Student.random.nextInt(101);
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "[" + this.num + "] " + this.name + "학생 " + this.score + "점";
}
}
public class StudentArrayList {
public static int hasStudent(ArrayList<Student> datas, int num) {
boolean flag = false;
int i;
for (i = 0; i < datas.size(); i++) {
if (num == datas.get(i).getNum()) { // private하니까 멤버접근연산자(.)으로 num 안보임!
flag = true;
break;
}
}
if (!flag) {
System.out.println("해당 번호의 학생은 없습니다...");
i = -1;
}
return i;
}
public static void main(String[] args) {
ArrayList<Student> datas = new ArrayList<Student>();
int PK = 1001;
datas.add(new Student(PK++, "샘플01"));
datas.add(new Student(PK++, "샘플02"));
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. 목록출력");
System.out.println("2. 학생검색");
System.out.println("3. 1등출력");
System.out.println("0. 프로그램 종료");
System.out.print("입력 >> ");
int action = sc.nextInt();
if (action == 0) {
break;
}
else if (action == 777) {
while (true) {
System.out.println("=== 관리자 모드 ===");
System.out.println("1. 학생추가");
System.out.println("2. 정보변경");
System.out.println("3. 학생삭제");
System.out.println("0. 관리자 모드 종료");
System.out.print("입력 >> ");
action = sc.nextInt();
if (action == 0) {
break;
}
else if (action == 1) {
System.out.println(PK + "번 학생을 추가합니다.");
System.out.print("이름입력 >> ");
String name = sc.next();
datas.add(new Student(PK++, name));
System.out.println("학생추가완료!");
}
else if (action == 2) {
System.out.print("변경할 학생의 번호를 입력 >> ");
int num = sc.nextInt();
int i = hasStudent(datas, num);
if (i < 0) {
continue;
}
int score;
while (true) {
System.out.print("재설정할 점수를 입력 >> ");
score = sc.nextInt();
if (0 <= score && score <= 100) {
break;
}
System.out.println("0~100점 사이로 입력해주세요!");
}
datas.get(i).setScore(score);
// datas.get(0).score = 100; 내동생100점
// 멤벼번수의 값을 main()에서
// 객체코드 외부(Student scope 외부)에서 접근이 가능한 상황
// JAVA는 디폴트가 public(공개정책,모두접근가능) 상태
// JAVA의 모든 멤버변수는 private 설정 해야함!!!!!
System.out.println("정보변경완료!");
}
else if (action == 3) {
System.out.print("삭제할 학생의 번호를 입력 >> ");
int num = sc.nextInt();
int i = hasStudent(datas, num);
if (i < 0) {
continue;
}
datas.remove(i);
System.out.println("학생삭제완료!");
}
else {
System.out.println("다시!");
}
}
}
else if (action == 1) {
if (datas.size() == 0) {
System.out.println("데이터없음");
continue;
}
for (Student data : datas) {
System.out.println(data);
}
}
else if (action == 2) {
System.out.print("학번입력 >> ");
int num = sc.nextInt();
boolean flag = false;
for (Student data : datas) {
if (data.getNum() == num) {
System.out.println(data);
flag = true;
break;
}
}
if (flag) {
System.out.println("해당 학번은 검색결과가 없습니다...");
}
}
else if (action == 3) {
int maxIndex = 0;
for (int i = 1; i < datas.size(); i++) {
if (datas.get(maxIndex).getScore() < datas.get(i).getScore()) {
maxIndex = i;
}
}
System.out.println("1등은 " + datas.get(maxIndex) + "입니다.");
}
else {
System.out.println("다시!");
}
}
}
}
'JAVA > JAVA_복습문제' 카테고리의 다른 글
JAVA_예외처리(Exception)_#2 (0) | 2023.12.08 |
---|---|
JAVA_예외처리(Exception)_#1 (0) | 2023.12.08 |
JAVA_인터페이스_#1_TV (+ 싱글톤 패턴) (2) | 2023.12.07 |
JAVA_추상클래스_#2_Pokemon (2) | 2023.12.06 |
JAVA_추상클래스_#1_Shape (1) | 2023.12.06 |