AnswerNote programming specifications and the printing statement.pdf
1. Answer:
Note: programming specifications and the printing statements are provided in the question itself
Program code:
import java.util.*;
import java.io.*;
//Implementing Projecto2 class
public class Project02
{
//begins main program for the class
public static void main(String[] args)
{
//creating the Scanner object
Scanner keyboards = new Scanner(System.in);
//getting file name as input
System.out.print("Enter an inventory filename: ");
String fnames = keyboards.nextLine();
//Implementing the product array list
ArrayList products1 = loadProductsS(fnames);
//calling the below functions
generateSummaryReportGen(products1);
highestAvgRatingCalc(products1);
lowestAvgRating(products1);
largestTotalDollarAmount(products1);
smallestTotalDollarAmount(products1);
}
//function definition for the class generateSummaryReportGen
public static void generateSummaryReportGen (ArrayList Products11)
{
int counters = 0;
System.out.println("Product Inventory Summary Report");
System.out.println("----------------------------------------------------------------------------------");
System.out.println();
System.out.printf("%-33s%-10s%-6s%-7s%-7s%-7s%-7s", "Product Name", "I Code",
"Type", "Rating", "# Rat.", "Quant.", "Price");
System.out.println();
2. System.out.printf("%-33s%-10s%-6s%-7s%-7s%-7s%-7s", "-------------------------------", "----
-----", "----", "------", "------", "------", "------");
System.out.println();
while(counters < Products11.size())
{
System.out.printf("%-33s%-10s%-6s%-7s%6s%7s%7s", Products11.get(counters).getName(),
Products11.get(counters).getInventoryCode(), Products11.get(counters).getType(),
Products11.get(counters).getAvgUserRating(), Products11.get(counters).getUserRatingCount(),
Products11.get(counters).getQuantity(), Products11.get(counters).getPrice());
System.out.println();
counters++;
}
System.out.println("----------------------------------------------------------------------------------");
System.out.println("Total products in the database: " + Products11.size());
}
//function definition for the class loadProductsS
public static ArrayList loadProductsS(String fnames)
{
//declares variables
int a = 0;
Integer b = 0;
//creates array list objects
ArrayList products11 = new ArrayList();
try
{
Scanner inFiles = new Scanner(new File(fnames));
while (inFiles.hasNext())
{
int counters = 0;
String name = inFiles.nextLine();
String code = inFiles.nextLine();
int quantity = inFiles.nextInt();
double price = inFiles.nextDouble();
String type = inFiles.next();
Product productObjectS = new Product(name, code, quantity, price, type);
while(inFiles.hasNextInt() && counters==0)
3. {
a = inFiles.nextInt();
if(a != -1)
{
b = new Integer(a);
productObjectS.addUserRating(b);
}
else
{
counters = 1;
}
}
products11.add(productObjectS);
if(inFiles.hasNext())
{
inFiles.nextLine();
}
}
inFiles.close();
}
catch (FileNotFoundException e) {
System.out.println("ERROR: " + e);
}
return products11;
}
//function definition for the class void highestAvgRatingCalc
public static void highestAvgRatingCalc(ArrayList products11)
{
//declares variables
int counters = 0;
int a = 1;
//checks the counters product size
while (counters <= products11.size()-1)
{
if(products11.get(counters).getAvgUserRating().length() >
4. products11.get(a).getAvgUserRating().length())
{
a = counters;
}
else
{
}
counters++;
}
System.out.println("Highest Average User Rating In Stock: " + products11.get(a).getName() +
" ("+products11.get(a).getAvgUserRating() + ")");
}
//function definiton for the lowestAvgRating()
public static void lowestAvgRating(ArrayList products11)
{
//declares variables
int counters = 0;
int a = 1;
while (counters <= products11.size()-1)
{
if(products11.get(counters).getAvgUserRating().length() products11)
{
int counters = 0;
int a = 1;
while (counters <= products11.size()-1){
if((products11.get(counters).getPrice())*(products11.get(counters).getQuantity()) >
((products11.get(a).getPrice())*(products11.get(a).getQuantity())))
{
a=counters;
}
else{
}
counters++;
}
System.out.println("Item With The Largest Total Dollar Amount In Inventory: " +
5. products11.get(a).getName() + " ($" +
((products11.get(a).getPrice())*(products11.get(a).getQuantity())) + ")");
}
//function definition for smallestTotalDollarAmount
public static void smallestTotalDollarAmount(ArrayList products11)
{
int counters = 0;
int a = 1;
while (counters <= products11.size()-1)
{
if((products11.get(counters).getPrice())*(products11.get(counters).getQuantity()) <
((products11.get(a).getPrice())*(products11.get(a).getQuantity())))
{
a=counters;
}
else
{
}
counters++;
}
System.out.println("Item With The Smallest Total Dollar Amount In Inventory: " +
products11.get(a).getName() + " ($" +
((products11.get(a).getPrice())*(products11.get(a).getQuantity())) + ")");
}
}
Sample Output:
C:jdk1.6bin>javac Product.java
C:jdk1.6bin>javac Project02.java
C:jdk1.6bin>java Project02
Enter an inventory filename: input.txt
Product Inventory Summary Report
--------------------------------------------------------------------------------
--
Product Name I Code Type Rating # Rat. Quant. Price
------------------------------- --------- ---- ------ ------ ------ ------
The Shawshank Redemption C0000001 DVD *** 4 100 19.95
6. The Dark Knight C0000003 DVD *** 3 50 19.95
Casablanca C0000007 DVD **** 4 137 9.95
The Girl With The Dragon Tattoo C0000015 Book *** 3 150 14.95
Vertigo C0000023 DVD **** 6 55 9.95
A Game of Thrones C0000019 Book 0 100 8.95
--------------------------------------------------------------------------------
--
Total products in the database: 6
Highest Average User Rating In Stock: Casablanca (****)
Lowest Average User Rating In Stock: A Game of Thrones ()
Item With The Largest Total Dollar Amount In Inventory: The Girl With The Dragon
Tattoo ($2242.5)
Item With The Smallest Total Dollar Amount In Inventory: Vertigo ($547.25)
C:jdk1.6bin>javac Project02.java
Solution
Answer:
Note: programming specifications and the printing statements are provided in the question itself
Program code:
import java.util.*;
import java.io.*;
//Implementing Projecto2 class
public class Project02
{
//begins main program for the class
public static void main(String[] args)
{
//creating the Scanner object
Scanner keyboards = new Scanner(System.in);
//getting file name as input
System.out.print("Enter an inventory filename: ");
String fnames = keyboards.nextLine();
//Implementing the product array list
ArrayList products1 = loadProductsS(fnames);
//calling the below functions
7. generateSummaryReportGen(products1);
highestAvgRatingCalc(products1);
lowestAvgRating(products1);
largestTotalDollarAmount(products1);
smallestTotalDollarAmount(products1);
}
//function definition for the class generateSummaryReportGen
public static void generateSummaryReportGen (ArrayList Products11)
{
int counters = 0;
System.out.println("Product Inventory Summary Report");
System.out.println("----------------------------------------------------------------------------------");
System.out.println();
System.out.printf("%-33s%-10s%-6s%-7s%-7s%-7s%-7s", "Product Name", "I Code",
"Type", "Rating", "# Rat.", "Quant.", "Price");
System.out.println();
System.out.printf("%-33s%-10s%-6s%-7s%-7s%-7s%-7s", "-------------------------------", "----
-----", "----", "------", "------", "------", "------");
System.out.println();
while(counters < Products11.size())
{
System.out.printf("%-33s%-10s%-6s%-7s%6s%7s%7s", Products11.get(counters).getName(),
Products11.get(counters).getInventoryCode(), Products11.get(counters).getType(),
Products11.get(counters).getAvgUserRating(), Products11.get(counters).getUserRatingCount(),
Products11.get(counters).getQuantity(), Products11.get(counters).getPrice());
System.out.println();
counters++;
}
System.out.println("----------------------------------------------------------------------------------");
System.out.println("Total products in the database: " + Products11.size());
}
//function definition for the class loadProductsS
public static ArrayList loadProductsS(String fnames)
{
//declares variables
int a = 0;
8. Integer b = 0;
//creates array list objects
ArrayList products11 = new ArrayList();
try
{
Scanner inFiles = new Scanner(new File(fnames));
while (inFiles.hasNext())
{
int counters = 0;
String name = inFiles.nextLine();
String code = inFiles.nextLine();
int quantity = inFiles.nextInt();
double price = inFiles.nextDouble();
String type = inFiles.next();
Product productObjectS = new Product(name, code, quantity, price, type);
while(inFiles.hasNextInt() && counters==0)
{
a = inFiles.nextInt();
if(a != -1)
{
b = new Integer(a);
productObjectS.addUserRating(b);
}
else
{
counters = 1;
}
}
products11.add(productObjectS);
if(inFiles.hasNext())
{
inFiles.nextLine();
}
}
inFiles.close();
}
9. catch (FileNotFoundException e) {
System.out.println("ERROR: " + e);
}
return products11;
}
//function definition for the class void highestAvgRatingCalc
public static void highestAvgRatingCalc(ArrayList products11)
{
//declares variables
int counters = 0;
int a = 1;
//checks the counters product size
while (counters <= products11.size()-1)
{
if(products11.get(counters).getAvgUserRating().length() >
products11.get(a).getAvgUserRating().length())
{
a = counters;
}
else
{
}
counters++;
}
System.out.println("Highest Average User Rating In Stock: " + products11.get(a).getName() +
" ("+products11.get(a).getAvgUserRating() + ")");
}
//function definiton for the lowestAvgRating()
public static void lowestAvgRating(ArrayList products11)
{
//declares variables
int counters = 0;
int a = 1;
while (counters <= products11.size()-1)
{
10. if(products11.get(counters).getAvgUserRating().length() products11)
{
int counters = 0;
int a = 1;
while (counters <= products11.size()-1){
if((products11.get(counters).getPrice())*(products11.get(counters).getQuantity()) >
((products11.get(a).getPrice())*(products11.get(a).getQuantity())))
{
a=counters;
}
else{
}
counters++;
}
System.out.println("Item With The Largest Total Dollar Amount In Inventory: " +
products11.get(a).getName() + " ($" +
((products11.get(a).getPrice())*(products11.get(a).getQuantity())) + ")");
}
//function definition for smallestTotalDollarAmount
public static void smallestTotalDollarAmount(ArrayList products11)
{
int counters = 0;
int a = 1;
while (counters <= products11.size()-1)
{
if((products11.get(counters).getPrice())*(products11.get(counters).getQuantity()) <
((products11.get(a).getPrice())*(products11.get(a).getQuantity())))
{
a=counters;
}
else
{
}
counters++;
}
11. System.out.println("Item With The Smallest Total Dollar Amount In Inventory: " +
products11.get(a).getName() + " ($" +
((products11.get(a).getPrice())*(products11.get(a).getQuantity())) + ")");
}
}
Sample Output:
C:jdk1.6bin>javac Product.java
C:jdk1.6bin>javac Project02.java
C:jdk1.6bin>java Project02
Enter an inventory filename: input.txt
Product Inventory Summary Report
--------------------------------------------------------------------------------
--
Product Name I Code Type Rating # Rat. Quant. Price
------------------------------- --------- ---- ------ ------ ------ ------
The Shawshank Redemption C0000001 DVD *** 4 100 19.95
The Dark Knight C0000003 DVD *** 3 50 19.95
Casablanca C0000007 DVD **** 4 137 9.95
The Girl With The Dragon Tattoo C0000015 Book *** 3 150 14.95
Vertigo C0000023 DVD **** 6 55 9.95
A Game of Thrones C0000019 Book 0 100 8.95
--------------------------------------------------------------------------------
--
Total products in the database: 6
Highest Average User Rating In Stock: Casablanca (****)
Lowest Average User Rating In Stock: A Game of Thrones ()
Item With The Largest Total Dollar Amount In Inventory: The Girl With The Dragon
Tattoo ($2242.5)
Item With The Smallest Total Dollar Amount In Inventory: Vertigo ($547.25)
C:jdk1.6bin>javac Project02.java