Professional Documents
Culture Documents
PTDip
PTDip
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;
}
Page | 5
import java.io.Serializable;
// 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);
}
Page | 6
import java.util.Calendar;
import java.io.Serializable;
// 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;
}
Page | 7
import java.util.Scanner;
import java.io.*;
// 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();
}
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;
}
Page | 9
return "Sardine";
else
return "Soup";
}
Page | 10
System.out.println("No more space available - The bin is full!");
Page | 11
public void exitSave()
{
writeToFile();
System.out.println("Exiting Program - Thank you!");
}
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