JSP 코드

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<% request.setCharacterEncoding("UTF-8"); %>
	<jsp:useBean id="cbean" class = "test.CoffeeBean"/>
	<%--클래스 생성 --%>
	<jsp:setProperty property="*" name="cbean"/>
	<%--setter 설정 --%>
<!DOCTYPE html>
<%
	if (request.getMethod().equals("POST")) {
		cbean.calcTotalPrice();
	}
%>
<html>
<head>
<meta charset="UTF-8">
<title>커피 키오스크</title>
<style type="text/css">
#wrap {
	margin: 0px;
	padding: 0px;
}

#priceTable {
	width: fit-content;
	text-align: center;
	position: inherit;
	padding: 5px;
	margin: 0px auto;
}

#tableHead {
	background-color: lightblue;
}

h1, h2{
	text-align: center;
	position: inherit;
	margin: 0px auto;
}
</style>
</head>
<body>
	<div id="wrap">
		<header>
			<h1>🐟🐋바다가 보이는 참치 카페🐳🐬</h1>
		</header>
		<div id="container">
			<form method="post">
				<table id="priceTable" border="1">
					<tr id="tableHead">
						<td>메뉴</td>
						<td>가격</td>
						<td>수량</td>
					</tr>
					<tr>
						<td>에스프레소</td>
						<td>2500</td>
						<td><input id="cntE" name="cntE" type="text" placeholder="수량을 입력해주세요!"></td>
					</tr>
					<tr>
						<td>아메리카노</td>
						<td>3000</td>
						<td><input id="cntA" name="cntA" type="text" placeholder="수량을 입력해주세요!"></td>
					</tr>
					<tr>
						<td>카페라떼</td>
						<td>4000</td>
						<td><input id="cntL" name="cntL" type="text" placeholder="수량을 입력해주세요!"></td>
					</tr>
					<tr>
						<td>카페모카</td>
						<td>5000</td>
						<td><input id="cntM" name="cntM" type="text" placeholder="수량을 입력해주세요!"></td>
					</tr>
					<tr>
					<td colspan="2"><input type = "reset" value = "다시입력"></td>
						<td><input id="calc" type="submit" value="계산하기!"></td>
					</tr>
				</table>
			</form>
			<h2>결재하실 금액은 <jsp:getProperty property="totalPrice" name="cbean"/>원 입니다!🐳🐠</h2>
		</div>
	</div>
</body>
</html>

JAVA코드

package test;

public class CoffeeBean {
	private int cntE;
	private int cntA;
	private int cntL;
	private int cntM;
	private int esPrice;
	private int amePrice;
	private int lattePrice;
	private int mocaPrice;
	private int totalPrice = 0;
	public CoffeeBean() {
		this.esPrice = 2500;
		this.amePrice = 3000;
		this.lattePrice = 4000;
		this.mocaPrice = 5000;
	}
	public void setEsPrice(int esPrice) {
		this.esPrice = esPrice;
	}
	public void setAmePrice(int amePrice) {
		this.amePrice = amePrice;
	}
	public void setLattePrice(int lattePrice) {
		this.lattePrice = lattePrice;
	}
	public int getCntE() {
		return cntE;
	}
	public void setCntE(int cntE) {
		this.cntE = cntE;
	}
	public int getCntA() {
		return cntA;
	}
	public void setCntA(int cntA) {
		this.cntA = cntA;
	}
	public int getCntL() {
		return cntL;
	}
	public void setCntL(int cntL) {
		this.cntL = cntL;
	}
	public int getCntM() {
		return cntM;
	}
	public void setCntM(int cntM) {
		this.cntM = cntM;
	}
	public int getEsPrice() {
		return esPrice;
	}
	public int getAmePrice() {
		return amePrice;
	}
	public int getLattePrice() {
		return lattePrice;
	}
	public int getMocaPrice() {
		return mocaPrice;
	}
	public void setMocaPrice(int mocaPrice) {
		this.mocaPrice = mocaPrice;
	}
	public void setTotalPrice(int totalPrice) {
		this.totalPrice = totalPrice;
	}
	public int getTotalPrice() {
		return totalPrice;
	}
	public void calcTotalPrice() {
		this.totalPrice += this.cntA*this.amePrice;
		this.totalPrice += this.cntE*this.esPrice;
		this.totalPrice += this.cntL*this.lattePrice;
		this.totalPrice += this.cntM*this.mocaPrice;
	}

}

실행화면

'JAVA' 카테고리의 다른 글

JSP 내장객체  (0) 2021.08.25
JSP로 계산기 만들기  (0) 2021.08.24
서블릿 vs jsp  (0) 2021.08.24
JAVA MySQL을 이용한 고객 관리 프로그램  (0) 2021.07.26
개발방법론 VMC 디자인패턴 정리  (0) 2021.07.21

+ Recent posts