1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
@SuppressWarnings("unused")
public class Main {
public static void main(String[] args) throws InterruptedException, FileNotFoundException, UnsupportedEncodingException {
//introduction
File fnSave = new File("nSave.txt");
System.out.println("Bienvenue");
System.out.println("Préparation");
Thread.sleep(1000);
System.out.println(".");
Thread.sleep(1000);
System.out.println(".");
Thread.sleep(1000);
System.out.println(".");
Thread.sleep(1000);
System.out.println("Ready");
Thread.sleep(1000);
System.out.println("===============");
System.out.println("Vous allez maintenant devoir les données relatives à votre stock.");
Thread.sleep(1000);
//Input MED
ArrayList<Medicament> medList = new ArrayList<Medicament>();
Scanner scan = new Scanner(System.in);
boolean medRestart = true;
int iMed = 1;
while(medRestart == true){
System.out.println("Médicament numéro " + iMed+"...");
System.out.println("Nom du médicament?");
boolean bNomMed = true;
String nomMed = "";
nomMed =scan.nextLine();
int prixMed = lireNombre(scan, "Prix du médicament?");
medList.add(new Medicament(nomMed, prixMed));
System.out.println("Autre medicament? (O/N)");
scan.nextLine();
char medRestartScan = scan.nextLine().toLowerCase().charAt(0);
while(medRestartScan != 'o' && medRestartScan != 'n'){
System.out.println("Veuillez répondre par 'O' ou 'N'.");
medRestartScan = scan.nextLine().toLowerCase().charAt(0);
}
if(medRestartScan == 'n'){
medRestart = false;
}
iMed++;
}
//input CLIENTS INTRO
ArrayList<Client> cList = new ArrayList<Client>();
//Make sure all files exists.
File fNames = new File("Names.txt");
File fSurnames = new File("Surnames.txt");
File fTowns = new File("Towns.txt");
File fAges = new File("Ages.txt");
if(fNames.exists() && fSurnames.exists() && fTowns.exists() && fAges.exists()){
//LOADING SAVE.
//ArrayLists creation.
ArrayList<String> lNames = new ArrayList<String> ();
ArrayList<String> lSurnames = new ArrayList<String> ();
ArrayList<String> lTowns = new ArrayList<String> ();
ArrayList<Integer> lAges = new ArrayList<Integer>();
//Loading AL.
database.DBMethods.toALString(lNames, "Names.txt");
database.DBMethods.toALString(lSurnames, "Surnames.txt");
database.DBMethods.toALString(lTowns, "Towns.txt");
database.DBMethods.toALInteger(lAges, "Ages.txt");
//Loading into cList.
for(int i = 0; i< lNames.size()-2;i++){
cList.add(new Client(lNames.get(i), lSurnames.get(i), lTowns.get(i), lAges.get(i)));
}
//correcting ints newline.
try {
FileWriter fwAges = new FileWriter("Ages.txt", true);
BufferedWriter bwAges = new BufferedWriter(fwAges);
bwAges.newLine();
bwAges.close();
} catch (IOException e) {
System.out.println("Error main filewriter correctin ints.");
}
}
Client.sysoutClients(cList);
System.out.println("Préparation");
Thread.sleep(1000);
System.out.println("Prêt à recevoir les clients.");
System.out.println("====================");
Thread.sleep(1000);
boolean cRestart = true;
//CLIENT INPUT DONNEES
int ic = cList.size()+1;
int icList = 0;
while(cRestart ==true){
System.out.println("Client numéro "+ic+"...");
System.out.println("Nom client?");
String nomC = scan.nextLine();
System.out.println("Prénom client?");
String prénomC = scan.nextLine();
System.out.println("Ville du client?");
String villeC = scan.nextLine();
int ageC = lireNombre(scan, "Age du client?");
cList.add(new Client(nomC, prénomC, villeC, ageC));
//CLIENT ACHATS
System.out.println("Preparation de la liste pour impression...");
Thread.sleep(1000);
System.out.println("=============");
int iSizeCorrected = (medList.size())-1;
for(int i = 0; i <= iSizeCorrected;i++){
System.out.println(i+" : "+ medList.get(i).getNom());
}
boolean medBoughtStatus = true;
int medBought = -1;
while(medBoughtStatus == true){
while(intervallContains(0, (medList.size()-1), medBought) == false){
System.out.println("Med. acheté? (répondre par l'indice de la liste ci dessus).");
boolean bMedBought = true;
while(bMedBought == true){
try{
medBought = scan.nextInt();
bMedBought = false;
} catch(InputMismatchException e){
System.out.println("Veuillez entrer un indice contenu dans la liste ci dessus.");
scan.nextLine();
}}
}
cList.get(icList).cMedList.add(medList.get(medBought));
medBought = -1;
System.out.println("Autre med. acheté? (O/N)");
scan.nextLine();
String medBoughtStatusBis = scan.nextLine();
char medBoughtStatusBisChar = medBoughtStatusBis.toLowerCase().charAt(0);
while (medBoughtStatusBisChar != 'o' && medBoughtStatusBisChar != 'n')
{
System.out.println("Veuillez répondre par 'O' ou 'N'.");
medBoughtStatusBis = scan.nextLine();
medBoughtStatusBisChar = medBoughtStatusBis.toLowerCase().charAt(0);
}
if(medBoughtStatusBisChar == 'n'){
medBoughtStatus = false;
}
else if(medBoughtStatusBisChar == 'o'){
medBoughtStatus = true;
}
}
System.out.println("Le client numéro "+ic+" a acheté :");
for(int iii = 0;iii< cList.get(icList).cMedList.size() ;iii++){
System.out.println(cList.get(icList).cMedList.get(iii).getNom()+" a "+cList.get(icList).cMedList.get(iii).getPrix()+" euro(s).");
}
//NEXT CLIENT (?)
ic++;
icList++;
System.out.println("Autre client? (O/N)");
String charC = scan.nextLine().toLowerCase();
boolean bCharC = true;
while(bCharC == true){
if(charC.charAt(0) != 'n' && charC.charAt(0) != 'o'){
System.out.println("Veuillez répondre par 'O' ou 'N'.");
charC = scan.nextLine().toLowerCase();
}
else {
bCharC = false;
}
}
if(charC.charAt(0)=='n')
cRestart = false;
}
//Final part
System.out.println("Fin de l'encodage client.");
System.out.println("Sous quel nom voulez-vous sortir l'historique?");
String nFile = scan.nextLine();
System.out.println("Impression de l'historique dans 3 secondes.");
Thread.sleep(3000);
int iLast =0;
for(iLast = 0; iLast < cList.size(); iLast++){
System.out.println("Le client "+cList.get(iLast).getNom()+" "+cList.get(iLast).getPrenom()+" a acheté :");
cList.get(iLast).PrintCMedList();
}
PrintWriter writer = new PrintWriter(nFile+".txt", "UTF-8");
for(int iWriteCPos = 0; iWriteCPos < cList.size();iWriteCPos++){
for(int iWriteMedPos = 0; iWriteMedPos < cList.get(iWriteCPos).cMedList.size();iWriteMedPos++){
writer.println(cList.get(iWriteCPos).toStringCMed(iWriteMedPos));
}
}
System.out.println("Liste des ventes sauvegardée sous '"+nFile+".txt'.");
System.out.println("Voulez-vous aussi sauvegarder la liste des clients?(o/n)");
String saveC = scan.nextLine().toLowerCase();
while(saveC.charAt(0) != 'o' && saveC.charAt(0) != 'n'){
System.out.println("Veuillez répondre par 'o' ou 'n'.");
saveC = scan.nextLine().toLowerCase();
}
if(saveC.charAt(0) == 'o'){
writer.println("================");
writer.println("Liste des clients : ");
for(int cWriter = 0;cWriter < cList.size();cWriter++){
writer.println(cList.get(cWriter).toString());
}
//number of entries save.
ArrayList<Integer> nSave = new ArrayList<Integer>();
nSave.add(cList.size());
try {
fnSave.delete();
fnSave.createNewFile();
} catch (IOException e) {
System.out.println("nSave Error.");
}
database.DBMethods.toDBInteger(nSave, "nSave.txt");
FileReader frnSave = new FileReader(fnSave);
BufferedReader brnSave = new BufferedReader(frnSave);
int nSaveInt=0;
//TODO RESOUDRE LE PROBLEME ICI. (N'ECRIS PAS AVEC LA METHODE IMPLIQUANT nSave.)
try {
nSaveInt = Integer.parseInt(brnSave.readLine().trim());
brnSave.close();
} catch (NumberFormatException e) {
System.out.println("nSaveInt Error.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("nSaveIntError");
e.printStackTrace();
}
//name arraylist
ArrayList<String> cNames = new ArrayList<String>();
for(int i=0; i< cList.size();i++){
cNames.add(cList.get(i).getNom());
}
//surname arraylist
ArrayList<String> cSurnames = new ArrayList<String>();
for(int i=0; i< cList.size(); i++){
cSurnames.add(cList.get(i).getPrenom());
}
//town Arraylist
ArrayList<String> cTown = new ArrayList<String>();
for(int i=0; i< cList.size(); i++){
cTown.add(cList.get(i).getVille());
}
//Age ArrayList
ArrayList<Integer> cAge = new ArrayList<Integer>();
for(int i=0; i< cList.size(); i++){
cAge.add(cList.get(i).getAge());
}
//Create save files
database.DBMethods.toDBString(cNames, "Names.txt", nSaveInt);
database.DBMethods.toDBString(cNames, "Names.txt");
database.DBMethods.toDBString(cSurnames, "Surnames.txt", nSaveInt);
database.DBMethods.toDBString(cTown, "Towns.txt", nSaveInt);
database.DBMethods.toDBInteger(cAge, "Ages.txt", nSaveInt);
System.out.println("Liste des clients sauvegardée.");
}
writer.close();
scan.close();
System.out.println("Fin de la session.");
System.out.println("END.");
}
public static boolean intervallContains(int low, int high, int n) {
return n >= low && n <= high;
}
public static int lireNombre(Scanner scan, String phrase){ //Demande un nombre, le lis et recommence jusqu'à ce que nomre soit un entier valable.
System.out.println(phrase);
int output = 0;
boolean b = true;
while(b == true){
try{
output = scan.nextInt();
b = false;
} catch(InputMismatchException e){
System.out.println("Veuillez entrer un nombre entier.");
scan.nextLine();
}
}
return output;
}
} |
Partager