[Java] 3권의 책을 꽂을 수 있는 책장이 2개 있는 서점을 표현하자. | 배열

Jeini·2022년 11월 18일
0

📌 Code list

목록 보기
25/55
post-thumbnail
//책
class Book {
	String title;
	int price;

	public Book(String title, int price) {
		this.title = title;
		this.price = price;
	}
	@Override
	public String toString() {
		return title + "(" + price + ")";
	}
}
//책장
class Bookcase {
	String category;
	Book[] books;

	public Bookcase(String category, Book... books) {
		this.category = category;
		this.books = books;
	}
	@Override
	public String toString() {
		String info = "- category : " + category + "\n";
		info += "- book list\n";
		for(Book temp: books) {
			info += "\t" + temp + "\n";
		}
		return info;
	}
}
//서점
class Bookstore {
	String name;
	String tel;
	Bookcase[] cases;

	public Bookstore(String name, String tel, Bookcase...cases) {
		this.name = name;
		this.tel = tel;
		this.cases = cases;
	}
	@Override
	public String toString() {
		String info = "<< " + name + " bookstore >>\n";
		info += "- tel : " + tel + "\n";
		for(Bookcase temp : cases) {
			info += temp + "\n";
		}
		return info;
	}
}

class BookCaseTest {
	public static void main(String[] args) {
		Bookcase case1 = new Bookcase(
			"IT",
			new Book("power java", 35000), 
			new Book("ultra java", 38000), 
			new Book("hyper java", 41000)
		);

		Bookcase case2 = new Bookcase(
			"novel",
			new Book("춘식이는 최고야", 35000), 
			new Book("춘식이는 집순이", 38000), 
			new Book("춘식이는 바보", 41000)
		);

		Bookstore store = new Bookstore("green", "051-900-1000", case1, case2);
		System.out.println(store);

	}
}
[결과값]
<< green bookstore >>
- tel : 051-900-1000
- category : IT
- book list
        power java(35000)
        ultra java(38000)
        hyper java(41000)

- category : novel
- book list
        춘식이는 최고야(35000)
        춘식이는 집순이(38000)
        춘식이는 바보(41000)
profile
Fill in my own colorful colors🎨

0개의 댓글