@@ -21,10 +21,11 @@ public static void BookGrabber()
2121 {
2222 List < string > Books = new List < string > ( ) ;
2323 string BookDir = Path . GetFullPath ( Path . Combine ( Directory . GetCurrentDirectory ( ) , $ "Books") ) ;
24- foreach ( string foldername in Directory . GetDirectories ( BookDir ) )
24+ foreach ( string folderPath in Directory . GetDirectories ( BookDir ) )
2525 {
26- Books . Add ( foldername ) ;
27- Console . WriteLine ( foldername . Replace ( BookDir + "\\ " , "" ) ) ;
26+ string folderName = new DirectoryInfo ( folderPath ) . Name ;
27+ Books . Add ( folderName ) ;
28+ Console . WriteLine ( folderName ) ;
2829 }
2930 }
3031 // Stack Overflow - javadch
@@ -50,58 +51,58 @@ public static void PageReader()
5051 string Book = Console . ReadLine ( ) ;
5152 Book = CapitalizeWithSpaces ( Book ) ;
5253 Console . WriteLine ( Book ) ;
53- if ( Directory . Exists ( Path . Combine ( BookDir , Book ) ) )
54- {
55- Console . WriteLine ( $ "Book Selected { Book } ") ;
56- } else
54+
55+ if ( ! Directory . Exists ( Path . Combine ( BookDir , Book ) ) )
5756 {
58- int A = 0 ;
59- int B = 0 ;
60- while ( A == B )
61- {
62- Console . Write ( "That was not a option, Try Again!: " ) ;
63- Book = Console . ReadLine ( ) ;
64- Book = CapitalizeWithSpaces ( Book ) ;
65- if ( Directory . Exists ( Path . Combine ( BookDir , Book ) ) )
66- {
67- Console . WriteLine ( $ "Book Selected { Book } ") ;
68- A ++ ;
69- }
70- }
57+ Console . Write ( "The Options are above the prior line Which one of those?: " ) ;
58+ Book = Console . ReadLine ( ) ;
59+ Book = CapitalizeWithSpaces ( Book ) ;
60+ Console . WriteLine ( Book ) ;
7161 }
62+
7263 List < string > Pages = new List < string > ( ) ;
73- for ( int i = 0 ; ; i ++ )
64+ int maxPages = 1000 ; // Adjust this number based on your expected maximum number of pages
65+
66+ for ( int i = 0 ; i < maxPages ; i ++ )
7467 {
75- if ( File . Exists ( Path . Combine ( BookDir , Book , $ "{ i } .txt") ) )
68+ string filePath = Path . Combine ( BookDir , Book , $ "{ i } .txt") ;
69+ if ( File . Exists ( filePath ) )
7670 {
7771 Pages . Add ( i . ToString ( ) ) ;
7872 }
79- else if ( ! File . Exists ( Path . Combine ( BookDir , Book , $ "{ i } .txt") ) )
80- {
81- break ;
82- }
8373 else
8474 {
85- Console . WriteLine ( "No Pages Available or hit cap" ) ;
86- break ;
75+ break ; // Exit the loop if no more pages are found
8776 }
8877 }
78+
79+ if ( Pages . Count == 0 )
80+ {
81+ Console . WriteLine ( "Error: No pages found for the selected book." ) ;
82+ return ;
83+ }
84+
8985 string saveFilePath = Path . Combine ( Directory . GetCurrentDirectory ( ) , "Save.txt" ) ;
9086 bool BookFinishedForNow = false ;
87+
9188 while ( BookFinishedForNow == false )
9289 {
9390 if ( File . Exists ( saveFilePath ) )
9491 {
9592 string [ ] saveLines = File . ReadAllLines ( saveFilePath ) ;
93+ bool foundCurrentPage = false ;
94+
9695 foreach ( string line in saveLines )
9796 {
9897 if ( line . StartsWith ( $ "{ Book } -Current-Page: ") )
9998 {
99+ foundCurrentPage = true ;
100100 string currentPageStr = line . Split ( ':' ) [ 1 ] . Trim ( ) ;
101101 if ( int . TryParse ( currentPageStr , out int currentPage ) )
102102 {
103103 if ( currentPage >= 0 && currentPage <= Pages . Count )
104104 {
105+ // Display current page and handle navigation
105106 Console . Clear ( ) ;
106107 Console . WriteLine ( "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" ) ;
107108 Console . WriteLine ( $ "Book: { Book } \n Page: { string . Join ( ", " , Pages . ElementAt ( currentPage ) ) } ") ;
@@ -115,7 +116,7 @@ public static void PageReader()
115116 Console . Write ( "What Navigation would you like to do from the above options?: " ) ;
116117 string Option = Console . ReadLine ( ) ;
117118 Functions ( Option , ref currentPage , ref BookFinishedForNow , Pages . Count ) ;
118- Save ( ref saveLines , ref Book , ref saveFilePath , ref currentPage ) ;
119+ Save ( ref saveLines , ref Book , ref saveFilePath , ref currentPage , ref BookDir ) ;
119120 }
120121 else
121122 {
@@ -129,6 +130,15 @@ public static void PageReader()
129130 break ;
130131 }
131132 }
133+
134+ if ( ! foundCurrentPage )
135+ {
136+ // Add line to save file for the current book
137+ using ( StreamWriter sw = File . AppendText ( saveFilePath ) )
138+ {
139+ sw . WriteLine ( $ "{ Book } -Current-Page: 0") ;
140+ }
141+ }
132142 }
133143 else
134144 {
@@ -206,22 +216,34 @@ public static void Functions(string Option, ref int currentPage, ref bool BookFi
206216 }
207217 }
208218 }
209- public static void Save ( ref string [ ] saveLines , ref string Book , ref string saveFilePath , ref int currentPage )
219+ public static void Save ( ref string [ ] saveLines , ref string Book , ref string SaveFilePath , ref int currentPage , ref string BookDir )
210220 {
211- // Provided by GPT and modified from it to work better.
212- string [ ] updatedSaveLines = new string [ saveLines . Length ] ;
221+ if ( ! Directory . Exists ( BookDir ) )
222+ {
223+ Directory . CreateDirectory ( BookDir ) ;
224+ }
225+
226+ bool bookFound = false ;
227+
213228 for ( int i = 0 ; i < saveLines . Length ; i ++ )
214229 {
215230 if ( saveLines [ i ] . StartsWith ( $ "{ Book } -Current-Page: ") )
216231 {
217- updatedSaveLines [ i ] = $ "{ Book } -Current-Page: { currentPage } ";
232+ saveLines [ i ] = $ "{ Book } -Current-Page: { currentPage } ";
233+ bookFound = true ;
234+ break ;
218235 }
219- else
236+ }
237+
238+ if ( ! bookFound )
239+ {
240+ using ( StreamWriter sw = File . AppendText ( SaveFilePath ) )
220241 {
221- updatedSaveLines [ i ] = saveLines [ i ] ;
242+ sw . WriteLine ( $ " { Book } -Current-Page: { currentPage } " ) ;
222243 }
223244 }
224- File . WriteAllLines ( saveFilePath , updatedSaveLines ) ;
245+
246+ File . WriteAllLines ( SaveFilePath , saveLines ) ;
225247 }
226248 }
227249}
0 commit comments