Product Class
package com.hw1.model.vo;
public class Product {
//필드부
private String productId; // 상품아이디
private String productName; // 상품명
private String productArea;
private int price;
private double tax;
// 생성자부 (기본 + 전체)
// 기본 생성자
public Product() {
}
// 전체 생성자 (매개변수 생성자)
public Product(String productId, String productName, String productArea, int price, double tax) {
this.productId = productId;
this.productName = productName;
this.productArea = productArea;
this.price = price;
this.tax = tax;
}
// 메소드부
// get - set => 한세트로 작성
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductId() {
return productId;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductName() {
return productName;
}
public void setProductArea(String productArea) {
this.productArea = productArea;
}
public String getProductArea() {
return productArea;
}
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
public void setTax(double tax) {
this.tax = tax;
}
public double getTax() {
return tax;
}
public String information() {
return productId + " " + productName + " " + productArea + " " + price + " " + tax;
}
}
실행 Class
package com.hw1.run;
import com.hw1.model.vo.Product;
public class ProductTest {
public static void main(String[] args) {
// 매개변수 생성자를 이용하여 3개의 객체 생성 (위의 사용 데이터 참고)
Product p1 = new Product("ssgnote9", "갤럭시노트9", "경기도 수원", 960000, 10.0);
Product p2 = new Product("lgxnote5", "LG스마트폰5", "경기도 평택", 780000, 0.7);
Product p3 = new Product("ktsnote3", "KT스마트폰3", "서울시 강남", 250000, 0.3);
// 객체가 가진 필드 값 출력 확인
System.out.println(p1.information());
System.out.println(p2.information());
System.out.println(p3.information());
System.out.println("=========================================");
// 각 객체의 가격을 모두 120만원으로 변경 / 부가세율도 모두 0.05로 변경
// 가격
p1.setPrice(1200000);
p2.setPrice(1200000);
p3.setPrice(1200000);
// 부가세율
p1.setTax(0.05);
p2.setTax(0.05);
p3.setTax(0.05);
// 객체가 가진 필드 값 출력 확인
System.out.println(p1.information());
System.out.println(p2.information());
System.out.println(p3.information());
System.out.println("=========================================");
// 각 객체의 가격에 부가세율을 적용한 실제 가격을 계산해서 출력함
// ** 실제가격 = 가격 + (가격 * 부가세율)
System.out.println("상품명 : " + p1.getProductName());
System.out.println("부가세 포함 가격 : " + (int) (p1.getPrice() + p1.getPrice() * p1.getTax()) + "원");
System.out.println("상품명 : " + p2.getProductName());
System.out.println("부가세 포함 가격 : " + (int) (p2.getPrice() + p2.getPrice() * p2.getTax()) + "원");
System.out.println("상품명 : " + p3.getProductName());
System.out.println("부가세 포함 가격 : " + (int) (p2.getPrice() + p3.getPrice() * p3.getTax()) + "원");
}
}
컴파일