Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Contents

Part 1 : Function of the program ------------------------- Page – 2

Part 2 : Class Diagram --------------------------------------- Page – 3

Part 3 : Codes ------------------------------------------------- Page – 4 to 12

Part 4 : Test Logs --------------------------------------------- Page – 13 to 17

Part 5 : Reflection -------------------------------------------- Page – 18

Page | 1
Part 1

The main function for the program is to list the canned foods, check the stock and to remove the expired
canned foods. Firstly, you need to run the program (CannedFoodWarehouse – Main Class), will show
the Programmed menu. When you choose Menu-1 and it will allow you to Insert new canned to the bin
by Can ID, Can Expiry Date and Description of the Can. Each Bin can store up to 10 only. If there is more
than 10 can, it will show you the error message “No more space available - The bin is full!”.

2nd option from the Main menu is to check the existing stock of the can from each bin. 3rd option is to
remove the Can from the bin. But you need to choose the bin that you want to remove the can. When
you choose 4th option from the Main menu, It will automatic remove the expired can. The last option
(No. 5) will exit you from the program.

Page | 2
Part 2

Page | 3
Part 3

import java.io.Serializable;
public class Bin implements Serializable
{
//instance variables
private String location;
private String description;
private CannedFood [] stock;
private int numOfCans;
// constructor
public Bin(String location, String description)
{
this.location = location;
this.description = description;
stock = new CannedFood[10];
numOfCans = 0;
}
// methods
public boolean addCannedFood(CannedFood item)
{
if (isFull())
return false;
else
{
stock[numOfCans] = item;
numOfCans++;
return true;
}
}
public CannedFood getCannedFood()
{
if (isEmpty())
return null;
else
{
numOfCans--;
return stock[numOfCans];
}
}
public boolean isFull()
{
if (numOfCans == 10)

Page | 4
return true;
else
return false;
}
public boolean isEmpty()
{
if (numOfCans == 0)
return true;
else
return false;
}
// accessor methods
public String getLocation()
{
return this.location;
}

public String getDescription()


{
return this.description;
}
public int getNumOfCans()
{
return this.numOfCans;
}
public CannedFood getStock(int index)
{
return this.stock[index];
}
public String toString()
{
String items = "";
for (int idx = 0; idx < numOfCans; idx++)
items = items + stock[idx];
return "Location : " + this.location + "\tDescription : " + this.description + "\n" + items;
}

Page | 5
import java.io.Serializable;

public class CannedFood implements Serializable


{
// data attributes (instance variables)
private String ID;
private String description;
private MyDate expiryDate;

// constructor
public CannedFood(String ID, String description, int year, int month, int day)
{
this.ID = ID;
this.description = description;
expiryDate = new MyDate(year, month, day);
}

public boolean isExpired()


{
if (expiryDate.isBefore(expiryDate.getYear(), expiryDate.getMonth(), expiryDate.getDay()))
return true;
else
return false;
}

public String toString()


{
return "ID : " + this.ID + "\tDesc : " + this.description + "\tExpiry : " + this.expiryDate + "\n";
}

public String getID() { return this.ID;}


public String getDescription() {return this.description;}
public MyDate getExpiryDate() { return expiryDate;}

Page | 6
import java.util.Calendar;
import java.io.Serializable;

public class MyDate implements Serializable


{
// data attributes (instance variables)
private int year;
private int month;
private int day;

// constuctor
public MyDate(int year, int month, int day)
{
this.year = year;
this.month = month;
this.day = day;
}

// method
public boolean isBefore(int year, int month, int day)
{
Calendar today = Calendar.getInstance();
if (today.get(Calendar.YEAR) > year)
return true;
else
if (today.get(Calendar.YEAR) == year && (today.get(Calendar.MONTH)+1) >
month)
return true;
else
if (today.get(Calendar.YEAR) == year &&
(today.get(Calendar.MONTH)+1) == month && today.get(Calendar.DAY_OF_MONTH) > day)
return true;
else
return false;
}

// overriden method of the Object class


public String toString()
{
return "" + this.day + "-" + this.month + "-" + this.year;
}
// accessor methods
public int getYear(){ return this.year; }
public int getMonth(){ return this.month; }
public int getDay(){ return this.day; }

Page | 7
import java.util.Scanner;
import java.io.*;

public class CannedFoodWarehouse


{
// instance variables
private Bin [] bins;
Scanner input = new Scanner(System.in);

// constructor
public CannedFoodWarehouse()
{
bins = new Bin[3];
bins[0] = new Bin("A1", "Beans");
bins[1] = new Bin("A2", "Sardine");
bins[2] = new Bin("A3", "Soup");
readFromFile();
}

public void readFromFile()


{
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new
File("Data.txt")));
for (int x = 0; x < 3; x++)
bins[x] = (Bin)ois.readObject();
System.out.println("Data Loaded");
ois.close();
}catch (Exception e){ System.out.println("File not Found!"); }
}

public void writeToFile()


{
try{
ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream(new File("Data.txt")));
for (int x = 0; x < 3; x++)
oos.writeObject(bins[x]);
oos.flush();
System.out.println("Saving data - Good Bye");
oos.close();
}catch (Exception e)
{
System.out.println("File not Found!");
}
}

