?? company.java
字號:
// Code Written by Kevin Dela Rosa, Vinney LaPenna, and John Sameulson June 14, 2004
/***********************************************************************************/
/** **/
/** Inventory Deluxe v 1.03 **/
/** Company Class **/
/** **/
/***********************************************************************************/
// Company class
// Workload of our program
// Computes all of the necessary mathematical and manipulative functions
import java.io.*;
import java.text.*;
import java.util.*;
class Company
{
private String fileName;
// Stores the name of the file for save purposes
private ArrayList currentItems;
// Stores the inventory; collection of Item objects
private double currentFunds;
// Represents how much money the company has to spend on inventory
public Company() throws IOException
// Default constructor; loads all the inventory data from the file
{
currentItems = new ArrayList();
fileName = "inventory.dat";
BufferedReader cin = new BufferedReader(new FileReader(fileName));
// For reading from the file
currentFunds = Double.parseDouble(cin.readLine());
String name;
String description;
String id;
int amt;
double sale;
double order;
while((name = cin.readLine()) != null)
{
description = cin.readLine();
StringTokenizer st = new StringTokenizer(cin.readLine());
id = st.nextToken();
amt = Integer.parseInt(st.nextToken());
sale = Double.parseDouble(st.nextToken());
order = Double.parseDouble(st.nextToken());
Item i = new Item(name, description,id,amt,sale,order);
currentItems.add(i);
// Adds the new Item to the list
}
cin.close();
}
public void openFile(String fileName) throws IOException
{
currentItems.clear();
BufferedReader cin = new BufferedReader(new FileReader(fileName));
// For reading from the file
currentFunds = Double.parseDouble(cin.readLine());
String name;
String description;
String id;
int amt;
double sale;
double order;
while((name = cin.readLine()) != null)
{
description = cin.readLine();
StringTokenizer st = new StringTokenizer(cin.readLine());
id = st.nextToken();
amt = Integer.parseInt(st.nextToken());
sale = Double.parseDouble(st.nextToken());
order = Double.parseDouble(st.nextToken());
Item i = new Item(name, description,id,amt,sale,order);
currentItems.add(i);
// Adds the new Item to the list
}
this.fileName = fileName;
cin.close();
}
public void sortByName()
// Implements an insertion/selection sort on currentItems, sorting by item name
{
ArrayList temp = new ArrayList(); // Empty ArrayList (for sorting)
Item smallest; // Stores the smallest item (for sorting)
Item go; // Temporary Item
while(currentItems.size() > 0)
{
smallest = (Item) currentItems.get(0);
for(int c = 0; c < currentItems.size(); c++)
{
go = (Item) currentItems.get(c);
if(go.getName().toUpperCase().compareTo(
smallest.getName().toUpperCase()) <= 0)
{
smallest = go;
}
}
temp.add(smallest);
currentItems.remove(smallest);
}
currentItems = temp;
}
public void sortById()
// Implements an insertion/selection sort on currentItems, sorting by ID number
{
ArrayList temp = new ArrayList(); // Empty ArrayList (for sorting)
Item smallest; // Stores the smallest item (for sorting)
Item go; // Temporary Item
while(currentItems.size() > 0)
{
smallest = (Item) currentItems.get(0);
for(int c = 0; c < currentItems.size(); c++)
{
go = (Item) currentItems.get(c);
if(Integer.parseInt(go.getId()) <
Integer.parseInt(smallest.getId()))
{
smallest = go;
}
}
temp.add(smallest);
currentItems.remove(smallest);
}
currentItems = temp;
}
public boolean newSales(String id, int amt)
// Checks to see if id is in currentItems
// If it is not, returns false
// If it is, it sells amt Items if there are enough Items
// If amount is insufficient, returns false
// If all conditions are met it sells amt of Item and returns true
{
boolean checkID = false; // Determines if id is in the list
Item i = new Item();
for(int c = 0; c < currentItems.size(); c++)
{
if(id.equals(((Item) currentItems.get(c)).getId()))
{
checkID = true;
i = (Item) currentItems.get(c);
}
}
if(checkID)
{
if(amt > i.getAmount())
return false;
else
{
currentFunds += i.sellItem(amt);
return true;
}
}
return false;
}
public boolean newOrders(String id, int amt)
//Checks to see if id is in currentItems
// If it is not, returns false
// If it is, it orders the passed amount if currentFunds is sufficient
// If money is insufficient, returns false
// If all conditions are met it orders amt of Item and returns true
{
boolean checkID = false;
Item i = new Item();
for(int c = 0; c < currentItems.size(); c++)
{
if(id.equals(((Item) currentItems.get(c)).getId()))
{
checkID = true;
i = (Item) currentItems.get(c);
}
}
if(checkID)
{
if(currentFunds < i.getOrderPrice(amt))
return false;
else
{
currentFunds -= i.orderItem(amt);
return true;
}
}
return false;
}
public boolean liquidateItem(String id)
//Sells all of one item at order price and adds the money to currentFunds
//returns true if successful, and false if id is not an Item
{
Item i = new Item();
Item go = new Item();
boolean isInList = false;
for(int c = 0; c < currentItems.size(); c++)
{
go = ((Item)currentItems.get(c));
if(id.compareTo(go.getId()) == 0)
{
i = go;
isInList = true;;
}
}
if(isInList)
{
currentFunds += i.liquidate();
currentItems.remove(i);
}
return isInList;
//returns true if you can liquidate
}
public boolean makeItem(String n, String d, String id, double s, double o, int a)
// If id is not a current id in currentItems, creates new item with passes data and returns true
// Otherwise returns false
{
boolean isInList = false;
for(int c = 0; c < currentItems.size(); c++)
{
String temp = ((Item) currentItems.get(c)).getId();
if(id.equals(temp))
{
isInList = true;
}
}
if(!isInList)
{
Item i = new Item(n,d,id,a,s,o);
if(currentFunds < i.getOrderPrice(a))
{
isInList = true;
}
else
{
currentFunds -= a * o;
currentItems.add(i);
}
}
return !isInList;
// returns false if it is in the list, or
// if Company does not have enough money
}
public void endProgram() throws IOException
// Saves the current data to the data file
{
BufferedWriter cout = new BufferedWriter(new FileWriter(fileName));
String cf = new Double(currentFunds).toString();
cout.write(cf);
cout.newLine();
for(int c = 0; c < currentItems.size(); c++)
{
Item i = ((Item) currentItems.get(c));
cout.write(i.getName());
cout.newLine();
cout.write(i.getDescription());
cout.newLine();
cout.write(i.getId() + " " + i.getAmount() + " " + i.getSalesPrice());
cout.write(" " + i.getOrderPrice());
cout.newLine();
}
cout.close();
}
public boolean changeSalesPrice(String id, double np)
//If the passed id is in the list, the Item's sales price is set to np and returns true
//Otherwise, returns false
{
boolean checkID = false;
Item i = new Item();
for(int c = 0; c < currentItems.size(); c++)
{
if(id.equals(((Item) currentItems.get(c)).getId()))
{
checkID = true;
i = (Item) currentItems.get(c);
}
}
if(checkID)
{
i.setSalesPrice(np);
return true;
}
return false;
//returns true if it succeeded, false if id not in list
//Please protect the double for negativity and zero
}
public boolean changeOrderPrice(String id, double np)
//If the passed id is in the list, the Item's order price is set to np and returns true
//Otherwise, returns false
{
boolean checkID = false;
Item i = new Item();
for(int c = 0; c < currentItems.size(); c++)
{
if(id.equals(((Item) currentItems.get(c)).getId()))
{
checkID = true;
i = (Item) currentItems.get(c);
}
}
if(checkID)
{
i.setOrderPrice(np);
return true;
}
return false;
//returns true if it succeeded, false if id not in list
//Please protect the double for negativity and zero
}
public ArrayList getItems()
//Returns the current list of items
{
return currentItems;
}
public double getFunds()
//Returns the current amount of money being used for inventory
{
return currentFunds;
}
public int getNumItems()
//returns the current number of Items in currentItems
{
return currentItems.size();
}
public void setFunds(double f)
// Sets the company's funds to f
{
currentFunds = f;
}
public void setFileName(String name)
{
fileName = name;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -