java ArrayList Book 객체 관리
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
class Book{
private String title;
private String author;
private int price;
Book(){
}
Book(String title,String author, int price){
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return title + ","+ author + ","+ price;
}
}
public class Book_test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader ( new InputStreamReader(System.in)); // 빈공백도 읽은 enter 치기전까지
Scanner sc = new Scanner(System.in);
ArrayList<Book> list = new ArrayList<Book>();
do{
System.out.print("제목 : ");
String title =br.readLine();
System.out.print("저자 : ");
String author = br.readLine();
System.out.print("가격 : ");
int price = sc.nextInt();
Book b = new Book(title,author,price);
list.add(b);
System.out.println("계속?");
String con = sc.next();
if(!con.equals("y")){
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
System.out.print("찾는 제목 : ");
String search =br.readLine();
boolean flag = false;
int i = 0;
for(i=0;i<list.size();i++){
if(list.get(i).getTitle().equals(search)){
System.out.println(list.get(i).getAuthor()+","+list.get(i).getPrice());
flag=true;
break;
}
}
if(flag==false){
System.out.println("찾는제목이 없습니다.");
}
break;
}
}while(true);
}
}
결과
계속?을 물었을때 y가 아닌값이면 반복문에서 빠져나오도록,
title, author, price 를 매개변수로 받아 객체생성
y값을 받지 않았을때 출력을 해주고 프로그램 종료
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
class Book{
private String title;
private String author;
private int price;
Book(){
}
Book(String title,String author, int price){
this.title = title;
this.author = author;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return title + ","+ author + ","+ price;
}
}
public class Book_test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader ( new InputStreamReader(System.in)); // 빈공백도 읽은 enter 치기전까지
Scanner sc = new Scanner(System.in);
ArrayList<Book> list = new ArrayList<Book>();
do{
System.out.print("제목 : ");
String title =br.readLine();
System.out.print("저자 : ");
String author = br.readLine();
System.out.print("가격 : ");
int price = sc.nextInt();
Book b = new Book(title,author,price);
list.add(b);
System.out.println("계속?");
String con = sc.next();
if(!con.equals("y")){
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
System.out.print("찾는 제목 : ");
String search =br.readLine();
boolean flag = false;
int i = 0;
for(i=0;i<list.size();i++){
if(list.get(i).getTitle().equals(search)){
System.out.println(list.get(i).getAuthor()+","+list.get(i).getPrice());
flag=true;
break;
}
}
if(flag==false){
System.out.println("찾는제목이 없습니다.");
}
break;
}
}while(true);
}
}
결과
계속?을 물었을때 y가 아닌값이면 반복문에서 빠져나오도록,
title, author, price 를 매개변수로 받아 객체생성
y값을 받지 않았을때 출력을 해주고 프로그램 종료
댓글
댓글 쓰기