📄 order.java
字号:
package Processing;
import java.util.ArrayList;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import Catalogue.*;
import Middle.*;
/**
* The order processing system.
* @author sjc8
* @version 2.0 February 2008
*/
public class Order implements OrderProcessing
{
private static int theNextNumber = 1; // Start at 1
private ArrayList<SoldBasket> theWaitingTray //waiting to be picked Tray ArrayList
= new ArrayList<SoldBasket>(10);
private ArrayList<SoldBasket> waitToCollect //waiting to be collect tray ArrayList
= new ArrayList<SoldBasket> ();
/**
* Used for debug information
*
*/
private String asString( SoldBasket bl )
{
ByteArrayOutputStream bos = new ByteArrayOutputStream( 1024 );
PrintStream ps = new PrintStream( bos );
ps.printf( "#%d (", bl.orderNo() );
Product p[] = bl.products();
for ( Product pr: p )
{
ps.printf( "%s:%d ", pr.getDescription(), pr.getQuantity() );
}
ps.printf( ")" );
return bos.toString();
}
/**
* Generates a unique order number
* @return A unique order number
*/
public synchronized int uniqueNumber()
{
return theNextNumber++;
}
/**
* Add a new order to the order processing system
*/
public synchronized void newOrder( SoldBasket bought )
{
theWaitingTray.add( bought ); //add an Order (may contain many items) into waiting Tray
for ( SoldBasket bl : theWaitingTray ) //iterate each order in the waiting tray
{
System.out.println( "Order: " + asString( bl ) ); //display
}
}
/**
* Returns an order to pick from the warehouse
* if no order then returns null.
* @return An order to pick or null if none
*/
public synchronized SoldBasket getOrderToPick()
throws OrderException
{
System.out.println( "Get Order to pick" );
int waitToPick = theWaitingTray.size(); //How many Customer Orders are still in the Waiting Tray;
if(waitToPick>0){ //If theWaitingTray is not empty(Some Order is still waiting to be picked)
return theWaitingTray.get(0); //return !!Only the First SoldBasket Object in theWaitingTray
}
else{
System.out.println("No Waiting Orders!!!");
return null; //theWaitingTray is empty--> All orders have been picked.
}
}
/**
* Informs the order processing system that the order has been
* picked and the products are now on the conveyor belt to
* the shop floor.
*/
public synchronized String informOrderPicked( int orderNo )
throws OrderException
{
int count = 0;
String s="";
for(SoldBasket sb : theWaitingTray){ //iterate each Order in the Waiting to Pick Tray
int sbNo = sb.orderNo();
if(sbNo==orderNo){ //if find the entered order number in the waiting Tray
count++; //count add 1
theWaitingTray.remove(theWaitingTray.indexOf(sb)); //remove the picked Order from the waiting Tray
waitToCollect.add(sb); //add the picked order into the Collect tray for waiting to collect
System.out.println( "Order picked " + orderNo );
s += "Order No Picked : "+ orderNo + " .";
break;
}
}
if (count<=0){ //if can not find entered order no in the waiting tray, then return error massage
System.out.println("Invalid order No. Or Order was picked!!");
s +="Invalid Order No!! Please enter again.";
}
return s;
}
/**
* Informs the order processing system that the order has been
* collected by the customer
*/
public synchronized String informOrderColected( int orderNo )
throws OrderException
{
int count = 0;
String s="";
for (SoldBasket sbC : waitToCollect){ //iterate each order in the waiting to be collect tray
int sbCNo = sbC.orderNo();
if (sbCNo == orderNo){ //if the entered order no match a order no in the tray
count ++ ;
waitToCollect.remove(waitToCollect.indexOf(sbC)); //remove collected order from wait to collect tray
System.out.println("Collected Order No : " + orderNo +" .");
s += "Collected Order No : "+ orderNo +" ."; //return a message
break;
}
}
if (count <= 0){ //if can not find any order no match, then return a error message
System.out.println("Invalid order No. Or Order was collected");
s += "Invalid order No, Please enter again !!";
}
System.out.println( "Order collected No : " + orderNo );
return s;
}
/**
* Returns information about all orders in the order processing system
* This will consist of two pieces of information
* 1> A list of orders waiting to be processed
* 2> A list of orders that can now be collected
* @return A string containing information about the state of the system
*/
public synchronized String getOrderState()
throws OrderException
{
// You need to modify and fill in the correct code
String s = "\nOrders Waiting to be Collect : \n\n";
if (waitToCollect.size()<=0){ //check if the waiting to collect tray is empty
s += "None!! All Orders are Collected!!!";
}
else{
for(SoldBasket sbc : waitToCollect){ //iterate each order in the waiting to collect tray
int sbcNo = sbc.orderNo(); // get the order number.
s += sbcNo;
s += " ; ";
}
}
s += "\n\n\nOrders Waiting to be Processed : \n\n";
if(theWaitingTray.size()<=0){ //check if the waiting to be picked tray is empty
s += " None!! All Orders are Processed!!" ;
}
else{
for(SoldBasket sbw : theWaitingTray){ //iterate each order in waiting to be picked tray
int sbOrderNoWaiting = sbw.orderNo(); // get order number
s += sbOrderNoWaiting;
s +=" ; ";
}
}
System.out.println( "get state of order system" );
return s;
}
public synchronized String getWaitingList()
throws OrderException
{
String s="";
s += "\nOrders Waiting to be Processed : \n";
if(theWaitingTray.size()<=0){ //check if the waiting to be picked tray is empty
s += " None!! All Orders are Processed!!" ;
}
else{
for(SoldBasket sbw : theWaitingTray){ //iterate each order in the waiting tray
int sbOrderNoWaiting = sbw.orderNo(); //get order number
s += sbOrderNoWaiting;
s +=" ; ";
}
s+="\n";
}
System.out.println( "get Waiting to Pick List" );
return s;
}
public int getWaitingTraySize(){ // get the size of waiting to be picked tray
return theWaitingTray.size();
}
public int getCollectSize(){ // get the size of the waiting to be collected tray
return waitToCollect.size();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -