📄 newticketauction.java
字号:
/* * * Copyright (c) 2007, Sun Microsystems, Inc. * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package example.auction;import java.util.*;import javax.microedition.lcdui.*;import javax.microedition.lcdui.List;import javax.microedition.midlet.*;public class NewTicketAuction extends MIDlet implements CommandListener { // Wait for 2sec static final int DefaultTimeout = 2000; static final Command BACK_CMD = new Command("Back", Command.BACK, 1); static final Command SAVE_CMD = new Command("Save", Command.SCREEN, 2); static final Command NEXT_CMD = new Command("Next", Command.SCREEN, 2); static final Command SUBMIT_CMD = new Command("Submit", Command.ITEM, 2); static final Command AUCTION_CMD = new Command("Auction", Command.ITEM, 2); static final Command CANCEL_CMD = new Command("Cancel", Command.CANCEL, 1); static final Command STOP_CMD = new Command("Stop", Command.STOP, 1); static final Command EXIT_CMD = new Command("Exit", Command.EXIT, 1); // commands for Band Screen bandForm static final Command SHOW_CMD = new Command("Show Auctions", Command.SCREEN, 1); static final Command ADD_CMD = new Command("Add Bands", Command.SCREEN, 1); static final Command RMV_CMD = new Command("Remove Bands", Command.SCREEN, 2); static final Command SETTING_CMD = new Command("Settings", Command.SCREEN, 3); // commands for Auction Screen ticketList static final Command SHOW_INFO_CMD = new Command("Show More Info", Command.ITEM, 1); static final Command MAKE_BID_CMD = new Command("Make a Bid", Command.ITEM, 2); static final Command SET_ALERT_CMD = new Command("Set an Alert", Command.ITEM, 3); // command for Bid screen ticketForm static final Command BAND_CMD = new Command("Bands", Command.SCREEN, 1); Display display; /** user interface alert component. */ Alert splashScreenAlert; Image splashScreen; boolean imageLoaded; Band band = new Band(); Login login = new Login(); TextBox addBand; RmBand rmBand = new RmBand(); List mainMenu; // bandList, ticketList; List cateMenu; // bandList, ticketList; List ticketList; // bandList, ticketList; Ticker ticker; Alert notImpl; Alert savedMsg; Alert alertMsg; Form band_ticket_Form; Form ticketForm; Form enterForm; Form bidForm; Form submitMsg; TextField enterText; ChoiceGroup bandCg; ChoiceGroup auctionCg; StringItem auctionName; UpdateAlert updateAlert = new UpdateAlert(); Gauge submitGauge; Timer TimerService; SetMenuForm setMenu2 = new SetMenuForm(); // Band Name and Index number of the selected band in // Band Screen band_ticket_Form String _bandName; int _bandIndex = 0; private boolean firstTime; public NewTicketAuction() { display = Display.getDisplay(this); notImpl = new Alert("Sorry!!!", "Not Implemented", null, null); savedMsg = new Alert(null, "Your new settings have been saved!", null, null); alertMsg = new Alert(null, "Your alerts have been saved!", null, null); submitGauge = new Gauge("", false, 100, 0); submitMsg = new Form("Submitting bid..."); submitMsg.append(submitGauge); submitMsg.addCommand(STOP_CMD); submitMsg.setCommandListener(this); ticker = new Ticker(""); ticker.setString(band.toTickerString(null)); band_ticket_Form = new Form("Welcome To Tickets"); band_ticket_Form.addCommand(SHOW_INFO_CMD); band_ticket_Form.addCommand(MAKE_BID_CMD); band_ticket_Form.addCommand(SET_ALERT_CMD); band_ticket_Form.addCommand(ADD_CMD); band_ticket_Form.addCommand(RMV_CMD); band_ticket_Form.addCommand(SETTING_CMD); band_ticket_Form.addCommand(EXIT_CMD); band_ticket_Form.setCommandListener(this); band_ticket_Form.setTicker(ticker); // bandCg = new ChoiceGroup("Choose a Band", Choice.EXCLUSIVE); bandCg = new ChoiceGroup("Choose a Band", Choice.POPUP); band_ticket_Form.append(bandCg); String[] list = band.getList(); for (int i = 0; i < list.length; i++) { bandCg.append(list[i], null); } String bandName = band.getName(bandCg.getSelectedIndex()); auctionCg = new ChoiceGroup(bandName + " Auctions:", Choice.EXCLUSIVE); auctionCg.setLayout(Item.LAYOUT_EXPAND); band_ticket_Form.append(auctionCg); reconstructList(auctionCg, band.getTicketList(bandName)); band_ticket_Form.setItemStateListener(new ItemStateListener() { public void itemStateChanged(Item item) { if (item instanceof ChoiceGroup) { ChoiceGroup obj = (ChoiceGroup)item; if (obj == bandCg) { int idx = obj.getSelectedIndex(); String bandName = band.getName(idx); auctionCg.setLabel(bandName + " Auctions:"); reconstructList(auctionCg, band.getTicketList(bandName)); } } } }); try { splashScreen = Image.createImage("/example/auction/images/splashScreen.png"); imageLoaded = true; } catch (java.io.IOException ex) { } splashScreenAlert = new Alert("Welcome to Ticket Auction", "", splashScreen, AlertType.INFO); splashScreenAlert.setTimeout(DefaultTimeout); ticketForm = new Form("unknown"); ticketForm.append("unknown"); ticketForm.addCommand(BACK_CMD); ticketForm.addCommand(BAND_CMD); ticketForm.addCommand(MAKE_BID_CMD); ticketForm.addCommand(SET_ALERT_CMD); ticketForm.setCommandListener(this); enterText = new TextField("Tell me when bid reaches:", "", 10, TextField.DECIMAL); enterForm = new Form("Set an Alert"); enterForm.append(enterText); enterForm.addCommand(BACK_CMD); enterForm.addCommand(SAVE_CMD); enterForm.setCommandListener(this); addBand = new TextBox("Add Bands, Bands:", "", 100, TextField.ANY); addBand.addCommand(BACK_CMD); addBand.addCommand(SAVE_CMD); addBand.setCommandListener(this); firstTime = true; } public void startApp() { if (firstTime) { display.setCurrent(splashScreenAlert, band_ticket_Form); firstTime = false; } } public void destroyApp(boolean unconditional) { } public void pauseApp() { } public void commandAction(Command c, Displayable s) { if (s instanceof Form) { Form obj = (Form)s; if (obj == band_ticket_Form) { int idx = auctionCg.getSelectedIndex(); _bandIndex = idx; int bandIdx = bandCg.getSelectedIndex(); String bandName = ""; if (bandIdx >= 0) { bandName = band.getName(bandIdx); } _bandName = bandName; if (c == SHOW_INFO_CMD) { deleteFormItem(ticketForm); ticketForm.setTitle(band.getTicketDataTitle(bandName, idx)); band.getTicketData(ticketForm, bandName, idx); display.setCurrent(ticketForm); } else if (c == MAKE_BID_CMD) { login.setBandAttributes(bandName, _bandIndex); deleteFormItem(login); login.append(login.id); login.id.setLabel("Enter Your ID:"); login.append(login.pd); login.pd.setLabel("Enter PIN:"); login.append(login.curBid); login.append(login.incBid); login.append(login.minBid); login.append(login.bidText); login.id.setString(null); login.pd.setString(null); login.bidText.setString(null); band.getTicketBidTitle(login, _bandName, _bandIndex); display.setCurrent(login); } else if (c == SET_ALERT_CMD) { display.setCurrent(enterForm); } else if (c == ADD_CMD) { display.setCurrent(addBand); } else if (c == RMV_CMD) { display.setCurrent(rmBand); } else if (c == SETTING_CMD) { display.setCurrent(setMenu2); } else if (c == EXIT_CMD) { notifyDestroyed(); } } else if (obj == ticketForm) { if (c == BACK_CMD) { display.setCurrent(band_ticket_Form); } else if (c == BAND_CMD) { display.setCurrent(band_ticket_Form); } else if (c == MAKE_BID_CMD) { login.setBandAttributes(band.getName(bandCg.getSelectedIndex()), _bandIndex); deleteFormItem(login); login.append(login.id); login.id.setLabel("Enter Auction ID:"); login.append(login.pd); login.pd.setLabel("Enter PIN:"); login.append(login.curBid); login.append(login.incBid); login.append(login.minBid); login.append(login.bidText); login.id.setString(null); login.pd.setString(null); login.bidText.setString(null); band.getTicketBidTitle(login, _bandName, _bandIndex); display.setCurrent(login); } else if (c == SET_ALERT_CMD) { deleteFormItem(enterForm); int selectedBand = bandCg.getSelectedIndex(); String ticketID = band.getTicketID(band.getName(selectedBand), login._bandIndex); enterForm.append(new StringItem("Auction:", band.getName(selectedBand) + " " + ticketID)); String curBid = band.getCurrentBid(band.getName(selectedBand), login._bandIndex); enterForm.append(new StringItem("Current Bid:", "$" + curBid)); enterForm.append(enterText); display.setCurrent(enterForm); } } else if (obj == enterForm) { if (c == BACK_CMD) { enterText.setString(null); display.setCurrent(band_ticket_Form); } else if (c == SAVE_CMD) { updateAlert.set(band.getName(bandCg.getSelectedIndex()), enterText.getString()); display.setCurrent(alertMsg, band_ticket_Form); } } else if (obj == submitMsg) { if (c == STOP_CMD) { TimerService.cancel(); display.setCurrent(login.confirm); } } } else if (s instanceof TextBox) { TextBox obj = (TextBox)s; if (obj == addBand) { if (c == BACK_CMD) { // display.setCurrent(setMenu); display.setCurrent(band_ticket_Form); } else if (c == SAVE_CMD) { updateBandList(addBand.getString()); display.setCurrent(savedMsg, band_ticket_Form); } } } } void updateBandList(String list) { if (list.length() == 0) { return; } Vector vec = new Vector(); int fidx = 0; while (true) { int idx = list.indexOf(',', fidx); if (idx == -1) { vec.addElement(list.substring(fidx)); break; } vec.addElement(list.substring(fidx, idx)); fidx = idx + 1; } for (int i = 0; i < vec.size(); i++) { String str = (String)vec.elementAt(i); int j = 0; int len = str.length(); for (; j < len; j++) { if (str.charAt(j) != ' ') { break; } } if (j == len) { continue; } if (j == 0) { band.add(str); } else { band.add(str.substring(j)); } } reconstructBandTicketForm(band.getList()); rmBand.reset(); } void reconstructBandTicketForm(String[] items) { if ((items == null) || (items.length == 0)) { bandCg.deleteAll(); auctionCg.setLabel(null); auctionCg.deleteAll(); return; } reconstructList(bandCg, items); bandCg.setSelectedIndex(0, true); String bandName = band.getName(bandCg.getSelectedIndex()); auctionCg.setLabel(bandName + " Auctions:"); reconstructList(auctionCg, band.getTicketList(bandName)); } void reconstructList(Choice list, String[] items) { list.deleteAll(); for (int i = 0; i < items.length; i++) { list.append(items[i], null); } } void deleteFormItem(Form f) { int num = f.size(); while (--num >= 0) { f.delete(num); } } class Login extends Form implements CommandListener { TextField id = new TextField("", "", 10, TextField.ANY); TextField pd = new TextField("", "", 10, (TextField.NUMERIC | TextField.PASSWORD)); TextField bidText = new TextField("Enter Bid:", "", 10, TextField.DECIMAL); StringItem submitButton = new StringItem("", "Submit", Item.BUTTON); StringItem auctionHyperlink = new StringItem("Auction:", "", Item.HYPERLINK); StringItem curBid = new StringItem("Current Bid:", ""); StringItem incBid = new StringItem("Increment:", ""); StringItem minBid = new StringItem("Minimum Bid:", ""); Form confirm = new Form("Confirm Bid"); Form notice = new Form("Bid Received"); Form auctionForm = new Form("unknown"); Alert loginAlert = new Alert("Alert", "Your must enter your ID and password" + " before you can proceed.", null, null); String bandName; String bid; String ticketID; String _bandName; int _bandIndex; Login() { super("Make A Bid"); id.setLabel("Enter Auction ID:"); append(id);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -