このコードの約90%は終了しましたが、最後の部分にはまっています。ですから、私は、MainClassからOrdersProcessedクラスへpartNumber、quantity、およびtoalCostを格納して、出力ファイルを作成したいと思っています。それをどうやって行うのですか?そのチュートリアルは自分のコードで投稿したのですか?出力ファイルをJavaで作成する
MainClass:
import java.util.Scanner;
public class MainClass
{
private static final String DATA_FILE = "masterInventory.dat";
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
InventoryData myData;
double price = 0.0;
int quantity = 0;
double totalCost = 0.0;
int index = 0;
int partNumber = 0;
char runProgram;
int totalFound;
int partFound = 0;
int partnotFound = 0;
myData = new InventoryData(DATA_FILE);
myData.loadArrays();
myData.bubbleSort();
System.out.println("Do you want to continue? prss 'Y' for yes or 'Q' to quit");
runProgram = input.nextLine().charAt(0);
runProgram = Character.toUpperCase(runProgram);
while (runProgram == 'Y' || runProgram == 'Q')
{
if(runProgram == 'Y')
{
System.out.print("Please enter your part number: ");
partNumber = input.nextInt();
// TODO start your loop here
index = myData.binSearch(partNumber);
if(index != -1)
{
price = myData.getPrice(index);
System.out.printf("The price is %.2f%n", price);
System.out.print("How many would you like?");
quantity = input.nextInt();
totalCost = price * quantity;
partFound++;
// TODO do printing, writing to file here
}
else
{
System.out.println("Part not found");
partnotFound++;
}
System.out.println("Part# ");
System.out.print(partNumber);
System.out.println();
System.out.println("Price $");
System.out.print(price);
System.out.println();
System.out.println("Quantity ");
System.out.print(quantity);
System.out.println();
System.out.println("totalPrice ");
System.out.print(totalCost);
System.out.println();
System.out.println("Do you want to continue? prss 'Y' for yes or 'Q' to quit");
runProgram = input.next().charAt(0);
runProgram = Character.toUpperCase(runProgram);
}
if (runProgram == 'Q')
{
totalFound = partFound + partnotFound;
System.out.println("Total parts found : " + partFound);
System.out.println("Total parts not found : " + partnotFound);
System.out.println("Total parts searched : " + totalFound);
break;
}// end if 'Q'
}// end while
}
}
OrderProcessedクラス:
public class OrdersProcessed
{
public OrdersProcessed(String File)
{
File = "ordersProcessed.dat";
}
public void saveOneRecord()
{
}
次に、ここで私が見つけたチュートリアルです:
public static void main(String[] args)
{
String lastName, firstName;
double salary;
try
{
//Instantiate a PrintWriter to append to an existing file
PrintWriter myPW = new PrintWriter (new FileWriter("employeeOut.dat", true));
Scanner myScanner = new Scanner (System.in);
//Input first value for lastName, which will be the LCV
System.out.println("Input the employee's last name (or Q to quit): ");
lastName = myScanner.nextLine();
while (!(lastName.equals("Q") || lastName.equals("q")))
{
System.out.println("Input the employee's first name: ");
firstName = myScanner.nextLine();
System.out.println("Input the employee's salary: ");
salary = myScanner.nextDouble();
myScanner.nextLine(); //Eliminate carriage return left in stream after numeric input
//Save one record
myPW.printf ("%s %s %.2f\n", lastName, firstName, salary);
//Input next last name
System.out.println("Input the employee's last name (or Q to quit): ");
lastName = myScanner.nextLine();
}//END while
myPW.close();
myScanner.close();
}//END try
catch (IOException ex)
{
ex.printStackTrace();
}
}//END main method
OrdersProcessedクラスをシリアル化しますか?下記の私の更新された答えを確認してください。 – Gustavo