|
@@ -0,0 +1,119 @@
|
|
|
+package com.kk;
|
|
|
+
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.FileWriter;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+public class LotteryTicketGenerator {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ int totalTickets = 100000;
|
|
|
+ int[] prizes = {1000000, 100000, 10000};
|
|
|
+ int[] prizeCounts = {1, 5, 100};
|
|
|
+ int[] prizeLevels = {1, 2, 3};
|
|
|
+
|
|
|
+ List<LotteryTicket> tickets = generateTickets(totalTickets, prizes, prizeCounts, prizeLevels);
|
|
|
+ exportToExcel(tickets, "lottery_tickets.xlsx");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static List<LotteryTicket> generateTickets(int totalTickets, int[] prizes, int[] prizeCounts, int[] prizeLevels) {
|
|
|
+ List<LotteryTicket> tickets = new ArrayList<>();
|
|
|
+ Random random = new Random();
|
|
|
+
|
|
|
+ // Generate winning tickets
|
|
|
+ for (int i = 0; i < prizes.length; i++) {
|
|
|
+ for (int j = 0; j < prizeCounts[i]; j++) {
|
|
|
+ int luckyNumber = random.nextInt(totalTickets) + 1;
|
|
|
+ tickets.add(new LotteryTicket(luckyNumber, prizeLevels[i], prizes[i]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Generate non-winning tickets
|
|
|
+ while (tickets.size() < totalTickets) {
|
|
|
+ int luckyNumber = random.nextInt(totalTickets) + 1;
|
|
|
+ tickets.add(new LotteryTicket(luckyNumber, 12, 0));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Shuffle the tickets
|
|
|
+ Collections.shuffle(tickets);
|
|
|
+ return tickets;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// ...
|
|
|
+
|
|
|
+ public static void exportToExcel(List<LotteryTicket> tickets, String fileName) {
|
|
|
+ Workbook workbook = new XSSFWorkbook();
|
|
|
+ Sheet sheet = workbook.createSheet("Lottery Tickets");
|
|
|
+
|
|
|
+ // Create header row
|
|
|
+ Row headerRow = sheet.createRow(0);
|
|
|
+ headerRow.createCell(0).setCellValue("Ticket Number");
|
|
|
+ headerRow.createCell(1).setCellValue("Lucky Number");
|
|
|
+ headerRow.createCell(2).setCellValue("Prize Level");
|
|
|
+ headerRow.createCell(3).setCellValue("Prize Amount");
|
|
|
+
|
|
|
+ // Create data rows
|
|
|
+ for (int i = 0; i < tickets.size(); i++) {
|
|
|
+ LotteryTicket ticket = tickets.get(i);
|
|
|
+ Row row = sheet.createRow(i + 1);
|
|
|
+ row.createCell(0).setCellValue(i + 1);
|
|
|
+ row.createCell(1).setCellValue(ticket.getLuckyNumber());
|
|
|
+ row.createCell(2).setCellValue(ticket.getPrizeLevel());
|
|
|
+ row.createCell(3).setCellValue(ticket.getPrizeAmount());
|
|
|
+ }
|
|
|
+
|
|
|
+ // Auto size columns
|
|
|
+ for (int i = 0; i < 4; i++) {
|
|
|
+ sheet.autoSizeColumn(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Write to file
|
|
|
+ try (FileOutputStream fileOut = new FileOutputStream(fileName)) {
|
|
|
+ workbook.write(fileOut);
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println("An error occurred while exporting the data to Excel.");
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ workbook.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ static class LotteryTicket {
|
|
|
+ private int luckyNumber;
|
|
|
+ private int prizeLevel;
|
|
|
+ private int prizeAmount;
|
|
|
+
|
|
|
+ public LotteryTicket(int luckyNumber, int prizeLevel, int prizeAmount) {
|
|
|
+ this.luckyNumber = luckyNumber;
|
|
|
+ this.prizeLevel = prizeLevel;
|
|
|
+ this.prizeAmount = prizeAmount;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getLuckyNumber() {
|
|
|
+ return luckyNumber;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getPrizeLevel() {
|
|
|
+ return prizeLevel;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getPrizeAmount() {
|
|
|
+ return prizeAmount;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|