我需要填充我的totalprices ArrayList,但不知道如何。基本上我需要把价格ArrayList和数量ArrayList乘以它们。然后取这些值并将它们添加到totalprices ArrayList中。然后找到总价的最小和最大值。我正在试图弄清楚这件事。请帮忙。谢谢!我需要帮助创建一个新的Arraylist从2个单独的Arraylists
我的代码:
import java.util.*;
import java.io.*;
public class Project01 {
public static void main(String[] args) {
ArrayList<String> titles = new ArrayList<String>();//Declare the array lists that will be used.
ArrayList<String> types = new ArrayList<String>();
ArrayList<Double> prices = new ArrayList<Double>();
ArrayList<Integer> quantities = new ArrayList<Integer>();
ArrayList<Double> totalprices = new ArrayList<Double>();
int count = 0;//Set the counter to zero.
Scanner in = new Scanner(System.in);//Establish the scanner so user input can be properly read.
String database = getFile(in);//Setting the file name variable from the method below that asks the user for the file's name.
try {
File file = new File(database);
Scanner inputFile = new Scanner(file);
System.out.println();
System.out.println("Product Summary Report");
System.out.println("------------------------------------------------------------");
while (inputFile.hasNextLine()) {
getTitle(titles, inputFile.nextLine());
getQuantity(quantities, inputFile.nextInt());
inputFile.nextLine();
getPrice(prices, inputFile.nextDouble());
inputFile.nextLine();
getType(types, inputFile.nextLine());
System.out.println("Title: " + titles.get(count));
System.out.println(" Product Type: " + types.get(count));
System.out.println(" Price: " + prices.get(count));
System.out.println(" Quantity: " + quantities.get(count));
System.out.println();
count++;
}
System.out.println("-----------------------------------------------------------------");
System.out.println("Total products in database: " + count);
Integer index = getLargestQuantityTitle(quantities);
System.out.println("Largest quantity item : " + titles.get(index) + " (" + types.get(index) + ")");
ArrayList<Double> highestTotalDollarAmount = getTotalprices(quantities, prices);
Double highestTotalDollarAmount = getHighestDollarAmount(totalprices);
System.out.println("Highest total dollar item: $" + highestTotalDollarAmount);
Integer index2 = getSmallestQuantityTitle(quantities);
System.out.println("Smallest quantity item: " + titles.get(index2) + " (" + types.get(index2) + ")");
System.out.println("Lowest total dollar item: ");
System.out.println("-----------------------------------------------------------------");
inputFile.close();
} catch (IOException e) {
System.out.println("There was a problem reading from " + database);
}
in.close();
}
private static String getFile(Scanner inScanner) {
System.out.print("Enter database filename: ");
String fileName = inScanner.nextLine();
return fileName;
}
private static void getTitle(ArrayList<String> titles, String title) { //This method is creating the array list of the titles from the input file.
titles.add(title);
}
private static void getType(ArrayList<String> types, String type) { //This method is creating the array list of the types from the input file.
types.add(type);
}
private static void getPrice(ArrayList<Double> prices, double price) { //This method is creating the array list of the prices from the input file.
prices.add(price);
}
private static void getQuantity(ArrayList<Integer> quantities, int quantity) { //This method is creating the array list of the quantities from the input file.
quantities.add(quantity);
}
private static Integer getLargestQuantityItem(ArrayList<Integer> quantities){ //This method is determining the maximum value within the quantities array list.
return Collections.max(quantities);
}
private static Double getHighestPricedItem(ArrayList<Double> prices){ //This method is determining the maximum price within the prices array list.
return Collections.max(prices);
}
private static Integer getHighestTotalDollarItem(ArrayList<Integer> prices){ //This method is determining the maximum total value, basically the highest quantity of the item multiplied by it's price.
return Collections.max(prices);
}
private static Integer getSmallestQuantityItem(ArrayList<Integer> quantities){ //This method is determining the minimum value within the quantities array list.
return Collections.min(quantities);
}
private static Integer getLargestQuantityTitle(ArrayList<Integer> quantities){
int index = 0;
Integer largestQuantityMainVariable = getLargestQuantityItem(quantities);
for (int i = 0; i < quantities.size(); i++) {
if (quantities.get(i) != null && quantities.get(i).equals(largestQuantityMainVariable)) {
index = i;
break;
}
}
return index;
}
private static Integer getSmallestQuantityTitle(ArrayList<Integer> quantities){
int index2 = 0;
Integer smallestQuantityMainVariable = getSmallestQuantityItem(quantities);
for (int i = 0; i < quantities.size(); i++) {
if (quantities.get(i) != null && quantities.get(i).equals(smallestQuantityMainVariable)) {
index2 = i;
break;
}
}
return index2;
}
private static ArrayList<Double> getTotalprices (List<Integer> quantities, List<Double> prices){
ArrayList<Double> totalprices = new ArrayList<Double>();
for (int i = 0; i < quantities.size(); i++) {
totalprices.add(quantities.get(i) * prices.get(i));
}
return totalprices;
}
private static Double getHighestDollarAmount(ArrayList<Double> totalprices){ //This method is determining the maximum price within the prices array list.
return Collections.max(totalprices);
}
}
输出应该是这样的:
Enter database filename: proj1_input.txt
Product Summary Report
------------------------------------------------------------
Title: The Shawshank Redemption
Product Type: DVD
Price: 19.95
Quantity: 100
Title: The Dark Knight
Product Type: DVD
Price: 19.95
Quantity: 50
Title: Casablanca
Product Type: DVD
Price: 9.95
Quantity: 137
Title: The Girl With The Dragon Tattoo
Product Type: Book
Price: 14.95
Quantity: 150
Title: Vertigo
Product Type: DVD
Price: 9.95
Quantity: 55
Title: A Game of Thrones
Product Type: Book
Price: 8.95
Quantity: 100
-----------------------------------------------------------------
Total products in database: 6
Largest quantity item: The Girl With The Dragon Tattoo (Book)
Highest total dollar item: $[1995.0, 997.5, 1363.1499999999999, 2242.5, 547.25, 894.9999999999999]
Smallest quantity item: The Dark Knight (DVD)
Lowest total dollar item: Vertigo ($547.25)
-----------------------------------------------------------------
输入文件(.txt文件):
The Shawshank Redemption
100
19.95
DVD
The Dark Knight
50
19.95
DVD
Casablanca
137
9.95
DVD
The Girl With The Dragon Tattoo
150
14.95
Book
Vertigo
55
9.95
DVD
A Game of Thrones
100
8.95
Book
我没有看到你调用'getTotalprices'最优雅的解决方案。 – rgettman 2015-01-21 00:08:41
@rgettman我不需要我不认为因为我打电话给getHighestDollarAmount。 – 2015-01-21 00:11:22
你应该修改你的代码实现。您应该有一个YourClass包含名称,类型,价格,数量和总价以及一个列表。 –
2015-01-21 00:13:53