Assign2.pdf
Assign2Test.java
Assign2Test.java
import java.util.Scanner;
**
* This class contains Assignment 1 with main method
* and create a reference of Inventory class
*
*
public class Assign2Test {
public static void main(String[] args) {
creating object of Inventory class
Inventory test=new Inventory();
initializing option variable
int option=0;
creating object of Scanner class
Scanner scan=new Scanner (System.in);
do {
try {
try statement
System.out.println(
epresent the option menu to read
"1: Add item to Inventory\r\n" +
"2: Display cu
ent inventory\r\n"+
"3: Buy item(s)\r\n"+
"4: Sell items(s)\r\n"+
"5: Convert A
ayList to Linkedlist and print it\r\n"+
"6: To exit\r\n");
System.out.print("> ");
user prompt to enter intege
option = scan.nextInt();
/**switch statement for the different option*
switch (option) {
case 1:
if (!test.addItem(scan))
System.out.print("Item already exist");
eak;
case 2:
System.out.println(test);
eak;
case 3:
test.buyItem(scan);
eak;
case 4:
test.sellitem(scan);
eak;
case 5:
test.printLL();
eak;
case 6:
System.out.println("Exiting....");
System.exit(0);
eak;
default:
System.out.println("Please select a valid selection between 1 - 6");
}
}catch (Exception e) {
System.out.println();
scan.next();
}
}while (option!=6);
}
end of main
}
end of class
FoodItem.JAVA
FoodItem.JAVA
**InputMismatchException class*
import java.util.InputMismatchException;
**Scanner class*
import java.util.Scanner;
**
* This class contains FoodItem and its items
*
*
public class FoodItem {
/** The item code. *
protected int itemCode;
/** The item name. *
protected String itemName;
/** The item price. *
protected float itemPrice;
/** The item quantity. *
protected int quantityInStock;
/** The item cost. *
protected float itemCost;
/**
* parameterized constructo
* Instantiates a new food item.
* @param itemCode represents item code
* @param itemName represents name of the item
* @param itemPrice represents price of the item
* @param quantityInStock represents the quantity
* @param itemCost represents the cost of the item
*
public FoodItem(int itemCode, String itemName, float itemPrice, int
quantityInStock, float itemCost) {
this.itemCode = itemCode;
this.itemName = itemName;
this.itemPrice = itemPrice;
this.quantityInStock = quantityInStock;
this.itemCost = itemCost;
}
/**
* no-argument constructo
* Instantiates a new food item.
*
public FoodItem() {
this.itemCode = 0;
this.itemName = null;
this.itemPrice = 0.00f;
this.quantityInStock = 0;
this.itemCost = 0.00f;
}
/**method getter to get item code
* @return itemCode
*/
public int getItemCode() {
return itemCode; }
/**method setter to set item code
* @param itemCode represents the code of the item
*/
public void setItemCode(int itemCode) {
this.itemCode = itemCode; }
/**method getter to get item name
* @return itemName
*/
public String getItemName() {
return itemName; }
/**method setter to set item name
* @param itemName represents the name of the item
*/
public void setItemName(String itemName) {
this.itemName =itemName; }
/**method getter to get item price
* @return itemPrice
*/
public float getItemPrice() {
return itemPrice; }
/**method setter to set item price
* @param itemPrice represents the price of the item
*/
public void setItemPrice(float itemPrice) {
this.itemPrice = itemPrice; }
/**method getter to get quantity
* @return quantityInStock
*/
public int getQuantityInStock() {
return quantityInStock; }
/**method setter to set item quantity
* @param quantityInStock represents the code of the item
*/
public void setQuantityInStock(int quantityInStock) {
this.quantityInStock = quantityInStock; }
/**method getter to get item cost
* @return itemCost
*/
public float getItemCost() {
return itemCost; }
/**method setter to set item cost
* @param itemCost represents the cost of the item
*/
public void setItemCost(float itemCost) {
this.itemCost = itemCost; }
/**method to print into String
* @return Item
*/
@Ove
ide
public String toString() {
return "Item:" + this.itemCode + " " + this.itemName + " " + this.quantityInStock +" price: $"+ this.itemPrice + " cost: $"+ this.itemCost ;
}
end of toString method
/**
* Method Input code.
*
* @param scan the scanne
* @return true, if successful
*
public boolean inputCode(Scanner scan) {
do {
try {
System.out.print("Enter the code for the item: ");
itemCode = scan.nextInt();
if (itemCode==0)
System.out.print("Invalid entry\n");
} catch (InputMismatchException e) {
System.out.print("Invalid entry\n");
scan.nextLine();
} }while(itemCode==0);
return true;
}
end of inputCode method
/**
* Checks if is equal.
*
* @param code the item
* @return true, if is equal
*
public boolean equals(FoodItem code) {
return this.itemCode==code.itemCode;
}
end of equals method
/**
* Method addItem
*
* @param scan the scanne
* @return true, if successful
*
public boolean addItem(Scanner scan){
scan.nextLine();
System.out.print("Enter the name of the item: ");
user prompt to enter String
itemName = scan.nextLine();
do {
try {
System.out.print("Enter the quantity of the item: ");
user prompt to enter intege
quantityInStock = scan.nextInt();
if (quantityInStock<=0)
checking condition
System.out.print("Invalid entry\n");
} catch (InputMismatchException e) {
System.out.print("Invalid entry\n");
catch exception
scan.nextLine();
} } while(quantityInStock<=0);
scan.nextLine();
do {
try {
System.out.print("Enter the cost of the item: ");
user prompt to enter intege
itemCost = scan.nextFloat();
if (itemCost<=0)
checking condition
System.out.print("Invalid entry\n");
} catch (InputMismatchException e) {
catch exception
System.out.print("Invalid entry\n");
scan.nextLine();
} } while(itemCost<=0);
do {
try {
System.out.print("Enter the sales price of the item: ");
user prompt to enter intege
itemPrice = scan.nextFloat();
if (itemPrice<=0)
checking condition
System.out.print("Invalid entry\n");
} catch (InputMismatchException e) {
catch exception
System.out.print("Invalid entry\n");
scan.nextLine();
} } while(itemPrice<=0);
return true;
}
end of addItem method
}
end of class
Fruit.JAVA
Fruit.JAVA
**Scanner class*
import java.util.Scanner;
**
* This class contains Fruit and its items and
* it extends class FoodItem
*
*
*
public class Fruit extends FoodItem {
/**orchard name*
protected String orchardName;
/**
* Default Constructo
*
public Fruit() {
calling no arg constructor of super class by using super keyword
super();
orchardName = "";
}
end of constructo
/**method to print into String
* ove
ide method in super class by using super keyword and
* adding orchardName item
* @return Item
*/
@Ove
ide
public String toString() {
return super.toString() + " orchard supplier: " + this.orchardName;
}
end of toString method
/**
* Ove
iding Method addItem of the super class by using supe
* keyword and adding orchard name
*
* @param scan the scanne
* @return true, if successful
*
@Ove
ide
public boolean addItem(Scanner scan) {
super.addItem(scan);
System.out.print("Enter the name of the orchard supplier: ");
user prompt to enter String
orchardName =scan.next();
}
return true;
}
end of addItem method
}
end of class
Inventory.java
Inventory.java
**A
ayList class*
import java.util.A
ayList;
**LinkedList class*
import java.util.LinkedList;
**Scanner class*
import java.util.Scanner;
**
* This class contains Inventory in an A
aylist and LinkedList
*
*
public class Inventory {
/** A
aylist of inventory *
private A
ayList
inventory;
/** represents the index *
private int index;
/** object of class FoodItem *
private FoodItem food= new FoodItem();
**default constructor*
public Inventory() {
inventory=new A
ayList(20);
inventory=new FoodItem (size);
food=null;
}
end of default constructo
/**method to print into String
* @return print
*/
@Ove
ide
public String toString() {
String print=" ";
for (int i=0; iprinting all elements
print+=inventory.get(i).toString()+"\n";
}
end of for loop
return "Inventory in A
ayList:\n"+print;
}
end of toString method
/**method to check if itemCode is equal in the inventory a
aylist
* @param item for the itemCode
* @return index
*/
public int alreadyExists(FoodItem item) {
int index=-1;
for (int i = 0; i < inventory.size(); i++) {
if (inventory.get(index).itemCode == food.itemCode) {
index = i;
eak;
}
}
end of for loop
return index;
}
end of alreadyExists method
/**
* Method addItem
*
* @param scan the scanne
* @return true, if successful
*
public boolean addItem(Scanner scan) {
String choice=null;
do {
try {
System.out.print("Do you wish to add a fruit(f), vegetable(v), sweetener (s) or a preserve(p)?: ");
user prompt to enter String
choice = scan.next();
switch (choice.toLowerCase()) {
case "f":
food = new Fruit();
creating object of Fruit
eak;
case "v":
food = new Vegetable();
creating object of Vegetable
eak;
case "s":
food=new Sweetener();
creating object of Sweetene
eak;
case "p":
food = new Preserve();
creating object of Preserve
eak;
default:
System.out.print("Invalid entry\n");
}
food.inputCode(scan);
boolean flag=false;
for (FoodItem foodItem : inventory) {
checking if itemCode is in inventory
if (foodItem.itemCode==food.itemCode) {
food=foodItem;
flag=true;
}
}
if (flag==true) {
if itemCode is inventory
System.out.print("Item already exist\n");
}else {
if not creating item
inventory.add(food);
food.addItem(scan);
}
eak;
}catch (NullPointerException npe) {
}catch (IndexOutOfBoundsException iob) {
}
}while(choice!="f"&&choice!="v"&&choice!="p"&&choice!="s");
checking condition is true
return true;
}
end of addItem method
/**
* Method buyItem checks if the item code is valid
* then buy quantity
*
* @param scan the scanne
*
*
public void buyItem(Scanner scan) {
System.out.println("Enter valid item code: ");
int code = scan.nextInt();
boolean response = false;
FoodItem result = null;
for (Object o : inventory) {
if (((FoodItem) o).getItemCode() == code) {
result = (FoodItem) o;
response = true;
}
}
if (response) {
System.out.print("Enter valid quantity to buy: ");
int quantity = scan.nextInt();
if (quantity >= 1 && result.getQuantityInStock() >= quantity) {
int old = result.getQuantityInStock();
result.setQuantityInStock(old + quantity);
System.out.println("Item buyed");
} else {
System.out.println("Invalid quantity...\n" +
"E
or...could not buy item");
}
} else {
System.out.println("Code not found in inventory...\n" +
"E
or...could not buy item");
}
}
end of buyItem method
/**
* Method printLL to print LinkedList object
* then buy quantity
*
*
*
*
public void printLL() {
LinkedList