This commit is contained in:
2025-10-30 10:51:23 -04:00
parent b9d3046dd3
commit 731dd2b991
11 changed files with 182 additions and 92 deletions

View File

@@ -43,10 +43,17 @@ public interface Api {
InventoryManager.getInstance().add(make(new Sculpture()));
InventoryManager.getInstance().add(make(new Sculpture()));
/* Add Customer */
//CustomerManager.getInstance().add(new Customer("Kate", "Demsey", "310-676-4844", "kate@museum.com"));
//CustomerManager.getInstance().add(new Customer("Jim", "Gumbly", "203-676-4844", "jim@homedeco.com"));
CustomerManager.getInstance().load();
if (CustomerManager.getInstance().CUSTOMERS.isEmpty()) {
/* Add Customer */
CustomerManager.getInstance().add(new Customer("Kate", "Demsey", "310-676-4844", "kate@museum.com"));
CustomerManager.getInstance().add(new Customer("Jim", "Gumbly", "203-676-4844", "jim@homedeco.com"));
CustomerManager.getInstance().add(new Customer("Evan", "Hao", "203-676-8943", "evan@homedeco.com"));
/* Update Customer List */
CustomerManager.getInstance().save();
}
/*
* 2. Remove art object from the inventory
*/
@@ -59,6 +66,10 @@ public interface Api {
/*
* 3. Return a list of all art objects from inventory
*/
System.out.println( "============= Inventory List ===============");
imgr.INVENTORY.forEach(System.out::println);
System.out.println( "\n");
CustomerManager cmgr = CustomerManager.getInstance();
Customer customer = cmgr.findByEmail("kate@museum.com");
InventoryList customerCart = new InventoryList();
@@ -68,7 +79,7 @@ public interface Api {
/*
* 3. Add a new transaction object to the list manager is handling
*/
Transaction tran = tmgr.TransactRequest(customer, customerCart, new Transaction());
Transaction tran = tmgr.TransactRequest(customer, customerCart);
/*
* 4. Calculate and return transaction price
* 5. Complete transaction
@@ -118,7 +129,7 @@ public interface Api {
}
static Painting make(Painting o) {
o.setTitle("Windy Rowing");
o.setTitle("Walking by the beach.");
o.setDescription("East Market Square find. Local Author.");
o.setAuthor("Dave Janson");
o.setCreated(ZonedDateTime.now());
@@ -126,7 +137,7 @@ public interface Api {
}
static Print make(Print o) {
o.setTitle("Windy Rowing");
o.setTitle("Different day same thing");
o.setDescription("East Market Square find. Local Author.");
o.setAuthor("Dave Janson");
@@ -135,7 +146,7 @@ public interface Api {
}
static Sculpture make(Sculpture o) {
o.setTitle("Windy Rowing");
o.setTitle("Jolly Romp");
o.setDescription("East Market Square find. Local Author.");
o.setAuthor("Dave Janson");

View File

@@ -1,16 +1,23 @@
[
{
"email": "kate@museum.com",
"id": "1039445606",
"id": "4659170390",
"nameFirst": "Kate",
"nameLast": "Demsey",
"phone": "310-676-4844"
},
{
"email": "jim@homedeco.com",
"id": "4312148107",
"id": "3722574478",
"nameFirst": "Jim",
"nameLast": "Gumbly",
"phone": "203-676-4844"
},
{
"email": "evan@homedeco.com",
"id": "3611023109",
"nameFirst": "Evan",
"nameLast": "Hao",
"phone": "203-676-8943"
}
]

View File

@@ -0,0 +1,56 @@
[
{
"Author": "Dave Janson",
"author": "Dave Janson",
"created": "2025-10-30T10:47:09.3038539-04:00[America/New_York]",
"description": "East Market Square find. Local Author.",
"id": "4187213045",
"price": 10.99,
"title": "Windy Rowing",
"type": "DRAWING"
},
{
"Author": "Dave Janson",
"author": "Dave Janson",
"created": "2025-10-30T10:47:13.3647577-04:00[America/New_York]",
"description": "East Market Square find. Local Author.",
"id": "3284384487",
"price": 10.99,
"title": "Different day same thing",
"type": "PRINT"
},
{
"Author": "Dave Janson",
"author": "Dave Janson",
"created": "2025-10-30T10:47:14.3174866-04:00[America/New_York]",
"description": "East Market Square find. Local Author.",
"id": "3420570978",
"price": 10.99,
"title": "Walking by the beach.",
"type": "PAINTING",
"height": 0,
"width": 0
},
{
"Author": "Dave Janson",
"author": "Dave Janson",
"created": "2025-10-30T10:47:14.9196999-04:00[America/New_York]",
"description": "East Market Square find. Local Author.",
"id": "4652544628",
"price": 10.99,
"title": "Jolly Romp",
"type": "SCULPTURE",
"weight": 1.0
},
{
"Author": "Dave Janson",
"author": "Dave Janson",
"created": "2025-10-30T10:47:15.4510495-04:00[America/New_York]",
"description": "East Market Square find. Local Author.",
"id": "3160354998",
"price": 10.99,
"title": "Jolly Romp",
"type": "SCULPTURE",
"weight": 1.0
}
]

View File

@@ -4,6 +4,7 @@
*/
package edu.inventorym.model;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CustomerManager {
@@ -48,7 +49,9 @@ public class CustomerManager {
public void load() {
try {
DataRepository.read(this);
DataRepository.WalkFileSystemTree(this);
} catch (FileNotFoundException e) {
System.out.println(String.format("CUSTOMERS: %s", e.getLocalizedMessage().toString()));
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {

View File

@@ -7,22 +7,20 @@ package edu.inventorym.model;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonException;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.JsonbConfig;
@@ -51,10 +49,18 @@ public class DataRepository {
return String.format("%010d", val2);
}
public static void WalkFileSystemTree(final InventoryManager manager) throws IOException {
public static void WalkFileSystemTree(final InventoryManager manager) throws JsonException, IOException, Exception {
String dataRoot = getRepositoryConfig.getPath();
dataRoot = dataRoot + "/inventory.json";
Path path = Paths.get(dataRoot);
read(manager, path);
}
public static void WalkFileSystemTree(final CustomerManager manager) throws IOException {
public static void WalkFileSystemTree(final CustomerManager manager) throws JsonException, IOException, Exception {
String dataRoot = getRepositoryConfig.getPath();
dataRoot = dataRoot + "/customers.json";
Path path = Paths.get(dataRoot);
read(manager, path);
}
public static void WalkFileSystemTree(final TransactionManager manager) throws IOException {
@@ -77,48 +83,9 @@ public class DataRepository {
} catch (Exception e) {
e.printStackTrace();
}
try {
// Configure pretty-printing and other settings
JsonbConfig config = new JsonbConfig()
.withFormatting(true); // Enable pretty-printing
Jsonb jsonb = JsonbBuilder.create(config);
// Serialize using the custom configuration
String prettyJsonString = jsonb.toJson(new Drawing());
System.out.println("json string: " + prettyJsonString);
} catch (Exception e) {
e.printStackTrace();
}
try {
List<Drawing> rpcs = new ArrayList<Drawing>();
rpcs.add(new Drawing());
rpcs.add(new Drawing());
rpcs.add(new Drawing());
// Create Jsonb and serialize
Jsonb jsonb = JsonbBuilder.create();
String result = jsonb.toJson(rpcs);
// Deserialize back
rpcs = jsonb.fromJson(result,
new ArrayList<Drawing>() {
}
.getClass()
.getGenericSuperclass());
System.out.println(rpcs);
} catch (Exception e) {
e.printStackTrace();
}
}
public static <T> List<T> mapJsonToObjectList(String body, Class<T> baseClass) throws Exception {
public static <T> List<T> mapJsonToObjectList(String body, Class<T> baseClass) throws JsonException, Exception {
try (var reader = Json.createReader(new StringReader(body));
Jsonb jsonb = JsonbBuilder.create()) {
JsonArray jsonArray = reader.readArray();
@@ -135,7 +102,6 @@ public class DataRepository {
Jsonb jsonb = JsonbBuilder.create(config);
String result = jsonb.toJson(mgr.CUSTOMERS);
String dataRoot = getRepositoryConfig.getPath();
dataRoot = dataRoot + "/customers.json";
Path path = Paths.get(dataRoot);
@@ -143,27 +109,47 @@ public class DataRepository {
writer.write(result);
writer.flush();
}
}
public static void read(CustomerManager customerManager) throws IOException, Exception {
static void write(InventoryManager mgr) throws IOException {
JsonbConfig config = new JsonbConfig()
.withFormatting(true); // Enable pretty-printing
Jsonb jsonb = JsonbBuilder.create(config);
String result = jsonb.toJson(mgr.INVENTORY);
String dataRoot = getRepositoryConfig.getPath();
dataRoot = dataRoot + "/customers.json";
dataRoot = dataRoot + "/inventory.json";
Path path = Paths.get(dataRoot);
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
writer.write(result);
writer.flush();
}
}
private static void read(CustomerManager customerManager, Path path) throws Exception {
String result;
try (BufferedReader in = new BufferedReader(new FileReader(path.toFile(), StandardCharsets.UTF_8))) {
result = in.readAllAsString();
}
List<Customer> customers = mapJsonToObjectList(result, Customer.class);
/* Jsonb jsonb = JsonbBuilder.create();
CustomerList customers = jsonb.fromJson(result, new CustomerList() { }
.getClass()
.getGenericSuperclass()); */
customerManager.CUSTOMERS.clear();
customerManager.CUSTOMERS.addAll(customers);
}
private static void read(InventoryManager manager, Path path) throws JsonException, IOException, Exception {
String result;
try (BufferedReader in = new BufferedReader(new FileReader(path.toFile(), StandardCharsets.UTF_8))) {
result = in.readAllAsString();
}
List<InventoryPiece> inventory = mapJsonToObjectList(result, InventoryPiece.class);
manager.INVENTORY.clear();
manager.INVENTORY.addAll(inventory);
}
public final static class getRepositoryConfig {
public final static String getPath() {
String home = System.getenv("HOME") != null ? System.getenv("HOME")
@@ -172,5 +158,4 @@ public class DataRepository {
}
}
}

View File

@@ -10,4 +10,10 @@ public class InventoryList extends ArrayList<InventoryPiece> {
public InventoryList() {
super();
}
@Override
public boolean add(InventoryPiece e) {
// TODO : check Uniqueness
return super.add(e);
}
}

View File

@@ -40,13 +40,17 @@ public class InventoryManager {
}
public void save() {
try {
DataRepository.write(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadAll() {
public void load() {
try {
DataRepository.WalkFileSystemTree(INSTANCE);
} catch (IOException e) {
// TODO Auto-generated catch block
DataRepository.WalkFileSystemTree(this);
} catch (Exception e) {
e.printStackTrace();
}
}

View File

@@ -7,7 +7,7 @@ package edu.inventorym.model;
public class Sculpture extends InventoryPiece {
String material;
float weight;
float weight = 1.0F;
public Sculpture(float weight) {
super();

View File

@@ -9,7 +9,7 @@ import java.time.ZonedDateTime;
import jakarta.json.JsonArray;
public class Transaction {
String id;
String id = DataRepository.generateRandomID(16);
ZonedDateTime created = ZonedDateTime.now();
ZonedDateTime completed = null;
String customerId;
@@ -18,7 +18,13 @@ public class Transaction {
float totalPrice = 0.0F;
JsonArray attributes;
public Transaction() {
protected Transaction (final Customer customer, final InventoryList cart){
this.customerId = customer.getId();
this.customerInventoryList = new InventoryList();
this.customerInventoryList.addAll(cart);
}
protected Transaction() {
}
@@ -86,4 +92,17 @@ public class Transaction {
this.customerId = id;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{ \"Transaction\":{");
sb.append("\"id\": \"").append(id).append("\",\n");
sb.append("\"created\": \"").append(created).append("\",\n");
sb.append("\"completed\": ").append(completed).append(",\n");
sb.append("\"customerId\": ").append(customerId).append(",\n");
sb.append("\"totalPrice\": ").append(totalPrice);
sb.append("}}\n");
return sb.toString();
}
}

View File

@@ -16,10 +16,9 @@ public class TransactionManager {
public TransactionList TRANSACTIONS = new TransactionList();
public Transaction TransactRequest(Customer customer, InventoryList iPieces, Transaction transacted) {
transacted.setCustomerId(customer.getId());
public Transaction TransactRequest(Customer customer, InventoryList iPieces) {
Transaction transacted = new Transaction(customer, iPieces);
transacted.setShipToAddress(customer.getMailing());
transacted.setCustomerInventoryList(iPieces);
compute(transacted);
return transacted;
}

Binary file not shown.