Created
December 5, 2010 01:44
-
-
Save danarbaugh/728685 to your computer and use it in GitHub Desktop.
rough draft of an inventory object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class Inventory{ | |
static int MAXARRAYSIZE = 100; // Max. size for arrays | |
int[] partNum; | |
int[] quantity; | |
double[] cost; | |
double[] wholesaleCost; | |
int[] reorderQuantity; | |
String[] description; | |
int invCnt; | |
//constructor | |
public Inventory() throws Exception{ | |
partNum = new int[MAXARRAYSIZE]; | |
quantity = new int[MAXARRAYSIZE]; | |
cost = new double[MAXARRAYSIZE]; | |
wholesaleCost = new double[MAXARRAYSIZE]; | |
reorderQuantity = new int[MAXARRAYSIZE]; | |
description = new String[MAXARRAYSIZE]; | |
invCnt = 0; | |
FileReader inputFile = new FileReader("initInv.dat"); // read input file | |
BufferedReader indata = new BufferedReader(inputFile); | |
String workingLine = ""; | |
while ((workingLine = indata.readLine()) != null){ // stop when no lines left | |
String[] tokens = workingLine.split(" ", 6); // split line into String array | |
int pn = Integer.parseInt(tokens[0]); | |
int q = Integer.parseInt(tokens[1]); | |
double c = Double.parseDouble(tokens[2]); // convert values to proper datatypes | |
double wc = Double.parseDouble(tokens[3]); | |
int rq = Integer.parseInt(tokens[4]); | |
if(pn!=-1)addPart(pn, q, c, wc, rq, tokens[5]); // add part if not last line | |
} | |
inputFile.close(); | |
indata.close(); | |
} | |
//method to add a new part, cost, and quantity | |
public void addPart(int pn, int q, double c, double wc, int rq, String d){ | |
boolean unique = true; | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) unique = false; // if part num already present, don't add it | |
} | |
if(unique == true){ | |
partNum[invCnt]=pn; | |
quantity[invCnt]=q; | |
cost[invCnt]=c; | |
wholesaleCost[invCnt]=wc; | |
reorderQuantity[invCnt]=rq; | |
description[invCnt]=d; | |
invCnt++; | |
} | |
} | |
public void setQuantity(int pn, int quant){ //method to set quantity | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) quantity[i]=quant; | |
} | |
} | |
public void setCost(int pn, double amnt){ //method to set cost | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) cost[i]=amnt; | |
} | |
} | |
public void setWholesale(int pn, double amnt){ //method to set wholesale cost | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) wholesaleCost[i]=amnt; | |
} | |
} | |
public void setReorder(int pn, int num){ //method to set reorder quantity | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) reorderQuantity[i]=num; | |
} | |
} | |
public void setDescription(int pn, String str){ //method to set description | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) description[i]=str; | |
} | |
} | |
//method to return the quantity of part 'pn' in stock | |
public int getQuantity(int pn){ | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) return quantity[i]; | |
} | |
return -1; // failure to find part | |
} | |
//method to return the cost of part 'pn' | |
public double getCost(int pn){ | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) return cost[i]; | |
} | |
return -0.001; // failure to find part | |
} | |
//method to return the wholesale cost of part 'pn' | |
public double getWholesale(int pn){ | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) return wholesaleCost[i]; | |
} | |
return -0.001; // failure to find part | |
} | |
//method to return the reorder quantity of part 'pn' | |
public int getReorder(int pn){ | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) return reorderQuantity[i]; | |
} | |
return -1; // failure to find part | |
} | |
//method to return the description of part 'pn' | |
public String getDescription(int pn){ | |
for(int i=0;i<invCnt;i++){ | |
if(partNum[i]==pn) return description[i]; | |
} | |
return "404_PART_NOT_FOUND"; // failure to find part | |
} | |
//finalizer | |
public void finalize() throws Exception{ | |
FileWriter outFile = new FileWriter("finalInv.dat", true); // write output file | |
PrintWriter fileOutput = new PrintWriter(outFile,true); | |
for(int i=0;i<invCnt;i++){ | |
fileOutput.println(partNum[i] + " " + quantity[i] + " " + cost[i] + " " + wholesaleCost[i] + " " + reorderQuantity[i] + " " + description[i]); | |
} | |
fileOutput.println("-1 0 0.0 0.0 0 0"); | |
fileOutput.close(); | |
outFile.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment