Skip to content

Instantly share code, notes, and snippets.

View ntub46010's full-sized avatar

Vincent Zheng ntub46010

View GitHub Profile
@ntub46010
ntub46010 / Lottery.java
Last active November 17, 2019 14:14
樂透程式
package com.vincent.demo;
import java.util.*;
import java.util.stream.Collectors;
public class JavaMain {
public static void main(String[] args) {
Set<Integer> selectedNumbers = new HashSet<>();
Scanner scan = new Scanner(System.in);
@ntub46010
ntub46010 / Discount.java
Created November 17, 2019 14:09
計算消費打折金額
package com.vincent.demo;
import java.util.*;
public class JavaMain {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("請輸入你的年齡");
// 找出name欄位值有包含參數的所有文件,且不分大小寫
List<Product> findByNameLikeIgnoreCase(String name);
// 找出id欄位值有包含在參數之中的所有文件
List<Product> findByIdIn(List<String> ids);
// 是否有文件的email欄位值等於參數
boolean existsByEmail(String email);
// 找出username與password欄位值皆符合參數的一筆文件
// 查詢price欄位在特定範圍的文件(參數亦可使用Date)
// gte:大於等於;lte:小於等於;Between:大於及小於,兩者略有差異。
@Query("{'price': {'$gte': ?0, '$lte': ?1}}")
List<Product> findByPriceBetween(int from, int to);
// 查詢name字串欄位有包含參數的文件,不分大小寫
@Query("{'name': {'$regex': ?0, '$options': 'i'}}")
List<Product> findByNameLikeIgnoreCase(String name);
// 查詢同時符合上述兩個條件的文件
// 回傳id欄位值有包含在參數之中的文件數量
@Query(value = "{'_id': {'$in': ?0}}", count = true)
int countByIdIn(List<String> ids);
// 回傳是否有文件的id欄位值包含在參數之中
@Query(value = "{'_id': {'$in': ?0}}", exists = true)
boolean existsByIdIn(List<String> ids);
// 刪除id欄位值包含在參數之中的文件
@Query(delete = true)
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("/{id}")
public ResponseEntity<Product> getProduct(@PathVariable("id") String id) {
Product product = productService.getProduct(id);
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class ProductController {
@GetMapping("/products/{id}")
public Product getProduct(@PathVariable("id") String id) {
Product product = new Product();
product.setId(id);
product.setName("Romantic Story");
product.setPrice(200);
@GetMapping("/products/{id}")
public ResponseEntity<Product> getProduct(@PathVariable("id") String id) {
Product product = new Product();
product.setId(request.getId());
product.setName(request.getName());
product.setPrice(request.getPrice());
return ResponseEntity.ok().body(product);
}
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class ProductController {
private final List<Product> productDB = new ArrayList<>();
@PostConstruct
private void initDB() {
productDB.add(new Product("B0001", "Android Development (Java)", 380));
productDB.add(new Product("B0002", "Android Development (Kotlin)", 420));
@PostMapping("/products")
public ResponseEntity<Product> createProduct(@RequestBody Product request) {
boolean isIdDuplicated = productDB.stream()
.anyMatch(p -> p.getId().equals(request.getId()));
if (isIdDuplicated) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
Product product = new Product();
product.setId(request.getId());