package day15_TotalScore;
public class ScoreDTO {
//이름,국어,영어,수학 -> 총점,평균,학점
String name;
int kor,eng,math;
int tot;
double avg;
char hak;
public ScoreDTO() {}
public ScoreDTO(String name, int kor, int eng, int math) {
super();
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public char getHak() {
return hak;
}
public void setHak(char hak) {
this.hak = hak;
}
}
package day15_TotalScore;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScoreMenu {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
ScoreDAO dao = new ScoreDAO();
while(true) {
System.out.println("[0]작업종료"+"\t"+"[1]데이터입력"+"\t"+"[2]데이터출력");
System.out.println("[3]데이터검색"+"\t"+"[4]데이터삭제"+"\t"+"[5]데이터수정");
System.out.print("원하는 작업번호를 입력");
int n = sc.nextInt();
switch (n) {
case 0:System.exit(0);break;
case 1:dao.insert();break;
case 2:dao.out();break;
case 3:dao.select();break;
case 4:dao.delete();break;
case 5:dao.update();break;
}
}
}
}
package day15_TotalScore;
import java.util.Scanner;
public class Input {
public String munja(String str, Scanner sc) {
System.out.println(str+"를 입력하시오");
return sc.next();
}
public int number(String str, Scanner sc) {
System.out.println(str+"를 입력하시오");
return sc.nextInt();
}
}
package day15_TotalScore;
public class Process {
public int tot(int kor, int eng, int math) {
return (kor+eng+math);
}
public double avg(int tot) {
return (double)tot/3;
}
public char hak(double avg) {
char hak;
switch ((int)avg/10) {
case 10:
case 9:hak='A';break;
case 8:hak='B';break;
case 7:hak='C';break;
case 6:hak='D';break;
default:hak='F';break;
}
return hak;
}
}
package day15_TotalScore;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class ScoreDAO {
ArrayList<ScoreDTO> list = new ArrayList<ScoreDTO>();
Input in = new Input();
Scanner sc = new Scanner(System.in);
Process pr = new Process();
//입력기능
public void insert() {
ScoreDTO dto = new ScoreDTO();
String name = in.munja("이름", sc);
int kor = in.number("국어", sc);
int eng = in.number("영어", sc);
int math = in.number("수학", sc);
dto.setName(name);
dto.setKor(kor);
dto.setEng(eng);
dto.setMath(math);
dto.setTot(pr.tot(kor, eng, math));
dto.setAvg(pr.avg(dto.getTot()));
dto.setHak(pr.hak(dto.getAvg()));
list.add(dto);
}
//출력기능
public void out() {
DecimalFormat df = new DecimalFormat("#,##0.00");
System.out.println("이름\t국어\t영어\t수학\t총점\t평균\t학점");
System.out.print("===========================");
System.out.println("=========================");
for(ScoreDTO a:list) {
System.out.print(a.getName()+"\t");
System.out.print(a.getKor()+"\t");
System.out.print(a.getEng()+"\t");
System.out.print(a.getMath()+"\t");
System.out.print(a.getTot()+"\t");
System.out.print(df.format(a.getAvg())+"\t");
System.out.println(a.getHak());
}
}
//검색기능
public void select() {
int po=-1; // 위치값이 -1 ..없는자료일것
System.out.print("검색할 이름을 입력");
String sname = sc.next();
//검색하고자 하는 이름을 list 객체저장소에서 찾기
for(int i=0;i<list.size();i++) {
if(list.get(i).getName().equals(sname)) {
po=i; //찾는 이름이 존재할때 존재하는 이름의 위치
}
}
if(po==-1) {
System.out.println(sname+"은 없는 자료입니다.");
}
else { //검색할 이름이 존재할경우
System.out.println("이름 : "+list.get(po).getName());
System.out.println("총점 : "+list.get(po).getTot());
System.out.println("평균 : "+list.get(po).getAvg());
}
}
//삭제기능
public void delete() {
int po=-1;
System.out.print("삭제할 이름을 입력");
String dname = sc.next();
for(int i=0;i<list.size();i++) {
if(list.get(i).getName().equals(dname)) {
po=i;
}
}
if(po==-1) {
System.out.println(dname+"은 없는 자료입니다.");
}
else {
System.out.println("이름 : "+list.get(po).getName());
System.out.println("총점 : "+list.get(po).getTot());
System.out.println("평균 : "+list.get(po).getAvg());
System.out.println("학점 : "+list.get(po).getHak());
System.out.print(dname+"을 정말 삭제할까요?(Y/N)");
String qq=sc.next();
if(qq.equals("Y")||qq.equals("y")||qq.equals("ㅛ")) {
list.remove(po);
System.out.println("데이터가 삭제되었습니다.");
}
}
}
//수정기능
public void update() {
int po=-1;
System.out.print("수정할 이름을 입력");
String uname = sc.next();
for(int i=0;i<list.size();i++) {
if(list.get(i).getName().equals(uname)) {
po=i;
}
}
if(po==-1) {
System.out.println(uname+"은 없는 자료입니다.");
}
else { //수정할 이름 존재..화면에 자료 보이기
System.out.println(uname+"의 자료보기");
System.out.print(" 국어 : "+list.get(po).getKor()+"\t");
System.out.print(" 영어 : "+list.get(po).getEng()+"\t");
System.out.println(" 수학 : "+list.get(po).getMath()+"\t");
System.out.println("위의 자료를 수정할까요?(Y/N)");
String qq = sc.next();
if(qq.equals("Y")||qq.equals("y")||qq.equals("ㅛ"))
{//국어,,영어,,수학 다시 입력
int kor = in.number("국어", sc);
int eng = in.number("영어", sc);
int math = in.number("수학", sc);
ScoreDTO dto = new ScoreDTO();
dto.setName(uname);
dto.setKor(kor);
dto.setEng(eng);
dto.setMath(math);
dto.setTot(pr.tot(kor, eng, math));
dto.setAvg(pr.avg(dto.getTot()));
dto.setHak(pr.hak(dto.getAvg()));
list.set(po, dto);
}
}
}
}
'JAVA' 카테고리의 다른 글
221227_static (0) | 2022.12.27 |
---|---|
221226_배열 (0) | 2022.12.26 |
221222_상속 (0) | 2022.12.22 |
221221_Interface,상속 (0) | 2022.12.21 |
221220_Interface (0) | 2022.12.20 |
댓글