⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp3playerservice.java

📁 Ajax开发精要——概念、案例与框架.很适合初学者学习。
💻 JAVA
字号:
package com.ajaxlab.ajax;
import java.io.*;
import java.util.*;

import org.jdom.*;
import org.jdom.output.*;

public class MP3PlayerService {
	private Map mp3Playeres;
	/**
	 * 构造函数,初始化产品数据
	 *
	 */
	public MP3PlayerService() {
		mp3Playeres = new HashMap();
		MP3Player mp31 = new MP3Player();
		mp31.setProductName("A3800");
		mp31.setProductColor("黑色");
		mp31.setCapability("256M");
		mp31.setInterfaceType("USB2.0");
		mp31.setScreenType("单色屏幕");
		mp31.setRecord("有");
		mp31.setStraightRecord("有");
		mp31.setFm("有");
		mp31.setBattery("内置锂电池,可充电");
		mp31.setEbookFunc("无");
		mp31.setGameFunc("有");
		mp31.setPlayAvailable("15个小时");
		mp31.setWeight("34g");
		mp31.setFileSupported("支持WMA/MP3/WAV多种音乐格式");
		mp31.setVoiceEffect("SRS、WOW、TruBass");
		mp31.setOutputPower("24mW");
		mp31.setFrequencyScope("20Hz-20KHz");
		mp31.setImagePath("../images/mp31.jpg");
		mp31.setPrice("499");
		
		MP3Player mp32 = new MP3Player();
		mp32.setProductName("A3901");
		mp32.setProductColor("银白色");
		mp32.setCapability("512M");
		mp32.setInterfaceType("USB2.0");
		mp32.setScreenType("OLED屏幕");
		mp32.setRecord("有");
		mp32.setStraightRecord("有");
		mp32.setFm("有");
		mp32.setBattery("内置锂电池,可充电");
		mp32.setEbookFunc("有");
		mp32.setGameFunc("有");
		mp32.setPlayAvailable("12个小时");
		mp32.setWeight("40g");
		mp32.setFileSupported("支持MP3/WAV音乐格式");
		mp32.setVoiceEffect("SRS、TruBass");
		mp32.setOutputPower("25mW");
		mp32.setFrequencyScope("20Hz-20KHz");
		mp32.setImagePath("../images/mp32.jpg");
		mp32.setPrice("699");
		
		mp3Playeres.put(mp31.getProductName(),mp31);
		mp3Playeres.put(mp32.getProductName(),mp32);
	}
	/**
	 * 获取全部MP3播放器
	 * @return
	 */
	public MP3Player[] getAllMP3Player() {
		return (MP3Player[])this.mp3Playeres.values().toArray(new MP3Player[0]);
	}
	/**
	 * 获取指定的MP3播放器
	 * @param productName
	 * @return
	 */
	public MP3Player getMP3Player(String productName) {
		Iterator iterator = mp3Playeres.values().iterator();
		do {
			MP3Player mp3Player = (MP3Player)iterator.next();
			if(productName.equalsIgnoreCase(mp3Player.getProductName()))
				return mp3Player;
		}while(iterator.hasNext());
		return null;
	}
	/**
	 * 将指定名称的产品信息输出为XML文档
	 * @param out
	 * @param productName
	 * @throws Exception
	 */
	public void outputProductInfo(PrintWriter out, String productName) throws Exception {
		Document doc = new Document();
		Element root = new Element("products");
		Iterator iterator = mp3Playeres.values().iterator();
		do {
			MP3Player mp3Player = (MP3Player)iterator.next();
			if(productName.equalsIgnoreCase(mp3Player.getProductName().trim())) {
				Element element = new Element("mp3Player");
				element.addContent(new Element("productName").
						addContent(mp3Player.getProductName()));
				element.addContent(new Element("productColor").
						addContent(mp3Player.getProductColor()));
				element.addContent(new Element("capability").
						addContent(mp3Player.getCapability()));
				element.addContent(new Element("interfaceType").
						addContent(mp3Player.getInterfaceType()));
				element.addContent(new Element("screenType").
						addContent(mp3Player.getScreenType()));
				element.addContent(new Element("record").
						addContent(mp3Player.getRecord()));
				element.addContent(new Element("straightRecord").
						addContent(mp3Player.getStraightRecord()));
				element.addContent(new Element("fm").
						addContent(mp3Player.getFm()));
				element.addContent(new Element("battery").
						addContent(mp3Player.getBattery()));
				element.addContent(new Element("ebookFunc").
						addContent(mp3Player.getEbookFunc()));
				element.addContent(new Element("gameFunc").
						addContent(mp3Player.getGameFunc()));
				element.addContent(new Element("playAvailable").
						addContent(mp3Player.getPlayAvailable()));
				element.addContent(new Element("weight").
						addContent(mp3Player.getWeight()));
				element.addContent(new Element("fileSupported").
						addContent(mp3Player.getFileSupported()));
				element.addContent(new Element("voiceEffect").
						addContent(mp3Player.getVoiceEffect()));
				element.addContent(new Element("outputPower").
						addContent(mp3Player.getOutputPower()));
				element.addContent(new Element("frequencyScope").
						addContent(mp3Player.getFrequencyScope()));
				element.addContent(new Element("imagePath").
						addContent(mp3Player.getImagePath()));
				element.addContent(new Element("price").
						addContent(mp3Player.getPrice()));
				root.addContent(element);
			}
		}while(iterator.hasNext());
		doc.addContent(root);
		XMLOutputter outputter = 
			new XMLOutputter(Format.getPrettyFormat().setEncoding("ISO8859-1"));
		try {
			outputter.output(doc,out);
		}catch(IOException ex) {
		}
	}
	/**
	 * 将全部产品信息输出为XML文档
	 * @param out
	 * @throws Exception
	 */
	public void output(PrintWriter out, Map mp3Playeres) throws Exception {
		Document doc = new Document();
		Element root = new Element("products");
		if(mp3Playeres.values().size()!=0) {
			Iterator iterator = mp3Playeres.values().iterator();
			do {
				MP3Player mp3Player = (MP3Player)iterator.next();
				Element element = new Element("mp3Player");
				element.addContent(new Element("productName").addContent(mp3Player.getProductName()));
				element.addContent(new Element("productColor").addContent(mp3Player.getProductColor()));
				element.addContent(new Element("capability").addContent(mp3Player.getCapability()));
				element.addContent(new Element("interfaceType").addContent(mp3Player.getInterfaceType()));
				element.addContent(new Element("screenType").addContent(mp3Player.getScreenType()));
				element.addContent(new Element("record").addContent(mp3Player.getRecord()));
				element.addContent(new Element("straightRecord").addContent(mp3Player.getStraightRecord()));
				element.addContent(new Element("fm").addContent(mp3Player.getFm()));
				element.addContent(new Element("battery").addContent(mp3Player.getBattery()));
				element.addContent(new Element("ebookFunc").addContent(mp3Player.getEbookFunc()));
				element.addContent(new Element("gameFunc").addContent(mp3Player.getGameFunc()));
				element.addContent(new Element("playAvailable").addContent(mp3Player.getPlayAvailable()));
				element.addContent(new Element("weight").addContent(mp3Player.getWeight()));
				element.addContent(new Element("fileSupported").addContent(mp3Player.getFileSupported()));
				element.addContent(new Element("voiceEffect").addContent(mp3Player.getVoiceEffect()));
				element.addContent(new Element("outputPower").addContent(mp3Player.getOutputPower()));
				element.addContent(new Element("frequencyScope").addContent(mp3Player.getFrequencyScope()));
				element.addContent(new Element("imagePath").addContent(mp3Player.getImagePath()));
				element.addContent(new Element("price").addContent(mp3Player.getPrice()));
				root.addContent(element);
			}while(iterator.hasNext());
		}
		doc.addContent(root);
		//将文档输出到控制台
		XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setEncoding("ISO8859-1"));
//		XMLOutputter outputter = new XMLOutputter();
		try {
			outputter.output(doc,out);
		}catch(IOException ex) {
		}
	}
	/**
	 * 将购物车中的产品以XML文档的格式输出
	 * @param out
	 * @param productOrdered
	 * @throws Exception
	 */
	public void outputCar(PrintWriter out, Map productOrdered)throws Exception {
		Document doc = new Document();
		Element root = new Element("orders");
		Iterator iterator = productOrdered.values().iterator();
		float totalPrice = 0;
		if(productOrdered.values().size()!=0) {
			do {
				ProductOrdered po = (ProductOrdered)iterator.next();
				Element element = new Element("order");
				element.addContent(new Element("productName").addContent(po.getProductName()));
				element.addContent(new Element("count").addContent(String.valueOf(po.getCount())));
				element.addContent(new Element("price").addContent(po.getPrice()));
				totalPrice = totalPrice + Float.parseFloat(po.getPrice())*po.getCount();
				root.addContent(element);
			}while(iterator.hasNext());
		}
		if(totalPrice!=0) root.addContent(new Element("totalPrice").addContent(String.valueOf(totalPrice)));
		doc.addContent(root);
		//将文档输出到控制台
		XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setEncoding("ISO8859-1"));
//		XMLOutputter outputter = new XMLOutputter();
		try {
			outputter.output(doc,out);
		}catch(IOException ex) {
		}
	}
	public void outputNode() throws Exception {
		Document doc = new Document();
		Element root = new Element("orders");
		Element element = new Element("order");
		element.addContent(new Element("productName").addContent("A3800"));
		element.addContent(new Element("count").addContent("1"));
		element.addContent(new Element("price").addContent("499"));
		root.addContent(element);
		root.addContent(new Element("totalPrice").addContent(String.valueOf(499)));
		doc.addContent(root);
		XMLOutputter outputter = new XMLOutputter();
		try {
			outputter.output(doc,System.out);
		}catch(IOException ex) {
			
		}
	}
	public static void main(String[] args) {
		MP3PlayerService service = new MP3PlayerService();
		try {
			service.outputNode();
		}catch(Exception ex) {
			
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -