My question is how to write the code in WareHouse.java This is WareHouse.java package warehouse; /* * * This class implements a warehouse on a
My question is how to write the code in WareHouse.java
This is WareHouse.java
package warehouse;
/*
*
* This class implements a warehouse on a Hash Table like structure,
* where each entry of the table stores a priority queue.
* Due to your limited space, you are unable to simply rehash to get more space.
* However, you can use your priority queue structure to delete less popular items
* and keep the space constant.
*
* @author Ishaan Ivaturi
*/
public class Warehouse {
private Sector[] sectors;
// Initializes every sector to an empty sector
public Warehouse() {
sectors = new Sector[10];
for (int i = 0; i < 10; i++) {
sectors[i] = new Sector();
}
}
/**
* Provided method, code the parts to add their behavior
* @param id The id of the item to add
* @param name The name of the item to add
* @param stock The stock of the item to add
* @param day The day of the item to add
* @param demand Initial demand of the item to add
*/
public void addProduct(int id, String name, int stock, int day, int demand) {
evictIfNeeded(id);
addToEnd(id, name, stock, day, demand);
fixHeap(id);
}
/**
* Add a new product to the end of the correct sector
* Requires proper use of the .add() method in the Sector class
* @param id The id of the item to add
* @param name The name of the item to add
* @param stock The stock of the item to add
* @param day The day of the item to add
* @param demand Initial demand of the item to add
*/
private void addToEnd(int id, String name, int stock, int day, int demand) {
// IMPLEMENT THIS METHOD
}
/**
* Fix the heap structure of the sector, assuming the item was already added
* Requires proper use of the .swim() and .getSize() methods in the Sector class
* @param id The id of the item which was added
*/
private void fixHeap(int id) {
// IMPLEMENT THIS METHOD
}
/**
* Delete the least popular item in the correct sector, only if its size is 5 while maintaining heap
* Requires proper use of the .swap(), .deleteLast(), and .sink() methods in the Sector class
* @param id The id of the item which is about to be added
*/
private void evictIfNeeded(int id) {
// IMPLEMENT THIS METHOD
}
/**
* Update the stock of some item by some amount
* Requires proper use of the .getSize() and .get() methods in the Sector class
* Requires proper use of the .updateStock() method in the Product class
* @param id The id of the item to restock
* @param amount The amount by which to update the stock
*/
public void restockProduct(int id, int amount) {
// IMPLEMENT THIS METHOD
}
/**
* Delete some arbitrary product while maintaining the heap structure in O(logn)
* Requires proper use of the .getSize(), .get(), .swap(), .deleteLast(), .sink() and/or .swim() methods
* Requires proper use of the .getId() method from the Product class
* @param id The id of the product to delete
*/
public void deleteProduct(int id) {
// IMPLEMENT THIS METHOD
}
/**
* Simulate a purchase order for some product
* Requires proper use of the getSize(), sink(), get() methods in the Sector class
* Requires proper use of the getId(), getStock(), setLastPurchaseDay(), updateStock(), updateDemand() methods
* @param id The id of the purchased product
* @param day The current day
* @param amount The amount purchased
*/
public void purchaseProduct(int id, int day, int amount) {
// IMPLEMENT THIS METHOD
}
/**
* Construct a better scheme to add a product, where empty spaces are always filled
* @param id The id of the item to add
* @param name The name of the item to add
* @param stock The stock of the item to add
* @param day The day of the item to add
* @param demand Initial demand of the item to add
*/
public void betterAddProduct(int id, String name, int stock, int day, int demand) {
// IMPLEMENT THIS METHOD
}
/*
* Returns the string representation of the warehouse
*/
public String toString() {
String warehouseString = "[ ";
for (int i = 0; i < 10; i++) {
warehouseString += "\t" + sectors[i].toString() + " ";
}
return warehouseString + "]";
}
/*
* Do not remove this method, it is used by Autolab
*/
public Sector[] getSectors () {
return sectors;
}
}
AddProduct.java
package warehouse;
/*
* Use this class to test to addProduct method.
*/
public class AddProduct {
public static void main(String[] args) {
StdIn.setFile(args[0]);
StdOut.setFile(args[1]);
// Use this file to test addProduct
}
}
BetterAddProduct.java
package warehouse;
/*
* Use this class to test the betterAddProduct method.
*/
public class BetterAddProduct {
public static void main(String[] args) {
StdIn.setFile(args[0]);
StdOut.setFile(args[1]);
// Use this file to test betterAddProduct
}
}
DeleteProduct.java
package warehouse;
/*
* Use this class to test the deleteProduct method.
*/
public class DeleteProduct {
public static void main(String[] args) {
StdIn.setFile(args[0]);
StdOut.setFile(args[1]);
// Use this file to test deleteProduct
}
}
Everything.java
package warehouse;
/*
* Use this class to put it all together.
*/
public class Everything {
public static void main(String[] args) {
StdIn.setFile(args[0]);
StdOut.setFile(args[1]);
// Use this file to test all methods
}
}
Product.java
package warehouse;
/*
* This class represents a warehouse Product.
*
* @author Ishaan Ivaturi
*
*/
public class Product {
private int id; // product identification
private String name; // product name
private int stock; // number of items in stock
private int lastPurchaseDay; // the number of days since the store opening that the item was last purchased
private int demand; // initial demand is obtained from a survey prior to product release
private int popularity; // Initial Demand + Total Amount Purchased + Date of Last Purchase
public Product(int i, String n, int s, int l, int d) {
id = i;
name = n;
stock = s;
lastPurchaseDay = l;
demand = d;
popularity = l + d;
}
public Product(int i, String n, int s, int l) {
this(i, n, s, l, 0);
}
public Product(int i, String n) {
this(i, n, 0, 0);
}
public int getId() {
return id;
}
public String getName() { return name; }
public int getStock() { return stock; }
public int getLastPurchaseDay() { return lastPurchaseDay; }
public int getDemand() { return demand; }
public int getPopularity() { return popularity; }
public void setId(int i) { id = i; }
public void setName(String n) { name = n; }
public void updateStock(int s) { stock += s; }
public void setStock(int s) { stock = s; }
public void setLastPurchaseDay(int l) {
lastPurchaseDay = l;
popularity = lastPurchaseDay + demand;
}
public void updateDemand(int d) {
demand += d;
popularity = lastPurchaseDay + demand;
}
public void setDemand(int d) {
demand = d;
popularity = lastPurchaseDay + demand;
}
/*
* Returns true if this object equals other
*/
public boolean equals (Object other) {
if ( !(other instanceof Product) ) {
return false;
}
Product o = (Product) other;
return this.getId() == o.getId();
}
public String toString() {
return String.format("(%s: %d, %d)", name, stock, popularity);
}
}
PurchaseProduct.java
package warehouse;
public class PurchaseProduct {
public static void main(String[] args) {
StdIn.setFile(args[0]);
StdOut.setFile(args[1]);
// Use this file to test purchaseProduct
}
}
Restock.java
package warehouse;
public class Restock {
public static void main(String[] args) {
StdIn.setFile(args[0]);
StdOut.setFile(args[1]);
// Uset his file to test restock
}
}
Sector.java
package warehouse;
public class Sector {
private Product[] products;
private int currentSize;
public Sector() {
products = new Product[6];
currentSize = 0;
}
// Add an item to the end of the sector, index 0 is ignored
public void add(Product prod) {
products[currentSize+1] = prod;
currentSize++;
}
// Set some index, valid indices are from 1 to currentSize inclusive
public void set(int index, Product prod) {
products[index] = prod;
}
// Delete the last element currently stored
public void deleteLast() {
products[currentSize] = null;
currentSize--;
}
// Get the product at some index
public Product get(int index) {
return products[index];
}
// Get the current size
public int getSize() {
return currentSize;
}
// Swap the items at 2 indices
public void swap(int index1, int index2) {
Product temp = products[index1];
products[index1] = products[index2];
products[index2] = temp;
}
// Apply the swim algorithm from class on some index
public void swim(int index) {
while (index > 1 && products[index].getPopularity() < products[index/2].getPopularity()) {
swap(index, index/2);
index /= 2;
}
}
// Apply the sink algorithm from class on some index
public void sink(int index) {
while (index*2 <= currentSize) {
int smallestChild;
if (index*2 + 1 > currentSize
|| products[index*2].getPopularity() < products[index*2 + 1].getPopularity()) {
smallestChild = index*2;
}
else smallestChild = index*2 + 1;
if (products[index].getPopularity() > products[smallestChild].getPopularity()) {
swap(index, smallestChild);
index = smallestChild;
}
else break;
}
}
public String toString() {
String sectorString = "{";
sectorString += "null";
for (int i = 1; i <= currentSize; i++) {
sectorString += "; " + products[i].toString();
}
return sectorString + "}";
}
}
These are in file that code need
addproduct.in
100 2 933 prod0 24 72 9 434 prod1 16 16 11 234 prod2 37 71 14 412 prod3 72 17 14 900 prod4 20 35 15 872 prod5 38 28 16 185 prod6 18 29 17 429 prod7 55 68 17 375 prod8 71 11 19 848 prod9 45 54 19 737 prod10 28 7 21 482 prod11 5 1 22 244 prod12 79 90 24 139 prod13 89 51 33 931 prod14 25 42 43 839 prod15 96 12 43 79 prod16 70 91 47 979 prod17 21 24 50 846 prod18 30 31 50 646 prod19 27 53 50 334 prod20 31 73 51 258 prod21 81 30 52 740 prod22 68 38 54 94 prod23 52 40 55 277 prod24 75 88 62 709 prod25 32 14 65 545 prod26 95 99 67 81 prod27 88 25 68 536 prod28 58 76 70 516 prod29 62 97 71 973 prod30 9 74 76 118 prod31 97 57 77 963 prod32 13 23 79 228 prod33 87 37 82 613 prod34 54 36 82 686 prod35 83 59 83 432 prod36 57 4 84 345 prod37 99 26 87 203 prod38 76 21 89 487 prod39 65 34 91 813 prod40 43 56 94 970 prod41 41 32 95 820 prod42 82 47 97 83 prod43 64 83 99 95 prod44 6 8 102 765 prod45 22 22 106 954 prod46 40 94 109 248 prod47 36 80 112 47 prod48 61 87 116 862 prod49 90 27 116 266 prod50 23 85 118 748 prod51 67 18 119 71 prod52 63 98 123 22 prod53 92 81 125 286 prod54 77 67 127 566 prod55 98 93 133 974 prod56 7 79 134 473 prod57 69 19 137 366 prod58 11 5 138 231 prod59 44 10 138 396 prod60 84 41 140 192 prod61 15 55 142 493 prod62 86 70 143 144 prod63 46 48 146 249 prod64 33 6 147 884 prod65 17 3 150 278 prod66 56 60 150 910 prod67 39 2 151 421 prod68 2 66 151 193 prod69 94 58 152 694 prod70 10 61 153 348 prod71 34 95 155 898 prod72 59 77 157 106 prod73 66 13 158 415 prod74 91 69 160 576 prod75 12 84 168 132 prod76 35 89 169 890 prod77 73 82 169 605 prod78 19 96 170 508 prod79 3 62 171 986 prod80 42 75 172 218 prod81 85 43 173 136 prod82 1 63 173 312 prod83 4 33 174 29 prod84 93 50 174 779 prod85 47 45 180 298 prod86 80 9 182 770 prod87 14 86 185 531 prod88 49 49 188 787 prod89 26 39 189 3 prod90 60 44 189 225 prod91 53 78 191 869 prod92 8 65 193 635 prod93 48 92 193 555 prod94 0 46 193 586 prod95 29 0 195 273 prod96 51 64 196 265 prod97 50 20 198 188 prod98 78 15 198 851 prod99 74 52
addtoend.in
10 3 980 Hoodie 15 3 6 603 Water-bottle 41 6 9 448 T-shirt 21 0 13 387 Lanyard 23 8 13 459 Key-chain 6 9 15 215 Pants 3 4 17 225 Notebook 24 7 19 475 Flag 47 1 19 139 Sunglasses 8 2 29 9 Stickers 19 5
betteraddproduct.in
100 2 149 prod0 52 1 3 921 prod1 60 43 5 713 prod2 31 72 5 199 prod3 84 18 6 864 prod4 16 86 11 532 prod5 97 75 12 591 prod6 7 71 12 719 prod7 43 97 15 200 prod8 39 69 15 806 prod9 93 12 24 498 prod10 53 67 26 483 prod11 57 89 29 901 prod12 86 82 29 972 prod13 59 42 29 305 prod14 49 55 29 858 prod15 65 68 29 380 prod16 36 46 30 945 prod17 22 48 32 40 prod18 55 35 33 91 prod19 45 63 37 128 prod20 62 78 38 160 prod21 21 77 38 790 prod22 26 81 39 970 prod23 6 2 40 346 prod24 54 13 40 167 prod25 5 92 40 81 prod26 12 91 43 367 prod27 77 17 43 45 prod28 71 36 46 421 prod29 82 47 51 43 prod30 14 90 52 665 prod31 10 6 52 927 prod32 83 95 55 856 prod33 23 33 58 647 prod34 67 28 59 264 prod35 81 70 63 378 prod36 0 49 64 176 prod37 50 80 65 323 prod38 51 53 65 34 prod39 8 79 67 63 prod40 61 11 68 899 prod41 76 59 70 938 prod42 48 60 70 677 prod43 56 27 71 674 prod44 33 84 76 192 prod45 35 37 79 62 prod46 74 24 81 838 prod47 85 15 85 384 prod48 46 31 88 500 prod49 4 44 90 397 prod50 94 50 93 718 prod51 19 20 93 395 prod52 80 32 94 942 prod53 42 96 95 687 prod54 41 14 98 898 prod55 89 7 99 805 prod56 2 41 101 415 prod57 34 4 102 86 prod58 1 22 105 56 prod59 15 5 106 759 prod60 99 34 112 544 prod61 32 19 113 788 prod62 90 93 118 454 prod63 66 25 120 728 prod64 98 76 122 515 prod65 20 45 124 313 prod66 37 74 124 327 prod67 11 52 126 226 prod68 64 16 128 784 prod69 9 3 131 250 prod70 38 85 132 197 prod71 69 39 135 405 prod72 79 30 135 46 prod73 70 56 135 577 prod74 68 73 136 359 prod75 17 10 137 720 prod76 24 21 141 136 prod77 87 29 143 622 prod78 63 98 145 404 prod79 92 83 146 423 prod80 27 26 149 58 prod81 40 87 151 673 prod82 95 40 156 172 prod83 13 51 159 216 prod84 78 66 161 994 prod85 72 64 162 766 prod86 88 88 165 844 prod87 25 99 167 42 prod88 75 0 167 345 prod89 58 8 169 157 prod90 96 65 171 997 prod91 73 58 174 869 prod92 47 62 175 896 prod93 30 23 175 850 prod94 44 94 181 676 prod95 3 54 184 436 prod96 18 57 195 623 prod97 91 9 198 980 prod98 29 61 199 861 prod99 28 38
deleteproduct.in
13 add 1 208 Hoodie 10 7 add 9 571 Water-bottle 40 8 add 10 43 T-shirt 36 9 delete 571 add 12 981 Lanyard 13 2 delete 208 add 15 566 Key-chain 0 4 add 15 405 Pants 39 0 add 17 62 Notebook 48 6 add 18 871 Flag 24 3 add 19 5 Sunglasses 21 1 add 25 148 Stickers 29 5 delete 5
everything.in
65 add 3 852 item0 0 19 add 9 201 item1 18 79 add 15 311 item2 44 23 add 18 844 item3 39 62 purchase 18 852 28 add 21 928 item4 56 25 restock 201 13 delete 311 add 22 777 item5 85 10 add 30 995 item6 51 2 add 32 505 item7 12 48 purchase 32 201 76 add 32 335 item8 84 70 add 36 927 item9 21 89 restock 777 80 purchase 36 505 59 add 36 135 item10 95 66 add 44 398 item11 47 1 add 47 761 item12 25 46 add 50 400 item13 22 8 add 51 434 item14 92 11 delete 777 add 55 798 item15 60 97 delete 398 add 63 923 item16 93 91 restock 761 71 add 81 260 item17 65 14 add 85 515 item18 14 30 add 87 986 item19 7 7 purchase 87 761 132 add 89 314 item20 97 17 delete 995 add 112 607 item21 4 42 delete 852 add 119 511 item22 33 87 add 120 596 item23 91 6 add 127 143 item24 3 94 add 129 926 item25 54 43 add 134 576 item26 83 45 add 137 388 item27 80 27 add 149 934 item28 74 24 add 150 793 item29 29 21 add 159 652 item30 43 22 add 159 884 item31 20 72 restock 986 23 add 160 611 item32 5 20 add 161 190 item33 48 38 add 164 66 item34 61 88 add 166 308 item35 89 31 add 169 191 item36 38 54 add 169 486 item37 98 99 add 170 112 item38 17 77 add 174 896 item39 79 32 add 176 369 item40 46 65 add 177 437 item41 16 44 add 179 138 item42 86 64 add 179 507 item43 15 96 add 183 281 item44 28 51 add 186 490 item45 72 85 add 190 353 item46 36 98 purchase 190 852 123 add 190 427 item47 77 5 add 193 581 item48 62 67 restock 191 11 add 195 630 item49 19 80
fixheap.in
5 2 140 Hoodie 41 123 4 0 Water-bottle 15 112 8 200 T-shirt 38 153 13 290 Lanyard 27 139 15 300 Key-chain 30 48
purchaseproduct.in
13 add 4 140 Hoodie 100 6 add 5 61 Water-bottle 100 4 purchase 5 61 107 add 6 9 T-shirt 100 8 add 9 377 Lanyard 100 5 add 14 923 Key-chain 100 3 purchase 14 61 71 add 14 637 Pants 100 7 add 16 378 Notebook 100 2 purchase 16 378 17 add 23 786 Flag 100 9 add 25 248 Sunglasses 100 0 add 26 28 Stickers 100 1
restock.in
13 add 1 316 Hoodie 0 6 restock 316 3 add 3 721 Water-bottle 0 2 add 4 904 T-shirt 0 9 add 5 686 Lanyard 0 3 restock 686 63 add 11 299 Key-chain 0 4 restock 721 20 add 16 712 Pants 0 8 add 19 897 Notebook 0 5 add 21 813 Flag 0 1 add 23 565 Sunglasses 0 0 add 26 39 Stickers 0 7
Step by Step Solution
3.47 Rating (157 Votes )
There are 3 Steps involved in it
Step: 1
Heres the Java implementation of the Warehouse class package warehouse import javautilPriorityQueue public class Warehouse private Sector sectors publ...See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started