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:

/**-----------------Employee.java-----------------------*/
public class Employee {
 
 private String name;
 private int id;
 
 public Employee(String name, int id) {
  this.name = name;
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 @Override
 public String toString() {
  return "[name=" + name + ", id=" + id + "]";
 }
}

/**-----------------Item.java-----------------------*/

public class Item {
 
 private String type;
 private String name;
 private int price;
 
 public Item(String type, String name, int price) {
  this.type = type;
  this.name = name;
  this.price = price;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getPrice() {
  return price;
 }

 public void setPrice(int price) {
  this.price = price;
 }

 @Override
 public String toString() {
  return "[type=" + type + ", name=" + name 
                             + ", price=" + price + "]";
 }
}


/**--------------SumperMarket.java-------------*/
import java.util.LinkedList;

public class SuperMarket {
 
 private LinkedList<Item> items;
 private LinkedList<Employee> employees;
 
 public SuperMarket() {
  items = new LinkedList<Item>();
  employees = new LinkedList<Employee>();
 }
 
 public void addItem(Item item) {
  items.add(item);
 }
 
 public void addEmployee(Employee employee) {
  employees.addFirst(employee);
 }
 
 public void deleteItem(String name) {
  for (int i = 0; i < items.size(); i++)
   if (items.get(i).getName().equals(name)) {
    items.remove(i);
    return;
   }
 }
 
 public void removeEmployees() {
  employees.clear();
 }
 
 public LinkedList<Item> getAllChocolates() {
  LinkedList<Item> chocolates = new LinkedList<Item>();
  
  for (int i = 0; i < items.size(); i++)
   if (items.get(i).getType().equalsIgnoreCase("chocolate"))
    chocolates.add(items.get(i));
  
  return chocolates;
 }
 
 public LinkedList<Item> getAllDairy() {
  LinkedList<Item> dairies = new LinkedList<Item>();
  
  for (int i = 0; i < items.size(); i++)
   if (items.get(i).getType().equalsIgnoreCase("dairy"))
    dairies.add(items.get(i));
  
  return dairies;
 }
 
 public LinkedList<Item> getAllCandy() {
  LinkedList<Item> candies = new LinkedList<Item>();
  
  for (int i = 0; i < items.size(); i++)
   if (items.get(i).getType().equalsIgnoreCase("candy"))
    candies.add(items.get(i));
  
  return candies;
 }
 
 public boolean hasItem(String name) {
  for (int i = 0; i < items.size(); i++)
   if (items.get(i).getName().equals(name))
    return true;
  
  requestOrder(name);
  return false;
 }
 
 public void printVehicles() {
  System.out.println(items);
 }
 
 private void requestOrder(String name) {
  System.out.println("Requesting an order for "+name);
 }
}


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