📄 cashiergui.java
字号:
package Clients;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
import java.util.Locale;
import Catalogue.*;
import Middle.*;
/**
* Implements the GUI for the Cashier client.
* @author
* @version 3.0 February 2008
*/
class CashierGUI
{
private enum State { process, checked, confirmed }
private static final String CHECK = "Check";
private static final String BUY = "Buy";
private static final String CANCEL = "Cancel";
private static final String BOUGHT = "Bought";
private static final String CONFIRM = "Confirm";
private static final int H = 450; // Height of window pixels
private static final int W = 400; // Width of window pixels
private JLabel theAction = new JLabel();
private JTextField theInput = new JTextField();
private JTextField theQuantity = new JTextField();
private JTextArea theOutput = new JTextArea();
private JScrollPane theSP = new JScrollPane();
private JButton theBtCheck = new JButton( CHECK );
private JButton theBtConfirm = new JButton (CONFIRM);
private JLabel theLabQuan = new JLabel ();
private JButton theBtBuy = new JButton( BUY );
private JButton theBtCancel= new JButton( CANCEL );
private JButton theBtBought= new JButton( BOUGHT );
private Picture thePicture = new Picture(80,80);
private State theState = State.process; // Current state
private Product theProduct = null; // Current product
private SoldBasket theBought = null; // Bought items
private Transaction theCB = new Transaction();
private StockReadWriter theStock = null;
private OrderProcessing theOrder = null;
private NumberFormat theMoney =
NumberFormat.getCurrencyInstance( Locale.UK );
public CashierGUI( RootPaneContainer rpc, MiddleFactory mf )
{
try //
{
theStock = mf.makeStockReadWriter(); // DataBase access
theOrder = mf.makeOrderProcessing(); // Process order
} catch ( Exception e )
{
System.out.println("Exception: " + e.getMessage() );
}
Container cp = rpc.getContentPane(); // Content Pane
Container rootWindow = (Container) rpc; // Root Window
cp.setLayout(null); // No layout manager
rootWindow.setSize( W, H ); // Size of Window
Font f = new Font("Monospaced",Font.PLAIN,12); // Font f is
theBtCheck.setBounds( 10, 25+60*0, 80, 40 ); // Check Button
theBtCheck.addActionListener( theCB ); // Listener
cp.add( theBtCheck ); // Add to canvas
theBtConfirm.setBounds(10,25+60*1,80,40); //Confirm Button
theBtConfirm.addActionListener(theCB); //Listener
cp.add(theBtConfirm); //Add to canvas
theLabQuan.setBounds(104,25+60*1,80,40); //Quantity label
theLabQuan.setText("Amount : "); //default text
cp.add(theLabQuan); //add to canvas
theBtBuy.setBounds( 10, 25+60*2, 80, 40 ); // Buy button
theBtBuy.addActionListener( theCB ); // Listener
cp.add( theBtBuy ); // Add to canvas
theBtCancel.setBounds( 10, 25+60*3, 80, 40 ); // Cancel Button
theBtCancel.addActionListener( theCB ); // Listener
cp.add( theBtCancel ); // Add to canvas
theBtBought.setBounds( 10, 25+60*4, 80, 40 ); // Clear Button
theBtBought.addActionListener( theCB ); // Listener
cp.add( theBtBought ); // Add to canvas
theAction.setBounds( 104, 25 , 270, 20 ); // Message area
theAction.setText( "" ); // Blank
cp.add( theAction ); // Add to canvas
theInput.setBounds( 104, 45, 270, 20 ); // Input Area
theInput.setText(""); // Blank
cp.add( theInput ); // Add to canvas
theQuantity.setBounds(164,25+60*1,210,40); //the quantity input area
theQuantity.setText(""); //clear
cp.add(theQuantity); //Add to canvas
thePicture.setBounds( 10, 25+60*5, 80, 80 ); // Picture area
cp.add( thePicture ); // Add to canvas
thePicture.clear();
theSP.setBounds( 104, 25+60*2, 270, 160 ); // Scrolling pane
theOutput.setText( "" ); // Blank
theOutput.setFont( f ); // Uses font
cp.add( theSP ); // Add to canvas
theSP.getViewport().add( theOutput ); // In TextArea
rootWindow.setVisible( true ); // Make visible
} //end of constructor
class Transaction implements ActionListener // Listener
{
public void actionPerformed( ActionEvent ae ) // Interaction
{
if ( theStock == null )
{
theAction.setText("No connection");
return; // No connection
}
String actionIs = ae.getActionCommand(); // Button
try
{
if ( theBought == null )
{ //
int on = theOrder.uniqueNumber(); // Unique order no.
theBought = new SoldBasket( on ); // Bought list
}
if ( actionIs.equals( CHECK ) ) // Button CHECK
{
theState = State.process; // State process
thePicture.clear(); //clear image
String pn = theInput.getText().trim(); // Product no.
int amount = 1; // & quantity
if ( theStock.exists( pn ) ) // Stock Exists?
{ // T
Product pr = theStock.getDetails(pn); // Get details
if ( pr.getQuantity() >= amount ) // In stock?
{ // T
theQuantity.setText("1");
theAction.setText( // Display
pr.getDescription() + " : " + // description
theMoney.format(pr.getPrice()) + // price
" (" + pr.getQuantity() + ")" // quantity
); // of product
theProduct = pr; // Remember product.
theProduct.setQuantity( amount ); // & quantity
theState = State.checked; // OK await BUY
}
else
{ // F
theAction.setText( // Not in Stock
pr.getDescription() +" not in stock"
);
}
thePicture.set( // Picture of
theStock.getImage( pn ) ); // product
}
else
{ // F Stock exists
theAction.setText( // Unknown
"Unknown product number " + pn // product no.
);
}
theState = State.checked;
} //end of CHECK
if(actionIs.equals( CONFIRM ) )
{
if ( theState != State.checked ) // Not checked
{ // with customer
theAction.setText("Check if OK with customer first");
return;
}
String pn = theInput.getText().trim(); // Product no.
if ( theStock.exists( pn ) ) // Stock Exists?
{ // T
Product pr = theStock.getDetails(pn);
int amount = Integer.parseInt(theQuantity.getText().trim());
theProduct.setQuantity( amount );
if ( pr.getQuantity() >= amount ) // Enough stock?
{ // T
theAction.setText( // Display
pr.getDescription() + " : " + // description
theMoney.format(pr.getPrice()) + // price
" (" + pr.getQuantity() + ")" ); // quantity
theBought.add(theProduct); //add to basket
theOutput.setText( "" ); // clear
theOutput.append(theBought.details()); // Display
theAction.setText("Confirmed " + // details
theProduct.getDescription()+" ("+amount+")"); //
theBought.remove(); //remove from basket
}
else
{
theAction.setText("Sorry! Not Enough Stock."); //not enough stock
}
}
theState = State.confirmed; // confirmed
} //end of COMFIRM
if ( actionIs.equals( BUY ) ) // Button BUY
{
if ( theState != State.confirmed ) // Not confirmed the amount
{ // with customer
theAction.setText("Please Confirm the Amount with Customer first");
return;
}
boolean stockBought = // Buy
theStock.buyStock( // however
theProduct.getProductNo(), // may fail
theProduct.getQuantity() );
int amount = Integer.parseInt(theQuantity.getText().trim()); //get amount to buy
theProduct.setQuantity( amount );
if ( stockBought ) // Stock bought
{ // True
theBought.add( theProduct ); // Add to bought
theOutput.setText( "" ); // clear
theOutput.append( theBought.details()); // Display
theAction.setText("Purchased " + // details
theProduct.getDescription()); //
}
else
{ // False
theAction.setText("!!! Not Enough in stock"); // Now no stock
}
theState = State.process; // All Done
} //end of BUY
if ( actionIs.equals( CANCEL ) ) // Button CANCEL
{
if( theState ==State.process)
{
if ( theBought.number() >= 1 ) // item to cancel
{ // T
Product dt = theBought.remove(); // Remove from list
theStock.addStock( dt.getProductNo(), // Re-stock
dt.getQuantity() ); // as not sold
theAction.setText(""); //
theOutput.setText(theBought.details()); // display sales
theAction.setText("Canceled " + // details
theProduct.getDescription()+" ("+dt.getQuantity()+") "); //
}
else
{ // F
theOutput.setText( "" ); // Clear
}
theState = State.process;
}
else if (theState == State.confirmed)
{
theOutput.setText("");
theAction.setText("Cancel confirmed " + // details
theProduct.getDescription()); //
}
else
{
theOutput.setText("");
theOutput.setText("Please Buy or Cnfirm amount first!!");
theAction.setText("");
theAction.setText("Warning!!");
}
} //end of CANCEL
if ( actionIs.equals( BOUGHT ) ) // Button Bought
{
if ( theBought.number() >= 1 ) // items > 1
{ // T
theOrder.newOrder( theBought ); // Process order
theBought = null; // reset
} //
theOutput.setText( "" ); // Clear
theInput.setText( "" ); //Clear
theAction.setText( "Next customer" ); // New Customer
theQuantity.setText(""); //Clear
thePicture.clear(); //Clear image
theState = State.process; // All Bought
} //end of BOUGHT
theInput.requestFocus(); // theInput has Focus
}
catch ( StockException e ) // Error
{ // Of course
theOutput.append( "Fail Stock access:" + // Should not
e.getMessage() + "\n" ); // happen
}
catch ( OrderException e ) // Error
{ // Of course
theOutput.append( "Fail Order process:" + // Should not
e.getMessage() + "\n" ); // happen
}
} //end of method actionPerformed
} //end of class Transaction
} //end of CashierGUI
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -