1.工具类在同一个包里无法用类名调用
2.SAXReader saxReader = new SAXReader();
3.多种if循环else每次都会与最后一个进行判定示例
if(){
}if (){
}else{
sout("每次都会输出")
}
4.整型,double进行转换
int num = Integer.parseInt();
double price = Double.parseDouble();5.声明的变量使用时注意赋值。
数据.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<info>
<sellId>pd0001</sellId>
<month>1</month>
<name>可口可乐</name>
<num>102</num>
<price>3.5</price>
<gwId>x0001</gwId>
<state>已结款</state>
</info>
</root>
工具类
public class Dom4JUtil {
public static Document readXml()throws Exception{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("src//sell.xml");
return document;
}
}
客户端
public class MarketMgr {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
SellFunction sellFunction = new SellFunction();
System.out.println("-----------------------------------------");
System.out.println("---------------销售数据分析系统-------------");
while (true){
System.out.println("1.显示全部销售单数据");
System.out.println("2.销售顾问业绩查询");
System.out.println("3.未结款总额查询");
System.out.println("4.商品销量查询");
System.out.println("5.退出系统");
System.out.println("-----------------------------------------");
System.out.print("请输入指令:");
String choose = input.next();
if (choose.equals("1")){
sellFunction.infos();
}if (choose.equals("2")){
System.out.print("请输入销售顾问单号:");
String sellNum = input.next(); //销售单号
sellFunction.gwQuery(sellNum);
}if (choose.equals("3")){
sellFunction.stateQuery();
}if (choose.equals("4")){
System.out.print("请输入商品名称:");
String name=input.next();
sellFunction.nameQuery(name);
}if (choose.equals("5")){
System.out.println("谢谢使用,退出系统");
break;
}
}
}
}
服务
public class SellFunction {
public static List<Sell> sellList= new ArrayList<>();
static {
try {
Document document= Dom4JUtil.readXml();
Element root = document.getRootElement();
Iterator<Element> infoIter = root.elementIterator("info");
while (infoIter.hasNext()){
Element info= infoIter.next();
String sellId = info.elementText("sellId");
int month = Integer.parseInt(info.elementText("month"));
String name = info.elementText("name");
int num = Integer.parseInt(info.elementText("num"));
double price = Double.parseDouble(info.elementText("price"));
String gwId = info.elementText("gwId");
String state = info.elementText("state");
Sell sell = new Sell(sellId,month,name,num,price,gwId,state);
sellList.add(sell);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void infos(){
System.out.println("销售单号\t\t销售月份\t\t商品名称\t\t销售数量\t单价\t\t营销顾问单号\t售单状态");
for (Sell sell : sellList){
System.out.println(sell.getSellId()+"\t\t"+sell.getMonth()+"\t\t\t"+sell.getName()
+"\t\t"+sell.getNum()+"\t\t"+sell.getPrice()+"\t\t"+sell.getGwId()+"\t\t"+sell.getState());
}
}
public static void gwQuery(String sellNum){
int count = 0; //签单数
double sumMoney =0; //总消费
int count2 = 0;
for (Sell sell : sellList){
if (sell.getGwId().equals(sellNum)){
count++;
sumMoney=sell.getPrice() * sell.getNum();
if (sell.getState().equals("未结款")){
count2++;
}
}
}
System.out.println("共成功签单"+count+"笔,销售总额:"+sumMoney+"其中"+count2+"笔签单未结款");
System.out.println("销售单号\t\t销售月份\t\t商品名称\t\t销售数量\t单价\t\t营销顾问单号\t售单状态");
for (Sell sell : sellList){
if (sell.getGwId().equals(sellNum)){
System.out.println(sell.getSellId()+"\t\t"+sell.getMonth()+"\t\t\t"+sell.getName()
+"\t\t"+sell.getNum()+"\t\t"+sell.getPrice()+"\t\t"+sell.getGwId()+"\t\t"+sell.getState());
}
}
}
public static void stateQuery(){
int stateNum = 0;//未签单
double stateMoney =0;
for (Sell sell : sellList){
if (sell.getState().equals("未结款")){
stateNum++;
stateMoney= sell.getPrice()*sell.getNum();
}
}
System.out.println("共有未签单"+stateNum+"笔,涉及金额:"+stateMoney);
}
public static void nameQuery(String name){
for (Sell sell : sellList){
if (sell.getName().equals(name)){
System.out.println(sell.getSellId()+"\t\t"+sell.getMonth()+"\t\t\t"+sell.getName()
+"\t\t"+sell.getNum()+"\t\t"+sell.getPrice()+"\t\t"+sell.getGwId()+"\t\t"+sell.getState());
}
}
}
}
实体类
public class Sell {
private String sellId; //销售单号
private int month; //月份
private String name; //商品名
private int num; //销售数量
private double price; //商品价格
private String gwId; //顾问Id
private String state; //商品状态
public Sell(String sellId, int month, String name,
int num, double price, String gwId, String state) {
this.sellId = sellId;
this.month = month;
this.name = name;
this.num = num;
this.price = price;
this.gwId = gwId;
this.state = state;
}
public Sell() {
}
public String getSellId() {
return sellId;
}
public void setSellId(String sellId) {
this.sellId = sellId;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getGwId() {
return gwId;
}
public void setGwId(String gwId) {
this.gwId = gwId;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}