Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Package Name: hmwk08 Source File Name: (Submit zip of these) Warehouse.java Ware

ID: 3598180 • Letter: P

Question

Package Name:

hmwk08

Source File Name:

(Submit zip of these)

Warehouse.java

WarehouseTest.java

Test Files:

C2800_Ch08_Warehouse_Test.zip

This program will manage the quantity of each item in a warehouse.

Your program will:

Initialize 2 warehouses

The first warehouse have 5 of each item (use the constructor that accepts a single integer)

The second warehouse has 1 of the first item, 5 of the second item, 2 of the third, 4 of the fourth, and 3 of the fifth. Use the constructor that accepts an array

Print the quantities of each item in both warehouses (use toString)

Keep reading warehouse, item, and quantities from the user

Stop reading when the user enters -1 for the warehouse

Otherwise, read the item and quantity from the user

If the user enters 1, then add stock to the first warehouse

If the user enters 2, then add stock to the second warehouse

Print the quantities of each item in both warehouses (use toString)

Add the quantities of the 2 warehouses and store this in a new warehouse

Then print the total quantities across both warehouses (use toString)

Note the Item number 0 represents the first item in the warehouse

Warehouse class will maintain the quantities of each item. At a minimum, you must have:

Instance variables

An array to keep the quantity of the 5 items. Quantities are integers

Constructors

A constructor that accepts a single integer. Set the quantity of each of the 5 items to this value

A constructor that accepts an array. Copy the values from the passed in array into the array that is the instance array

Methods

A method that accepts an item (index) and quantity to add to that item in the array

toString() that returns a string with the quantities of each item. If the quantities are 1, 2, 3, 4, 5. Then this method would return “{ 1 2 3 4 5 }”

A method that accepts a Warehouse object. It will add the quantities from the current Warehouse and the passed in Warehouse. The results will be in a new Warehouse object that this method will return.

WarehouseTest class will contain main(). It should do all of the user-prompting/reading and call Warehouse methods. Warehouse should have no input/output lines.

Requirements:

Put System.out.println() after each line of input. This will help you match the Test Program and make it easier to use it.

Sample Run #1: (the highlighted text is what the user types)

Warehouse 1: { 5 5 5 5 5 }

Warehouse 2: { 1 5 2 4 3 }

Warehouse? 1

Item qty? 0 5

Warehouse? 1

Item qty? 2 5

Warehouse? 2

Item qty? 1 3

Warehouse? -1

Warehouse 1: { 10 5 10 5 5 }

Warehouse 2: { 1 8 2 4 3 }

All Warehouses: { 11 13 12 9 8 }

Package Name:

hmwk08

Source File Name:

(Submit zip of these)

Warehouse.java

WarehouseTest.java

Test Files:

C2800_Ch08_Warehouse_Test.zip

Explanation / Answer

Implemented both the classes as per the requirement and tested.

WarehouseTest.java

--------------------------------

import java.util.Scanner;

public class WarehouseTest {

public static void main(String[] args) {

int qt[] = {1,5,2,4,3}, choic = 0, ind=0, qty=0; //Initializing some mandatory variables

Warehouse wh1 = new Warehouse(5); //Objects creation

Warehouse wh2 = new Warehouse(qt);

System.out.println(wh1.toString());

System.out.println(wh2.toString());

Scanner sc = new Scanner(System.in);

while(choic!=-1) { //Proces starts here

System.out.print("Warehouse? ");

choic = sc.nextInt();

System.out.println();

if(choic == 1) { //Condition to add qty to first warehouse

System.out.print("Item qty? ");

ind = sc.nextInt();

System.out.println();

while(ind<0 && ind>4) {

System.out.println("Enter valid index: ");

ind = sc.nextInt();

}

qty = sc.nextInt();

wh1.addQuantity(ind, qty);

}

else if(choic == 2) {//Condition to add qty to second warehouse

System.out.print("Item qty? ");

ind = sc.nextInt();

System.out.println();

while(ind<0 && ind>4) {

System.out.println("Enter valid index: ");

ind = sc.nextInt();

}

qty = sc.nextInt();

wh2.addQuantity(ind, qty);

}

else if(choic == -1) { //Termination condition

break;

}

else {

System.out.println("Enter a valid warehouse number: ");

}

}

//Printing results

System.out.println(wh1.toString());

System.out.println(wh2.toString());

System.out.println(wh1.totalQty(wh2).toString());

}

}

Warehose.java

-----------------

public class Warehouse {

int war_no;

static int war_num = 0;

int[] war = new int[5];

//Constant constructor

public Warehouse(int qt) {

for(int i=0; i<5; i++) {

war[i] = qt;

}

war_no = 1;

}

//Array constructor

public Warehouse(int[]war) {

this.war = war;

war_no=2;

}

//Method to add qty to the arrays

public void addQuantity(int ind, int qty) {

war[ind]+=qty;

}

//Method to sum up all the qtys of both the warehouses

public Warehouse totalQty(Warehouse wh) {

int [] qtys = new int[5];

for(int i=0; i<5 ; i++) {

qtys[i] = this.war[i]+wh.war[i];

}

war_num = 3;

return new Warehouse(qtys);

}

//toString method overridden

public String toString(){

String re = "Warehouse "+war_no+": { ";

if(war_num == 3) {

re = "All warehouses "+": { ";

war_num=0;

}

for(int i=0; i<war.length; i++) {

re = re+war[i]+" ";

}

return re+"}";

}

}