Androne

Friday, March 11, 2005

ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh...

/* * Program Name : EComputer.java * Module : PSP2 (Assignment) * Student Name : * Student No : * Tutorial Group : */
import java.awt.*;import java.awt.event.*;import javax.swing.*;
public class EComputer extends JApplet implements ActionListener {
// Data Structures
int numberOfItems = 5;
// 1-D Array to store the names of the items String [] nameArr = {"Zen Micro", "Zen Touch", "IPod Shuffle", "IPod Mini","IPod U2", "", "", "", "", ""};
// 1-D Array to store the attribute names of the items String [] attributeArr = {"Capacity", "Battery Life", "Price", "Quantity"};
// 2-D Array to store the data (attribute values) of the items int [][] dataArr = { { 5, 20, 1, 4, 5, -1, -1, -1, -1, -1 },
{ 12, 8, 12, 8, 20, -1, -1, -1, -1, -1 },
{ 400, 450, 500, 548, 600, -1, -1, -1, -1, -1 },
{ 100, 80, 0, 120, 80, -1, -1, -1, -1, -1 }, };
// --------------------- for Additional Features ONLY ----------------------- //to store the selected index form the combo box int cartIndex; int cartQty; int num; String name; int numberOfCartItems = 0;
// 1-D Array to store the names of the cart items String [] cartItemArr = new String [10];
// 1-D Array to store the quantity of the cart items int [] cartQtyArr = new int[10];
// --------------------------------------------------------------------------
//GUI Components JButton fnBtn1, fnBtn2, fnBtn3, fnBtn4, fnBtn5, fnBtn6, fnBtn7, searchBtn1,searchBtn2,searchBtn3, addCartBtn, removeCartBtn; JButton adminOptionsBtn,addItemBtn, editItemBtn, delItemBtn,loginBtn,myDelBtn;
JPanel westPanel, centerPanel,searchPanel1, searchPanel2, displayPanel, cartPanel1, cartPanel2,northPanel, adminOptionsPanel; JPanel loginPanel, mainAdminPanel, addItemPanel, editItemPanel, delItemPanel; JTextField nameTf1, nameTf3,priceTf21, priceTf22,userIdTf, passwordTf, myDelTf;
JLabel headerLb,subheaderLb,lineLb, nameLb1, nameLb3, priceLb21,priceLb22, userIdLb, passwordLb; JLabel cartLb1, cartLb2; JComboBox cartCombo1, cartCombo2, cartCombo3;
// add item panel JLabel itemLb1, itemLb2, itemLb3, itemLb4, itemLb5; JTextField itemLTf1,itemLTf2,itemLTf3,itemLTf4,itemLTf5; JButton searchBtn, saveBtn;
int option = 0; boolean searching1 = false; boolean searching2 = false; boolean addCartItem= false; boolean delCartItem= false; boolean addItem = false; boolean editItem = false; boolean delItem = false;
String selectedOption = " Please Choose one of the Options from the Navigation Panel ";
int itemIndex = 0; // used in function 1 String itemName = ""; // used in function 1 int minPrice, maxPrice; // used in function 2 int[] resultArr = new int[10]; // used in function 2 String[] CartArr; // used in function 4
public void init() {
// ===================== North Panel - Heading ==================================
headerLb = new JLabel("Online Computer Store", SwingConstants.CENTER); headerLb.setForeground(Color.red); headerLb.setFont(new Font("Sans Serif", Font.BOLD, 24));
lineLb = new JLabel("____________________",SwingConstants.CENTER); lineLb.setForeground(Color.red); lineLb.setFont(new Font("Sans Serif", Font.BOLD, 24));
subheaderLb = new JLabel(selectedOption,SwingConstants.CENTER); subheaderLb.setForeground(Color.blue); subheaderLb.setFont(new Font("Sans Serif", Font.BOLD, 16));
northPanel = new JPanel(); northPanel.setBackground(Color.cyan); northPanel.setLayout(new GridLayout(3,1)); northPanel.add(headerLb); northPanel.add(lineLb); northPanel.add(subheaderLb);
// ======================== West Panel - Menu ==================================
fnBtn1 = new JButton("Search by Name"); // Function 1 fnBtn1.setBackground(Color.green); fnBtn1.addActionListener(this); fnBtn2 = new JButton("Search by Price"); // Function 2 fnBtn2.setBackground(Color.green); fnBtn2.addActionListener(this); fnBtn3 = new JButton("Display All Items"); // Function 3 fnBtn3.setBackground(Color.green); fnBtn3.addActionListener(this); fnBtn4 = new JButton("Add Item to Cart"); // Function 4 fnBtn4.setBackground(Color.pink); fnBtn4.addActionListener(this); fnBtn5 = new JButton("Remove Item from Cart"); // Function 5 fnBtn5.setBackground(Color.pink); fnBtn5.addActionListener(this); fnBtn6 = new JButton("Show Cart Items"); // Function 6 fnBtn6.setBackground(Color.pink); fnBtn6.addActionListener(this); fnBtn7 = new JButton("Clear Cart Items"); // Function 7 fnBtn7.setBackground(Color.pink); fnBtn7.addActionListener(this); adminOptionsBtn = new JButton("Administrator options"); // Admin Functions adminOptionsBtn.setBackground(Color.green); adminOptionsBtn.addActionListener(this);
westPanel = new JPanel(); westPanel.setLayout(new GridLayout(8,1)); westPanel.add(fnBtn1); westPanel.add(fnBtn2); westPanel.add(fnBtn3); westPanel.add(fnBtn4); westPanel.add(fnBtn5); westPanel.add(fnBtn6); westPanel.add(fnBtn7); westPanel.add(adminOptionsBtn);
// ============================= Center Panel ==================================
// GUI for Function 1 - Search by Name nameLb1 = new JLabel("Enter Item Name : "); nameTf1 = new JTextField(20); searchBtn1 = new JButton("Search"); searchBtn1.addActionListener(this); searchPanel1 = new JPanel(); searchPanel1.setBackground(Color.white); searchPanel1.add(nameLb1); searchPanel1.add(nameTf1); searchPanel1.add(searchBtn1);
// GUI for Function 2 - Search by Price priceLb21 = new JLabel("Enter Price Range from "); priceTf21 = new JTextField(6); priceLb22 = new JLabel(" to "); priceTf22 = new JTextField(6); searchBtn2 = new JButton("Search"); searchBtn2.addActionListener(this); searchPanel2 = new JPanel(); searchPanel2.setBackground(Color.white); searchPanel2.add(priceLb21); searchPanel2.add(priceTf21); searchPanel2.add(priceLb22); searchPanel2.add(priceTf22); searchPanel2.add(searchBtn2);
// GUI for Function 3 - Display ALL Items // GUI for Function 6 - Show cart Items // GUI for Function 7 - Clear cart Items displayPanel = new JPanel(); displayPanel.setBackground(Color.white);
// GUI for Function 4 - Add Item to Cart cartLb1 = new JLabel("Select Item"); cartCombo1 = new JComboBox(); cartLb2 = new JLabel("Quantity"); cartCombo2 = new JComboBox(); addCartBtn = new JButton("Add"); addCartBtn.addActionListener(this);
cartPanel1 = new JPanel(); cartPanel1.setBackground(Color.white); cartPanel1.add(cartLb1); cartPanel1.add(cartCombo1); cartPanel1.add(cartLb2); cartPanel1.add(cartCombo2); cartPanel1.add(addCartBtn);
// GUI for Function 5 - Remove Item from Cart cartLb1 = new JLabel("Select Item"); cartCombo3 = new JComboBox(); removeCartBtn = new JButton("Remove"); removeCartBtn.addActionListener(this);
cartPanel2 = new JPanel(); cartPanel2.setBackground(Color.white); cartPanel2.add(cartLb1); cartPanel2.add(cartCombo3); cartPanel2.add(removeCartBtn);
// GUI for Function 8 - Admin Funtions //login panel loginPanel =new JPanel(new GridLayout(3,2,2,2)); loginPanel.setBackground(Color.white); userIdLb=new JLabel("Username"); userIdTf= new JTextField(8); passwordLb=new JLabel("Password"); passwordTf= new JPasswordField(8); loginBtn=new JButton("Login"); loginBtn.addActionListener(this); loginPanel.add(userIdLb); loginPanel.add(userIdTf); loginPanel.add(passwordLb); loginPanel.add(passwordTf); loginPanel.add(loginBtn);
// function 9 - add item panel addItemPanel=new JPanel(new GridLayout(6,2,50,2)); addItemPanel.setBackground(Color.white); itemLb1=new JLabel("Product Name"); itemLb2=new JLabel("Capacity"); itemLb3=new JLabel("Battery Life"); itemLb4=new JLabel("Price"); itemLb5=new JLabel("Quantity");
itemLTf1=new JTextField(); itemLTf2=new JTextField(); itemLTf3=new JTextField(); itemLTf4=new JTextField(); itemLTf5=new JTextField();
addItemPanel.add(itemLb1); addItemPanel.add(itemLTf1); addItemPanel.add(itemLb2); addItemPanel.add(itemLTf2); addItemPanel.add(itemLb3); addItemPanel.add(itemLTf3); addItemPanel.add(itemLb4); addItemPanel.add(itemLTf4); addItemPanel.add(itemLb5); addItemPanel.add(itemLTf5); searchBtn=new JButton("Search"); saveBtn=new JButton("Save"); searchBtn.addActionListener(this); saveBtn.addActionListener(this); addItemPanel.add(searchBtn); addItemPanel.add(saveBtn); // function 10 - add item panel adminOptionsPanel = new JPanel(); adminOptionsPanel.setBackground(Color.white); addItemBtn = new JButton("ADD Item"); editItemBtn = new JButton("EDIT Item"); delItemBtn = new JButton("DELETE Item"); addItemBtn.addActionListener(this); editItemBtn.addActionListener(this); delItemBtn.addActionListener(this); adminOptionsPanel.add(addItemBtn); adminOptionsPanel.add(editItemBtn); adminOptionsPanel.add(delItemBtn); //delete item panel delItemPanel = new JPanel(); myDelTf = new JTextField(20); delItemPanel.add(myDelTf); myDelBtn = new JButton ("Search & Destroy"); delItemPanel.add(myDelBtn); myDelBtn.addActionListener(this); //main admin panel //mainAdminPanel=new JPanel(new GridLayout(0,1)); mainAdminPanel=new JPanel(); mainAdminPanel.setLayout(new BoxLayout(mainAdminPanel, BoxLayout.PAGE_AXIS)); mainAdminPanel.setBackground(Color.white); mainAdminPanel.add(adminOptionsPanel); mainAdminPanel.add(delItemPanel); mainAdminPanel.add(addItemPanel);
centerPanel = new JPanel(); centerPanel.setBackground(Color.white); centerPanel.add(searchPanel1); centerPanel.add(searchPanel2); centerPanel.add(displayPanel); centerPanel.add(cartPanel1); centerPanel.add(cartPanel2); centerPanel.add(mainAdminPanel); centerPanel.add(loginPanel);
// ============================= Layout of the Panel =========================
Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(westPanel,BorderLayout.WEST); c.add(northPanel,BorderLayout.NORTH); c.add(centerPanel,BorderLayout.CENTER);
resetAllPanels(); }
// -------------------- Methods to respnse to user request ----------------------
// used when a button is clicked public void actionPerformed(ActionEvent e) {
if(e.getSource() == fnBtn1){ // search by name option = 1; selectedOption = "Searching Item by Name"; subheaderLb.setText(selectedOption); resetAllPanels(); searchPanel1.setVisible(true); }
else if(e.getSource() == fnBtn2){ // search by price option = 2; selectedOption = "Searching Item by Price"; subheaderLb.setText(selectedOption); resetAllPanels(); searchPanel2.setVisible(true); }
else if(e.getSource() == fnBtn3){ // display ALL item details option = 3; selectedOption = "Displaying ALL Items"; subheaderLb.setText(selectedOption); resetAllPanels(); displayPanel.setVisible(true); }
else if(e.getSource() == fnBtn4){ // add item to cart option = 4; selectedOption = "Adding Item to Shopping Cart"; subheaderLb.setText(selectedOption); resetAllPanels(); refreshCartCombos(); cartPanel1.setVisible(true); }
else if(e.getSource() == fnBtn5){ // remove item from cart System.out.println(name); option = 5; selectedOption = "Removing Item from Shopping Cart"; subheaderLb.setText(selectedOption); resetAllPanels(); refreshCartCombos(); cartPanel2.setVisible(true); }
else if(e.getSource() == fnBtn6){ // show ALL cart items option = 6; selectedOption = "Showing all the Items in the Shopping Cart"; subheaderLb.setText(selectedOption); resetAllPanels(); displayPanel.setVisible(true); }
else if(e.getSource() == fnBtn7){ // remove ALL cart items option = 7; selectedOption = "Clearing all the Items from the Shopping Cart"; subheaderLb.setText(selectedOption); resetAllPanels(); displayPanel.setVisible(true); }
if(e.getSource() == adminOptionsBtn) { // Admin functions option = 8; selectedOption = "Administrator Options"; subheaderLb.setText(selectedOption); resetButtons(); resetAllPanels(); loginPanel.setVisible(true); }
else if (e.getSource() == loginBtn) { // login function // get username and password // call method to validate username and password boolean validated = true; String username = userIdTf.getText(); String password = passwordTf.getText(); if(( username.equals ("administrator"))&&(password.equals("no password"))){ validated = true; } if(validated) { // valid username and password resetAllPanels(); resetAdminPanels(); resetButtons(); mainAdminPanel.setVisible(true); } else // invalid username or password JOptionPane.showMessageDialog(this, "Login Failed.\n Please enter correct userid and password!!","Error Message", JOptionPane.ERROR_MESSAGE); }
else if(e.getSource() == searchBtn1){ // search Button in function 1 resetButtons(); searching1 = true; itemName = nameTf1.getText(); itemIndex = searchForName(nameArr, numberOfItems, itemName); }
else if(e.getSource() == searchBtn2){ // search Button in function 2 resetButtons(); searching2 = true; minPrice = Integer.parseInt(priceTf21.getText()); maxPrice = Integer.parseInt(priceTf22.getText()); for (int i = 0; i < resultArr.length; i++) resultArr[i] = 0; for (int i = 0; i < numberOfItems; i++) if (minPrice <= dataArr[2][i] && dataArr[2][i] <= maxPrice) resultArr[i] = 26; }
else if(e.getSource() == addCartBtn){ // add button in function 4 resetButtons(); cartIndex = cartCombo1.getSelectedIndex(); cartQty = cartCombo2.getSelectedIndex(); numberOfCartItems += 1; addCartItem = true; SearchName(); }
else if(e.getSource() == removeCartBtn){ // remove button in function 5 resetButtons(); delCartItem = true; }
else if(e.getSource() == addItemBtn){ // add item button in Admin functions option=9; resetButtons(); resetAdminPanels(); addItemPanel.setVisible(true); searchBtn.setVisible(false); }
else if(e.getSource() == editItemBtn){ // edit item button in Admin functions option=10; resetButtons(); resetAdminPanels(); addItemPanel.setVisible(true); searchBtn.setVisible(true); }
else if(e.getSource() == delItemBtn){ // delete item button in Admin functions option=11; resetButtons(); resetAdminPanels(); delItemPanel.setVisible(true); }
else if(e.getSource() == searchBtn){ // save item details itemName = itemLTf1.getText(); itemIndex = searchForName(nameArr, numberOfItems, itemName); if (itemIndex != -1) { itemLTf2.setText("" + dataArr[0][itemIndex]); itemLTf3.setText("" + dataArr[1][itemIndex]); itemLTf4.setText("" + dataArr[2][itemIndex]); itemLTf5.setText("" + dataArr[3][itemIndex]); } } else if (e.getSource() == saveBtn) { if (option == 9) { itemIndex = searchForName(nameArr, nameArr.length, ""); if (itemIndex != -1) { nameArr[itemIndex] = itemLTf1.getText(); dataArr[0][itemIndex] = Integer.parseInt(itemLTf2.getText()); dataArr[1][itemIndex] = Integer.parseInt(itemLTf3.getText()); dataArr[2][itemIndex] = Integer.parseInt(itemLTf4.getText()); dataArr[3][itemIndex] = Integer.parseInt(itemLTf5.getText()); numberOfItems++; JOptionPane.showMessageDialog(this, "Item added."); } else { JOptionPane.showMessageDialog(this, "Array is full!"); }
} else // editing { itemName = itemLTf1.getText(); itemIndex = searchForName(nameArr, numberOfItems, itemName); if (itemIndex != -1) { dataArr[0][itemIndex] = Integer.parseInt(itemLTf2.getText()); dataArr[1][itemIndex] = Integer.parseInt(itemLTf3.getText()); dataArr[2][itemIndex] = Integer.parseInt(itemLTf4.getText()); dataArr[3][itemIndex] = Integer.parseInt(itemLTf5.getText()); } else { JOptionPane.showMessageDialog(this, "No such product!");} } }
else if (e.getSource()==myDelBtn) { itemName=myDelTf.getText(); itemIndex=searchForName (nameArr, itemName); if (itemIndex != -1) { nameArr[itemIndex] = ""; for (int i=0; i repaint(); } public int searchForName (String[]nameArr, String name) { if (name.equals("")) return -1; for (int i=0; i // ---------------------- Method to display data on the screen -------------------------
public void paint(Graphics g){ super.paint(g);
int x = 200, y= 180;
g.setFont(new Font("Sans Serif", Font.BOLD, 14)); g.setColor(Color.blue);
if(option == 1) { if (searching1) { if(itemIndex != -1) { g.drawString(nameArr[itemIndex], x, y-10); for (int i = 0; i < attributeArr.length; i++) { y += 20; g.drawString(attributeArr[i],x, y); g.drawString("" + dataArr[i][itemIndex], x+100, y); } } else g.drawString("Product not found!", x+100, y); x += 150; } }
else if(option == 2) { if (searching2) { int oldY = y; for (int i = 0; i < attributeArr.length; i++) { y += 20; g.drawString(attributeArr[i], x, y); } for (int i = 0; i < numberOfItems; i++) { if (resultArr[i] == 26) { y = oldY; g.drawString(nameArr[i], x+100, y); for (int j = 0; j < attributeArr.length; j++) { y += 20; g.drawString("" + dataArr[j][i], x+100, y); } x += 150; } } } }
else if(option == 3) displayItemDetails(g, nameArr, attributeArr, dataArr, numberOfItems, x, y);
else if(option == 4){ if (addCartItem) g.drawString("Add Item to Cart - to be implemented ...", x+20, y); }
else if(option == 5){ if (delCartItem) g.drawString("Remove Item from Cart - to be implemented ...", x+20, y); }
else if(option == 6) g.drawString("Show Cart Items - to be implemented ...", x+20, y);
else if(option == 7) g.drawString("Clear Cart Items - to be implemented ...", x+20, y);
else if(option == 9){ if (addItem) g.drawString("Add Item - to be implemented ...", x+20, y); }
else if(option == 10){ if (editItem) g.drawString("Edit Item - to be implemented ...", x+20, y); }
else if(option == 11){ if (delItem) g.drawString("Delete Item - to be implemented ...", x+20, y); }
}
//------------------------------- Common Methods ---------------------------------------
public void resetAllPanels() { searchPanel1.setVisible(false); searchPanel2.setVisible(false); displayPanel.setVisible(false); cartPanel1.setVisible(false); cartPanel2.setVisible(false); loginPanel.setVisible(false); mainAdminPanel.setVisible(false); }
public void resetAdminPanels() { addItemPanel.setVisible(false); //editItemPanel.setVisible(false); delItemPanel.setVisible(false); } public void resetButtons() { searching1 = false; searching2 = false; addCartItem= false; delCartItem= false; addItem = false; editItem = false; delItem = false; }


// ---------------------------- function 3 - method ----------------------------------- // write the methods needed for function 3 here
// display ALL the item details public void displayItemDetails(Graphics g, String[] nameArr, String[] attributeArr, int[][] dataArr, int numberOfItems, int x, int y){
int startCol = x;
// display the item names g.setColor(Color.blue); x += 100; // display item names g.setColor(Color.blue); for (int i=0; i 0) { // stock available g.drawString(nameArr[i], x, y); x += 90; } }
// display the attribute names and values of the items for (int row=0; row 0) { // stock available g.drawString("" + dataArr[row][col], x, y); x += 90; } } }
x = startCol; y += 30; g.setFont(new Font("Sans Serif", Font.ITALIC, 13)); g.setColor(Color.red); g.drawString("Note : Capacity is in GigaBytes, Battery Life is in Hours, Price is in S$", x, y);
}
// ---------------------------- function 4 - methods ----------------------------------- // write the methods needed for function 4 here public void SearchName(){ name = nameArr[cartIndex]; for (int i = 0; i < nameArr.length; i++){ if (cartItemArr[i] == "") { cartItemArr[i] = name; numberOfItems ++ ; //nameArr[cartIndex]; //nameArr[cartIndex] = cartItemArr[i]; //num = i; //break; } //} // cartQtyArr[num] = cartQty+1;}
}
public void refreshCartCombos() {
cartCombo1.removeAllItems(); cartCombo2.removeAllItems(); cartCombo3.removeAllItems();
// initialise Combo box with item Names for (int i=0; i // initialise Combo box with numbers 1 to 10 for (int i=1; i<=10; i++) cartCombo2.addItem(""+i);
// initialise Combo box with cart item Names if (numberOfCartItems > 0) { for (int i=0; i-----"); }
// ---------------------------- function 5 - methods ----------------------------------- // write the methods needed for function 5 here
// ---------------------------- function 6 - methods ----------------------------------- // write the methods needed for function 6 here
// ---------------------------- function 7 - methods ----------------------------------- // write the methods needed for function 7 here

public int searchForName(String[] nameArr, int numberOfItems, String name ){ for (int i =0; i < numberOfItems; i++) { if (nameArr[i].equalsIgnoreCase(name)){ return i; } } return -1;}}
man, im gonna be late for Benny Hinn's healing crusade...sobs

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home