ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 15일 차 도서 대여 프로그램
    삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 16:56
    반응형

    package book;


    public abstract class Book {


    private int id;

    private String name;

    private String author;

    private boolean rental;


    public boolean equlas(int id) {


    if (this.id == id)

    return true;

    else

    return false;


    }


    public Book() {

    id = 0;

    name = "제목없음";

    author = "저자미상";

    rental = false;

    }


    public Book(int id, String name, String author) {

    this.id = id;

    this.name = name;

    this.author = author;

    rental = false;

    }


    public abstract int getLateFees(int days);


    public int getId() {

    return id;

    }


    public void setId(int id) {

    this.id = id;

    }


    public String getName() {

    return name;

    }


    public void setName(String name) {

    this.name = name;

    }


    public String getAuthor() {

    return author;

    }


    public void setAuthor(String author) {

    this.author = author;

    }


    public boolean isRental() {

    return rental;

    }


    public void setRental(boolean rental) {

    this.rental = rental;

    }


    @Override

    public String toString() {

    return "Book [id=" + id + ", name=" + name + ", author=" + author + ", rental=" + rental + "]";

    }


    }




    package book;

    public class Novel extends Book {

    @Override
    public int getLateFees(int days) {
    // TODO Auto-generated method stub

    days = 700;
    return 3 * days;
    }
    }

    package book;

    public class Poet extends Book {

    @Override
    public int getLateFees(int days) {
    // TODO Auto-generated method stub

    days = 300;
    return 3 * days;
    }
    }


    package book;

    public class ScienceFiction extends Book {

    @Override
    public int getLateFees(int days) {
    // TODO Auto-generated method stub

    days = 500;
    return 3 * days;
    }
    }







    package book;

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

    public class InputTest {

    public static void main(String[] args) {
    ArrayList<Book> bookList = new ArrayList<Book>();
    int num;

    do {
    Scanner scan = new Scanner(System.in);

    System.out.println("-----------------");
    System.out.println("번호를 입력하세요");
    System.out.println("-----------------");
    System.out.println("1.도서추가      |");
    System.out.println("2.도서삭제      |");
    System.out.println("3.도서조회      |");
    System.out.println("4.도서대여      |");
    System.out.println("5.도서반납      |");
    System.out.println("6.종료          |");
    System.out.println("-----------------");

    num = scan.nextInt();
    scan.nextLine();
    switch (num) {

    case 1:
    System.out.println("id 입력");
    int id = scan.nextInt();
    scan.nextLine();

    System.out.println("책 이름 입력");
    String name = scan.nextLine();

    System.out.println("저자 입력");
    String author = scan.nextLine();

    System.out.println("1.소설책 2.과학책 3.시");
    int category = scan.nextInt();
    scan.nextLine();

    Book b;

    switch (category) {
    case 1:
    b = new Novel();
    b.setId(id);
    b.setName(name);
    b.setAuthor(author);
    bookList.add(b);
    break;

    case 2:
    b = new ScienceFiction();
    b.setId(id);
    b.setName(name);
    b.setAuthor(author);
    bookList.add(b);
    break;

    case 3:
    b = new Poet();
    b.setId(id);
    b.setName(name);
    b.setAuthor(author);
    bookList.add(b);
    break;
    }
    System.out.println("저장되었습니다");
    break;

    case 2:

    System.out.println("삭제할 책의 id를 입력하세요");
    int s = scan.nextInt();
    for (int i = 0; i < bookList.size(); i++) {
    if (s == bookList.get(i).getId())
    bookList.remove(i);
    }
    System.out.println("삭제 되었습니다.");
    break;

    case 3:

    for (Book bo : bookList)
    System.out.println(bo);
    break;

    case 4:

    System.out.println("대여할 책 아이디를 입력하세요");
    int q = scan.nextInt();
    scan.nextLine();
    boolean isExist = false;

    for (int i = 0; i < bookList.size(); i++) {
    if (q == bookList.get(i).getId())
    if (bookList.get(i).isRental()) {
    System.out.println("이미 대여중입니다");
    } else if ((!bookList.get(i).isRental())) {
    System.out.println("정상적으로 대여 되었습니다");
    bookList.get(i).setRental(true);
    }
    }
    // if (!isExist)
    // System.out.println("해당 도서가 존재하지 않습니다");
    break;

    case 5:

    System.out.println("반납할 책 아이디를 입력하세요");
    int w = scan.nextInt();
    scan.nextLine();
    boolean isExist2 = false;

    for (int i = 0; i < bookList.size(); i++) {
    if (w == bookList.get(i).getId())
    if (!bookList.get(i).isRental()) {
    System.out.println("대여중이 아닙니다");
    } else if (bookList.get(i).isRental()) {
    System.out.println("정상적으로 반납 되었습니다");
    bookList.get(i).setRental(false);
    System.out.println("연체료 :" + bookList.get(i).getLateFees(3));
    }

    }
    // if (!isExist2)
    // System.out.println("해당 도서가 존재하지 않습니다");
    break;

    case 6:

    System.exit(0);
    break;
    }
    } while (num != 6);
    }
    }





    ----------------------------------------------------------------------------------------

    Book클래스를 추상클래스로 만들고

    소설, 시, 과학클래스에 상속받아서

    추상메소드를 구현하고

    메인에서는 입력받은 값을 구현한다.


    지금은 소스정리를 안해서 매우매우 더러운 상태인데 

    차차 시간은 들여서 깨끗하게 만들어보겠습니다.


    혹시몰라서 첨부파일 동봉하니

    import해서 쓰실분은 쓰셔도 괜찮습니다.





    ----------------------------------------------------------------------------------------------------

    1차 수정



    package book;

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

    public class InputTest {

    static Scanner scan = new Scanner(System.in);
    static ArrayList<Book> bookList = new ArrayList<Book>();
    static int num;

    public static void main(String[] args) {

    do {

    num = printMenu();
    switch (num) {

    case 1:
    plus();
    break;

    case 2:
    delete();
    break;

    case 3:
    for (Book bo : bookList)
    System.out.println(bo);
    break;

    case 4:

    rent();
    break;

    case 5:
    gift();
    break;

    case 6:

    System.exit(0);
    break;
    }
    } while (num != 6);
    }

    public static int printMenu() {

    System.out.println("-----------------");
    System.out.println("번호를 입력하세요");
    System.out.println("-----------------");
    System.out.println("1.도서추가      |");
    System.out.println("2.도서삭제      |");
    System.out.println("3.도서조회      |");
    System.out.println("4.도서대여      |");
    System.out.println("5.도서반납      |");
    System.out.println("6.종료          |");
    System.out.println("-----------------");

    num = scan.nextInt();
    scan.nextLine();
    return num;
    }

    public static void plus() {
    System.out.println("id 입력");
    int id = scan.nextInt();
    scan.nextLine();

    System.out.println("책 이름 입력");
    String name = scan.nextLine();

    System.out.println("저자 입력");
    String author = scan.nextLine();

    System.out.println("1.소설책 2.과학책 3.시");
    int category = scan.nextInt();
    scan.nextLine();

    Book b;

    switch (category) {
    case 1:
    b = new Novel();
    b.setId(id);
    b.setName(name);
    b.setAuthor(author);
    bookList.add(b);
    break;

    case 2:
    b = new ScienceFiction();
    b.setId(id);
    b.setName(name);
    b.setAuthor(author);
    bookList.add(b);
    break;

    case 3:
    b = new Poet();
    b.setId(id);
    b.setName(name);
    b.setAuthor(author);
    bookList.add(b);
    break;
    }
    System.out.println("저장되었습니다");

    }

    public static void delete() {
    System.out.println("삭제할 책의 id를 입력하세요");
    int s = scan.nextInt();
    for (int i = 0; i < bookList.size(); i++) {
    if (s == bookList.get(i).getId())
    bookList.remove(i);
    }
    System.out.println("삭제 되었습니다.");
    }

    public static void rent() {
    System.out.println("대여할 책 아이디를 입력하세요");
    int q = scan.nextInt();
    scan.nextLine();
    boolean isExist = false;

    for (int i = 0; i < bookList.size(); i++) {
    if (q == bookList.get(i).getId())
    if (bookList.get(i).isRental()) {
    System.out.println("이미 대여중입니다");
    } else if ((!bookList.get(i).isRental())) {
    System.out.println("정상적으로 대여 되었습니다");
    bookList.get(i).setRental(true);
    }
    }
    // if (!isExist)
    // System.out.println("해당 도서가 존재하지 않습니다");
    }

    public static void gift() {
    System.out.println("반납할 책 아이디를 입력하세요");
    int w = scan.nextInt();
    scan.nextLine();
    boolean isExist2 = false;

    for (int i = 0; i < bookList.size(); i++) {
    if (w == bookList.get(i).getId())
    if (!bookList.get(i).isRental()) {
    System.out.println("대여중이 아닙니다");
    } else if (bookList.get(i).isRental()) {
    System.out.println("정상적으로 반납 되었습니다");
    bookList.get(i).setRental(false);
    System.out.println("연체료 :" + bookList.get(i).getLateFees(3));
    }

    }
    // if (!isExist2)
    // System.out.println("해당 도서가 존재하지 않습니다");
    }

    }
    -----------------------------------------------------------------------------------------------------------------









    반응형
Designed by Tistory.