본문 바로가기

Java/Java 예제6

Java HashMap 예제 ArrayList를 다루는 OnlineStore 클래스는 controller 패키지에product 객체를 다루는 Product 클래스는 entity 패키지에 두고 코딩해보자.import controller.OnlineStore;import entity.Product;public class HashMapMain { public static void main(String[] args) { OnlineStore store = new OnlineStore(); // TODO: 최소 3개의 상품을 생성하고 store에 추가하세요. Product product1 = new Product("P001", "스마트폰", 899000.0); Product produc.. 2024. 12. 17.
Java ArrayList 예제 ArrayList를 다루는 StudentManager 클래스는 controller 패키지에student 객체를 다루는 Student 클래스는 entity 패키지에 두고 코딩해보자. import controller.StudentManager;import entity.Student;public class ArrayMain { public static void main(String[] args) { StudentManager manager = new StudentManager(); // TODO: 최소 3명의 학생을 생성하고 manager에 추가하세요. // id 1, 홍길동, 30 Student s1=new Student(1,"홍길동", 30); .. 2024. 12. 17.
Java interface와 extends 실습 예제 package entity;public interface Payable { // 상품 가격 반환 메서드 double getPrice(); // 결제 처리 메서드 void processPayment();} package entity;public class Product { // 기본 상품 클래스 protected String name; protected double price; // 디폴트 생성자 public Product() { } // 생성자: 상품 이름과 가격 초기화 public Product(String name, double price) { this.name = name; this.price = price; .. 2024. 12. 16.
Java 추상화 예제 - 상품 정보, 할인 금액 출력 package entity;public abstract class Product { protected String productId; protected String name; protected double price; // 디폴트 생성자 public Product() { } // 매개변수가 있는 생성자 public Product(String productId, String name, double price) { this.productId = productId; this.name = name; this.price = price; } // 상품 정보를 출력하는 추상 메서드 public abstract void d.. 2024. 12. 16.
Java if와 논리 연산자를 이용한 날짜 출력 예제 package entity;public class MyDate { private int day; private int month; private int year; //디폴트 생성자 public MyDate() { } //MyDate(int day, int month, int year) 생성자 public MyDate(int day, int month, int year) { this.day = day; this.month = month; this.year = year; } // todo 우리가 만들 메소드 public boolean isValid() { if (month 12) { .. 2024. 12. 12.
Java 메소드 예제 public class User { // 유저 이름, 나이, 전화번호를 저장하려 합니다. String userName; int age; String phone; // 유저이름, 나이, 전화번호를 출력하는 함수 void print(){ System.out.println("유저이름: "+userName+" 나이: "+age+" 전화번호: "+phone); } // 10년 후의 나이를 계산해서 리턴하는 함수 int futureAge(){ int add10=age+10; return add10; } // say("안녕?"); // 화면에 출력 : 홍길동님 안녕? void say(String sentence.. 2024. 12. 10.