UML diagram java
A company manages many stores.
Each Store contains many products. The UML diagram of this company system is
represented as follow: (Write a program based on the UML diagram)
Product.java
public class Product {
private String name;
int quality;
double price;
Product(String name, int quality, double price){
}
public String getName() {
return name;
}
}
Store.java
import java.util.ArrayList;
public class Store {
String name;
String location;
ArrayList<Product> productList = new ArrayList<Product>();
int nbProduct = 0;
Store(String name, String location) {
this.name = name;
this.location = location;
}
public String getName() {
return name;
}
public void addProduct(Product p) {
if (productList.size() >= 100) {
return;
}
productList.add(p);
nbProduct++;
}
public boolean searchProduct(String productName) {
for (int i = 0; i < nbProduct; i++) {
if (productList.get(i).getName() == productName)
return true;
}
return false;
}
public Product deleteProduct(String productName) {
for (int i = 0; i < nbProduct; i++) {
if (productList.get(i).getName() == productName) {
Product p = productList.get(i);
productList.remove(i);
nbProduct--;
return p;
}
}
return null;
}
public void displayAll() {
for (int i = 0; i < nbProduct; i++) {
System.out.println(productList.get(i).getName());
}
}
}
Company.java
public class Company {
String name;
ArrayList<Store> arrayStore = new ArrayList<Store>();
int nbStore;
Company(String name) {
this.name = name;
}
void addStore(Store store) {
if (arrayStore.size() >= 10) {
return;
}
this.arrayStore.add(store);
this.nbStore++;
}
int searchNbofStore(String productName) {
int storesHavingProduct = 0;
for (int i = 0; i < nbStore; i++) {
if (this.arrayStore.get(i).searchProduct(productName)) {
storesHavingProduct++;
}
}
return storesHavingProduct;
}
void displayAll() {
System.out.println("All the stores that " + this.name + " owns.");
for (int i = 0; i < nbStore; i++) {
System.out.println(this.arrayStore.get(i).getName());
}
}
}
TestCompany.java
public class TestCompany {
public static void main(String[] args) {
Product p1 = new Product("TV", 4, 34000);
Product p2 = new Product("Bicycle", 4, 5500);
Product p3 = new Product("Oven", 3, 70000);
Store s1 = new Store("Makro", "Karachi");
Store s2 = new Store("Hypermart", "Karachi");
s1.addProduct(p1);
s1.addProduct(p2);
s1.addProduct(p3);
System.out.println("\nDisplaying all the products in store 1:\n ");
s1.displayAll();
Product tempProduct = s1.deleteProduct("Bicycle");
if (tempProduct != null)
System.out.println("\n\nProduct " + tempProduct.getName() + " is deleted\n");
else
System.out.println("\n\nThere is no product to delete");
s1.displayAll();
s2.addProduct(p1);
s2.addProduct(p2);
s2.addProduct(p3);
s2.displayAll();
Company c1 = new Company("Unilever");
c1.addStore(s1);
c1.addStore(s2);
c1.displayAll();
int n = c1.searchNbofStore("TV");
System.out.println("\nNumber of stores that have TV " + n);
}
}