Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
samuel sharon sidabutar
tutorial-0
Commits
446754e8
Commit
446754e8
authored
Feb 13, 2022
by
Samuel Sharon
Browse files
Implement student page
parent
8ed7021a
Changes
8
Hide whitespace changes
Inline
Side-by-side
src/main/java/id/ac/ui/cs/advprog/tutorial0/controller/StudentController.java
0 → 100644
View file @
446754e8
package
id.ac.ui.cs.advprog.tutorial0.controller
;
import
id.ac.ui.cs.advprog.tutorial0.exception.DuplicateStudentNameException
;
import
id.ac.ui.cs.advprog.tutorial0.model.Student
;
import
id.ac.ui.cs.advprog.tutorial0.service.StudentService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
@Controller
@RequestMapping
(
"/student"
)
public
class
StudentController
{
@Autowired
private
StudentService
service
;
@GetMapping
(
"/list"
)
public
String
studentListPage
(
Model
model
)
{
List
<
Student
>
allStudents
=
service
.
findAll
();
model
.
addAttribute
(
"students"
,
allStudents
);
return
"studentList"
;
}
@GetMapping
(
"/create"
)
public
String
createStudentPage
(
Model
model
)
{
Student
student
=
new
Student
();
model
.
addAttribute
(
"student"
,
student
);
return
"createStudent"
;
}
@PostMapping
(
"/create"
)
public
String
createStudentPost
(
@ModelAttribute
Student
student
,
Model
model
)
{
try
{
service
.
create
(
student
);
}
catch
(
DuplicateStudentNameException
e
)
{
model
.
addAttribute
(
"error"
,
e
);
model
.
addAttribute
(
"student"
,
student
);
return
"createStudent"
;
}
return
"redirect:list"
;
}
}
src/main/java/id/ac/ui/cs/advprog/tutorial0/exception/DuplicateStudentNameException.java
0 → 100644
View file @
446754e8
package
id.ac.ui.cs.advprog.tutorial0.exception
;
public
class
DuplicateStudentNameException
extends
RuntimeException
{
public
DuplicateStudentNameException
(
String
studentName
)
{
super
(
String
.
format
(
"The student name %s is a duplicate!"
,
studentName
));
}
}
src/main/java/id/ac/ui/cs/advprog/tutorial0/model/Student.java
0 → 100644
View file @
446754e8
package
id.ac.ui.cs.advprog.tutorial0.model
;
import
lombok.Getter
;
import
lombok.Setter
;
@Getter
@Setter
public
class
Student
{
private
String
name
;
private
String
npm
;
private
String
address
;
}
src/main/java/id/ac/ui/cs/advprog/tutorial0/repository/StudentRepository.java
0 → 100644
View file @
446754e8
package
id.ac.ui.cs.advprog.tutorial0.repository
;
import
id.ac.ui.cs.advprog.tutorial0.model.Student
;
import
org.springframework.stereotype.Repository
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.Iterator
;
import
java.util.List
;
@Repository
public
class
StudentRepository
{
private
List
<
Student
>
studentsInMemory
=
new
ArrayList
<>();
public
Student
create
(
Student
student
)
{
studentsInMemory
.
add
(
student
);
return
student
;
}
public
Iterator
<
Student
>
findAll
()
{
return
studentsInMemory
.
iterator
();
}
}
src/main/java/id/ac/ui/cs/advprog/tutorial0/service/StudentService.java
0 → 100644
View file @
446754e8
package
id.ac.ui.cs.advprog.tutorial0.service
;
import
id.ac.ui.cs.advprog.tutorial0.model.Student
;
import
java.util.List
;
public
interface
StudentService
{
public
Student
create
(
Student
student
);
public
List
<
Student
>
findAll
();
}
src/main/java/id/ac/ui/cs/advprog/tutorial0/service/StudentServiceImpl.java
0 → 100644
View file @
446754e8
package
id.ac.ui.cs.advprog.tutorial0.service
;
import
id.ac.ui.cs.advprog.tutorial0.exception.DuplicateStudentNameException
;
import
id.ac.ui.cs.advprog.tutorial0.model.Student
;
import
id.ac.ui.cs.advprog.tutorial0.repository.StudentRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.List
;
@Service
public
class
StudentServiceImpl
implements
StudentService
{
@Autowired
private
StudentRepository
studentRepository
;
@Override
public
Student
create
(
Student
student
)
{
validateName
(
student
);
generateNPM
(
student
);
studentRepository
.
create
(
student
);
return
student
;
}
private
void
validateName
(
Student
student
)
{
List
<
Student
>
allStudents
=
findAll
();
for
(
Student
dbStudent:
allStudents
)
{
if
(
dbStudent
.
getName
().
equals
(
student
.
getName
()))
{
throw
new
DuplicateStudentNameException
(
student
.
getName
());
}
}
}
private
void
generateNPM
(
Student
student
)
{
StringBuilder
stringBuilder
=
new
StringBuilder
();
for
(
char
letter:
student
.
getName
().
toCharArray
())
{
stringBuilder
.
append
(
String
.
valueOf
((
int
)
letter
));
}
String
npm
=
stringBuilder
.
toString
();
student
.
setNpm
(
npm
);
}
@Override
public
List
<
Student
>
findAll
()
{
Iterator
<
Student
>
studentIterator
=
studentRepository
.
findAll
();
List
<
Student
>
allStudents
=
new
ArrayList
<>();
studentIterator
.
forEachRemaining
(
allStudents:
:
add
);
return
allStudents
;
}
}
\ No newline at end of file
src/main/resources/templates/createStudent.html
0 → 100644
View file @
446754e8
<!DOCTYPE html>
<html
lang=
"en"
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Create new Student
</title>
<link
href=
"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel=
"stylesheet"
integrity=
"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin=
"anonymous"
>
</head>
<body>
<h3>
Create new Student
</h3>
<div
th:if=
"${error!=null}"
class=
"alert alert-danger"
role=
"alert"
>
Duplicate student name
</div>
<form
th:action=
"@{/student/create}"
th:object=
"${student}"
method=
"post"
>
<div
class=
"form-group"
>
<label
for=
"nameInput"
>
Name
</label>
<input
th:field=
"*{name}"
type=
"text"
class=
"form-control"
id=
"nameInput"
aria-describedby=
"nameHelp"
placeholder=
"Enter student' name"
>
<small
id=
"nameHelp"
class=
"form-text text-muted"
>
Please enter unique name.
</small>
</div>
<div
class=
"form-group"
>
<label
for=
"addressTextarea"
>
Address
</label>
<textarea
th:field=
"*{address}"
class=
"form-control"
id=
"addressTextarea"
rows=
"3"
></textarea>
</div>
<button
type=
"submit"
class=
"btn btn-primary"
>
Submit
</button>
</form>
<script
src=
"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin=
"anonymous"
></script>
</body>
</html>
\ No newline at end of file
src/main/resources/templates/studentList.html
0 → 100644
View file @
446754e8
<!DOCTYPE html>
<html
lang=
"en"
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Student List
</title>
<link
href=
"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel=
"stylesheet"
integrity=
"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin=
"anonymous"
>
</head>
<body>
<h2>
Student' List
</h2>
<a
th:href=
"@{/student/create}"
class=
"btn btn-primary btn-sm mb-3"
>
Create student
</a>
<table
class=
"table"
>
<thead>
<tr>
<th
scope=
"col"
>
Name
</th>
<th
scope=
"col"
>
NPM
</th>
<th
scope=
"col"
>
Address
</th>
</tr>
</thead>
<tbody
th:each=
"student: ${students}"
>
<tr>
<td
th:text=
"${student.name}"
></td>
<td
th:text=
"${student.npm}"
></td>
<td
th:text=
"${student.address}"
></td>
</tr>
</tbody>
</table>
<script
src=
"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin=
"anonymous"
></script>
</body>
</html>
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment