Share
Explore

Java Programming Lab Lesson: Beauty Product Management System

Learning Outcome:

Write a Java application to teach the construction of an Object Oriented java program with 5 classes that uses Arrays to store and retrieve the OBJECTS.

Overview

In this lesson, we are going to construct an Object-Oriented Java program that incorporates arrays to store and retrieve objects.
An object is a BOX into which you can put business data and business processes.
We will be creating a simple beauty product management system.
The five classes we'll create include:
Product: This class will represent individual beauty products.
Category: This class will represent different categories of beauty products.
Inventory: This class will represent an inventory of beauty products.
Customer: This class will represent the customers.
Shop: This class will represent a beauty shop and serves as the main application class.

Class 1: Product

First, we define the Product class. Each product will have a name, price, and category.
public class Product {
private String name;
private double price;
private Category category;

// constructor is a method that creates an object reference to a Class
// via the new Operator
public Product(String name, double price, Category category) {
this.name = name;
this.price = price;
this.category = category;
}

// getters and setters

public String getName() {
return name;
}

public double getPrice() {
return price;
}

public Category getCategory() {
return category;
}

@Override
public String toString() {
return "Product: " + name + ", Category: " + category.getName() + ", Price: " + price;
}
}

Class 2: Category

Next, we create the Category class. Each category will have a name and an array of products.
public class Category {
private String name;
private Product[] products;

public Category(String name, Product[] products) {
this.name = name;
this.products = products;
}

// getters and setters

public String getName() {
return name;
}

public Product[] getProducts() {
return products;
}

@Override
public String toString() {
return "Category: " + name;
}
}

Class 3: Inventory

The Inventory class will keep track of the different categories in our shop.
public class Inventory {
private Category[] categories;

public Inventory(Category[] categories) {
this.categories = categories;
}

// getters and setters

public Category[] getCategories() {
return categories;
}
}

Class 4: Customer

The Customer class will represent our customers. Each customer will have a name and a favorite category of products.
public class Customer {
private String name;
private Category favoriteCategory;

public Customer(String name, Category favoriteCategory) {
this.name = name;
this.favoriteCategory = favoriteCategory;
}

// getters and setters

public String getName() {
return name;
}

public Category getFavoriteCategory() {
return favoriteCategory;
}

@Override
public String toString() {
return "Customer: " + name + ", Favorite Category: " + favoriteCategory.getName();
}
}

Class 5: Shop

Finally, we create our Shop class which is the main application class.
public class Shop {
private Inventory inventory;
private Customer[] customers;

public Shop(Inventory inventory, Customer[] customers) {
this.inventory = inventory;
this.customers = customers;
}

public static void main(String[] args) {
// Initialize categories, products, inventory, customers and the shop
// Also, print out the details of the customers and the inventory
}
}

You can fill the main method with the appropriate initializations for the categories, products, inventory, customers, and the shop. You can also use this method to print out the details of the customers and the inventory.
This basic system can be extended further according to your needs.
The key takeaway from this lesson is the utilization of Object-Oriented concepts in Java and the use of arrays to manage and store objects.
image.png
image.png
image.png
image.png

To create a customer with a favorite product, you would need to instantiate the required objects and set up the relationships between them. Here's an example of how you can implement the main method to create a customer with their favorite product:
javaCopy code
public class Shop {
private Inventory inventory;
private Customer[] customers;

public Shop(Inventory inventory, Customer[] customers) {
this.inventory = inventory;
this.customers = customers;
}

public static void main(String[] args) {
// Create the categories
Category makeup = new Category("Makeup", null);

// Create the products
Product lipstick = new Product("Lipstick", 9.99, makeup);
Product mascara = new Product("Mascara", 12.99, makeup);

// Assign the products to the makeup category
makeup.setProducts(new Product[]{lipstick, mascara});

// Create the inventory
Inventory inventory = new Inventory(new Category[]{makeup});

// Create the customer with their favorite category
Customer shakira = new Customer("Shakira", makeup);

// Print the customer's details
System.out.println(shakira);
}
}

In this updated code, we first create the makeup category with null as the initial products array. Then, we create the lipstick and mascara products, passing the makeup category as the category for both.
After creating the products, we assign them to the makeup category using the setProducts method. Finally, we create the inventory object, and the shakira customer with the makeup category as their favorite category.
Please make sure you have the appropriate import statements and separate the classes into their respective files.
In this example, we first create the required products (lipstick and mascara) and assign them to the makeup category. Then, we create the inventory object with the makeup category.
Finally, we create the shakira customer and pass the makeup category as their favorite category. The customer's details are printed using the toString() method.
Please note that the code provided assumes you have defined the classes Product, Customer, Category, and Inventory in separate files and imported them correctly.

Final Working Code:

public class Shop { private Inventory inventory; private Customer[] customers;
public Shop(Inventory inventory, Customer[] customers) { this.inventory = inventory; this.customers = customers; }
public static void main(String[] args) { // Create the categories Category makeup = new Category("Makeup", null);
// Create the products Product lipstick = new Product("Lipstick", 9.99, makeup); Product mascara = new Product("Mascara", 12.99, makeup);
// Assign the products to the makeup category makeup.setProducts(new Product[]{lipstick, mascara});
// Create the inventory Inventory inventory = new Inventory(new Category[]{makeup});
// Create the customer with their favorite category Customer shakira = new Customer("Shakira", makeup); Customer Lilly = new Customer("Lilly", makeup);
// Print the customer's details System.out.println(Lilly); } }
class Category { private String name; private Product[] products;
public Category(String name, Product[] products) { this.name = name; this.products = products; }
public String getName() { return name; }
public void setProducts(Product[] products) { this.products = products; }
// other methods }
class Customer { private String name; private Category favoriteCategory;
public Customer(String name, Category favoriteCategory) { this.name = name; this.favoriteCategory = favoriteCategory; }
public String getName() { return name; }
public Category getFavoriteCategory() { return favoriteCategory; }
@Override public String toString() { return "Customer: " + name + ", Favorite Category: " + favoriteCategory.getName(); } }
class Product { private String name; private double price; private Category category;
public Product(String name, double price, Category category) { this.name = name; this.price = price; this.category = category; }
// getters and other methods }
class Inventory { private Category[] categories;
public Inventory(Category[] categories) { this.categories = categories; }
// getters and other methods }

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.