Java编写整个销售数据分析系统的代码
时间: 2024-10-26 17:17:00 浏览: 18
编写一个完整的销售数据分析系统涉及到多个模块和功能。以下是基于您提供的需求,使用Java编写的示例代码框架。这个框架涵盖了基本的功能,您可以根据具体需求进一步完善和优化。
### 1. 数据模型类 `SaleRecord`
首先定义一个表示销售记录的数据模型类:
```java
import java.util.Date;
public class SaleRecord {
private String saleId;
private int month;
private String productName;
private int quantity;
private double price;
private String advisorId;
private String status;
public SaleRecord(String saleId, int month, String productName, int quantity, double price, String advisorId, String status) {
this.saleId = saleId;
this.month = month;
this.productName = productName;
this.quantity = quantity;
this.price = price;
this.advisorId = advisorId;
this.status = status;
}
// Getters and Setters
public String getSaleId() { return saleId; }
public void setSaleId(String saleId) { this.saleId = saleId; }
public int getMonth() { return month; }
public void setMonth(int month) { this.month = month; }
public String getProductName() { return productName; }
public void setProductName(String productName) { this.productName = productName; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public String getAdvisorId() { return advisorId; }
public void setAdvisorId(String advisorId) { this.advisorId = advisorId; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
@Override
public String toString() {
return "SaleRecord{" +
"saleId='" + saleId + '\'' +
", month=" + month +
", productName='" + productName + '\'' +
", quantity=" + quantity +
", price=" + price +
", advisorId='" + advisorId + '\'' +
", status='" + status + '\'' +
'}';
}
}
```
### 2. XML 文件读写工具类 `XmlUtil`
用于读取和写入XML文件的工具类:
```java
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class XmlUtil {
public static List<SaleRecord> readSalesRecordsFromXml(String filePath) {
List<SaleRecord> records = new ArrayList<>();
try {
File file = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("record");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String saleId = element.getAttribute("saleId");
int month = Integer.parseInt(element.getElementsByTagName("month").item(0).getTextContent());
String productName = element.getElementsByTagName("productName").item(0).getTextContent();
int quantity = Integer.parseInt(element.getElementsByTagName("quantity").item(0).getTextContent());
double price = Double.parseDouble(element.getElementsByTagName("price").item(0).getTextContent());
String advisorId = element.getElementsByTagName("advisorId").item(0).getTextContent();
String status = element.getElementsByTagName("status").item(0).getTextContent();
SaleRecord record = new SaleRecord(saleId, month, productName, quantity, price, advisorId, status);
records.add(record);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return records;
}
public static void writeSalesRecordsToXml(List<SaleRecord> records, String filePath) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.newDocument();
Element rootElement = doc.createElement("sales");
doc.appendChild(rootElement);
for (SaleRecord record : records) {
Element recordElement = doc.createElement("record");
recordElement.setAttribute("saleId", record.getSaleId());
Element monthElement = doc.createElement("month");
monthElement.appendChild(doc.createTextNode(String.valueOf(record.getMonth())));
recordElement.appendChild(monthElement);
Element productNameElement = doc.createElement("productName");
productNameElement.appendChild(doc.createTextNode(record.getProductName()));
recordElement.appendChild(productNameElement);
Element quantityElement = doc.createElement("quantity");
quantityElement.appendChild(doc.createTextNode(String.valueOf(record.getQuantity())));
recordElement.appendChild(quantityElement);
Element priceElement = doc.createElement("price");
priceElement.appendChild(doc.createTextNode(String.valueOf(record.getPrice())));
recordElement.appendChild(priceElement);
Element advisorIdElement = doc.createElement("advisorId");
advisorIdElement.appendChild(doc.createTextNode(record.getAdvisorId()));
recordElement.appendChild(advisorIdElement);
Element statusElement = doc.createElement("status");
statusElement.appendChild(doc.createTextNode(record.getStatus()));
recordElement.appendChild(statusElement);
rootElement.appendChild(recordElement);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filePath));
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
### 3. 主程序类 `SalesDataAnalyzer`
主程序类,提供菜单和功能选择:
```java
import java.util.List;
import java.util.Scanner;
public class SalesDataAnalyzer {
private static final String FILE_PATH = "sales_data.xml";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<SaleRecord> salesRecords = XmlUtil.readSalesRecordsFromXml(FILE_PATH);
while (true) {
System.out.println("销售数据分析系统");
System.out.println("1. 查看所有销售单");
System.out.println("2. 按营销顾问工号查询");
System.out.println("3. 统计总销售额");
System.out.println("4. 按商品编号查询");
System.out.println("5. 退出系统");
System.out.print("请选择操作: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
printAllSalesRecords(salesRecords);
break;
case 2:
queryByAdvisorId(scanner, salesRecords);
break;
case 3:
calculateTotalSales(salesRecords);
break;
case 4:
queryByProductId(scanner, salesRecords);
break;
case 5:
System.out.println("退出系统!");
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新输入!");
}
}
}
private static void printAllSalesRecords(List<SaleRecord> salesRecords) {
for (SaleRecord record : salesRecords) {
System.out.println(record);
}
}
private static void queryByAdvisorId(Scanner scanner, List<SaleRecord> salesRecords) {
System.out.print("请输入营销顾问工号: ");
String advisorId = scanner.next();
boolean found = false;
for (SaleRecord record : salesRecords) {
if (record.getAdvisorId().equals(advisorId)) {
System.out.println(record);
found = true;
}
}
if (!found) {
System.out.println("未找到该营销顾问的销售记录!");
}
}
private static void calculateTotalSales(List<SaleRecord> salesRecords) {
double totalSales = salesRecords.stream()
.filter(record -> record.getStatus().equals("已结款"))
.mapToDouble(record -> record.getQuantity() * record.getPrice())
.sum();
System.out.println("总销售额: " + totalSales);
}
private static void queryByProductId(Scanner scanner, List<SaleRecord> salesRecords) {
System.out.print("请输入商品编号: ");
String productId = scanner.next();
boolean found = false;
for (SaleRecord record : salesRecords) {
if (record.getProductName().equals(productId)) {
System.out.println(record);
found = true;
}
}
if (!found) {
System.out.println("未找到该商品的销售记录!");
}
}
}
```
### 4. 运行程序
将上述代码保存为相应的文件,然后运行 `SalesDataAnalyzer` 类即可启动销售数据分析系统。确保 `sales_data.xml` 文件存在并且格式正确。
### 5. 示例 `sales_data.xml` 文件
```xml
<sales>
<record saleId="S001">
<month>1</month>
<productName>ProductA</productName>
<quantity>10</quantity>
<price>100.0</price>
<advisorId>A001</advisorId>
<status>已结款</status>
</record>
<record saleId="S002">
<month>2</month>
<productName>ProductB</productName>
<quantity>5</quantity>
<price>200.0</price>
<advisorId>A002</advisorId>
<status>未结款</status>
</record>
<!-- 更多记录 -->
</sales>
```
以上代码提供了一个基本的销售数据分析系统的实现。您可以根据实际需求添加更多的功能和优化现有代码。
阅读全文