This commit is contained in:
2025-10-29 22:27:51 -04:00
parent 1a4014c503
commit b9d3046dd3
18 changed files with 422 additions and 145 deletions

View File

@@ -20,10 +20,11 @@ public class Customer {
this.nameLast = nameLast;
this.phone = phone;
this.email = email;
this.id = DataRepository.generateRandom10DigitID();
this.updated = ZonedDateTime.now();
}
public Customer(){}
protected Customer(){}
public String getEmail() {
return email;

View File

@@ -4,6 +4,8 @@
*/
package edu.inventorym.model;
import java.io.IOException;
public class CustomerManager {
final static CustomerManager INSTANCE = new CustomerManager();
@@ -28,6 +30,29 @@ public class CustomerManager {
}
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);
} catch (IOException e) {
e.printStackTrace();
}
}
public void load(){
try {
DataRepository.read(this);
} catch (IOException e) {
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}
}

View File

@@ -4,9 +4,18 @@
*/
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;
@@ -25,7 +34,7 @@ public class DataRepository {
return INSTANCE;
}
final public static String generateRandomID(int byteLength) {
final public static String generateRandomID(int byteLength) {
SecureRandom secureRandom = new SecureRandom();
byte[] bytes = new byte[byteLength];
secureRandom.nextBytes(bytes);
@@ -41,7 +50,7 @@ public class DataRepository {
BigInteger val2 = randomCountLength.add(val1);
return String.format("%010d", val2);
}
public static void WalkFileSystemTree(final InventoryManager manager) throws IOException {
}
@@ -51,84 +60,117 @@ public class DataRepository {
public static void WalkFileSystemTree(final TransactionManager manager) throws IOException {
}
static void serialize_deserialize(){
static void serialize_deserialize() {
String jsonResponse = "";
try{
// Deserialize the JSON string to a Java RpcResponse object
Jsonb jsonb = JsonbBuilder.create();
jsonResponse = jsonb.toJson((InventoryPiece)new Drawing());
try {
// Deserialize the JSON string to a Java RpcResponse object
Jsonb jsonb = JsonbBuilder.create();
jsonResponse = jsonb.toJson((InventoryPiece) new Drawing());
System.out.println("json string: " + jsonResponse);
System.out.println("json string: " + jsonResponse);
InventoryPiece response = jsonb.fromJson(jsonResponse, Drawing.class);
InventoryPiece response = jsonb.fromJson(jsonResponse, Drawing.class);
System.out.println("Result: " + response);
System.out.println("ID: " + response.getId());
System.out.println("Result: " + response);
System.out.println("ID: " + response.getId());
}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){
} catch (Exception e) {
e.printStackTrace();
}
try {
List<Drawing> rpcs = new ArrayList<Drawing>();
rpcs.add(new Drawing());
rpcs.add(new Drawing());
rpcs.add(new Drawing());
// Configure pretty-printing and other settings
JsonbConfig config = new JsonbConfig()
.withFormatting(true); // Enable pretty-printing
Jsonb jsonb = JsonbBuilder.create(config);
// Create Jsonb and serialize
Jsonb jsonb = JsonbBuilder.create();
String result = jsonb.toJson(rpcs);
// Serialize using the custom configuration
String prettyJsonString = jsonb.toJson(new Drawing());
// Deserialize back
rpcs = jsonb.fromJson(result,
new ArrayList<Drawing>() {}
.getClass()
.getGenericSuperclass());
System.out.println( rpcs );
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 {
try (var reader = Json.createReader(new StringReader(body));
Jsonb jsonb = JsonbBuilder.create()) {
JsonArray jsonArray = reader.readArray();
return jsonArray.stream()
.map(jsonValue -> jsonb.fromJson(jsonValue.toString(), baseClass))
.toList();
}
}
public static <T> List<T> mapJsonToObjectList(String body, Class<T> baseClass) {
try (var reader = Json.createReader(new StringReader(body));
Jsonb jsonb = JsonbBuilder.create()){
JsonArray jsonArray = reader.readArray();
static void write(CustomerManager mgr) throws IOException{
JsonbConfig config = new JsonbConfig()
.withFormatting(true); // Enable pretty-printing
Jsonb jsonb = JsonbBuilder.create(config);
String result = jsonb.toJson(mgr.CUSTOMERS);
return jsonArray.stream()
.map(jsonValue -> jsonb.fromJson(jsonValue.toString(), baseClass))
.toList();
} catch (Exception e) {
return (List<T>) new ArrayList<Drawing>();
String dataRoot = getRepositoryConfig.getPath();
dataRoot = dataRoot + "/customers.json";
Path path = Paths.get(dataRoot);
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
writer.write(result);
writer.flush();
}
}
public static void read(CustomerManager customerManager) throws IOException, Exception {
String dataRoot = getRepositoryConfig.getPath();
dataRoot = dataRoot + "/customers.json";
Path path = Paths.get(dataRoot);
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);
}
public final static class getRepositoryConfig {
public final static String getPath() {
String home = System.getenv("HOME") != null ? System.getenv("HOME")
: System.getenv("HOMEDRIVE") + System.getenv("HOMEPATH");
return home.replace('\\', '/') + "/workspace/reservationsystem/src/resources/db";
return home.replace('\\', '/') + "/workspace/inventorym/Api/src/resources/db";
}
}
}

View File

@@ -11,6 +11,12 @@ public class Drawing extends InventoryPiece {
public String category;
public Drawing() {
super(InventoryType.DRAWING);
super();
}
@Override
public float computePrice() {
return price;
}
}

View File

@@ -21,15 +21,17 @@ public class InventoryManager {
private InventoryManager() {
}
public Set<InventoryType> getArtPieceTypes() {
return EnumSet.allOf(InventoryType.class);
}
public void add(InventoryPiece iPiece) {
INVENTORY.add(iPiece);
}
public InventoryPiece find(String id) {
for (InventoryPiece iv : INVENTORY) {
if (iv.getId() == id) {
return iv;
}
}
return null;
}
@@ -40,7 +42,7 @@ public class InventoryManager {
public void save() {
}
public void loadAll(){
public void loadAll() {
try {
DataRepository.WalkFileSystemTree(INSTANCE);
} catch (IOException e) {
@@ -49,11 +51,9 @@ public class InventoryManager {
}
}
public void Remove(InventoryPiece iPiece) {
//int i = find(iPiece);
//INVENTORY.remove(i)
//*** multiple quantiies of iPiece */
public InventoryPiece Remove(InventoryPiece iPiece) {
int i = INVENTORY.indexOf(iPiece);
return INVENTORY.remove(i);
};
}

View File

@@ -15,7 +15,7 @@ public abstract class InventoryPiece {
public float price;
public ZonedDateTime created;
public String title;
/**
* max 500 characters
*/
@@ -23,23 +23,14 @@ public abstract class InventoryPiece {
public String Author;
InventoryType iventoryType;
public InventoryPiece(InventoryType iventoryType) {
this.iventoryType = iventoryType;
this.created = ZonedDateTime.now();
this.id = DataRepository.generateRandom10DigitID();
}
public InventoryPiece() {
}
public Boolean isArt(InventoryPiece piece) {
return false;
this.created = ZonedDateTime.now();
this.id = DataRepository.generateRandom10DigitID();
this.price = 10.99F;
}
public String getType() {
return this.iventoryType.toString();
return this.getClass().getSimpleName().toUpperCase();
};
public String getId() {
@@ -89,4 +80,6 @@ public abstract class InventoryPiece {
public void setAuthor(String author) {
Author = author;
}
public abstract float computePrice();
}

View File

@@ -1,9 +0,0 @@
/**
* license: GPLv3
edu.inventorym
*/
package edu.inventorym.model;
public enum InventoryType {
PAINTING, DRAWING, PRINT, SCULPTURE
}

View File

@@ -5,14 +5,77 @@
package edu.inventorym.model;
public class Painting extends InventoryPiece {
int height;
int height;
int width;
String style;
String technique;
String category;
public Painting (){
super(InventoryType.PAINTING);
public Painting(int height, int width) {
super();
this.height = height;
this.width = width;
}
public Painting() {
super();
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getTechnique() {
return technique;
}
public void setTechnique(String technique) {
this.technique = technique;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
@Override
public float computePrice() {
int sqarea = height * width;
float additional = 0;
if (sqarea < 100) {
additional = 5.99F;
}
if (sqarea > 100 && sqarea < 300) {
additional = 10.99F;
}
if (sqarea > 300) {
additional = 15.99F;
}
return price + additional;
}
}

View File

@@ -10,7 +10,7 @@ public class Print extends InventoryPiece {
String category;
public Print(){
super(InventoryType.PRINT);
super();
}
public Print(String openEditionType, String category) {
@@ -33,4 +33,9 @@ public class Print extends InventoryPiece {
public void setCategory(String category) {
this.category = category;
}
@Override
public float computePrice() {
return price;
}
}

View File

@@ -9,8 +9,33 @@ public class Sculpture extends InventoryPiece {
String material;
float weight;
public Sculpture (){
super(InventoryType.SCULPTURE);
public Sculpture(float weight) {
super();
this.weight = weight;
}
public Sculpture() {
super();
}
public String getMaterial() {
return material;
}
public void setMaterial(String material) {
this.material = material;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
@Override
public float computePrice() {
return price + (weight * 0.20F);
}
}

View File

@@ -15,6 +15,7 @@ public class Transaction {
String customerId;
Address shipToAddress;
InventoryList customerInventoryList;
float totalPrice = 0.0F;
JsonArray attributes;
public Transaction() {
@@ -33,6 +34,38 @@ public class Transaction {
return created;
}
public String getCustomerId() {
return customerId;
}
public Address getShipToAddress() {
return shipToAddress;
}
public InventoryList getCustomerInventoryList() {
return customerInventoryList;
}
public void setCustomerInventoryList(InventoryList customerInventoryList) {
this.customerInventoryList = customerInventoryList;
}
public float getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(float totalPrice) {
this.totalPrice = totalPrice;
}
public JsonArray getAttributes() {
return attributes;
}
public void setAttributes(JsonArray attributes) {
this.attributes = attributes;
}
public void setCreated(ZonedDateTime created) {
this.created = created;
}
@@ -45,5 +78,12 @@ public class Transaction {
this.completed = completed;
}
public void setShipToAddress(Address mailing) {
this.shipToAddress = mailing;
}
public void setCustomerId(String id) {
this.customerId = id;
}
}

View File

@@ -17,19 +17,23 @@ public class TransactionManager {
public TransactionList TRANSACTIONS = new TransactionList();
public Transaction TransactRequest(Customer customer, InventoryList iPieces, Transaction transacted) {
transacted.setCustomerId(customer.getId());
transacted.setShipToAddress(customer.getMailing());
transacted.setCustomerInventoryList(iPieces);
compute(transacted);
return transacted;
}
public void compute(Transaction tran, TransactionOperationEnum operation) {
public void complete(Transaction tran) {
tran.setCompleted(ZonedDateTime.now());
}
public void compute(Transaction tran) {
}
public void complete(Transaction tran) {
float price = 0.0F;
for (InventoryPiece piece : tran.customerInventoryList) {
price += piece.computePrice();
}
tran.totalPrice = price;
}
public TransactionList findByCustomerEmail(String email) {
@@ -49,8 +53,7 @@ public class TransactionManager {
}
public void save() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'save'");
}
}

View File

@@ -1,14 +0,0 @@
/**
* license: GPLv3
edu.inventorym
*/
package edu.inventorym.model;
public enum TransactionOperationEnum {
Completed,
Draft,
Pricing,
Removed,
Shipping
}