객체 vs 객체배열
package com.kh.chap01_oneVsmany.model.vo;
public class Book {
private String title;
private String author;
private String publisher;
private int price;
public Book() {}
public Book(String title, String author ,String publisher, int price) {
this.title = title;
this.author = author;
this.publisher=publisher;
this.price = price;
}
public void setTitle(String title) {
this.title = title;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public void setPrice(int price) {
this.price = price;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public int getPrice() {
return price;
}
public String information() {
return "title : "+title+", author : "+author+
", publisher : "+publisher + ", price: "+price;
}
}
1. 객체 사용
package com.kh.chap01_oneVsmany.run;
import java.util.Scanner;
import com.kh.chap01_oneVsmany.model.vo.Book;
public class ObjectRun {
public static void main(String[] args) {
Book bk1;
Book bk2;
Book bk3;
Book bk1 = null;
Book bk2 = null;
Book bk3 = null;
System.out.println(bk1);
System.out.println(bk2);
System.out.println(bk3);
Scanner sc = new Scanner(System.in);
for(int i=0; i<3; i++) {
System.out.print("제목 : ");
String title = sc.nextLine();
System.out.print("저자명 : ");
String author = sc.nextLine();
System.out.print("출판사 : ");
String publisher = sc.nextLine();
System.out.print("가격 : ");
int price = sc.nextInt();
sc.nextLine();
if(i==0) {
bk1 = new Book(title, author, publisher, price);
}else if(i==1) {
bk2 = new Book(title, author, publisher, price);
}else {
bk3 = new Book(title, author, publisher, price);
}
}
System.out.println(bk1.information());
System.out.println(bk2.information());
System.out.println(bk3.information());
System.out.print("검색할 책 제목 : ");
String searchTitle = sc.nextLine();
if(searchTitle.equals(bk1.getTitle())) {
System.out.println(bk1.getPrice());
}
if(searchTitle.equals(bk2.getTitle())) {
System.out.println(bk2.getPrice());
}
if(searchTitle.equals(bk3.getTitle())) {
System.out.println(bk3.getPrice());
}
}
}
2. 객체배열 사용
package com.kh.chap01_oneVsmany.run;
import java.util.Scanner;
import com.kh.chap01_oneVsmany.model.vo.Book;
public class ObjectArrayRun {
public static void main(String[] args) {
Book[] books = new Book[3];
Scanner sc = new Scanner(System.in);
for(int i=0; i<books.length; i++) {
System.out.print("제목 : ");
String title = sc.nextLine();
System.out.print("저자 : ");
String author = sc.nextLine();
System.out.print("출판사 : ");
String publisher = sc.nextLine();
System.out.print("가격 : ");
int price = sc.nextInt();
sc.nextLine();
books[i] = new Book(title,author,publisher,price);
}
for(int i=0; i<books.length;i++) {
System.out.println(books[i].information());
}
System.out.print("책제목 : ");
String searchTitle = sc.nextLine();
for(int i=0; i<books.length;i++) {
if(searchTitle.equals(books[i].getTitle())) {
System.out.println(books[i].getPrice());
}
}
}
}
객체배열 응용
package com.kh.chap02_objectArray.model.vo;
public class Phone {
private String name;
private String series;
private String brand;
private int price;
public Phone() {}
public Phone(String name, String series, String brand, int price) {
this.name = name;
this.series = series;
this.brand = brand;
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public void setSeries(String series) {
this.series = series;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void setPrice(int price) {
this.price = price;
}
public String getName() {
return name;
}
public String getSeries() {
return series;
}
public String getBrand() {
return brand;
}
public int getPrice() {
return price;
}
public String information() {
return "name : "+name + ", series : "+series + ", brand : "+brand + ", price : "+price;
}
}
package com.kh.chap02_objectArray.run;
import com.kh.chap02_objectArray.model.vo.Phone;
public class ObjectArrayRun {
public static void main(String[] args) {
Phone[] arr = new Phone[3];
Phone p1 = new Phone();
arr[0] = p1;
System.out.println(p1);
p1 = null;
System.out.println(p1);
arr[0].setName("갤럭시");
arr[0].setBrand("삼성");
arr[0].setPrice(1000000)
System.out.println(arr[0].information());
arr[1] = new Phone("아이폰","11pro","애플",1000000);
System.out.println(arr[1]);
System.out.println(arr[1].information());
System.out.println("---- 총 마무리 ----");
for(int i=0; i<arr.length; i++) {
System.out.println(arr[i]);
}
for(int i=0; i<arr.length; i++) {
System.out.println(arr[i].information());
}
for(Phone p : list) {
System.out.println(p.information());
}
}
}
package com.kh.chap02_objectArray.run;
import java.util.Scanner;
import com.kh.chap02_objectArray.model.vo.Phone;
public class PhoneRun {
public static void main(String[] args) {
System.out.println("-----당신 휴대폰 -----");
Phone[] arr = new Phone[100];
arr[0] = new Phone("갤럭시 s","10","삼성",1200000);
arr[1] = new Phone("아이폰","11","애플",1300000);
arr[2] = new Phone("갤럭시노트","10","삼성",1500000);
arr[3] = new Phone("갤럭시 z플립","1","삼성",1500000);
arr[4] = new Phone("미노트","10","샤오미",550000);
arr[5] = new Phone("아이폰","8","애플",800000);
arr[6] = new Phone("아이폰","XR","애플",1000000);
System.out.println("구매 가능한 휴대폰 목록");
for(int i=0; i<arr.length; i++) {
if(arr[i] != null) {
System.out.println(arr[i].information());
}else {
continue;
}
}
System.out.println("--------------------------------------------------");
System.out.print("구매하고 싶은 기기명을 입력하시오. : ");
Scanner sc = new Scanner(System.in);
String searchName = sc.nextLine();
System.out.print("기기의 시리즈를 입력하세요 : ");
String searchSeries = sc.nextLine();
int count = 0;
int pick = 0;
for(int i=0; i<arr.length; i++) {
if(arr[i] == null) {
continue;
}else {
if(arr[i].getName().equals(searchName)&&
arr[i].getSeries().equals(searchSeries)) {
System.out.println("기기정보");
System.out.println(arr[i].information());
count++;
pick = i;
break;
}
}
}
if(count == 0) {
System.out.println("현재 찾으시는 재고가 없어요 ㅠ");
}else {
System.out.print("정말 구매하시겠습니까?(Y,N)");
char answer = sc.nextLine().charAt(0);
if(answer == 'Y') {
System.out.println("지불할 가격은 "+arr[pick].getPrice()+"원 입니다.");
arr[pick] = null;
}else {
System.out.println("안녕히 가세요.");
}
}
}
}