我有以下代码:
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Arrays;
public class UserInterface
{
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(System.in); // for user input
Scanner filein = new Scanner(new FileReader("products.txt")); // file of product information
final int maxProducts = 10; // change this for maximum number of products in the catalog
Product[] catalog = new Product[maxProducts]; // product information
int productCount; // number of products read in from the file
/*
* You will need to add additional arrays and variables here.
* The quantity array is given as an example:
*/
int[] quantity = new int[maxProducts];
double[] totalPrice = new double[maxProducts];
int[] large = new int[maxProducts];
int[] medium = new int[maxProducts];
int[] small = new int[maxProducts];
double[] shippingCost;
double[] discount;
// Read in the products from the file
productCount = 0;
while (filein.hasNext()) {
catalog[productCount] = new Product();
catalog[productCount].setName(filein.next());
catalog[productCount].setUPC(filein.nextInt());
catalog[productCount].setPrice(filein.nextDouble());
catalog[productCount].setLargeCapacity(filein.nextInt());
catalog[productCount].setMediumCapacity(filein.nextInt());
productCount++;
}
// zero out all the totals and amounts to start a new order
/*
* You will have to zero out all the quantity and total arrays and variables at the start
* of each new order, or else you will end up with incorrect amounts.
* The line below shows an easy way to fill an array with zeros, no matter how long it is:
*/
Arrays.fill(quantity, 0);
Arrays.fill(totalPrice, 0);
Arrays.fill(large, 0);
Arrays.fill(medium, 0);
Arrays.fill(small, 0);
///////////// THIS IS LINE 56 ///////////////
// Ask the user to enter a quantity for each product, calculate totals and shipping boxes
/*
* You will want to do this in a loop that goes through the array of products
* (remember that the array may not be full - so you may not need to go to the end).
* You should print out information about each product, then ask for a quantity for
* that product, and store it in the quantity array.
* Remember that you can call methods on the product objects even when they are in the
* array e.g. catalog[i].getName()
*/
for (int i = 0; i < catalog.length; i++){
if (catalog[i] != null){
System.out.println(catalog[i].getName());
}
}
// Print the invoice line for each product ordered
/*
* You will want to do this in a loop that goes through the array of products.
* Skip over products that were not ordered i.e. quantity is zero.
*/
// Print the totals at the bottom of the invoice
// Calculate the discounts and the final amounts, and print them
}
}
请转到上面代码中标记的第 56 行。这就是我正在研究的问题。我在下面有我的循环。它可以编译等,但我不确定这是否能满足问题的要求。
编辑:第 56 行开始像“要求用户输入每个产品的数量......”