J'ai un petit problème avec mon programme d'inventaire d'E/S simple. C'est un programme qui écrit la sortie que l'utilisateur a saisie dans un fichier texte qui sert d'inventaire. J'étais capable de le faire. Toutefois, lorsque j'essayais de créer et d'ajouter un fichier texte différent sur le même dossier, il ne l'écrivait pas dans le nouveau fichier texte. Je sais que je manque vraiment d'expérience pour expliquer correctement mes préoccupations. Mais je fais de mon mieux, donc, fondamentalement, je voudrais juste imprimer les informations que j'ai entrées en tant que "ventes" dans le deuxième fichier texte sur le même dossier de chemin. J'espère que vous pouvez obtenir quelques idées si vous vérifiez et exécutez mon code. Le code fonctionne mais, comme je l'ai dit, le seul problème est l'entrée/sortie sur un fichier texte différent. J'apprécierais vraiment votre aide. Merci.Le deuxième programme d'écriture en mémoire tampon ne fonctionnera pas
Carl
Voici mon code:
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.*;
import static java.nio.file.StandardOpenOption.*;
import java.text.*;
import java.util.*;
public class Inventorypos {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
String File ="D:\\Copro2\\Inventory.txt";
String File2 ="D:\\Copro2\\Sales.txt";
Path file = Paths.get("D:\\Copro2\\Inventory.txt");
Path salefile = Paths.get("D:\\Copro2\\Sales.txt");
String st = " ";
String sH = " ";
String sF = " ";
String sS = " ";
String delimeter = "................ ";
int itemnum;
int itemname;
String H="Hamburger";
String F="French Fries";
String S="Softdrinks";
int menu;
long menu2 = 0;
final long QUIT = 5;{
{
try{
Runtime rt=Runtime.getRuntime();
OutputStream invent = new BufferedOutputStream(Files.newOutputStream(file, CREATE));
BufferedWriter writer = new
BufferedWriter(new OutputStreamWriter(invent));
//so here is my problem here, I couldn't make this work
OutputStream sale = new BufferedOutputStream(Files.newOutputStream(salefile, CREATE,StandardOpenOption.APPEND));
BufferedWriter writer2 = new
BufferedWriter(new OutputStreamWriter(sale));
String sa= "---- Sales ----";
writer2.write(sa);
writer2.newLine();
String in= "---- Inventory ----";
writer.write(in);
writer.newLine();
System.out.println("---- Page Menu ----");
System.out.println("1. Add Inventory \n2. Sell \n3. View Inventory \n4. View Sales \n5. Exit ");
System.out.print("Enter selection: ");
menu= input.nextInt();
while(menu2 != QUIT){
if(menu==1) {
System.out.println("Add Inventory: ");
System.out.print("1.) Hamburger \n2.)French Fries \n3.)Softdrinks\n");
System.out.println("Enter Item: ");
itemname= input.nextInt();
if (itemname == 1){
System.out.print("How many do you want to store? ");
itemnum = input.nextInt();
sH = H + delimeter + itemnum;
writer.write(sH, 0, sH.length());
writer.newLine();
}
else if (itemname == 2){
System.out.print("How many do you want to store? ");
itemnum = input.nextInt();
sF = F + delimeter + itemnum;
writer.write(sF, 0, sF.length());
writer.newLine();
}
else if (itemname == 3){
System.out.print("How many do you want to store? ");
itemnum = input.nextInt();
sS = S + delimeter + itemnum;
writer.write(sS, 0, sS.length());
writer.newLine();
}
else{
System.out.println("Invalid input!");
menu=1;
}
System.out.print("Enter 1 to store another item, 2 to view inventory, 3 sell,4 to quit: ");
menu2 = input.nextLong();
if (menu2==1){
menu=1;
}
else if (menu2==2){
menu=3;
}
else if (menu2==3){
menu=6;
}
else if (menu2==4){
menu2=QUIT;
}
else{
System.out.println("Invalid Input!");
}
}
if(menu==2){
System.out.print("---- Food Menu ----");
System.out.println("\n1.) Hamburger P25 \n2.) French Fries P25 \n3.) Softdrinks P25");
System.out.println("Enter your order: ");
int ord = input.nextInt();
if (ord==1){
System.out.println("How many?");
int hm = input.nextInt();
int totph = hm*25;
/*String intwo = "---- Updated inventory ----";
writer.write(intwo);
writer.newLine();
/*int left =
String nI = H+delimeter+*/
st = H+hm+ delimeter+ totph;
writer2.write(st,0,st.length());
writer2.newLine();
System.out.println("You purchased "+hm+" "+H+"s with the total price of "+totph+".");
}
else if (ord==2){
System.out.println("How many?");
int hm = input.nextInt();
int totph = hm*25;
/*String intwo = "---- Updated inventory ----";
writer.write(intwo);
writer.newLine();
/*int left =
String nI = H+delimeter+*/
st = F + hm + delimeter + totph;
writer2.write(st, 0, st.length());
writer2.newLine();
System.out.println("You purchased "+hm+" "+F+" with the total price of "+totph+".");
}
else if (ord==3){
System.out.println("How many?");
int hm = input.nextInt();
int totph = hm*25;
/*String intwo = "---- Updated inventory ----";
writer.write(intwo);
writer.newLine();
/*int left =
String nI = H+delimeter+*/
st = S+hm+ delimeter+ totph;
writer2.write(st,0,st.length());
writer2.newLine();
System.out.println("You purchased "+hm+" "+S+"s with the total price of "+totph+".");
}
else{
System.out.println("Invalid input!");
menu=2;
}
System.out.print("Enter 1 to go back to the food menu, 2 to view sales, 3 to go back to the main option or 4 to quit: ");
menu2 = input.nextInt();
if (menu2==1){
menu=2;
}
else if (menu2==2){
menu=4;
}else if (menu2==4){
menu2=QUIT;
}
else if (menu2==3){
menu=6;
}
else{
System.out.println("Invalid Input!");
}
}
if(menu==3){
{
menu2 = QUIT;
writer.close();
Process p=rt.exec("notepad "+File);
System.out.println("Do you want to continue? y for yes, n for no");
String con = input.next();
if (con.equalsIgnoreCase("y")){
main(null);
}
else if(con.equalsIgnoreCase("n")){
menu2=QUIT;
}
}
}
if (menu==4){
{
menu2 = QUIT;
writer.close();
Process p=rt.exec("notepad "+File2);
System.out.println("Do you want to continue? y for yes, n for no");
String con = input.next();
if (con.equalsIgnoreCase("y")){
main(null);
}
else if(con.equalsIgnoreCase("n")){
menu2=QUIT;
}
}
}
if (menu==5){
System.exit(0);
}
else if (menu ==6){
main(null);
}
else{
System.out.println("Invalid Input!");
}
}
}catch (Exception e){
System.out.println("Message: " +e);
}
}
}
}
}
Side note: Vous devriez vraiment diviser ce code en plusieurs fonctions distinctes. Cela rendrait beaucoup plus simple la compréhension du code et en parlerait. – Hulk
Side note2: Vous ne devez pas déclarer toutes ces variables sur la portée la plus externe. Vos variables 'st',' sH' etc. devraient être déclarées aussi proches que possible du point où vous les utilisez. – Hulk
Pour 292 lignes de code, un [MCVE] (http://stackoverflow.com/help/mcve) devrait probablement être conçu. – Linus