📄 backdoorgui.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 Customer client.
* @author sjc8
* @version 2.0 February 2008
*/
class BackDoorGUI
{
private enum State { process, checked, modified, added}
private static final String CHECK = "Check Item";
private static final String ADD = "Add Stock";
private static final String CANCEL = "Cancel ";
private static final String DONE = "Reset";
private static final int H = 600; // Height of window pixels
private static final int W = 430; // Width of window pixels
private JLabel theAction = new JLabel();
private JLabel checkLbl = new JLabel();
private JTextField theInput = new JTextField();
private JTextField theQuantity = new JTextField();
// private JTextField theDesc = new JTextField();
// private JTextField thePrice = new JTextField();
//private JTextField theModQuantity = new JTextField();
private JTextArea theOutput = new JTextArea();
private JScrollPane theSP = new JScrollPane();
private JButton theBtCheck = new JButton( CHECK );
//private JButton theBtModify = new JButton (MODIFY);
private JLabel theLabQuan = new JLabel ();
private JLabel theLabModQuantity = new JLabel();
private JLabel theLabDesc = new JLabel();
private JLabel theLabPrice = new JLabel();
private JButton theBtAdd = new JButton( ADD );
private JButton theBtCancel= new JButton( CANCEL );
private JButton theBtBought= new JButton( DONE );
private Picture thePicture = new Picture(100,100);
private State theState = State.process; // Current state
private Product theProduct = null; // Current product
private Basket theAdd = new Basket();; // Add items
private Transaction theCB = new Transaction();
private StockReadWriter theStock = null;
private OrderProcessing theOrder = null;
private NumberFormat theMoney =
NumberFormat.getCurrencyInstance( Locale.UK );
public BackDoorGUI( 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( 300, 25+60*0+40, 100, 40 ); // Check Button
theBtCheck.addActionListener( theCB ); // Listener
cp.add( theBtCheck ); // Add to canvas
// theBtModify.setBounds(300,25+60*4+30+40,100,40);
// theBtModify.addActionListener(theCB);
// cp.add(theBtModify);
checkLbl.setBounds(16,25+60*0+40,100,40);
checkLbl.setText("Product No : ");
cp.add(checkLbl);
theLabQuan.setBounds(16,25+60*1+40,100,20);
theLabQuan.setText("Add Amount : ");
cp.add(theLabQuan);
theLabModQuantity.setBounds(16,25+60*3+30+40,100,20);
// theLabModQuantity.setText("Modify Amount : ");
cp.add(theLabModQuantity);
theLabDesc.setBounds(16, 25+60*4+40, 100,20);
// theLabDesc.setText("Modify Desc. : ");
cp.add(theLabDesc);
theLabPrice.setBounds(16,25+60*3+40,100,20);
//theLabPrice.setText("Modify Price : ");
cp.add(theLabPrice);
theBtAdd.setBounds( 300, 25+60*1+40, 100, 40 ); // Buy button
theBtAdd.addActionListener( theCB ); // Listener
cp.add( theBtAdd ); // Add to canvas
theBtCancel.setBounds( 300, 25+60*2+40, 100, 40 ); // Cancel Button
theBtCancel.addActionListener( theCB ); // Listener
cp.add( theBtCancel ); // Add to canvas
theBtBought.setBounds( 16, 25+60*5+30+40, 100, 40 ); // Clear Button
theBtBought.addActionListener( theCB ); // Listener
cp.add( theBtBought ); // Add to canvas
theAction.setBounds( 130, 25 , 350, 40 ); // Top Message area
theAction.setText( "" ); // Blank
cp.add( theAction ); // Add to canvas
theInput.setBounds( 130, 25+60*0+50, 150, 20 ); // Product No: Input Area
theInput.setText(""); // Blank
cp.add( theInput ); // Add to canvas
theQuantity.setBounds(130,25+60*1+40,150,20); //Amount: Input Area
theQuantity.setText(""); //Blank
cp.add(theQuantity); //Add to canvas
theSP.setBounds( 130, 25+60*5+30+40, 270, 150 ); // Scrolling pane
theOutput.setText( "" ); // Blank
theOutput.setFont( f ); // Uses font
cp.add( theSP ); // Add to canvas
theSP.getViewport().add( theOutput ); // In TextArea
thePicture.setBounds( 125, 125+60*1+30+40, 100, 180 ); // Picture area
cp.add( thePicture ); // Add to canvas
thePicture.clear();
rootWindow.setVisible( true ); // Make visible
}
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();
try
{
if ( actionIs.equals( CHECK ) ) // Button CHECK
{
thePicture.clear(); //clear all areas
// theDesc.setText("");
theQuantity.setText("");
theOutput.setText("");
// thePrice.setText("");
//theModQuantity.setText("");
if(theAdd!=null){ //if basket consists of orders
for(int i=0; i<theAdd.number();i++){ //iterate each order in the basket
theAdd.remove(); //empty the basket
}
}
else{}
theState = State.process; // State process
String pn = theInput.getText().trim(); // Product no.
int amount = 0; // quantity set to zero
if ( theStock.exists( pn ) ) // Stock Exists? True
{
Product pr = theStock.getDetails(pn); // Get details
String sQuantity ="";
int sQ = pr.getQuantity();
sQuantity += sQ;
String sPrice = "";
double sP = pr.getPrice();
sPrice += sP;
theQuantity.setText("1"); //set the amount input to 1 to default
if ( pr.getQuantity() >= amount ) // In stock?
{ // T
theAction.setText( // Display
pr.getDescription() + " : " + // description
theMoney.format(pr.getPrice()) + // price
" (" + pr.getQuantity() + ")" // quantity
); // of product
theProduct = pr; // Remember prod.
theProduct.setQuantity( amount ); // & quantity
thePicture.set( // Picture of
theStock.getImage( pn ) );
} else { // F
theAction.setText( // Not in Stock
pr.getDescription() +" None stock"
);
}
}
else { // F Stock exists
theAction.setText( // Unknown
"Unknown product number " + pn // product no.
);
}
theState = State.checked;
}
// if(actionIs.equals( MODIFY ) ) //Modify the quantity, price and description
{
if ( theState != State.checked ) // Not checked
{
theAction.setText("Check Stock exist first"); //warning message
theOutput.setText("Check Stock exist first"); //warning message
return;
}
String pn = theInput.getText().trim(); // Product no.
if ( theStock.exists( pn ) ) // Stock Exists?
{
String s = "";
theProduct.setProductNo(theInput.getText().trim());
theStock.modifyStock(theProduct); //modify item
theAction.setText( theProduct.getProductNo()+" : "+ // Display
theProduct.getDescription() + " : " + // description
theMoney.format(theProduct.getPrice()) + // price
" (" + theProduct.getQuantity() + ")" ); // quantity
theAdd.add(theProduct);
theOutput.setText( "" ); // clear
theOutput.append("Modified : "+"\n");
theOutput.append(theAdd.details()); // Display
theAdd.remove(); //theAdd.remove();
theState = State.modified; // All modified
}
}
if ( actionIs.equals( ADD ) ) // Button Add amount
{
if ( theState != State.checked ) // Not checked product information first
{
theAction.setText("Confirm Stock No and Add amount first"); //give warning message
theOutput.setText("Confirm Stock No and Add amount first"); //give warning message
return;
}
String productNo = theInput.getText().trim(); //get text from input and remove all spaces
theProduct.setProductNo(productNo); //product no equals inputed text
int amount = Integer.parseInt(theQuantity.getText().trim()); //convert String text to integer
theProduct.setQuantity( amount ); //set quantity to inputed amount
int theAddSize = theAdd.number();
if(theAddSize!=0){
for(int i =0; i< theAddSize; i++){
theAdd.remove(); //remove all lists if basket is not empty
}
}
else{}
theAdd.add( theProduct ); // Add to list
theStock.addStock(productNo,amount); // Add to stock
theOutput.setText( "" ); // clear
theOutput.append("Added : "+"\n");
theOutput.append( theAdd.details()); // Display
theAction.setText("Added : " + // details
theProduct.getDescription() + " : ( "+amount+" )"); //
theState = State.added; // All Done
}
if ( actionIs.equals( CANCEL ) ) // Button CANCEL
{
if(theState == State.added) //Already add amount? True
{
if ( theAdd.number() >= 1 ) // item to cancel
{
theOutput.setText(""); //clear
theAction.setText(""); //clear
theOutput.setText(("Cancelled : "+ theInput.getText().trim() + " : ( "+theQuantity.getText()+" ).")); //display canceling message
Product dt = theAdd.remove(); // Remove from list
theStock.addStock( dt.getProductNo(), // Remove-stock
((-1)*dt.getQuantity()) ); // as not added
}
else { // F
theOutput.setText( "" ); // Clear
}
theState = State.process;
}
else{
theOutput.setText("");
theAction.setText("Invalid!!");
theOutput.setText("Please Add amount before cancelling added!");
}
}
if ( actionIs.equals( DONE ) ) // Button Reset
{ int theAddSize = theAdd.number();
if ( theAddSize >= 1 ) // items > 1
{ // T
for(int i =0; i <theAddSize ; i++){
theAdd.remove(); //empty all lists
}
}
theOutput.setText( "" ); // Clear
theInput.setText( "" ); //Clear
theQuantity.setText(""); //Clear
// theModQuantity.setText("");
// thePrice.setText("");
// theDesc.setText("");
thePicture.clear(); //clear image
theAction.setText( "Enter Next Stock" ); // New Customer
theState = State.process; // All Done
}
theInput.requestFocus(); // theInput has Focus
}
catch ( StockException e ) // Error
{ // Of course
theOutput.append( "Fail Stock access:" + // Should not
e.getMessage() + "\n" ); // happen
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -