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-----------------------*/ |
002 | public class Employee { |
007 | public Employee(String name, int id) { |
012 | public String getName() { |
016 | public void setName(String name) { |
024 | public void setId( int id) { |
029 | public String toString() { |
030 | return "[name=" + name + ", id=" + id + "]" ; |
034 | /**-----------------Item.java-----------------------*/ |
042 | public Item(String type, String name, int price) { |
048 | public String getType() { |
052 | public void setType(String type) { |
056 | public String getName() { |
060 | public void setName(String name) { |
064 | public int getPrice() { |
068 | public void setPrice( int price) { |
073 | public String toString() { |
074 | return "[type=" + type + ", name=" + name |
075 | + ", price=" + price + "]" ; |
080 | /**--------------SumperMarket.java-------------*/ |
081 | import java.util.LinkedList; |
083 | public class SuperMarket { |
085 | private LinkedList<Item> items; |
086 | private LinkedList<Employee> employees; |
088 | public SuperMarket() { |
089 | items = new LinkedList<Item>(); |
090 | employees = new LinkedList<Employee>(); |
093 | public void addItem(Item item) { |
097 | public void addEmployee(Employee employee) { |
098 | employees.addFirst(employee); |
101 | public void deleteItem(String name) { |
102 | for ( int i = 0 ; i < items.size(); i++) |
103 | if (items.get(i).getName().equals(name)) { |
109 | public void removeEmployees() { |
113 | public LinkedList<Item> getAllChocolates() { |
114 | LinkedList<Item> chocolates = new LinkedList<Item>(); |
116 | for ( int i = 0 ; i < items.size(); i++) |
117 | if (items.get(i).getType().equalsIgnoreCase( "chocolate" )) |
118 | chocolates.add(items.get(i)); |
123 | public LinkedList<Item> getAllDairy() { |
124 | LinkedList<Item> dairies = new LinkedList<Item>(); |
126 | for ( int i = 0 ; i < items.size(); i++) |
127 | if (items.get(i).getType().equalsIgnoreCase( "dairy" )) |
128 | dairies.add(items.get(i)); |
133 | public LinkedList<Item> getAllCandy() { |
134 | LinkedList<Item> candies = new LinkedList<Item>(); |
136 | for ( int i = 0 ; i < items.size(); i++) |
137 | if (items.get(i).getType().equalsIgnoreCase( "candy" )) |
138 | candies.add(items.get(i)); |
143 | public boolean hasItem(String name) { |
144 | for ( int i = 0 ; i < items.size(); i++) |
145 | if (items.get(i).getName().equals(name)) |
152 | public void printVehicles() { |
153 | System.out.println(items); |
156 | private void requestOrder(String name) { |
157 | System.out.println( "Requesting an order for " +name); |
No comments :
Post a Comment