걷기반 중요 학습
private final String name;
private final String breed;
private로 캡슐화 진행 후, final로 상수 선언. 상수 필드는 한번 들어온 값이 변하지 않는다.
커머스
// 생성자
public Product(String name, double price, String description) {
this.name = name;
this.price = price;
this.description = description;
}
// 객체 데이터 생성
products.add(new Product("Galaxy S25", 1200000, "최신 안드로이드 스마트폰"));
products.add(new Product("Iphone", 1300000, "Apple의 최신 스마트폰"));
products.add(new Product("MacBook Pro", 2400000, "M3 칩셋이 탑재된 노트북"));
// 상품 목록 출력
for (int i = 0; i < products.size(); i++) {
System.out.print((i + 1) + ". " + products.get(i).getName() + " | ");
System.out.print(products.get(i).getPrice() + " | ");
System.out.println(products.get(i).getDescription());
}
생성자를 선언하여 Product 객체 생성 시 필요한 필수 필드의 초기화를 강제하였고, 이를 활용하여 리스트 안에 Product 객체 데이터를 추가하였습니다. 리스트의 크기만큼 리스트 목록을 출력하는 조건문을 작성하였습니다.