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

@@ -4,10 +4,11 @@
*/
package edu.inventorym.model;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CustomerManager {
final static CustomerManager INSTANCE = new CustomerManager();
public static CustomerManager getInstance() {
@@ -16,28 +17,28 @@ public class CustomerManager {
public CustomerList CUSTOMERS = new CustomerList();
public boolean add(Customer c) throws InvalidRecordDataException{
public boolean add(Customer c) throws InvalidRecordDataException {
check(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;
}
public Customer findByEmail(String email){
public Customer findByEmail(String email) {
for (Customer c : CUSTOMERS) {
if (email.equalsIgnoreCase(c.getEmail())) {
return c;
}
}
}
return null;
}
public void save() {
try {
DataRepository.write(this);
@@ -46,13 +47,15 @@ public class CustomerManager {
}
}
public void load(){
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){
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

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();
@@ -126,16 +93,15 @@ public class DataRepository {
return jsonArray.stream()
.map(jsonValue -> jsonb.fromJson(jsonValue.toString(), baseClass))
.toList();
}
}
}
static void write(CustomerManager mgr) throws IOException{
static void write(CustomerManager mgr) throws IOException {
JsonbConfig config = new JsonbConfig()
.withFormatting(true); // Enable pretty-printing
.withFormatting(true); // Enable pretty-printing
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))){
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()); */
List<Customer> customers = mapJsonToObjectList(result, Customer.class);
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;
}