public class Book {
// 필드
// 제목, 저자, 출판사, 가격, 출판일, 절판 여부
String title;
String writer;
String publisher;
int price;
int pubDate;
boolean sell;
}
public class BookUtils {
// Book 객체 하나를 전달받아서 책 정보를 출력하는 기능
void printBook(Book book) {
System.out.println("제 목: " + book.title);
System.out.println("저 자: " + book.writer);
System.out.println("출 판 사: " + book.publisher);
System.out.println("가 격: " + book.price);
System.out.println("출 판 일: " + book.pubDate);
System.out.println("판매여부: " + book.sell);
System.out.println("=======================================");
}
// Book 배열을 전달받아서 판매하지 않는 책만 출력하는 기능
void printBookForSell(Book[] books) {
for (Book book : books) {
if (!book.sell == true) {
printBook(book);
}
}
}
// Book 배열을 전달받아서 가장 비싼 책(Book)을 제공하는 기능
Book getExpensiveBook(Book[] books) {
Book expensiveBook = null;
int expensivePrice = 0;
for (Book book : books) {
if (expensivePrice < book.price) {
expensivePrice = book.price;
expensiveBook = book;
}
}
return expensiveBook;
}
}
public class BookPrinter {
void displayBook(Book book) {
System.out.println("제 목: " + book.title);
System.out.println("저 자: " + book.writer);
System.out.println("출 판 사: " + book.publisher);
System.out.println("가 격: " + book.price);
System.out.println("출 판 일: " + book.pubDate );
System.out.println("판매여부: " + book.sell);
BookPrinter print = new BookPrinter();
}
}
public class BookApp {
public static void main(String[] args) {
// Book 객체 생성 후 책 정보 저장 및 출력하기
Book b1 = new Book();
b1.title = "잃어버린 시간을 찾아서";
b1.writer = "마르셀 프루스트";
b1.publisher = "비상";
b1.price = 10000;
b1.pubDate = 19901023;
b1.sell = true;
BookPrinter printer = new BookPrinter();
printer.displayBook(b1);
/* System.out.println(b1.name);
System.out.println(b1.writer);
System.out.println(b1.price);
System.out.println(b1.publishdate);
System.out.println(b1.sale);
*/
}
}
public class BookApp2 {
public static void main(String[] args) {
Book[] books = new Book[3];
Book book1 = new Book();
book1.title = "이것이 자바다";
book1.writer = "신용권";
book1.publisher = "한빛";
book1.price = 30000;
book1.pubDate = 20190228;
book1.sell = true;
Book book2 = new Book();
book2.title = "자바의 정석";
book2.writer = "남궁성";
book2.publisher = "강산북스";
book2.price = 27000;
book2.pubDate = 20190101;
book2.sell = true;
Book book3 = new Book();
book3.title = "메롱";
book3.writer = "메롱의 저자";
book3.publisher = "메롱의 출판사";
book3.price = 12345;
book3.pubDate = 20190301;
book3.sell = false;
books[0] = book1;
books[1] = book2;
books[2] = book3;
BookUtils util = new BookUtils();
System.out.println("----책 정보 출력 기능----");
util.printBook(book1);
util.printBook(book2);
util.printBook(book3);
System.out.println("----판매하지 않는 책 출력 기능----");
util.printBookForSell(books);
System.out.println("----가장 비싼 책 획득하기----");
Book b = util.getExpensiveBook(books);
util.printBook(b);
}
}
public class MethodOverloading {
void m() {
System.out.println("매개변수 없는 m()이 실행됨");
}
void m(int a) {
System.out.println("int m()이 실행됨");
}
void m(int a, int b) {
System.out.println("m(int a, int b)이 실행됨");
}
void m(String a) {
System.out.println("m(String a)이 실행됨");
}
}
public class MethodOverloadingApp {
public static void main(String[] args) {
MethodOverloading x = new MethodOverloading();
x.m();
x.m(20);
x.m(20, 30);
x.m("홍길동");
}
}
'자바 > oop1' 카테고리의 다른 글
Car(App), Item(App), Student(App), Account(App), Hello(App), Calculator(App), ArrayUtils(App).java (0) | 2019.06.07 |
---|