Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## InventoryProgram class
- package inventory;
- import java.util.*;
- import java.io.*;
- public class InventoryProgram {
- static ArrayList<String> lines = new ArrayList<String>(100);
- static ArrayList<InventoryItem> items = new ArrayList<InventoryItem>(100);
- public static void main(String args[]) {
- int totalItems = 0;
- double totalValue = 0.0;
- try{readFile("..\\InventoryIn.txt");}
- catch(IOException ioe){System.err.println("File Not Found");}
- setUpInventory(lines);
- for (int i = 0; i<items.size(); i++) {
- totalItems += items.get(i).getQuantity();
- totalValue += items.get(i).getTotalValue();
- }
- for (int i = 0; i<items.size(); i++) {
- if(items.get(i).isOverStocked(totalItems) && items.get(i).isOverValued(totalValue)){
- items.get(i).markDown(.5);
- System.out.println (items.get(i));
- }
- else if(items.get(i).isOverStocked(totalItems) || items.get(i).isOverValued(totalValue)){
- items.get(i).markDown(.25);
- System.out.println (items.get(i));
- }
- else
- System.out.println (items.get(i));
- }
- }
- public static void readFile(String fileName) throws IOException{
- File inputFile = new File(fileName);
- Scanner sc = new Scanner(inputFile);
- while(sc.hasNextLine())
- lines.add(sc.nextLine());
- }
- public static void setUpInventory(ArrayList<String> info){
- for (int i = 0; i<info.size(); i++) {
- StringTokenizer st = new StringTokenizer(info.get(i));
- InventoryItem item = new InventoryItem(
- Double.parseDouble(st.nextToken().substring(1)), st.nextToken(), Integer.parseInt(st.nextToken()), st.nextToken());
- items.add(item);
- }
- }
- }
- ## InventoryItem Object Class
- /**
- * This class represents a single item in the
- * store. Based on the 2007 SJCNY Programming
- * Competition.
- */
- package inventory;
- import java.text.*;
- public class InventoryItem {
- //fields:
- double price, salePrice, totalValue;
- int quantity;
- String manufacturer, name;
- boolean onSale;
- DecimalFormat formPrice = new DecimalFormat("#########.00");
- //constructor:
- public InventoryItem(double p, String m,
- int q, String name) {
- price = p;
- quantity = q;
- manufacturer = m;
- this.name = name;
- onSale = false;
- }
- /**
- * Produces a one line string that gives the data
- * for this class. If the item is on sale, then
- * this will be indicated by ****
- * For Example:
- * "Abit Motherboard 75.95 200 **** Sale item, price: 37.98 ****"
- * "Canon Camera 299.99 1"
- */
- public String toString() {
- String output = this.manufacturer + " " + this.name + " " + this.price + " " + this.quantity;
- if(!this.onSale)
- return output;
- else return output.concat(" **** Sale item, price: " + formPrice.format(salePrice) + " ****");
- }
- /**
- * Reduces the price by the given percentage.
- * For example: if the initial price is 100
- * and percent is 10, then the new price is 90.
- * If initial price is 500, percent is 50, then
- * the new price will be 250.
- * The new price is stored in salePrice (field).
- * percent is in the decimal format
- * for example: if the percent mark down is 25
- * double percent = .75
- */
- public void markDown(double percent) {
- this.salePrice = (1 - percent) * price;
- this.onSale = true;
- }
- public int getQuantity(){
- return quantity;
- }
- public double getTotalValue(){
- totalValue = price * quantity;
- return totalValue;
- }
- public boolean isOverStocked(double totalQuantity){
- double percent = 100.0 * (this.quantity / totalQuantity);
- if (percent > 10){
- return percent > 10;
- }
- return false;
- }
- public boolean isOverValued(double totalValue){
- double percent = 100.0 * (this.price / totalValue);
- if (percent > 5){
- return percent > 5;
- }
- return false;
- }
- //testing only
- public static void main(String args[]) {
- InventoryItem box = new InventoryItem(75.92, "Abit", 500, "Mobo");
- box.markDown(.25);
- System.out.println (box);
- }
- }
Add Comment
Please, Sign In to add comment