Implementing a Market System using LinkedList in Java

Problem:

Product
- id : int
- name : String
- expYear : int
+ Product(id : int, name : String, expYear : int)
+ setId(id : int) : void
+ getId() : int
+ setName(name : String) : void
+ getName(): String
+ setExpYear(expYear : int) : void
+ getExpYear() : int

Market
- products : LinkedList<Product>
+ Market()
+ getProducts() : LinkedList<Product>
+ addProduct(Product product) : void
+ expireProducts() : void

You are asked to implement a simple market system. The system is composed of two classes which are Product and Market.
The Product class has three private variables which are id, name, and expiration year. The class’ constructor should initialize these variables and there should be setter and getter methods for them.
The Market class has one private variable which is a linked list of type Product. The constructor should initialize the linked list. There are three methods to implement in this class. The first method is getProducts which returns the linked list. The method addProduct should add the product in its parameter to the linked list. Finally, expireProducts should remove from the list the products which have an expiration year less than 2012.
Write a tester class that has a Market object and add five products using the addProducts method. After adding, print the list, then call expireProducts and print the list after removing products that have an expiration year less than 2012.


Solution:

/**-----------------------Market.java------------------*/
import java.util.LinkedList;
import java.util.ListIterator;

public class Market {
 
 private LinkedList products;
 
 public Market() {
  products = new LinkedList();
 }
 
 public LinkedList getProducts() {
  return products;
 }
 
 public void addProduct(Product product) {
  products.add(product);
 }
 
 public void expireProducts() {
  ListIterator itr = products.listIterator(0);
  
  while (itr.hasNext()) {
   Product temp = itr.next();
   
   if (temp.getExpYear() < 2012)
    itr.remove();
  }
 }
}

/*-----------------------OrderedList.java-------------*/
import java.util.LinkedList;
import java.util.ListIterator;

public class OrderedList> {
 
 private LinkedList list;
 
 public OrderedList() {
  list = new LinkedList();
 }
 
 public void addSorted(E item) {
  ListIterator itr = list.listIterator(0);
  
  while (itr.hasNext()) {
   E temp = itr.next();
   
   if (item.compareTo(temp) < 0) {
    itr.previous();
    itr.add(item);
    return;
   }
  }
  
  list.add(item);
 }
 
 public E retrieve(E item) {
  ListIterator itr = list.listIterator(0);
  
  while (itr.hasNext()) {
   E temp = itr.next();
   
   if (temp.equals(item))
    return temp;
  }
  
  return null;
 }
 
 public void delete(E item) {
  ListIterator itr = list.listIterator(0);
  
  while (itr.hasNext()) {
   E temp = itr.next();
   
   if (temp.equals(item)) {
    itr.remove();
    return;
   }
  }
 }
 
 public int search(E item) {
  ListIterator itr = list.listIterator(0);
  
  while (itr.hasNext()) {
   E temp = itr.next();
   
   if (temp.equals(item))
    return itr.previousIndex();
  }
  
  return -1;
 }
}

/**-----------------------Product.java---------------------*/
public class Product {

 private int id;
 private String name;
 private int expYear;
 
 public Product(int id, String name, int expYear) {
  this.id = id;
  this.name = name;
  this.expYear = expYear;
 }
 
 public int getId() {
  return id;
 }
 
 public void setId(int id) {
  this.id = id;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public int getExpYear() {
  return expYear;
 }
 
 public void setExpYear(int expYear) {
  this.expYear = expYear;
 }
 
 public String toString() {
  return "Product "+id+": "+name+" "+expYear;
 }
}


1 comment :

  1. after liposuction Korea's #1 Liposculpture Clinic. Lydian plastic surgery is the home of VIP patients. Celebrities, Influencers and Diplomats all know and trust Doctor An and Lydian plastic surgery clinic to provide detailed results.

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact