7월9일 JAVA(학점관리,도서관리예외처리)
2010. 7. 9. 11:37ㆍ2010년/7월
import java.io.*;
import java.util.*;
public class uni{
public static void main(String []args)throws IOException{
BufferedReader stdin = new BufferedReader //입력받는부분
(new InputStreamReader (System.in));
int state,sub; //선택select 숫자
String sname,smajor; //이름 , 전공
String snumber; //학번
Vector str = new Vector(); //벡터생성
while(true)
{
System.out.println("1. 입력");
System.out.println("2. 출력");
System.out.println("3. 종료");
state=Integer.parseInt(stdin.readLine());//입력
System.out.flush(); //입력버퍼비우기
switch(state)
{
case 1:
while(true)
{
System.out.println("****입력모드****");
System.out.println("\t 1. 학부생");
System.out.println("\t 2. 대학원생");
System.out.println("\t 3. 종료 ");
System.out.print("choose : ");
sub=Integer.parseInt(stdin.readLine());
System.out.flush();
if(sub==3)
{
break;
}
switch(sub)
{
case 1:
System.out.print("\t이름 : ");
sname = stdin.readLine();
System.out.flush();
System.out.print("\t학번 : ");
snumber =stdin.readLine();
System.out.flush();
System.out.print("\t전공 : ");
smajor = stdin.readLine();
System.out.flush();
department dep = new department(sub,sname,snumber,smajor);
System.out.print("\tDB 성적 : ");
dep.DB =Double.parseDouble(stdin.readLine());
System.out.print("\tOS 성적 : ");
dep.OS =Double.parseDouble(stdin.readLine());
System.out.print("\tNW 성적 : ");
dep.NW =Double.parseDouble(stdin.readLine());
dep.recode();
dep.CDB=dep.averg(dep.DB);
dep.CNW=dep.averg(dep.NW);
dep.COS=dep.averg(dep.OS);
dep.sum=dep.sumall(dep.DB,dep.NW,dep.OS);
dep.CALL=dep.averg(dep.sum/3);
str.addElement(dep);
break;
case 2:
System.out.print("\t이름 : ");
sname = stdin.readLine();
System.out.flush();
System.out.print("\t학번 : ");
snumber =stdin.readLine();
System.out.flush();
System.out.print("\t전공 : ");
smajor = stdin.readLine();
System.out.flush();
superdepart sep = new superdepart(sub,sname,snumber,smajor);
System.out.print("\tDB 성적 : ");
sep.DB =Double.parseDouble(stdin.readLine());
System.out.print("\tOS 성적 : ");
sep.OS =Double.parseDouble(stdin.readLine());
System.out.print("\tNW 성적 : ");
sep.NW =Double.parseDouble(stdin.readLine());
sep.recode();
sep.CDB=sep.averg(sep.DB);
sep.CNW=sep.averg(sep.NW);
sep.COS=sep.averg(sep.OS);
sep.sum=sep.sumall(sep.DB,sep.NW,sep.OS);
sep.CALL=sep.averg(sep.sum/3);
str.addElement(sep);
break;
}
}
break;
case 2:
if((str.isEmpty())!=false)
{
System.out.println("검색되는 학생이없습니다");
break;
}
System.out.println("********학생정보********");
System.out.println("정보\t이름\t학번\t전공\t DB\t OS\t NW\t총점\t평점");
for(int i=0 ;i<str.size();i++)
{
((StudentInformation)str.get(i)).print();
}
break;
case 3:
System.out.println("종료합니다.....");
return ;
}
}
}
}
interface addition
{
double NW_OS_BALL= 0.05; //학부생 가산점
double DB_PLUS =0.2; //대학원생 가산점
}
class StudentInformation implements addition//학생정보
{
protected String name; //이름
protected int state; //학부/대학원생
protected String number;//학번
protected String major,hagbu; // 전공//학부//대학원생
protected String CDB,COS,CNW,CALL; //각 수업 성적(A+...)
protected double DB; //DB성적
protected double OS; //OS성적
protected double NW; //NW성적
public double sum; //성적합계
StudentInformation(int astate,String sname, String inumber, String smajor,String ahagbu)
{ //생성자
name = sname;
number = inumber;
major = smajor;
state = astate;
hagbu =ahagbu;
}
protected void print()
{
//출력
System.out.print(hagbu+"\t");
System.out.print(name+"\t");
System.out.print(number+"\t");
System.out.print(major+"\t");
System.out.print(DB);
System.out.print("["+CDB+"]");
System.out.print(" "+OS);
System.out.print("["+COS+"]");
System.out.print(" "+NW);
System.out.print("["+CNW+"]");
System.out.print(" "+sum+"\t");
System.out.println(CALL);
}
public void recode() //학과비교 가산점
{
if(this.major.equals("컴공"))
{
this.NW =this.NW+this.NW*NW_OS_BALL;
}
else if(this.major.equals("컴과"))
{
this.OS = this.OS+this.OS*NW_OS_BALL;
}
}
public String averg(double avr) //성적출력
{
if((avr>=95)) return "A+";
else if((avr<95)&&(avr>=90)) return "A";
else if((avr<90)&&(avr>=85)) return "B+";
else if((avr<85)&&(avr>=80)) return "B";
else if((avr<80)&&(avr>=70)) return "C";
else if((avr<70)&&(avr>=60)) return "D";
else if((avr<60)&&(avr>=0)) return "F";
return "-F";
}
public double sumall(double a,double b,double c) //총점
{
return a+b+c;
}
}
class department extends StudentInformation //학부
{
private String hagbu="학부";
public department(int astate,String sname,String inumber, String smajor)
{
super(astate,sname,inumber,smajor,"학부");
}
protected void print() //출력
{
System.out.print(hagbu+"\t");
System.out.print(name+"\t");
System.out.print(number+"\t");
System.out.print(major+"\t");
System.out.print(DB);
System.out.print("["+CDB+"]");
System.out.print(" "+OS);
System.out.print("["+COS+"]");
System.out.print(" "+NW);
System.out.print("["+CNW+"]");
System.out.print(" "+sum+"\t");
System.out.println(CALL);
}
public void recode() //전공비교
{
if(this.major.equals("컴공"))
{
this.NW =this.NW+this.NW*NW_OS_BALL;
}
else if(this.major.equals("컴과"))
{
this.OS = this.OS+this.OS*NW_OS_BALL;
}
}
}
class superdepart extends StudentInformation //대학원
{
private String hagbu="대학원";
public superdepart(int astate,String sname,String inumber, String smajor)
{
super(astate,sname,inumber,smajor,"대학원");
}
protected void print()
{
System.out.print(hagbu+"\t");
System.out.print(name+"\t");
System.out.print(number+"\t");
System.out.print(major+"\t");
System.out.print(DB);
System.out.print("["+CDB+"]");
System.out.print(" "+OS);
System.out.print("["+COS+"]");
System.out.print(" "+NW);
System.out.print("["+CNW+"]");
System.out.print(" "+sum+"\t");
System.out.println(CALL);
}
public void recode()
{
if(this.major.equals("NW"))
{
this.NW =this.NW+this.NW*DB_PLUS;
}
else if(this.major.equals("OS"))
{
this.OS = this.OS+this.OS*DB_PLUS;
}
else if(this.major.equals("DB"))
{
this.DB = this.DB+this.DB*DB_PLUS;
}
}
}
======================================================
======================================================
예외처리에서는 Throwable 클래스에서 파생되는 Error클래스와
Exception클래스가 잇다. 그리고 그밑에서는 여러가지 예외가 있고
처리할수가 있다.
====================================================================
====================================================================
7장 연습문제 5장의 도서관리를 예외처리로 바꾸서 다시 만들었다.
import java.io.*;
import java.util.*;
class Library {
public static void main(String[] args) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
Vector Booklist = new Vector(5, 5);
int istate = 0; // 선택 변수
String sname;
String swri;
String scom;
int ipri=0;
int i;
int a = -1;
int b = -1;
while (true) {
try
{
System.out.println("****원하는 서비스를 선택하세요****");
System.out.println();
System.out.println("\t1. Search Book.");
System.out.println("\t2. Add Book.");
System.out.println("\t3. Delete Book.");
System.out.println("\t4. List Book.");
System.out.println("\t5. Quit");
System.out.println();
System.out.print("Number :");
istate = Integer.parseInt(stdin.readLine());
System.out.flush();
System.out.println();
if(istate>5||istate<1)
{
throw new MyException("throw an Exception");
}
}catch(NumberFormatException exp)
{
System.out.println("숫자로 입력하세요");
}catch(MyException exp)
{
System.out.println("1~5사이의 숫자로입력하세요");
}
switch (istate) {
case 2: // 추가
System.out.println();
System.out.println("Adding Book!");
System.out.println();
System.out.print("\tBook Name : ");
sname = stdin.readLine();
System.out.flush();
System.out.print("\tAuthor : ");
swri = stdin.readLine();
System.out.flush();
System.out.print("\tPublisher : ");
scom = stdin.readLine();
System.out.flush();
while(true)
{
try
{
System.out.print("\tPrice : ");
ipri = Integer.parseInt(stdin.readLine());
Integer Int = new Integer(ipri);
System.out.flush();
if(Int.equals(ipri))
{
break;
}
}catch(NumberFormatException axp)
{
System.out.println("숫자로 다시입력하세요");
}
}
BOOK bk = new BOOK(sname, swri, scom, ipri);
Booklist.addElement(bk.bookname);
Booklist.addElement(bk.writer);
Booklist.addElement(bk.company);
Booklist.addElement(bk.price);
break;
case 1: // 검색
System.out.println();
System.out.println("Searching Book!");
System.out.println();
System.out.print("\tBook Name : ");
sname = stdin.readLine();
System.out.flush();
a = Booklist.indexOf(sname);
if (a != -1) {
System.out.println("=========검색==============");
System.out
.println("\tBook Name : " + Booklist.elementAt(a));
System.out.println("\tAuthor : "
+ Booklist.elementAt(a + 1));
System.out.println("\tPublisher : "
+ Booklist.elementAt(a + 2));
System.out
.println("\tPrice : " + Booklist.elementAt(a + 3));
System.out.println("===========================");
} else {
System.out.println("검색되는 책이없습니다");
}
break;
case 3: // 삭제
System.out.println();
System.out.println("Deleting Book!");
System.out.println();
System.out.print("\tBook Name : ");
sname = stdin.readLine();
System.out.flush();
a = Booklist.indexOf(sname);
System.out.println(a);
Booklist.remove(Booklist.get(a)); // remove될때 반이 되면.. 자동으로 순서가
// 바뀐다.
Booklist.remove(Booklist.get(a + 1));
Booklist.remove(Booklist.get(a));
Booklist.remove(Booklist.get(a));
break;
case 4: // 전체검색
if ((Booklist.isEmpty()) != false) {
System.out.println("검색되는 책이없습니다");
break;
}
System.out.println("=========검색==============");
for (i = 0; i < Booklist.size(); i = i + 4) {
System.out
.println("\tBook Name : " + Booklist.elementAt(i));
System.out.println("\tAuthor : "
+ Booklist.elementAt(i + 1));
System.out.println("\tPublisher : "
+ Booklist.elementAt(i + 2));
System.out
.println("\tPrice : " + Booklist.elementAt(i + 3));
System.out.println("===========================");
}
break;
case 5:
System.out.println("종료합니다");
return;
}
}
}
}
class BOOK {
String bookname;
String writer;
String company;
int price;
public BOOK(String bo, String wr, String co, int pr) {
bookname = bo;
writer = wr;
company = co;
price = pr;
}
}
class MyException extends Throwable {
public MyException() {
}; // default 생성자
public MyException(String myMessage) {
super(myMessage);
}
}
'2010년 > 7월' 카테고리의 다른 글
7월13일 그래픽인터페이스(inner클래스) (0) | 2010.07.13 |
---|---|
7월12일 그래픽(Toolkit ,Dimension (0) | 2010.07.12 |
7월8일 JAVA(도서관리) (0) | 2010.07.09 |
7월7일 안드로이드 JAVA(도서관리) (0) | 2010.07.07 |
7월6일 JAVA(java 입출력) (0) | 2010.07.06 |