0% found this document useful (0 votes)
14 views4 pages

Eee

The document contains Java classes for managing products and stock, including a Product class with attributes like id, name, supplier, and stock, and methods for manipulating these attributes. It also includes a StockManager class for adding products, finding products by supplier, and removing products with no stock. Additionally, there is a MUArrayList class that implements a dynamic array with methods for adding elements and finding indices of items.

Uploaded by

10121442
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Eee

The document contains Java classes for managing products and stock, including a Product class with attributes like id, name, supplier, and stock, and methods for manipulating these attributes. It also includes a StockManager class for adding products, finding products by supplier, and removing products with no stock. Additionally, there is a MUArrayList class that implements a dynamic array with methods for adding elements and finding indices of items.

Uploaded by

10121442
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

public class Product {

private String id;


private String name;
private String supplier;
private int stock;

public Product(String id) {


this.id = id;
}

public String getId() {


return id;
}

public void setId(String id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getSupplier() {


return supplier;
}

public void setSupplier(String supplier) {


this.supplier = supplier;
}

public int getStock() {


return stock;
}

public void setStock(int stock) {


this.stock = stock;
}

public Product(String id, String name, String supplier, int stock) {


this.id = id;
this.name = name;
this.supplier = supplier;
this.stock = stock;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product product = (Product) o;
return stock == product.stock && Objects.equals(id, product.id) &&
Objects.equals(name, product.name) && Objects.equals(supplier, product.supplier);
}
@Override
public String toString() {
return "Product{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", supplier='" + supplier + '\'' +
", stock=" + stock +
'}';
}
}

public class StockManager {


private ArrayList<Product> products;

public StockManager() {
products = new ArrayList<>();
}

public void addproduct(Product p) {


products.add(p);
}

public ArrayList<Product> findproductBySupplier(String supplier) {


ArrayList<Product> result = new ArrayList<>();
for (int i = 0; i < products.size(); i++) {

{
if (products.get(i).getSupplier().equals(supplier))
result.add(products.get(i));
}
}
return result;
}
public Product removeProductIFNOStock(String id){
int index= products.indexOf(new Product(id));
if(index!=-1){
Product found =products.get(index);
if(found.getStock()==0){
products.remove(index);
return found;
}
}
return null;
}
public String toString(){
String s="[";
for(int i=0;i< products.size();i++){
s+=products.get(i);
}
return s;
}
}

public class Main {


public static void main(String[] args) {
StockManager manager=new StockManager();
Product p1=new Product("12","bananA","SUPP1",1);
Product p2=new Product("13","bananA","SUPP2",2);
Product p3=new Product("14","Strawberry","SUPP3",3);
Product p4=new Product("15","Apple","SUPP4",4);
manager.addproduct(p1);
manager.addproduct(p2);
manager.addproduct(p3);
manager.addproduct(p4);
manager.findproductBySupplier("Supp1");
manager.removeProductIFNOStock("12");
System.out.println("Print all");
System.out.println(manager);
}

public class MUArrayList<E> {


private static final int InitialCApacity = 10;
private E[] theData;
private int size = 0;
private int capacity = 0;

public MUArrayList() {
capacity = InitialCApacity;
theData = (E[]) new Object[capacity];
}

public boolean add(E item) {


if (size == capacity)
reallocate();
theData[size] = item;
size++;
return true;
}

public void reallocate() {


theData = Arrays.copyOf(theData, capacity);
}

public void AllINDEXof(E item) {


for (int i = 0; i < size; i++) {
if (theData[i].equals(item))
System.out.println(i);
}
}

public void addFirstLAst(E item) {


if (size == capacity)
reallocate();
for (int i = size; i > 0; i--) {
theData[i] = theData[i - 1];
}
//add first
theData[0] = item;
size++;
//add last
theData[size++] = item;

public String toString() {


String s = "[";
for (int i = 0; i < size; i++)
s += theData[i];

return s;
}
}

public class Main2 {


public static void main(String[] args) {
MUArrayList<Integer> myList=new MUArrayList<>();
myList.add(1);
myList.add(2);
myList.add(3);
myList.add(1);

System.out.println("The list:");
myList.AllINDEXof(1);
System.out.println(myList);
myList.AllINDEXof(2);
System.out.println(myList);
myList.addFirstLAst(7);
System.out.println(myList);
}
}

You might also like