반응형
소개
이번 포스팅에서는 Eclipse와 Java Spring 프레임워크를 이용하여 간단한 요금 계산 시스템을 만들어 보겠습니다. 이 시스템은 상품 및 서비스 요금을 계산하고, 최종 결제 금액을 사용자 화면에 표시하는 기능을 포함합니다.
주요 기능
- 상품 및 서비스 리스트 표시
- 선택된 상품의 개수 및 가격 계산
- 부가세(VAT)와 총 금액 계산
프로젝트 설정
1. Eclipse 설치 및 Spring Boot 초기화
- Eclipse IDE를 다운로드하고 설치합니다.
- Spring Initializr를 이용하여 기본 프로젝트를 생성합니다.
- Dependencies: Spring Web, Thymeleaf, Lombok
- 생성한 프로젝트를 Eclipse에 Import합니다.
코드 구현
1. 프로젝트 구조
src/main/java
├── com.example.billing
│ ├── BillingApplication.java
│ ├── controller
│ │ └── BillingController.java
│ ├── model
│ │ └── Product.java
│ ├── service
│ └── BillingService.java
src/main/resources
├── templates
│ └── billing.html
├── static
└── styles.css
2. 모델 클래스: Product
package com.example.billing.model;
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
3. 서비스 클래스: BillingService
package com.example.billing.service;
import com.example.billing.model.Product;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
@Service
public class BillingService {
public List<Product> getProducts() {
return Arrays.asList(
new Product("Product A", 10.0),
new Product("Product B", 20.0),
new Product("Product C", 30.0)
);
}
public double calculateTotal(double price, int quantity) {
double subtotal = price * quantity;
double vat = subtotal * 0.1; // 10% VAT
return subtotal + vat;
}
}
4. 컨트롤러 클래스: BillingController
package com.example.billing.controller;
import com.example.billing.model.Product;
import com.example.billing.service.BillingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
public class BillingController {
@Autowired
private BillingService billingService;
@GetMapping("/")
public String showProducts(Model model) {
List<Product> products = billingService.getProducts();
model.addAttribute("products", products);
return "billing";
}
@PostMapping("/calculate")
public String calculateTotal(@RequestParam("productName") String productName,
@RequestParam("quantity") int quantity,
Model model) {
Product selectedProduct = billingService.getProducts().stream()
.filter(p -> p.getName().equals(productName))
.findFirst()
.orElse(null);
if (selectedProduct != null) {
double total = billingService.calculateTotal(selectedProduct.getPrice(), quantity);
model.addAttribute("productName", productName);
model.addAttribute("quantity", quantity);
model.addAttribute("total", total);
}
return "billing";
}
}
5. HTML 템플릿: billing.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Billing System</title>
<link rel="stylesheet" href="/styles.css">
</head>
<body>
<h1>Billing System</h1>
<form action="/calculate" method="post">
<label for="productName">Select Product:</label>
<select name="productName" id="productName">
<option th:each="product : ${products}" th:value="${product.name}" th:text="${product.name}"></option>
</select>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1" required>
<button type="submit">Calculate</button>
</form>
<div th:if="${total != null}">
<h2>Receipt</h2>
<p>Product: <span th:text="${productName}"></span></p>
<p>Quantity: <span th:text="${quantity}"></span></p>
<p>Total: $<span th:text="${total}"></span></p>
</div>
</body>
</html>
6. CSS 파일: styles.css
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
form {
margin-bottom: 20px;
}
label {
margin-right: 10px;
}
button {
margin-top: 10px;
padding: 5px 10px;
}
실행 및 테스트
- 프로젝트를 실행한 후 브라우저에서
http://localhost:8080
으로 이동합니다. - 상품과 개수를 선택하여 계산 버튼을 누르면 총 금액과 세부 정보가 화면에 표시됩니다.
마무리
위 과정을 통해 간단한 요금 계산 시스템을 구현했습니다. 이 프로젝트는 Spring MVC, Thymeleaf, 기본적인 서비스 계층 설계 패턴을 이해하는 데 유용합니다. 필요에 따라 추가 기능(예: 할인, 결제 통합)을 확장할 수 있습니다.
반응형
'JAVA' 카테고리의 다른 글
JavaFX 프레임워크 최적화 팁 (0) | 2024.12.25 |
---|---|
Java Spring Framework 최적화 팁 (0) | 2024.12.23 |
Eclipse + JavaFX: 간단한 요금 입력 시스템 만들기 (4) | 2024.12.19 |
Java와 Oracle 데이터베이스 조회 쿼리 성능 최적화: setFetchSize 사용법 (2) | 2024.11.18 |
Java와 Oracle 데이터베이스 조회 쿼리 성능 최적화 방법 (0) | 2024.11.11 |
댓글