In: Computer Science
CSC
MUST BE IN JAVA
Design a program as per the following information using the accompanying data file.
Here is the datafile: sodaSalesB.dat
Make sure the program compiles and that you include pseudo code. The assignment
is worth 100 points. (Lack of pseudo code will cost you 15pts)
You have been asked to produce the report below. This report will tell the company how sales are going. You will read the file named sodasales.dat The values are the brand name, number sold, the cost per bottle, the retail price.
Soda Sales
Bottles Cost Retail Profit
Soda Sold Bottle Bottle Brand
----- ---- ----- ------- -------
You will produce a summary line at the end of the report. The
summary section should display the average cost, the average price,
the highest and the lowest priced soda. You will also display total
profit.
You will give the user the choice of either sending the report to the screen, file, or both.
Remember profit is the profit per bottle multiplied by the number of bottles sold.
I have given you two files the A file has a stock ID (int) for the brand the second uses a name (String).
INFORMATION FOR ABOVE PROGRAM:
Coke 34 3.00 5.00
Sprite 18 2.89 4.30
Fanta Mango 21 3.35 5.99
Orange Crush 35 4.00 6.00
Browns Cream Soda 43 4.25 5.75
package file;
import java.io.*;
import java.util.Scanner;
// Defines class SodaSales
public class SodaSales
{
// Instance variables to store product
information
String productName[];
int quantity[];
double cost[];
double retailCost[];
double profit[];
// To store number of products
int numberOfProduct;
// To store calculated result
double averageCost;
double averagePrice;
double highestPricedSoda;
double lowestPricedSoda;
double totalProfit;
// Method to read the file contents and stores it
in
// respective array
void readProduct()
{
// Initializes number of products
to 0
numberOfProduct = 0;
// Scanner class object
declared
Scanner readF = null;
// try block begins
try
{
// Opens the
file for reading
readF = new
Scanner(new File("sodaSalesB.dat"));
// Loops till
end of the file to count number of records
while(readF.hasNextLine())
{
// Reads a record
readF.nextLine();
// Increase the counter by one
numberOfProduct++;
}// End of while
loop
// Closer the
file
readF.close();
// Allocates
memory to array of size number of products
// available in
the file
productName =
new String[numberOfProduct];
quantity = new
int[numberOfProduct];
cost = new
double[numberOfProduct];
retailCost = new
double[numberOfProduct];
profit = new
double[numberOfProduct];
// Re -
initializes number of products to 0
numberOfProduct
= 0;
// Re - Opens
the file for reading
readF = new
Scanner(new File("sodaSalesB.dat"));
// Loops till
end of the file to read records
while(readF.hasNextLine())
{
// Reads a record
String record = readF.nextLine();
// To store the position of the digit
int pos = 0;
// Loops till end of the record
for(int c = 0; c < record.length();
c++)
{
// Checks if current
character is a digit
if(Character.isDigit(record.charAt(c)))
{
// Stores
the position
pos =
c;
// Come
out of the loop
break;
}// End of if condition
}// End of for loop
// Extracts substring from record from 0th index
position
// to found digit position minus one (for space)
as product name
// Stores the product name at numberOfProduct
index position
productName[numberOfProduct] =
record.substring(0, pos-1);
// Extracts substring from found digit position
to end of record
record = record.substring(pos);
// Splits the record with space
String each[] = record.split(" ");
// Stores quantity after converting to integer
at numberOfProduct index position
quantity[numberOfProduct] =
Integer.parseInt(each[0]);
// Stores cost after converting to double at
numberOfProduct index position
cost[numberOfProduct] =
Double.parseDouble(each[1]);
// Stores retail cost after converting to double
at numberOfProduct index position
retailCost[numberOfProduct] =
Double.parseDouble(each[2]);
// Increase the record counter by one
numberOfProduct++;
}// End of while
loop
}// End of try block
// Catch block to handle file not
found exception
catch(FileNotFoundException
fe)
{
System.out.println("\n ERROR: Unable to open the file for
reading.");
}// End of catch block
// Closer the file
readF.close();
}// End of method
// Method write product information to file
void writeProducts()
{
// File class object created
File file = new File
("sodaReport.txt");
// PrintWriter object initialized
to null
PrintWriter pw = null;
// Try block begins
try
{
// PrintWriter
object created to write onto the file name
// specified
with file object
pw = new
PrintWriter (file);
// Writes
heading
pw.printf("%n
%40s", "Soda Sales");
pw.printf("%n
%26s %8s %10s %8s",
"Bottles", "Cost", "Retail",
"Profit");
pw.printf("%n
%-18s %-11s %-8s %-8s %-8s",
" Soda", "Sold", "Bottle", "Bottle",
"Brand");
pw.printf("%n
%-18s %-11s %-8s %-8s %-8s%n",
" -----", "----", "-----", "-------",
"-------");
// Loops till
number of products
for(int c = 0; c
< numberOfProduct; c++)
// Writes current record data
pw.printf(" %-18s %3d %11.2f %8.2f
%9.2f%n",
productName[c], quantity[c],
cost[c], retailCost[c], profit[c]);
// Writes
report
pw.printf("%n
Average Cost: $ %.2f", averageCost);
pw.printf("%n
Average Price: $ %.2f", averagePrice);
pw.printf("%n
Highest Priced Soda: $ %.2f", highestPricedSoda);
pw.printf("%n
Lowest Priced Soda: $ %.2f", lowestPricedSoda);
pw.printf("%n
Total Profit: $ %.2f", totalProfit);
System.out.println("\n " + numberOfProduct + " product report "
+
"written
successfully.");
// For new
line
pw.println();
}// End of try block
// Catch to handle file not found
exception
catch (FileNotFoundException
e)
{
System.out.println("\n Error: Unable to open the file for
writing!");
}//End of catch
//Close printWriter
pw.close();
}//End of method
// Method to perform calculation
void calculate()
{
double totalCost = 0;
double totalPrice = 0;
// Initially stores the first
record cost as highest and lowest
highestPricedSoda = cost[0];
lowestPricedSoda = cost[0];
// Loops till number of
products
for(int c = 0; c <
numberOfProduct; c++)
{
// Calculates
profit
profit[c] =
(retailCost[c] - cost[c]) * quantity[c];
// Calculates
total profit
totalProfit +=
profit[c];
// Calculates
total cost
totalCost +=
cost[c];
// Calculates
total retail price
totalPrice +=
retailCost[c];
// Checks if
current retail cost is greater than earlier highest price
soda
if(retailCost[c]
> highestPricedSoda)
// Assigns the current retail cost as
highest
highestPricedSoda = retailCost[c];
// Checks if
current retail cost is less than earlier lowest price soda
if(retailCost[c]
< lowestPricedSoda)
// Assigns the current retail cost as
lowest
lowestPricedSoda = retailCost[c];
}// End of for loop
// Calculates average cost
averageCost = totalCost /
numberOfProduct;
// Calculates average retail
price
averagePrice = totalPrice /
numberOfProduct;
}// End of method
// Method to either display the report on the screen
or
// writes report to file based on the parameter
choice
void show(int option)
{
// Checks if option is 1 then
display the report
if(option == 1)
{
// Displays
heading
System.out.printf("\n %40s", "Soda Sales");
System.out.printf("\n %26s %8s %10s %8s",
"Bottles", "Cost", "Retail",
"Profit");
System.out.printf("\n %-18s %-11s %-8s %-8s %-8s",
" Soda", "Sold", "Bottle", "Bottle",
"Brand");
System.out.printf("\n %-18s %-11s %-8s %-8s %-8s\n",
" -----", "----", "-----", "-------",
"-------");
// Loops till
number of products
for(int c = 0; c
< numberOfProduct; c++)
// Writes current record data
System.out.printf(" %-18s %3d %11.2f %8.2f
%9.2f\n",
productName[c], quantity[c],
cost[c], retailCost[c], profit[c]);
// Displays
report
System.out.printf("\n Average Cost: $ %.2f", averageCost);
System.out.printf("\n Average Price: $ %.2f", averagePrice);
System.out.printf("\n Highest Priced Soda: $ %.2f",
highestPricedSoda);
System.out.printf("\n Lowest Priced Soda: $ %.2f",
lowestPricedSoda);
System.out.printf("\n Total Profit: $ %.2f",
totalProfit);
}// End of if condition
// Otherwise checks if option is
2
else if(option == 2)
// Calls the
method to write report
writeProducts();
// Otherwise displays error
message
else
System.out.println("\n ERROR: Invalid Choice!!");
}// End of method
// main method definition
public static void main(String s[])
{
// Scanner class object created to
accept data from console
Scanner sc = new
Scanner(System.in);
// SodaSales class object
created
SodaSales ss = new
SodaSales();
// Calls the method to read data
from file
ss.readProduct();
// Calls the method to perform
calculation
ss.calculate();
// Displays option
System.out.println("\n 1 - Display
Product Report " +
"\n 2 - Write Product Report.");
// Accepts user choice
int ch = sc.nextInt();
// Checks if choice is one
if(ch == 1)
// Calls the
method by passing 1 to display data
ss.show(1);
// Otherwise calls the method by
passing 2 to write data
else
ss.show(2);
// Closes the scanner
sc.close();
}// End of main method
}// End of class
sodaSalesB.dat file contents
Coke 34 3.00 5.00
Sprite 18 2.89 4.30
Fanta Mango 21 3.35 5.99
Orange Crush 35 4.00 6.00
Browns Cream Soda 43 4.25 5.75
Sample Output: