Java 學習記錄74 — Sorted Collections — 1/4

張小雄
6 min readJul 14, 2021

--

今天要學的是 Sorted Collections

StockItem.java

package Stock;public class StockItem implements Comparable<StockItem> {
private final String name;
private double price;
private int quantityStock = 0;
public StockItem(String name, double price) {
this.name = name;
this.price = price;
this.quantityStock = 0; // or here ( but you wouldn't normally do both)
}
public StockItem(String name, double price, int quantityStock) {
this.name = name;
this.price = price;
this.quantityStock = quantityStock;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
if (price > 0.0) {
this.price = price;
}
}
public int quantityInStock() {
return quantityStock;
}
public void adjustStock(int quantity) {
int newQuantity = this.quantityStock + quantity;
if (newQuantity >= 0) {
this.quantityStock = newQuantity;
}
}
@Override
public int hashCode() {
return this.name.hashCode() + 17;
}
@Override
public boolean equals(Object obj) {
System.out.println("Call Stock.StockItem.equals()");
if (obj == this) {
return true;
}
if ((obj == null) || (obj.getClass() != this.getClass())) {
return false;
}
String objName = ((StockItem) obj).getName();
return this.name.equals(objName);
}
@Override
public int compareTo(StockItem o) {
System.out.println("Call Stock.StockItem.compareTo()");
if (this == o) {
return 0;
}
if (o != null) {
return this.name.compareTo(o.getName());
}
throw new NullPointerException();
}
@Override
public String toString() {
return name + " : $" + price;
}
}

StockList.java

package Stock;import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class StockList {
private final Map<String, StockItem> list;
public StockList() {
this.list = new HashMap<>();
}
public int addStock(StockItem stockItem) {
if (stockItem != null) {
// check if already have stockItem, jf get null just pass stockItem of parameter
StockItem inStock = list.getOrDefault(stockItem.getName(), stockItem);
// if there are already stocks on this item, adjust the quantity
if (inStock != stockItem) {
stockItem.adjustStock(inStock.quantityInStock());
}
list.put(stockItem.getName(), stockItem);
return stockItem.quantityInStock();
}
return 0;
}
public int sellStock(String item, int quantity) {
StockItem inStock = list.getOrDefault(item, null);
if ((inStock != null) && (inStock.quantityInStock() >= quantity) && (quantity > 0)) {
inStock.adjustStock(-quantity);
return quantity;
}
return 0;
}
public StockItem getStockItem(String key) {
return list.get(key);
}
public Map<String, StockItem> Items() {
return Collections.unmodifiableMap(list);
}
@Override
public String toString() {
String s = "\nStock List\n";
double totalCost = 0.0;
for (Map.Entry<String, StockItem> item : list.entrySet()) {
StockItem stockItem = item.getValue();
double itemValue = stockItem.getPrice() * stockItem.quantityInStock(); s = s + stockItem + " There are " + stockItem.quantityInStock() + " items in stock. Total value of items: ";
s = s + itemValue + "\n";
totalCost += itemValue;
}
return s + " Total stock value " + totalCost;
}
}

Basket.java

package Stock;import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Basket {
private final String name;
private final Map<StockItem, Integer> list;
public Basket(String name) {
this.name = name;
this.list = new HashMap<>();
}
public int addToBasket(StockItem item, int quantity) {
if ((item != null) && (quantity > 0)) {
int inBasket = list.getOrDefault(item, 0);
list.put(item, inBasket + quantity);
return inBasket;
}
return 0;
}
public Map<StockItem, Integer> Items() {
return Collections.unmodifiableMap(list);
}
@Override
public String toString() {
String s = "\nShopping basket " + name + " contains " + list.size() + " items\n";
double totalCost = 0.0;
for (Map.Entry<StockItem, Integer> item : list.entrySet()) {
s = s + item.getKey() + ". " + item.getValue() + " purchased\n";
totalCost += item.getKey().getPrice() * item.getValue();
}
return s + " Total cost " + totalCost;
}
}

Main.java

package Stock;public class Main {
private static StockList stockList = new StockList();
public static void main(String[] args) {
StockItem temp = new StockItem("bread", 0.86, 100);
stockList.addStock(temp);
temp = new StockItem("cake", 1.10, 7);
stockList.addStock(temp);
temp = new StockItem("car", 12.50, 2);
stockList.addStock(temp);
temp = new StockItem("chair", 62.0, 10);
stockList.addStock(temp);
temp = new StockItem("cup", 0.50, 200);
stockList.addStock(temp);
temp = new StockItem("door", 72.95, 4);
stockList.addStock(temp);
temp = new StockItem("juice", 2.50, 36);
stockList.addStock(temp);
temp = new StockItem("phone", 96.99, 35);
stockList.addStock(temp);
temp = new StockItem("towel", 2.40, 80);
stockList.addStock(temp);
temp = new StockItem("vase", 8.76, 40);
stockList.addStock(temp);
System.out.println(stockList);
}
}

輸出結果:

Stock List

door : $72.95 There are 4 items in stock. Total value of items: 291.8

bread : $0.86 There are 100 items in stock. Total value of items: 86.0

car : $12.5 There are 2 items in stock. Total value of items: 25.0

phone : $96.99 There are 35 items in stock. Total value of items: 3394.6499999999996

towel : $2.4 There are 80 items in stock. Total value of items: 192.0

cake : $1.1 There are 7 items in stock. Total value of items: 7.700000000000001

chair : $62.0 There are 10 items in stock. Total value of items: 620.0

