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:

001/**-----------------------Market.java------------------*/
002import java.util.LinkedList;
003import java.util.ListIterator;
004 
005public class Market {
006  
007 private LinkedList<product> products;
008  
009 public Market() {
010  products = new LinkedList<product>();
011 }
012  
013 public LinkedList<product> getProducts() {
014  return products;
015 }
016  
017 public void addProduct(Product product) {
018  products.add(product);
019 }
020  
021 public void expireProducts() {
022  ListIterator<product> itr = products.listIterator(0);
023   
024  while (itr.hasNext()) {
025   Product temp = itr.next();
026    
027   if (temp.getExpYear() < 2012)
028    itr.remove();
029  }
030 }
031}
032 
033/*-----------------------OrderedList.java-------------*/
034import java.util.LinkedList;
035import java.util.ListIterator;
036 
037public class OrderedList<e comparable="" extends="">> {
038  
039 private LinkedList<e> list;
040  
041 public OrderedList() {
042  list = new LinkedList<e>();
043 }
044  
045 public void addSorted(E item) {
046  ListIterator<e> itr = list.listIterator(0);
047   
048  while (itr.hasNext()) {
049   E temp = itr.next();
050    
051   if (item.compareTo(temp) < 0) {
052    itr.previous();
053    itr.add(item);
054    return;
055   }
056  }
057   
058  list.add(item);
059 }
060  
061 public E retrieve(E item) {
062  ListIterator<e> itr = list.listIterator(0);
063   
064  while (itr.hasNext()) {
065   E temp = itr.next();
066    
067   if (temp.equals(item))
068    return temp;
069  }
070   
071  return null;
072 }
073  
074 public void delete(E item) {
075  ListIterator<e> itr = list.listIterator(0);
076   
077  while (itr.hasNext()) {
078   E temp = itr.next();
079    
080   if (temp.equals(item)) {
081    itr.remove();
082    return;
083   }
084  }
085 }
086  
087 public int search(E item) {
088  ListIterator<e> itr = list.listIterator(0);
089   
090  while (itr.hasNext()) {
091   E temp = itr.next();
092    
093   if (temp.equals(item))
094    return itr.previousIndex();
095  }
096   
097  return -1;
098 }
099}
100 
101/**-----------------------Product.java---------------------*/
102public class Product {
103 
104 private int id;
105 private String name;
106 private int expYear;
107  
108 public Product(int id, String name, int expYear) {
109  this.id = id;
110  this.name = name;
111  this.expYear = expYear;
112 }
113  
114 public int getId() {
115  return id;
116 }
117  
118 public void setId(int id) {
119  this.id = id;
120 }
121  
122 public String getName() {
123  return name;
124 }
125  
126 public void setName(String name) {
127  this.name = name;
128 }
129  
130 public int getExpYear() {
131  return expYear;
132 }
133  
134 public void setExpYear(int expYear) {
135  this.expYear = expYear;
136 }
137  
138 public String toString() {
139  return "Product "+id+": "+name+" "+expYear;
140 }
141}
142</e></e></e></e></e></e></e></product></product></product></product>


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