In: Computer Science
Objective
This assignment is to review what you should have learned in CSC 142 and learn how I want you to do pseudocode and commenting for 143. Here’s the highlights this assignment will focus on:
Indefinite Loops
File Processing
Arrays
Classes
Description
You will choose a subject that’s familiar to you and create a program to read an inventory of items. You will create a java Class to hold the items, store them into an array of that class, and print a filtered set of them using a fencepost loop.
As an example, I created an inventory of fly fishing rods. Here’s a sample input file and console output:
Manufacturer Type Weight Length
Sage Standard 5 108
Sage Spey 9 132
TFO Standard 8 108
Orvis Standard 4 96
Which Field to filter on? Manufacturer
Enter the string: sage
Rod [Manufacturer=Sage, Type=Standard, Weight=5, Length=108]
Rod [Manufacturer=Sage, Type=Spey, Weight=9, Length=132]
Which Field to filter on? type
Enter the string: spey
Rod [Manufacturer=Sage, Type=Spey, Weight=9, Length=132]
Which Field to filter on? w
Enter the value: 5
Rod [Manufacturer=Sage, Type=Standard, Weight=5, Length=108]
Which Field to filter on? l
Enter the value: 108
Rod [Manufacturer=Sage, Type=Standard, Weight=5, Length=108]
Rod [Manufacturer=TFO, Type=Standard, Weight=8, Length=108]
Requirements
Your inventory must have the following:
At least 10 items
At least 4 fields with 2 different data types
The names of the fields should start with different letters to make it easier to code
Phase 1 - Pseudocode
You will write some brief pseudocode for your code so I can give you feedback your project before you go through writing the code.
Start by identifying a subject you’re familiar with which has a list of items you can put together. Then create the design for a Java Class to hold those items.
See the “What is Pseudocode” page for information on how to write pseudocode and “Simple Guidelines” for what is correct pseudocode.
Phase 2 - Coding
You will write the complete code for your project and submit both the code, output, and debugging.
Code Requirements
Name the client class with main “Inventory” with matching case. This is extremely important or your code won’t run in my test environment.
Put all classes into one file by omitting the “public” from all but the first
Javadoc function headers with explanations of the function purpose, inputs, and returns
Comments above each code block, blank lines separating blocks that carry out one purpose, but none within the block
Do not comment each line, or lines which are obvious (e.g. print a blank line)
Data must be encapsulated as best as you can
Your main() should be a small as possible
All the real work should be in several very methods with clear purposes.
Ignores upper and lowercase
Accepts any input with the correct first letter in upper or lowercase
package file;
import java.io.*;
import java.util.Scanner;
/**
* Defines a class to store inventory information
*/
public class Inventory
{
/**
* Instance variable to store data
*/
String manufacturer;
String type;
int weight;
int length;
/**
* Default constructor to initialize default values to
* instance variables
*/
Inventory()
{
manufacturer = type = "";
weight = length = 0;
}// End of default constructor
/**
* Parameterized constructor to assign parameter values
* to instance variables
*/
Inventory(String ma, String ty, int we, int le)
{
manufacturer = ma;
type = ty;
weight = we;
length = le;
}// End of parameterized constructor
/*
* Overrides toString() method to return
* inventory information
* @see java.lang.Object#toString()
*/
public String toString()
{
return "Rod [Manufacturer = " + manufacturer +
", Type = " + type + ", Weight = " + weight +
", Length = " + length + "]";
}// End of method
/**
* Method to red file contents
* @param inv[] array of Inventory class object
* @return number of records
*/
int readFile(Inventory inv[])
{
// Local variable to store data read from file
String manufacturer;
String type;
int weight;
int length;
// Record counter
int counter = 0;
// Try block
try
{
// File Reader object created to read data
// from Popularity.txt file
File reader = new File("Inventory.txt");
// Scanner class object created
Scanner sc = new Scanner(reader);
// Loops till data available
while(sc.hasNext())
{
// Reads data
manufacturer = sc.next();
type = sc.next();
weight = sc.nextInt();
length = sc.nextInt();
/*
* Creates an object using
* parameterized constructor and assigns it
* at counter index position of array of object
*/
inv[counter] = new Inventory(manufacturer, type,
weight, length);
// Increase counter by one
counter++;
}// End of while
// Closer the file
sc.close();
} // End of try block
// Catch block to handle file not found exception
catch(FileNotFoundException e)
{
System.out.println("File Not found");
e.printStackTrace();
}// End of catch
// Returns record counter
return counter;
}// End of method
/**
* Method to search data based on filter type
* @param inv[] - array of Inventory class object
* @param len - number of records
* @param filterType - Searching sting type
*/
void search(Inventory inv[], int len, String filterType)
{
// Scanner class object created
Scanner keyboard = new Scanner(System.in);
// Found status initially -1 for not found
int f = -1;
// To store the string data entered by the user to search
String strData;
// To store the integer data entered by the user to search
int intData;
// Checks the first character of the filterType
switch(filterType.charAt(0))
{
// Manufacturer
case 'M':
case 'm':
// Accepts manufacturer data
System.out.print("\n Enter the string: ");
strData = keyboard.next();
// Loops till number of records
for(int c = 0; c < len; c++)
/**
* Checks if current object manufacturer
* is equals to the user entered manufacturer
* sets the found status f to c value
* displays the found inventory record
*/
if(inv[c].manufacturer.
compareTo(strData) == 0)
{
f = c;
System.out.println(inv[c]);
}// End of if condition
break;
// Type
case 'T':
case 't':
// Accepts type data
System.out.print("\n Enter the string: ");
strData = keyboard.next();
// Loops till number of records
for(int c = 0; c < len; c++)
/**
* Checks if current object type is equals
* to the user entered type
* sets the found status f to c value
* displays the found inventory record
*/
if(inv[c].type.compareTo(strData) == 0)
{
f = c;
System.out.println(inv[c]);
}// End of if condition
break;
// Weight
case 'W':
case 'w':
// Accepts weight data
System.out.print("\n Enter the value: ");
intData = keyboard.nextInt();
// Loops till number of records
for(int c = 0; c < len; c++)
/**
* Checks if current object weight is equals
* to the user entered weight
* sets the found status f to c value
* displays the found inventory record
*/
if(inv[c].weight == intData)
{
f = c;
System.out.println(inv[c]);
}// End of if condition
break;
// Length
case 'L':
case 'l':
// Accepts length data
System.out.print("\n Enter the value: ");
intData = keyboard.nextInt();
// Loops till number of records
for(int c = 0; c < len; c++)
/**
* Checks if current object length is equals
* to the user entered length
* sets the found status f to c value
* displays the found inventory record
*/
if(inv[c].length == intData)
{
f = c;
System.out.println(inv[c]);
}// End of if condition
break;
case 'E':
case 'e':
System.exit(0);
default:
System.out.println("Invalid Field " +
"to filter");
}// End of switch case
// Checks if found status f value is -1 record not found
if(f == -1)
System.out.println("\n No such item.");
}// End of method
// main method definition
public static void main(String ss[])
{
// Scanner class object created
Scanner input = new Scanner(System.in);
// Creates an array of object of class Inventory
// of size 5
Inventory inv[] = new Inventory[5];
// Creates the first object using default constructor
inv[0] = new Inventory();
// Calls the method to read file and stores it in
// array of object
// Stores the return value as number of objects
int len = inv[0].readFile(inv);
// To store filter type entered by the user
String filterType;
// Loops till number of records and displays each record
for(int c = 0; c < len; c++)
System.out.println(inv[c]);
// Loops till user choice is not 'Exit' or 'E' or 'e'
do
{
// Accepts filter type from the user
System.out.print("\n Which Field to filter " +
"on (Exit to stop)? ");
filterType = input.next();
// Call the method to search record
// and displays it
inv[0].search(inv, len, filterType);
}while(true);// End of do - while loop
}// End of main method
}// End of class
Sample Output:
Rod [Manufacturer = Sage, Type = Standard, Weight = 5, Length =
108]
Rod [Manufacturer = Sage, Type = Spey, Weight = 9, Length =
132]
Rod [Manufacturer = TFO, Type = Standard, Weight = 8, Length =
108]
Rod [Manufacturer = Orvis, Type = Standard, Weight = 4, Length =
96]
Rod [Manufacturer = Orvis, Type = Spey, Weight = 8, Length =
132]
Which Field to filter on (Exit to stop)? Danger
Invalid Field to filter
No such item.
Which Field to filter on (Exit to stop)? Manufacturer
Enter the string: Orvis
Rod [Manufacturer = Orvis, Type = Standard, Weight = 4, Length =
96]
Rod [Manufacturer = Orvis, Type = Spey, Weight = 8, Length =
132]
Which Field to filter on (Exit to stop)? Type
Enter the string: Sage
No such item.
Which Field to filter on (Exit to stop)? Type
Enter the string: Standard
Rod [Manufacturer = Sage, Type = Standard, Weight = 5, Length =
108]
Rod [Manufacturer = TFO, Type = Standard, Weight = 8, Length =
108]
Rod [Manufacturer = Orvis, Type = Standard, Weight = 4, Length =
96]
Which Field to filter on (Exit to stop)? Weight
Enter the value: 9
Rod [Manufacturer = Sage, Type = Spey, Weight = 9, Length =
132]
Which Field to filter on (Exit to stop)? Length
Enter the value: 192
No such item.
Which Field to filter on (Exit to stop)? Length
Enter the value: 132
Rod [Manufacturer = Sage, Type = Spey, Weight = 9, Length =
132]
Rod [Manufacturer = Orvis, Type = Spey, Weight = 8, Length =
132]
Which Field to filter on (Exit to stop)? Exit
Inventory.txt file contents
Sage Standard 5 108
Sage Spey 9 132
TFO Standard 8 108
Orvis Standard 4 96
Orvis Spey 8 132