Skip to content

Commit c0fd62e

Browse files
author
Paulo Henrique Ortolan
committed
Adding more examples and classes
1 parent 5b595e5 commit c0fd62e

File tree

9 files changed

+163
-5
lines changed

9 files changed

+163
-5
lines changed

Java10Examples.iml

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/main/java/org/java10/examples/BasicExample.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
public class BasicExample {
44

55
public static void main(String[] args) {
6+
var b = false;
7+
var y = (byte)10;
8+
var c = 'A';
9+
var s = (short)100;
610
var i = 0;
11+
var l = 999l;
12+
var f = 3.14f;
13+
var d = 1.61803398874d;
714

15+
System.out.println(b);
16+
System.out.println(y);
17+
System.out.println(c);
18+
System.out.println(s);
819
System.out.println(i);
20+
System.out.println(l);
21+
System.out.println(f);
22+
System.out.println(d);
923
}
1024
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
11
package org.java10.examples;
22

3+
import org.java10.examples.beans.Bill;
4+
import org.java10.examples.beans.DiscoutType;
5+
import org.java10.examples.beans.Product;
6+
37
public class ComplexObjectExample {
48

59
public static void main(String[] args) {
10+
var bill = Bill.newBill();
11+
12+
var productLaptop = new Product("Good Laptop", 1000f, 10f);
13+
var productGamerMouse = new Product("Gamer Mouse", 20f, 1);
14+
var productGamerKeyboard = new Product("Gamer Keyboard",15, 1);
15+
var productUSBController = new Product("USB Controller", 35, 2);
16+
17+
bill
18+
.addProduct(productLaptop, DiscoutType.COMMON_DISCOUNT, 0.15f)
19+
.addProduct(productGamerMouse, DiscoutType.INTERNET_DISCOUNT, 0f)
20+
.addProduct(productGamerKeyboard, DiscoutType.NO_DISCOUNT, 0f)
21+
.addProduct(productUSBController, DiscoutType.COMMON_DISCOUNT, 0.20f);
22+
23+
var productList = bill.productList();
24+
25+
System.out.println("-[ Bill ]-----------------------------------------------------------");
26+
27+
for(Product product : productList) {
28+
System.out.println(product.toString());
29+
}
30+
31+
System.out.println("--------------------------------------------------------------------");
32+
33+
System.out.println(String.format("%44.2f", bill.total()));
34+
System.out.println(String.format("%68.2f", bill.taxes()));
635

36+
System.out.println("--------------------------------------------------------------------");
737
}
838

939
}

src/main/java/org/java10/examples/ObjectExample.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@ public static void main(String[] args) {
2828
System.out.println(string);
2929
System.out.println(now.format(formatter));
3030
System.out.println(stringList);
31+
32+
System.out.println(string.getClass());
33+
System.out.println(now.getClass());
34+
System.out.println(formatter.getClass());
35+
System.out.println(stringList.getClass());
3136
}
3237
}

src/main/java/org/java10/examples/ProductExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class ProductExample {
66

77
public static void main(String[] args) {
8-
var modifiedProduct = new Product(8f, 0.5f) {
8+
var modifiedProduct = new Product("An internet product", 8f, 0.5f) {
99
public float applyInternetPrice() {
1010
return getPrice() - 1f + getTax();
1111
}
@@ -17,7 +17,7 @@ public float applyInternetPrice() {
1717
System.out.println(modifiedProduct.applyInternetPrice());
1818
System.out.println("----------------");
1919

20-
var commonProduct = new Product(8f, 0.5f);
20+
var commonProduct = new Product("A not internet product", 8f, 0.5f);
2121

2222
System.out.println("Common product");
2323
System.out.println(commonProduct.calculateTotal());
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.java10.examples.beans;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class Bill {
8+
9+
private List<ProductLine> products;
10+
11+
private Bill() {
12+
products = new ArrayList<>();
13+
}
14+
15+
public static Bill newBill() {
16+
return new Bill();
17+
}
18+
19+
public Bill addProduct(Product product, DiscoutType type, float discount) {
20+
products.add(ProductLine.newLine(product, type, discount));
21+
return this;
22+
}
23+
24+
public List<Product> productList() {
25+
return products
26+
.stream()
27+
.map(ProductLine::getProduct)
28+
.collect(Collectors.toList());
29+
}
30+
31+
public double total() {
32+
double total = 0d;
33+
34+
for(ProductLine productLine : products) {
35+
switch (productLine.getDiscoutType()) {
36+
case COMMON_DISCOUNT:
37+
total += productLine.getProduct().applyDiscount(productLine.getDiscount());
38+
break;
39+
case INTERNET_DISCOUNT:
40+
var internetProduct = new Product(productLine.getProduct().getName(), productLine.getProduct().getPrice(), productLine.getProduct().getTax()) {
41+
public float applyInternetPrice() {
42+
return getPrice() - 1f + getTax();
43+
}
44+
};
45+
46+
total += internetProduct.applyInternetPrice();
47+
break;
48+
default:
49+
total += productLine.getProduct().calculateTotal();
50+
}
51+
}
52+
53+
return total;
54+
}
55+
56+
public double taxes() {
57+
return productList()
58+
.stream()
59+
.mapToDouble(p -> p.getTax())
60+
.sum();
61+
}
62+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.java10.examples.beans;
2+
3+
public enum DiscoutType {
4+
5+
NO_DISCOUNT,
6+
COMMON_DISCOUNT,
7+
INTERNET_DISCOUNT
8+
9+
}

src/main/java/org/java10/examples/beans/Product.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
public class Product {
44

5+
private final String name;
56
private final float price;
67
private final float tax;
78

8-
public Product(float price, float tax) {
9+
public Product(String name, float price, float tax) {
10+
this.name = name;
911
this.price = price;
1012
this.tax = tax;
1113
}
1214

15+
public String getName() {
16+
return name;
17+
}
18+
1319
public float getPrice() {
1420
return price;
1521
}
@@ -25,4 +31,8 @@ public float calculateTotal() {
2531
public float applyDiscount(float discount) {
2632
return (price - (price * discount)) + tax;
2733
}
34+
35+
@Override public String toString() {
36+
return String.format("%-20s\t%20.2f\t%20.2f", name, price, tax);
37+
}
2838
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.java10.examples.beans;
2+
3+
public class ProductLine {
4+
5+
private final Product product;
6+
private final DiscoutType discoutType;
7+
private final float discount;
8+
9+
private ProductLine(Product product, DiscoutType discoutType, float discount) {
10+
this.product = product;
11+
this.discoutType = discoutType;
12+
this.discount = discount;
13+
}
14+
15+
public static ProductLine newLine(Product product, DiscoutType discoutType, float discount) {
16+
return new ProductLine(product, discoutType, discount);
17+
}
18+
19+
public DiscoutType getDiscoutType() {
20+
return discoutType;
21+
}
22+
23+
public Product getProduct() {
24+
return product;
25+
}
26+
27+
public float getDiscount() {
28+
return discount;
29+
}
30+
}

0 commit comments

Comments
 (0)