Page | 8
public char menu()
{
System.out.println("\n[1] Insert Canned Food ");
System.out.println("[2] List Canned Foods ");
System.out.println("[3] Remove Canned Food ");
System.out.println("[4] Remove Expired Canned Food");
System.out.println("[5] Exit & Save");
System.out.print("Enter choice : ");
char choice = input.nextLine().charAt(0);
return choice;
}

public void process()


{
char choice;
do{
choice = menu();
switch(choice)
{
case '1' : insertCannedFood();
break;
case '2' : listCannedFoods();
break;
case '3' : removeCannedFood();
break;
case '4' : removeExpired();
break;
case '5' : exitSave();
break;
default : System.out.println("Invalid choice!");
}
}while (choice!='5');
}

public String getTypeOfCan()


{
char choice = ' ';
do{
System.out.println("[1] Beans [2] Sardine [3] Soup ");
System.out.print("Enter choice ");
choice = input.nextLine().charAt(0);
if (choice < '1' || choice > '3')
System.out.println("Invalid choice - try again ");
}while (choice < '1' || choice > '3');
if (choice == '1')
return "Beans";
else
if (choice == '2')

Page | 9
return "Sardine";
else
return "Soup";
}

public boolean isUnique(String id)


{
boolean unique = true;;
for (int index = 0; index < bins.length; index++)
for(int can = 0; can < bins[index].getNumOfCans(); can++)
if (bins[index].getStock(can).getID().equals(id))
{
System.out.println("ID already exists! - try again");
unique = false;
break;
}
return unique;
}

public String getID()


{
String id = "";
do{
System.out.print("Enter can ID : ");
id = input.nextLine();
}while (!isUnique(id));
return id;
}

public CannedFood makeCannedFood()


{
String id = getID();
System.out.print("Enter the expiry date dd/mm/yyyy ");
String expiry = input.nextLine();
String [] dateParts = expiry.split("/",0);
String type = getTypeOfCan();
return new CannedFood(id, type, Integer.parseInt(dateParts[2]),
Integer.parseInt(dateParts[1]), Integer.parseInt(dateParts[0]));
}

public void insertCannedFood()


{
CannedFood can = makeCannedFood();
for (int idx = 0; idx < 3; idx++)
if (bins[idx].getDescription().equals(can.getDescription()))
if (bins[idx].addCannedFood(can))
System.out.println("Can was successfully added to the bin");
else

Page | 10
System.out.println("No more space available - The bin is full!");

public void listCannedFoods()


{
System.out.println();
for (int idx = 0; idx < 3; idx++)
{
System.out.println("Bin of " + bins[idx].getDescription() + " currently contains " +
bins[idx].getNumOfCans() + " cans");
System.out.println(bins[idx]);
}
}

public void removeCannedFood()


{
String type = getTypeOfCan();
for (int idx = 0; idx < 3; idx++)
{
if (bins[idx].getDescription().equals(type))
{
CannedFood can = bins[idx].getCannedFood();
if (can == null)
System.out.println("Item unavailable!");
else
System.out.println(can.getID() + " was removed from the bin");
}
}
}

public Bin removeCans(Bin bin)


{
Bin temp = new Bin(bin.getLocation(), bin.getDescription());
for(int idx = 0; idx < bin.getNumOfCans(); idx++)
if (!bin.getStock(idx).isExpired()){
CannedFood item = bin.getStock(idx);
temp.addCannedFood(item);
}
return temp;
}

public void removeExpired()


{
for (int idx = 0; idx < 3; idx++)
bins[idx] = removeCans(bins[idx]);
System.out.println("Removed Expired Can");
}

Page | 11
public void exitSave()
{
writeToFile();
System.out.println("Exiting Program - Thank you!");
}

public static void main(String [] args)


{
CannedFoodWarehouse warehouse = new CannedFoodWarehouse();
warehouse.process();
}

Page | 12
Part 4

Page | 13
Page | 14
Page | 15
Page | 16
Page | 17
Part 5

During past 1 month of the classes, I was trying to concentrate the class but lacked concentration due to
the situation in my country (Myanmar). Although in this difficult time I was able to learn and did the
Assignment smoothly because of Mr. Professor’s teaching and guidance.

Page | 18

You might also like