vase : $8.76 There are 40 items in stock. Total value of items: 350.4

juice : $2.5 There are 36 items in stock. Total value of items: 90.0

cup : $0.5 There are 200 items in stock. Total value of items: 100.0

Total stock value 5157.549999999999

看起來小複雜,其實就是一個簡單的庫存清單

裡面有三個 Class,分別是商品、庫存、購物籃

還有個小問題要修改,就是數字那邊怪怪的

StockList.java 改

package Stock;import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class StockList {
private final Map<String, StockItem> list;
public StockList() {
this.list = new HashMap<>();
}
public int addStock(StockItem stockItem) {
if (stockItem != null) {
// check if already have stockItem, jf get null just pass stockItem of parameter
StockItem inStock = list.getOrDefault(stockItem.getName(), stockItem);
// if there are already stocks on this item, adjust the quantity
if (inStock != stockItem) {
stockItem.adjustStock(inStock.quantityInStock());
}
list.put(stockItem.getName(), stockItem);
return stockItem.quantityInStock();
}
return 0;
}
public int sellStock(String item, int quantity) {
StockItem inStock = list.getOrDefault(item, null);
if ((inStock != null) && (inStock.quantityInStock() >= quantity) && (quantity > 0)) {
inStock.adjustStock(-quantity);
return quantity;
}
return 0;
}
public StockItem getStockItem(String key) {
return list.get(key);
}
public Map<String, StockItem> Items() {
return Collections.unmodifiableMap(list);
}
@Override
public String toString() {
String s = "\nStock List\n";
double totalCost = 0.0;
for (Map.Entry<String, StockItem> item : list.entrySet()) {
StockItem stockItem = item.getValue();
double itemValue = stockItem.getPrice() * stockItem.quantityInStock(); s = s + stockItem + " There are " + stockItem.quantityInStock() + " items in stock. Total value of items: ";
s = s + String.format("%.2f", itemValue) + "\n";
totalCost += itemValue;
}
return s + " Total stock value " + String.format("%.2f", totalCost);
}
}

輸出結果:

Stock List

door : $72.95 There are 4 items in stock. Total value of items: 291.80

bread : $0.86 There are 100 items in stock. Total value of items: 86.00

car : $12.5 There are 2 items in stock. Total value of items: 25.00

phone : $96.99 There are 35 items in stock. Total value of items: 3394.65

towel : $2.4 There are 80 items in stock. Total value of items: 192.00

cake : $1.1 There are 7 items in stock. Total value of items: 7.70

chair : $62.0 There are 10 items in stock. Total value of items: 620.00

vase : $8.76 There are 40 items in stock. Total value of items: 350.40

juice : $2.5 There are 36 items in stock. Total value of items: 90.00

cup : $0.5 There are 200 items in stock. Total value of items: 100.00

Total stock value 5157.55

這樣就只會顯示到小數點第二位,數字多少就是幾位

但物品的排列不是按照字母,是無序隨機的,想要有順序怎改?

StockList.java 改

package Stock;import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
public class StockList {
private final Map<String, StockItem> list;
public StockList() {
this.list = new LinkedHashMap<>();
}
public int addStock(StockItem stockItem) {
if (stockItem != null) {
// check if already have stockItem, jf get null just pass stockItem of parameter
StockItem inStock = list.getOrDefault(stockItem.getName(), stockItem);
// if there are already stocks on this item, adjust the quantity
if (inStock != stockItem) {
stockItem.adjustStock(inStock.quantityInStock());
}
list.put(stockItem.getName(), stockItem);
return stockItem.quantityInStock();
}
return 0;
}
public int sellStock(String item, int quantity) {
StockItem inStock = list.getOrDefault(item, null);
if ((inStock != null) && (inStock.quantityInStock() >= quantity) && (quantity > 0)) {
inStock.adjustStock(-quantity);
return quantity;
}
return 0;
}
public StockItem getStockItem(String key) {
return list.get(key);
}
public Map<String, StockItem> Items() {
return Collections.unmodifiableMap(list);
}
@Override
public String toString() {
String s = "\nStock List\n";
double totalCost = 0.0;
for (Map.Entry<String, StockItem> item : list.entrySet()) {
StockItem stockItem = item.getValue();
double itemValue = stockItem.getPrice() * stockItem.quantityInStock(); s = s + stockItem + " There are " + stockItem.quantityInStock() + " items in stock. Total value of items: ";
s = s + String.format("%.2f", itemValue) + "\n";
totalCost += itemValue;
}
return s + " Total stock value " + String.format("%.2f", totalCost);
}
}

輸出結果:

Stock List

bread : $0.86 There are 100 items in stock. Total value of items: 86.00

cake : $1.1 There are 7 items in stock. Total value of items: 7.70

car : $12.5 There are 2 items in stock. Total value of items: 25.00

chair : $62.0 There are 10 items in stock. Total value of items: 620.00

cup : $0.5 There are 200 items in stock. Total value of items: 100.00

door : $72.95 There are 4 items in stock. Total value of items: 291.80

juice : $2.5 There are 36 items in stock. Total value of items: 90.00

phone : $96.99 There are 35 items in stock. Total value of items: 3394.65

towel : $2.4 There are 80 items in stock. Total value of items: 192.00

vase : $8.76 There are 40 items in stock. Total value of items: 350.40

Total stock value 5157.55

只需要改一行而已非常方便

public StockList() {
this.list = new LinkedHashMap<>();
}

本篇使用代碼

--

--

張小雄
張小雄

Written by 張小雄

記錄成為軟體工程師的過程

No responses yet