11package cn .rocket .deksrt .main ;
22
3+ import cn .rocket .deksrt .util .Student ;
4+ import cn .rocket .deksrt .util .StudentList ;
35import javafx .application .Application ;
46import javafx .fxml .FXMLLoader ;
57import javafx .scene .Parent ;
68import javafx .scene .Scene ;
79import javafx .stage .Stage ;
810import org .apache .poi .ss .usermodel .*;
9- import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
1011
11- import java .io .File ;
12- import java .io .FileInputStream ;
13- import java .io .IOException ;
14- import java .io .InputStream ;
12+ import java .io .*;
13+ import java .nio .charset .StandardCharsets ;
1514import java .nio .file .Files ;
16- import java .util .ArrayList ;
1715import java .util .Objects ;
18- import java .util .Properties ;
1916
2017/**
2118 * @author Rocket
2219 * @version 1.0
2320 */
2421public class Entry extends Application {
25- private Student [] stuInfo ;
2622
2723 @ Override
2824 public void start (Stage primaryStage ) throws Exception {
25+ // File propertiesFile = new File(GlobalVariables.env+"config.properties");
26+ if (!importStuInfo ())
27+ System .out .println ("errors:" + scanStuInfo ());
28+ exportStuInfo ();
29+
2930 Parent root = FXMLLoader .load (Objects .requireNonNull (
3031 getClass ().getResource ("/cn/rocket/deksrt/resource/MainWindow.fxml" )
3132 ));
@@ -36,66 +37,140 @@ public void start(Stage primaryStage) throws Exception {
3637
3738 }
3839
39- private void detectUserFile () throws IOException {
40- String _userProfile = System .getenv ("USERPROFILE" ) + "/DeskSorting/env.properties" ;
41- File userProfile = new File (_userProfile );
42- if (!userProfile .exists ()) {
43- String jarPath = this .getClass ().getProtectionDomain ().getCodeSource ().getLocation ().getPath ();
44- String globalPath = new File (jarPath ).getParentFile ().getPath (); //jar file parent path
45- InputStream in = this .getClass ().getResourceAsStream ("/cn/rocket/deksrt/resource/templateOfStuInfo.xlsx" );
46- File out = new File (globalPath + "StuInfo.xlsx" );
47- if (!out .exists ()) {
48- assert in != null ;
49- Files .copy (in , out .toPath ());
50- } else {
51- XSSFWorkbook wb = (XSSFWorkbook ) WorkbookFactory .create (out );
52- ArrayList <String > names = new ArrayList <>();
53- ArrayList <String > pinyinList = new ArrayList <>();
54- ArrayList <Boolean > boarding = new ArrayList <>();
55- Sheet sheet = wb .getSheetAt (0 );
56- DataFormatter df = new DataFormatter ();
57- for (int row = 1 ; row <= 50 ; row ++) {
58- Row r = sheet .getRow (row );
59- if (r == null )
60- continue ;
40+ /**
41+ * @return errors in xlsx file
42+ * @throws IOException if input a incorrect file
43+ */
44+ private String scanStuInfo () throws IOException {
45+ StringBuilder errors = new StringBuilder ();
46+ InputStream in = this .getClass ().getResourceAsStream (GlobalVariables .stuInfoTemplateP );
47+ File infoXlsx = new File (GlobalVariables .jarParentPath + "StuInfo.xlsx" );
48+ if (!infoXlsx .exists ()) {
49+ assert in != null ;
50+ Files .copy (in , infoXlsx .toPath ());
51+ System .exit (99 );
52+ } else {
53+ Workbook wb = WorkbookFactory .create (infoXlsx );
54+ GlobalVariables .stuInfo = new StudentList <>(52 );
55+ StudentList <Student > stuIn = GlobalVariables .stuInfo ;
56+
57+ Sheet sheet = wb .getSheetAt (0 );
58+ DataFormatter df = new DataFormatter ();
59+
60+ Student queue ;
61+ String name ;
62+ String pinyin ;
63+ boolean boarding ;
64+ for (int row = 1 ; row <= 52 ; row ++) {
65+ Row r = sheet .getRow (row );
66+ if (r == null )
67+ continue ;
68+
69+ Cell c0 = r .getCell (0 , Row .MissingCellPolicy .RETURN_BLANK_AS_NULL );
70+ Cell c1 = r .getCell (1 , Row .MissingCellPolicy .RETURN_BLANK_AS_NULL );
71+ Cell c2 = r .getCell (2 , Row .MissingCellPolicy .RETURN_BLANK_AS_NULL );
6172
62- Cell c0 = r .getCell (0 , Row .MissingCellPolicy .RETURN_BLANK_AS_NULL );
63- Cell c1 = r .getCell (1 , Row .MissingCellPolicy .RETURN_BLANK_AS_NULL );
64- Cell c2 = r .getCell (2 , Row .MissingCellPolicy .RETURN_BLANK_AS_NULL );
65- if (c0 != null ) {
66- names .add (c0 .getStringCellValue ());
67- pinyinList .add (c1 != null ? c1 .getStringCellValue () : "" );
68- if (c2 == null || !df .formatCellValue (c2 ).equals ("1" ))
69- boarding .add (false );
70- else
71- boarding .add (true );
73+ if (c0 != null ) {
74+ if (c1 == null || !GlobalVariables .validatePinyin ((pinyin = df .formatCellValue (c1 )))) {
75+ errors .append ("B" ).append (row + 1 ).append ("," );
76+ continue ;
7277 }
78+ if (c2 != null ) {
79+ String b = df .formatCellValue (c2 );
80+ if (!(b .equals ("1" ) || b .equals ("0" ))) {
81+ errors .append ("C" ).append (row + 1 ).append ("," );
82+ continue ;
83+ } else
84+ boarding = b .equals ("1" );
85+ } else
86+ boarding = false ;
87+ name = df .formatCellValue (c0 );
88+ } else if (c1 == null && c2 == null )
89+ continue ;
90+ else {
91+ errors .append ("A" ).append (row + 1 ).append ("," );
92+ continue ;
7393 }
74- stuInfo = new Student [names .size ()];
75- for (int i = 0 ; i < stuInfo .length ; i ++)
76- stuInfo [i ] = new Student (names .get (i ), pinyinList .get (i ), boarding .get (i ));
77- System .out .println ("name\t py\t boarding" );
78- for (int i = 0 ; i < names .size (); i ++) {
79- System .out .print (names .get (i ) + (names .get (i ).length () == 2 ? "\t \t " : "\t " ));
80- System .out .print (pinyinList .get (i ) + "\t " );
81- System .out .println (boarding .get (i ).toString ());
82- }
94+ queue = new Student (name , pinyin .toLowerCase (), boarding );
95+ if (!stuIn .contains (queue ))
96+ stuIn .add (new Student (name , pinyin .toLowerCase (), boarding ));
8397 }
98+ System .out .println ("name\t py\t boarding" );
99+ for (Student student : stuIn )
100+ System .out .println (student );
101+ wb .close ();
102+ }
103+
104+ return errors .length () != 0 ? errors .deleteCharAt (errors .length () - 1 ).toString () : "" ;
105+ }
84106
85- return ;
107+ private void exportStuInfo () {
108+ File parent = new File (GlobalVariables .env );
109+ if (!parent .exists ())
110+ //noinspection ResultOfMethodCallIgnored
111+ parent .mkdirs ();
112+ File infoFile = new File (GlobalVariables .env + "student.info" );
113+ if (!infoFile .exists ())
114+ try {
115+ //noinspection ResultOfMethodCallIgnored
116+ infoFile .createNewFile ();
117+ } catch (IOException e ) {
118+ e .printStackTrace ();
119+ }
120+ StringBuilder sb = new StringBuilder ();
121+
122+ sb .append ("default:" );
123+ for (Student student : GlobalVariables .stuInfo ) {
124+ sb .append (student .getName ()).append ("$" ).append (student .getPinyin ()).append ("$" )
125+ .append (Boolean .valueOf (student .isBoarding ())).append ("," );
126+ }
127+ sb .replace (sb .length () - 1 , sb .length () - 1 , ";" );
128+ sb .deleteCharAt (sb .length () - 1 );
129+ try (OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream (infoFile ), StandardCharsets .UTF_8 )) {
130+ osw .write (sb .toString ());
131+ osw .flush ();
132+ } catch (IOException e ) {
133+ e .printStackTrace ();
86134 }
87- FileInputStream fis = new FileInputStream (userProfile );
88- Properties p = new Properties ();
89- p .load (fis );
90- String info = p .getProperty ("stuinfo" );
135+ }
136+
137+ /**
138+ * @return <code>false</code> if no info in "student.info" or the file doesn't exist.
139+ */
140+ private boolean importStuInfo () {
141+ File infoFile = new File (GlobalVariables .env + "student.info" );
142+ String info = null ;
143+ if (!infoFile .exists ())
144+ return false ;
145+ try (BufferedReader reader = new BufferedReader (
146+ new InputStreamReader (
147+ new FileInputStream (infoFile ), StandardCharsets .UTF_8 ))) {
148+ info = reader .readLine ();
149+ } catch (IOException e ) {
150+ e .printStackTrace ();
151+ }
152+ if (info == null )
153+ return false ;
154+ // HashMap<String, StudentList<Student>> classMap = new HashMap<>();
155+ String [] classes = new String []{info }/*.split(";")*/ ;
156+ for (String cla : classes ) {
157+ String [] detailedInfo = cla .split (":" );
158+ String [] studentArray = detailedInfo [1 ].split ("," );
159+ StudentList <Student > studentInfo = new StudentList <>(studentArray .length );
160+ for (String student : studentArray ) {
161+ String [] basicInfo = student .split ("\\ $" );
162+ studentInfo .add (new Student (basicInfo [0 ], basicInfo [1 ], Boolean .parseBoolean (basicInfo [2 ])));
163+ }
164+ GlobalVariables .stuInfo = studentInfo ;
165+ // classMap.put(detailedInfo[0],studentInfo);
166+ }
167+ return true ;
91168 }
92169
93170 public static void main (String [] args ) {
94- // try {
95- // new Entry().detectUserFile();
96- // } catch (IOException e) {
97- // e.printStackTrace();
98- // }
171+ GlobalVariables .jarPath = Entry .class .getProtectionDomain ().getCodeSource ().getLocation ().getPath ();
172+ GlobalVariables .jarParentPath = new File (GlobalVariables .jarPath ).getParent () + "/" ;
173+
99174 launch (args );
100175 }
101176}
0 commit comments