All pastes #270187 Raw Edit

bitch

public java v1 · immutable
·published 2006-12-07 04:13 UTC
rendered paste body
//Jonathan Kovach//ITK 177, Secton 7, Dr. Vila//Program 8, Reading and Writing an object arrayimport java.io.*;//Import throws exceptionsimport java.util.Scanner;//Imports Scanner classimport java.text.NumberFormat;;//Imports numberformatpublic class Inventory { //Creates Inventory class	private Item[] items;; //Creates a single object array	public Inventory(String fileName) throws FileNotFoundException { //Creates Inventory constructor and accepts fileName as argument	Scanner scan = new Scanner(new File(fileName)); //Initializes keyboard Scanner as scan	items = new Item[scan.nextInt()]; //initializes the length of the items array	for(int i = 0; i < items.length; i++){ //for loop to cycle through all of the items arrays		items[i] = new Item(); //initializes an item class for each items array varible 		items[i].setQuantity(scan.nextInt()); //sets quantity 		items[i].setCost(scan.nextDouble()); //sets cost		items[i].setName(scan.nextLine()); //sets name		}}	public void increase(int number, int incr){ // creates an increase method and accepts the items number and the incriment number to the quantity	items[number].setQuantity(items[number].getQuantity() + incr);}public void decrease(int number, int decre){ // creates a decrease method identical to the increase except decriments the number to the quantity	items[number].setQuantity(items[number].getQuantity() - decre);}public void displayInventory() { // creates a displayInventory method that displays all information of inventory	NumberFormat format = NumberFormat.getCurrencyInstance(); //initializes the numberformat as format	int totalNum = 0; //initializes a total number variable to totalNum and sets to 0	double totalCost = 0; //initializes a total cost varible to totalCost and sets to 0		System.out.println("\t\tInventory Report");		System.out.println("\nPart#\tQOH\tCost\tTotal Cost\t Name"); //proper line formatting			for(int i = 0; i < items.length; i++){ //for loop to access all data in the items arrays		totalNum = items[i].getQuantity() + totalNum; //incriments totalNum with the quantity of the items		totalCost =(items[i].getCost() * items[i].getQuantity()) + totalCost; //imcrements totalCost by multiplying the items cost by quantity and adds to totalCost		System.out.println(i + "\t" + items[i].getQuantity() + "\t" + items[i].getCost() + "\t" + format.format((items[i].getCost()) * items[i].getQuantity()) + "\t\t" + items[i].getName());		 //proper line formatting and display of information	}			System.out.println("\t______\t\t______"); //Line Formatting		System.out.println("\t" + totalNum + "\t\t" + format.format(totalCost)); //formats the total cost and line formatted correctly		System.out.println(); //extra space		System.out.println(); //estra space}public void displayHighest() { //creates method for displaying the highest quantity	int highest = 0; //intitializes highest to 0		System.out.println("The Part with the Highest QOH is:");		for(int i = 0; i < items.length; i++){	if(items[i].getQuantity() > highest) // simple if statement to replace highest if it less than the quantity of a certain item		highest = items[i].getQuantity();}for(int i = 0; i < items.length; i++){ // another for statement to display the highest items data	NumberFormat format = NumberFormat.getCurrencyInstance();	if(highest == items[i].getQuantity())		System.out.println(i + "\t" + items[i].getQuantity() + "\t" + items[i].getCost() + "\t" + format.format((items[i].getCost() * items[i].getQuantity())) + "\t" + items[i].getName());}		System.out.println(); //extra space		System.out.println(); //extra space}public void reordering() { // creates a method to display the items number and name if the quantity is equal to 0		System.out.println("Parts that Need Reordering");	for(int i = 0; i < items.length; i++){		if(items[i].getQuantity() == 0)			System.out.println(i + "\t" + items[i].getName());	}}public void writeInventory(String fileName) throws IOException { //creates a method to write data to a new file	FileWriter fwriter = new FileWriter(fileName); //initializes the file writer to accept fileName as the file name	PrintWriter outputFile = new PrintWriter(fwriter); //initializes the printWriter to write the data to the file writer		outputFile.println(items.length); // writes the length of the array (OUT SIDE OF LOOP)					for(int i = 0; i< items.length; i++){ // for loop to write in ONE line the quantity, cost, and name with a space between each				outputFile.print(items[i].getQuantity() + " ");				outputFile.print(items[i].getCost() + " ");				outputFile.print(items[i].getName() + " ");				outputFile.println();	}	outputFile.close(); // close print writer}}