Creating a LinkedList Application in Java

Problem:

Item
- type : String
- name : String
- price : int
+ item(type : String, name : String, price : int)
// setters getters
+ toString() : String

Employee

- name : String
- id : int 
+ Employee(name : String, id : int)
 // setters getters
 + toString() : String


Supermarket
-item: LinkedList<Item>
 - Employee : Linked List <Employee>
+Supermarket()
+ addItem(item : Item) : void
+ deleteltem(name : String) : void
+ getAllChocolates() : LinkedList<Item>
+ getAllDairy() : LinkedList<Item>
+ getAllCandies() : LinkedList<Item>
+ hasItem(name : String) : Boolean
-requestOrder(name : String) : void
+ printltems() : void



You have to implement the previous three classes. The class Item has three private variables, a constructor, setters and getters, and a toString method. The class Employee has two private variables, a constructor, setters and getters, and a toString method. The class Supermarket has a LinkedList of items, a linked list of employees, and a couple of methods that apply operations on the LinkedList.

-addEmployee( employee: Employee): Inserts the specific employee at the beginning of the list -removeEmployees(): Removes all elements from this list
- addItem(item : Item): takes an item as parameter and adds it to items
- deleteltem (name : String): deletes an item from Item with the specified name
-getAllChocolates (): returns a LinkedList<Item> that contains all the items that have a type equal to Chocolate in items
- getAllDairy (): returns a LinkedList< Item > that contains all the items that have a type equal to Dairy in items
– getAllCandies (): returns a LinkedList<Item> that contains all the items that have a type equal to candies In items
 -hasItem(name : String): returns true if the item with the specified name is found in items; else, it returns false and calls requestOrder(name)
- requestOrder(name : String): prints "Requesting an order for "+name
- printltems(): prints the elements in item

In the tester class, you are required to create an object of type Supermarket and add to its lists a number of items and employees.

You should add each new employee to the beginning of the list.

You should retrieve all items that are chocolate and print them, retrieve all items that are dairy and print them, and retrieve all items that are candies and print them.

You should test the deleteltem method and print the items list after deletion.

Test the requestOrder method and verify that it is calling the requestOrder method if the item is not found in the list.

Finally, remove all employees and hire new employees.

Solution:

001/**-----------------Employee.java-----------------------*/
002public class Employee {
003  
004 private String name;
005 private int id;
006  
007 public Employee(String name, int id) {
008  this.name = name;
009  this.id = id;
010 }
011 
012 public String getName() {
013  return name;
014 }
015 
016 public void setName(String name) {
017  this.name = name;
018 }
019 
020 public int getId() {
021  return id;
022 }
023 
024 public void setId(int id) {
025  this.id = id;
026 }
027 
028 @Override
029 public String toString() {
030  return "[name=" + name + ", id=" + id + "]";
031 }
032}
033 
034/**-----------------Item.java-----------------------*/
035 
036public class Item {
037  
038 private String type;
039 private String name;
040 private int price;
041  
042 public Item(String type, String name, int price) {
043  this.type = type;
044  this.name = name;
045  this.price = price;
046 }
047 
048 public String getType() {
049  return type;
050 }
051 
052 public void setType(String type) {
053  this.type = type;
054 }
055 
056 public String getName() {
057  return name;
058 }
059 
060 public void setName(String name) {
061  this.name = name;
062 }
063 
064 public int getPrice() {
065  return price;
066 }
067 
068 public void setPrice(int price) {
069  this.price = price;
070 }
071 
072 @Override
073 public String toString() {
074  return "[type=" + type + ", name=" + name
075                             + ", price=" + price + "]";
076 }
077}
078 
079 
080/**--------------SumperMarket.java-------------*/
081import java.util.LinkedList;
082 
083public class SuperMarket {
084  
085 private LinkedList<Item> items;
086 private LinkedList<Employee> employees;
087  
088 public SuperMarket() {
089  items = new LinkedList<Item>();
090  employees = new LinkedList<Employee>();
091 }
092  
093 public void addItem(Item item) {
094  items.add(item);
095 }
096  
097 public void addEmployee(Employee employee) {
098  employees.addFirst(employee);
099 }
100  
101 public void deleteItem(String name) {
102  for (int i = 0; i < items.size(); i++)
103   if (items.get(i).getName().equals(name)) {
104    items.remove(i);
105    return;
106   }
107 }
108  
109 public void removeEmployees() {
110  employees.clear();
111 }
112  
113 public LinkedList<Item> getAllChocolates() {
114  LinkedList<Item> chocolates = new LinkedList<Item>();
115   
116  for (int i = 0; i < items.size(); i++)
117   if (items.get(i).getType().equalsIgnoreCase("chocolate"))
118    chocolates.add(items.get(i));
119   
120  return chocolates;
121 }
122  
123 public LinkedList<Item> getAllDairy() {
124  LinkedList<Item> dairies = new LinkedList<Item>();
125   
126  for (int i = 0; i < items.size(); i++)
127   if (items.get(i).getType().equalsIgnoreCase("dairy"))
128    dairies.add(items.get(i));
129   
130  return dairies;
131 }
132  
133 public LinkedList<Item> getAllCandy() {
134  LinkedList<Item> candies = new LinkedList<Item>();
135   
136  for (int i = 0; i < items.size(); i++)
137   if (items.get(i).getType().equalsIgnoreCase("candy"))
138    candies.add(items.get(i));
139   
140  return candies;
141 }
142  
143 public boolean hasItem(String name) {
144  for (int i = 0; i < items.size(); i++)
145   if (items.get(i).getName().equals(name))
146    return true;
147   
148  requestOrder(name);
149  return false;
150 }
151  
152 public void printVehicles() {
153  System.out.println(items);
154 }
155  
156 private void requestOrder(String name) {
157  System.out.println("Requesting an order for "+name);
158 }
159}


No comments :

Post a Comment

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