This program is supposed to write a food storage that fills with 2 different items: food name and the amount. It then writes the food items into a text file. Also, it is supposed to read the same text file and load all the contents into an array to be edited again. Do the comments explain enough and are there enough or are there too many? Is the code organized in an efficient and easy to read manner?
public class Storage {
private Food[] food;
static final String IN_FILE_NAME = "FoodStorage.txt";
static final String OUT_FILE_NAME = "FoodStorage.txt";
public static final int MAX_STORAGE = 100;
public static final String DELIM = "\t";
public static final int FIELD_AMT = 2;
public Storage()
{
food = new Food[MAX_STORAGE];
}
public Food[] getFood()
{
return this.food;
}
//Adds more food to the first empty spot in the array food
public void addFood(Food aF)
{
for(int i = 0; i < food.length; i++)
{
if(food[i] == null)
{
food[i] = aF;
return;
}
}
//If storage is full
System.out.println("You storage is full");
}
//Removes food from the Storage
public void removeFood(Food food2)
{
for(int i = 0; i < food.length; i++)
{
if(food[i] != null && food[i].equals(food2))
{
food[i] = null;
return;
}
}
//If the food searched isn't in the storage
System.out.println("The food was not found in storage");
}
//Prints the different options for manipulating the food array
public static void printOptions()
{
System.out.println("1: Add Food\n2: Remove Food\n3: See current Food\n4: Quit");
}
//Returns an instance of a food based on user input
public static Food makeAFood()
{
Food retF;
System.out.println("Enter the Food's Name");
//foodName is a string
String foodName = keyboard.nextLine();
System.out.println("Enter the amount of food");
double amount = keyboard.nextDouble();
keyboard.nextLine();
retF = new Food(foodName,amount);
return retF;
}
//Prints the storage of food into the current list
public static void printStorage(Storage aF)
{
for(Food f : aF.getFood())
{
if(f == null)
continue;
System.out.println(f);
}
}
//Reads the txt file for the array
public static void readFile(String food)
{
Scanner scan = null;
try
{
scan = new Scanner(new File(IN_FILE_NAME));
BufferedReader in = new BufferedReader(new FileReader(IN_FILE_NAME));
String str;
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
list.add(str);
}
in.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
StringBuffer sb = new StringBuffer("");
while(scan.hasNextLine())
{
String fileLine = scan.nextLine();
//Splits the lines at the DELIM Making them different
//pieces and adds them to the array split Lines
String[] splitLines = fileLine.split(DELIM);
//Takes the first array index and sets it equal
//to the variable name of the food
String foodName = splitLines[0];
if(!foodName.equals("null"))
{
System.out.println(fileLine);
sb.append(fileLine + "\n");
}
}
scan.close();
}
//Writes the Storage into the txt file
public static void writeFile(String OUT_FILE_NAME, Storage aF)
{
PrintWriter pw = null;
try
{
//Makes a new PW, BW and FW for the txt file
pw = new PrintWriter(new BufferedWriter
(new FileWriter(IN_FILE_NAME)));
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
//Reads the current food in storage and prints it
//into the txt file
for(Food f : aF.getFood())
{
if(f == null)
continue;
pw.println(f);
}
pw.close();
}
static Scanner keyboard;
//Entry point of the program
public static void main(String[] args)
{
keyboard = new Scanner(System.in);
Storage S = new Storage();
boolean quit = false;
while(!quit)
{
System.out.println("Welcome to your food Cabinet");
//Prints out the 4 different options for the Storage
printOptions();
//writes the storage S to the file
writeFile(OUT_FILE_NAME, S);
//sets pick to the next user input
int pick = keyboard.nextInt();
keyboard.nextLine();
//The 4 different Options there are
switch(pick)
{
case 1: //Adds Food to storage
S.addFood(makeAFood());
break;
case 2: //Removes Food from storage
S.removeFood(makeAFood());
break;
case 3: //Reads File from the txt file
readFile(IN_FILE_NAME);
break;
case 4: // Quits the Program
quit = true;
break;
default:
//for invalid values that are entered
System.out.println("Invalid input");
}
}
//If the program is exited this prints
System.out.println("Goodbye");
}
}
and this is class food
public class Food
{
private String foodName;
private double amount;
public Food()
{
this.foodName = "No Food name yet";
this.amount = 0.0;
}
//Constructors
public Food(String aFoodName, double amount2) {
this.foodName = aFoodName;
this.amount = amount2;
}
//Accessors
public String getFoodName()
{
return this.foodName;
}
public double getAmount()
{
return this.amount;
}
//Mutators
public void setFoodName(String aFoodName)
{
this.foodName = aFoodName;
}
public void setAmount(int anAmount)
{
if(anAmount > 0)
{
this.amount = anAmount;
}
else
{
System.out.println("That is not a valid amount");
}
}
public String toString()
{
return this.foodName + "\t" + amount;
}
public boolean equals(Food aFood)
{
return aFood != null &&
this.foodName.equals(aFood.getFoodName()) &&
this.amount == aFood.getAmount();
}
}
Foodclass to make the code complete. \$\endgroup\$