Question

In: Computer Science

CSC MUST BE IN JAVA Design a program as per the following information using the accompanying...

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


The data file is added below already

Solutions

Expert Solution

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:


Related Solutions

Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
Develop an Algorithm and java program using Design Recipe for the following problems. Draw a flowchart...
Develop an Algorithm and java program using Design Recipe for the following problems. Draw a flowchart to compute the largest and smallest of 4 numbers : Write a Java Program for the above flowchart, use methods(functions), and nested if-else statements. Take input from the user using the Scanner object. Use separate methods to compute the Largest and Smallest of the numbers. Method 1 Name: findLargest(param 1, param 2, param 3, param 4) Method 2 Name: findSmallest(param 1, param 2, param...
Write a java program using the information given Design a class named Pet, which should have...
Write a java program using the information given Design a class named Pet, which should have the following fields (i.e. instance variables):  name - The name field holds the name of a pet (a String type)  type - The type field holds the type of animal that a pet is (a String type). Example values are “Dog”, “Cat”, and “Bird”.  age - The age field holds the pet’s age (an int type) Include accessor methods (i.e. get...
Create a program in java using the following information: Trainers at Tom's Athletic Club are encouraged...
Create a program in java using the following information: Trainers at Tom's Athletic Club are encouraged to enroll new members. Write an application that extracts the names of Trainers and groups them based on the number of new members each trainer has enrolled this year. Output is the number of trainers who have enrolled 0 to 5 members, 6 to 12 members, 13 to 20 members, and more than 20 members. Give their names as well as the number of...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads in the principle 2) reads in additional money deposited each year (treat this as a constant) 3) reads in years to grow, and 4) reads in interest rate And then finally prints out how much money they would have each year. See below for formatting. Enter the principle: XX Enter the annual addition: XX Enter the number of years to grow: XX Enter the...
Design a program in Java to display the following: Car Design Enter the car model's year:...
Design a program in Java to display the following: Car Design Enter the car model's year: 1957 Enter the car's make: Chevy The model year is 1957 The make is Chevy The speed is 0 Let's see what it can do!! The speed is,... 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 STOP! STOP! Let me OUT! The...
Design a program in Java to display the following: Car Design Enter the car model's year:...
Design a program in Java to display the following: Car Design Enter the car model's year: 1957 Enter the car's make: Chevy The model year is 1957 The make is Chevy The speed is 0 Let's see what it can do!! The speed is,... 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 STOP! STOP! Let me OUT! The...
Design a program that displays the following in Java: Enter the grades for an Essay: Grammer...
Design a program that displays the following in Java: Enter the grades for an Essay: Grammer (must be 30 or less): 40 Grammer (must be 30 or less): 26.3 Spelling (must be 20 or less): 17.4 Correct Length (must be 20 or less): 19 Content (must be 30 or less): 23.5 The recorded scores are: Grammer: 26.3 Spelling: 17.4 Correct Length: 19.0 Content: 23.5 Total score: 86.2 The grade for this essay is B
Write a program in Java Using object Orientation Design to determine the status of Mini Van...
Write a program in Java Using object Orientation Design to determine the status of Mini Van Doors. A logical circuit receives a different binary code to allow opening different doors. The doors can be opened by a dashboard switch, inside or outside handle. The inside handle will not open the door if the child safety lock is on or the master lock is on. The gear shift must be in the park to open the door. A method must be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT