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()));
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(); 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 * 2. Remove art object from the inventory
*/ */
@@ -59,6 +66,10 @@ public interface Api {
/* /*
* 3. Return a list of all art objects from inventory * 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(); CustomerManager cmgr = CustomerManager.getInstance();
Customer customer = cmgr.findByEmail("kate@museum.com"); Customer customer = cmgr.findByEmail("kate@museum.com");
InventoryList customerCart = new InventoryList(); InventoryList customerCart = new InventoryList();
@@ -68,7 +79,7 @@ public interface Api {
/* /*
* 3. Add a new transaction object to the list manager is handling * 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 * 4. Calculate and return transaction price
* 5. Complete transaction * 5. Complete transaction
@@ -80,13 +91,13 @@ public interface Api {
/* /*
* 7. Format and return transaction data in print format * 7. Format and return transaction data in print format
*/ */
System.out.println( tran ); System.out.println(tran);
/* /*
* 8. Retrieve and return transactions based on: * 8. Retrieve and return transactions based on:
* customer email * customer email
*/ */
TransactionList foundTrans = tmgr.findByCustomerEmail(customer.getEmail()); TransactionList foundTrans = tmgr.findByCustomerEmail(customer.getEmail());
/* /*
* transaction date * transaction date
*/ */
foundTrans = tmgr.findByTransactionDate(tran.getCompleted()); foundTrans = tmgr.findByTransactionDate(tran.getCompleted());
@@ -99,7 +110,7 @@ public interface Api {
*/ */
foundTrans = tmgr.findByTransactionId(tran.getId()); foundTrans = tmgr.findByTransactionId(tran.getId());
/* /*
* 9. Update inventory file(s) on local system * 9. Update inventory file(s) on local system
*/ */
InventoryManager.getInstance().save(); InventoryManager.getInstance().save();
@@ -118,7 +129,7 @@ public interface Api {
} }
static Painting make(Painting o) { static Painting make(Painting o) {
o.setTitle("Windy Rowing"); o.setTitle("Walking by the beach.");
o.setDescription("East Market Square find. Local Author."); o.setDescription("East Market Square find. Local Author.");
o.setAuthor("Dave Janson"); o.setAuthor("Dave Janson");
o.setCreated(ZonedDateTime.now()); o.setCreated(ZonedDateTime.now());
@@ -126,7 +137,7 @@ public interface Api {
} }
static Print make(Print o) { static Print make(Print o) {
o.setTitle("Windy Rowing"); o.setTitle("Different day same thing");
o.setDescription("East Market Square find. Local Author."); o.setDescription("East Market Square find. Local Author.");
o.setAuthor("Dave Janson"); o.setAuthor("Dave Janson");
@@ -135,7 +146,7 @@ public interface Api {
} }
static Sculpture make(Sculpture o) { static Sculpture make(Sculpture o) {
o.setTitle("Windy Rowing"); o.setTitle("Jolly Romp");
o.setDescription("East Market Square find. Local Author."); o.setDescription("East Market Square find. Local Author.");
o.setAuthor("Dave Janson"); o.setAuthor("Dave Janson");

View File

@@ -1,16 +1,23 @@
[ [
{ {
"email": "kate@museum.com", "email": "kate@museum.com",
"id": "1039445606", "id": "4659170390",
"nameFirst": "Kate", "nameFirst": "Kate",
"nameLast": "Demsey", "nameLast": "Demsey",
"phone": "310-676-4844" "phone": "310-676-4844"
}, },
{ {
"email": "jim@homedeco.com", "email": "jim@homedeco.com",
"id": "4312148107", "id": "3722574478",
"nameFirst": "Jim", "nameFirst": "Jim",
"nameLast": "Gumbly", "nameLast": "Gumbly",
"phone": "203-676-4844" "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,10 +4,11 @@
*/ */
package edu.inventorym.model; package edu.inventorym.model;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
public class CustomerManager { public class CustomerManager {
final static CustomerManager INSTANCE = new CustomerManager(); final static CustomerManager INSTANCE = new CustomerManager();
public static CustomerManager getInstance() { public static CustomerManager getInstance() {
@@ -16,28 +17,28 @@ public class CustomerManager {
public CustomerList CUSTOMERS = new CustomerList(); public CustomerList CUSTOMERS = new CustomerList();
public boolean add(Customer c) throws InvalidRecordDataException{ public boolean add(Customer c) throws InvalidRecordDataException {
check(c); check(c);
return CUSTOMERS.add(c); return CUSTOMERS.add(c);
} }
void check(Customer c) throws InvalidRecordDataException{ void check(Customer c) throws InvalidRecordDataException {
} }
public Customer findById(String id){ public Customer findById(String id) {
return null; return null;
} }
public Customer findByEmail(String email){ public Customer findByEmail(String email) {
for (Customer c : CUSTOMERS) { for (Customer c : CUSTOMERS) {
if (email.equalsIgnoreCase(c.getEmail())) { if (email.equalsIgnoreCase(c.getEmail())) {
return c; return c;
} }
} }
return null; return null;
} }
public void save() { public void save() {
try { try {
DataRepository.write(this); DataRepository.write(this);
@@ -46,13 +47,15 @@ public class CustomerManager {
} }
} }
public void load(){ public void load() {
try { try {
DataRepository.read(this); DataRepository.WalkFileSystemTree(this);
} catch (FileNotFoundException e) {
System.out.println(String.format("CUSTOMERS: %s", e.getLocalizedMessage().toString()));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch(Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@@ -7,22 +7,20 @@ package edu.inventorym.model;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
import jakarta.json.Json; import jakarta.json.Json;
import jakarta.json.JsonArray; import jakarta.json.JsonArray;
import jakarta.json.JsonException;
import jakarta.json.bind.Jsonb; import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder; import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.JsonbConfig; import jakarta.json.bind.JsonbConfig;
@@ -51,10 +49,18 @@ public class DataRepository {
return String.format("%010d", val2); 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 { public static void WalkFileSystemTree(final TransactionManager manager) throws IOException {
@@ -77,48 +83,9 @@ public class DataRepository {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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)); try (var reader = Json.createReader(new StringReader(body));
Jsonb jsonb = JsonbBuilder.create()) { Jsonb jsonb = JsonbBuilder.create()) {
JsonArray jsonArray = reader.readArray(); JsonArray jsonArray = reader.readArray();
@@ -126,16 +93,15 @@ public class DataRepository {
return jsonArray.stream() return jsonArray.stream()
.map(jsonValue -> jsonb.fromJson(jsonValue.toString(), baseClass)) .map(jsonValue -> jsonb.fromJson(jsonValue.toString(), baseClass))
.toList(); .toList();
} }
} }
static void write(CustomerManager mgr) throws IOException{ static void write(CustomerManager mgr) throws IOException {
JsonbConfig config = new JsonbConfig() JsonbConfig config = new JsonbConfig()
.withFormatting(true); // Enable pretty-printing .withFormatting(true); // Enable pretty-printing
Jsonb jsonb = JsonbBuilder.create(config); Jsonb jsonb = JsonbBuilder.create(config);
String result = jsonb.toJson(mgr.CUSTOMERS); String result = jsonb.toJson(mgr.CUSTOMERS);
String dataRoot = getRepositoryConfig.getPath(); String dataRoot = getRepositoryConfig.getPath();
dataRoot = dataRoot + "/customers.json"; dataRoot = dataRoot + "/customers.json";
Path path = Paths.get(dataRoot); Path path = Paths.get(dataRoot);
@@ -143,27 +109,47 @@ public class DataRepository {
writer.write(result); writer.write(result);
writer.flush(); 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(); String dataRoot = getRepositoryConfig.getPath();
dataRoot = dataRoot + "/customers.json"; dataRoot = dataRoot + "/inventory.json";
Path path = Paths.get(dataRoot); 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; String result;
try (BufferedReader in = new BufferedReader(new FileReader(path.toFile(), StandardCharsets.UTF_8))){ try (BufferedReader in = new BufferedReader(new FileReader(path.toFile(), StandardCharsets.UTF_8))) {
result = in.readAllAsString(); result = in.readAllAsString();
} }
List<Customer> customers = mapJsonToObjectList(result, Customer.class); 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.clear();
customerManager.CUSTOMERS.addAll(customers); 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 class getRepositoryConfig {
public final static String getPath() { public final static String getPath() {
String home = System.getenv("HOME") != null ? System.getenv("HOME") 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() { public InventoryList() {
super(); 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() { public void save() {
try {
DataRepository.write(this);
} catch (IOException e) {
e.printStackTrace();
}
} }
public void loadAll() { public void load() {
try { try {
DataRepository.WalkFileSystemTree(INSTANCE); DataRepository.WalkFileSystemTree(this);
} catch (IOException e) { } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

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

View File

@@ -9,7 +9,7 @@ import java.time.ZonedDateTime;
import jakarta.json.JsonArray; import jakarta.json.JsonArray;
public class Transaction { public class Transaction {
String id; String id = DataRepository.generateRandomID(16);
ZonedDateTime created = ZonedDateTime.now(); ZonedDateTime created = ZonedDateTime.now();
ZonedDateTime completed = null; ZonedDateTime completed = null;
String customerId; String customerId;
@@ -18,7 +18,13 @@ public class Transaction {
float totalPrice = 0.0F; float totalPrice = 0.0F;
JsonArray attributes; 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; 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 TransactionList TRANSACTIONS = new TransactionList();
public Transaction TransactRequest(Customer customer, InventoryList iPieces, Transaction transacted) { public Transaction TransactRequest(Customer customer, InventoryList iPieces) {
transacted.setCustomerId(customer.getId()); Transaction transacted = new Transaction(customer, iPieces);
transacted.setShipToAddress(customer.getMailing()); transacted.setShipToAddress(customer.getMailing());
transacted.setCustomerInventoryList(iPieces);
compute(transacted); compute(transacted);
return transacted; return transacted;
} }

Binary file not shown.