Example project.

This commit is contained in:
Cameron Redmore 2025-04-27 16:51:38 +01:00
commit adf5d19942
9 changed files with 510 additions and 0 deletions

29
.gitignore vendored Normal file
View file

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

6
.idea/misc.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/StockManagement.iml" filepath="$PROJECT_DIR$/StockManagement.iml" />
</modules>
</component>
</project>

BIN
StockData.dat Normal file

Binary file not shown.

11
StockManagement.iml Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

43
src/StockItem.java Normal file
View file

@ -0,0 +1,43 @@
import java.io.Serializable;
public class StockItem implements Serializable {
private static final long serialVersionUID = 1L;
public String SKU;
public String name;
public String description;
public String[] tags;
public StockItem(String SKU, String name, String description, String[] tags) {
this.SKU = SKU;
this.name = name;
this.description = description;
this.tags = tags;
}
public StockItem(String SKU, String name, String description) {
this.SKU = SKU;
this.name = name;
this.description = description;
this.tags = new String[0];
}
public String getSKU() {
return SKU;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String[] getTags() {
return tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
}

100
src/StockLocation.java Normal file
View file

@ -0,0 +1,100 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.UUID;
public class StockLocation implements Serializable {
private static final long serialVersionUID = 1L;
private UUID id;
private String name;
private String description;
private String address;
private String city;
private String country;
private String postalCode;
private HashMap<String, Integer> stockItems = new HashMap<>();
public StockLocation(String name, String description, String address, String city, String country, String postalCode)
{
this.id = UUID.randomUUID();
this.name = name;
this.description = description;
this.address = address;
this.city = city;
this.country = country;
this.postalCode = postalCode;
}
public StockLocation(UUID id, String name, String description, String address, String city, String country, String postalCode)
{
this.id = id;
this.name = name;
this.description = description;
this.address = address;
this.city = city;
this.country = country;
this.postalCode = postalCode;
}
public String getDescription() {
return description;
}
public UUID getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getCity() {
return city;
}
public String getCountry() {
return country;
}
public String getPostalCode() {
return postalCode;
}
public int getQuantity(StockItem item) {
return stockItems.getOrDefault(item.getSKU(), 0);
}
public void addStockItem(StockItem item, int quantity) {
if (stockItems.containsKey(item.getSKU())) {
stockItems.put(item.getSKU(), stockItems.get(item.getSKU()) + quantity);
} else {
stockItems.put(item.getSKU(), quantity);
}
}
public boolean removeStockItem(StockItem item, int quantity) {
if (stockItems.containsKey(item.getSKU())) {
int currentQuantity = stockItems.get(item.getSKU());
if (currentQuantity > quantity) {
stockItems.put(item.getSKU(), currentQuantity - quantity);
return true;
}
}
return false;
}
public boolean transferStockItem(StockLocation destination, StockItem item, int quantity) {
if (stockItems.containsKey(item.getSKU()) && stockItems.get(item.getSKU()) >= quantity) {
removeStockItem(item, quantity);
destination.addStockItem(item, quantity);
return true;
}
return false;
}
}

305
src/StockManagement.java Normal file
View file

@ -0,0 +1,305 @@
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class StockManagement {
private static final ArrayList<StockItem> stockItems = new ArrayList<>();
private static final ArrayList<StockLocation> stockLocations = new ArrayList<>();
private static final Scanner scanner = new Scanner(System.in);
private static final String DATA_FILE = "StockData.dat";
public static void main(String[] args) {
loadData();
while (true) {
System.out.println("=== Stock Management System ===");
System.out.println("1. Create Stock Item");
System.out.println("2. Create Stock Location");
System.out.println("3. Add Stock to Location");
System.out.println("4. Transfer Stock Between Locations");
System.out.println("5. View Stock in a Location");
System.out.println("6. Search Products");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(scanner.nextLine());
switch (choice) {
case 1 -> createStockItem();
case 2 -> createStockLocation();
case 3 -> addStockToLocation();
case 4 -> transferStockBetweenLocations();
case 5 -> viewStockInLocation();
case 6 -> searchProducts();
case 7 -> {
System.out.println("Exiting...");
return;
}
default -> System.out.println("Invalid choice. Please try again.");
}
//Wait for user input before continuing
System.out.println();
System.out.print("Press Enter to continue...");
scanner.nextLine();
}
}
private static void saveData() {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DATA_FILE))) {
oos.writeObject(stockItems.toArray(new StockItem[0]));
oos.writeObject(stockLocations.toArray(new StockLocation[0]));
System.out.println("Data saved successfully.");
} catch (IOException e) {
System.out.println("Error saving data: " + e.getMessage());
}
}
private static void createStockItem() {
System.out.print("Enter SKU: ");
String sku = scanner.nextLine();
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter description: ");
String description = scanner.nextLine();
System.out.print("Enter tags (comma-separated): ");
String[] tags = scanner.nextLine().split(",");
StockItem item = new StockItem(sku, name, description, tags);
stockItems.add(item);
System.out.println("Stock item created successfully.");
saveData();
}
private static void createStockLocation() {
System.out.print("Enter location name: ");
String name = scanner.nextLine();
System.out.print("Enter description: ");
String description = scanner.nextLine();
System.out.print("Enter address: ");
String address = scanner.nextLine();
System.out.print("Enter city: ");
String city = scanner.nextLine();
System.out.print("Enter country: ");
String country = scanner.nextLine();
System.out.print("Enter postal code: ");
String postalCode = scanner.nextLine();
StockLocation location = new StockLocation(name, description, address, city, country, postalCode);
stockLocations.add(location);
System.out.println("Stock location created successfully.");
saveData();
}
private static void addStockToLocation() {
StockLocation location = selectStockLocation();
if (location == null)
return;
StockItem item = selectStockItem();
if (item == null)
return;
System.out.print("Enter quantity to add: ");
int quantity = Integer.parseInt(scanner.nextLine());
location.addStockItem(item, quantity);
System.out.println("Stock added successfully.");
saveData();
}
private static void transferStockBetweenLocations() {
System.out.println("Select source location:");
StockLocation source = selectStockLocation();
if (source == null)
return;
System.out.println("Select destination location:");
StockLocation destination = selectStockLocation();
if (destination == null)
return;
StockItem item = selectStockItem();
if (item == null)
return;
System.out.print("Enter quantity to transfer: ");
int quantity = Integer.parseInt(scanner.nextLine());
if (source.transferStockItem(destination, item, quantity)) {
System.out.println("Stock transferred successfully.");
saveData();
} else {
System.out.println("Failed to transfer stock. Check availability.");
}
}
private static void viewStockInLocation() {
StockLocation location = selectStockLocation();
if (location == null)
return;
System.out.println("### Stock in " + location.getName() + " ###");
for (StockItem item : stockItems) {
int quantity = location.getQuantity(item);
if (quantity > 0) {
System.out.println("Item: " + item.getName() + ", Quantity: " + quantity);
}
}
}
private static void searchProducts() {
System.out.print("Enter search term (SKU, name, description, or tag): ");
String query = scanner.nextLine().toLowerCase().trim();
if (query.isEmpty()) {
System.out.println("Search term cannot be empty.");
return;
}
ArrayList<StockItem> matchingItems = new ArrayList<>();
// Search through all stock items for matches
for (StockItem item : stockItems) {
boolean matches = false;
// Check if SKU contains the query
if (item.getSKU().toLowerCase().contains(query)) {
matches = true;
}
// Check if name contains the query
if (item.getName().toLowerCase().contains(query)) {
matches = true;
}
// Check if description contains the query
if (item.getDescription().toLowerCase().contains(query)) {
matches = true;
}
// Check if any tags match the query
for (String tag : item.getTags()) {
if (tag != null && tag.toLowerCase().trim().contains(query)) {
matches = true;
break;
}
}
if (matches) {
matchingItems.add(item);
}
}
// Display search results with stock levels
if (matchingItems.isEmpty()) {
System.out.println("No products found matching '" + query + "'");
} else {
System.out.println("\n=== Search Results for '" + query + "' ===");
for (StockItem item : matchingItems) {
System.out.println("\nProduct: " + item.getName() + " (SKU: " + item.getSKU() + ")");
System.out.println("Description: " + item.getDescription());
// Display tags if any
if (item.getTags().length > 0) {
System.out.print("Tags: ");
for (int i = 0; i < item.getTags().length; i++) {
if (item.getTags()[i] != null && !item.getTags()[i].trim().isEmpty()) {
System.out.print(item.getTags()[i].trim());
if (i < item.getTags().length - 1) {
System.out.print(", ");
}
}
}
System.out.println();
}
// Display stock levels across all locations
System.out.println("Stock Levels:");
boolean hasStock = false;
for (StockLocation location : stockLocations) {
int quantity = location.getQuantity(item);
if (quantity > 0) {
System.out.println(" - " + location.getName() + ": " + quantity + " units");
hasStock = true;
}
}
if (!hasStock) {
System.out.println(" - No stock available at any location");
}
System.out.println("--------------------------------------------------");
}
}
}
private static StockLocation selectStockLocation() {
if (stockLocations.isEmpty()) {
System.out.println("No stock locations available.");
return null;
}
System.out.println("Available locations:");
for (int i = 0; i < stockLocations.size(); i++) {
System.out.println((i + 1) + ". " + stockLocations.get(i).getName());
}
System.out.print("Select a location: ");
int choice = Integer.parseInt(scanner.nextLine());
if (choice < 1 || choice > stockLocations.size()) {
System.out.println("Invalid choice.");
return null;
}
return stockLocations.get(choice - 1);
}
private static StockItem selectStockItem() {
if (stockItems.isEmpty()) {
System.out.println("No stock items available.");
return null;
}
System.out.println("Available stock items:");
for (int i = 0; i < stockItems.size(); i++) {
System.out.println((i + 1) + ". " + stockItems.get(i).getName());
}
System.out.print("Select a stock item: ");
int choice = Integer.parseInt(scanner.nextLine());
if (choice < 1 || choice > stockItems.size()) {
System.out.println("Invalid choice.");
return null;
}
return stockItems.get(choice - 1);
}
private static void loadData() {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(DATA_FILE))) {
StockItem[] items = (StockItem[]) ois.readObject();
StockLocation[] locations = (StockLocation[]) ois.readObject();
stockItems.addAll(Arrays.asList(items));
stockLocations.addAll(Arrays.asList(locations));
System.out.println("Data loaded successfully.");
} catch (FileNotFoundException e) {
System.out.println("Data file not found, starting with example stock.");
// Initialise with some default data
stockItems.add(new StockItem("SKU001", "Item 1", "Description 1", new String[]{"tag1", "tag2"}));
stockItems.add(new StockItem("SKU002", "Item 2", "Description 2", new String[]{"tag3", "tag4"}));
stockLocations.add(new StockLocation("Location 1", "Description 1", "Address 1", "City 1", "Country 1", "PostalCode 1"));
stockLocations.add(new StockLocation("Location 2", "Description 2", "Address 2", "City 2", "Country 2", "PostalCode 2"));
stockLocations.get(0).addStockItem(stockItems.get(0), 10);
stockLocations.get(1).addStockItem(stockItems.get(1), 20);
System.out.println("Default data initialized.");
} catch (IOException | ClassNotFoundException e) {
System.out.println("Error loading data: " + e.getMessage());
}
}
}