Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project Summary You own a bakery and want to track sales of your products. Write

ID: 3714761 • Letter: P

Question

Project Summary

You own a bakery and want to track sales of your products. Write a program that tracks the number of each product you sell.

Instructions:

In this project you will work with parallel arrays. One array will hold the product names as Strings; the second array will hold the number of sales for each product as ints. Thus, if the first element in the products array is “Lemon bar”, the first element in the sales array will be the number of lemon bars sold, and so on.

1.     First, define an array of your 15 product names. You can use this code:

String [] products = { "Lemon bar", "Snickerdoodle", "Angel cake",
                       "Cupcake", "Cherry pie", "Eclair",
                       "Cream puff", "Roll", "Rye bread",
                       "Sticky bun", "Bagel", "Biscuit",
                       "Brownie", "Pumpkin pie", "Flatbread" };

2.     Then define an array of ints to hold sales for each of these products. This array should be the same length as the products array.

3.     The attached sales.txt file contains alternating lines with a product name followed on the next line by the number of sales for that product. (This is similar to the movies.txt file with the titles and running times.) Read the file; for each product name, search for its name in the products array. Then using that index, add the number of sales to the corresponding element in the sales array. You can assume that all product names will be found.

4.     When you have finished reading the file, output each product name and the number of sales for that product.

5.     Output the product with the highest sales. (Hint: find the index of the maximum value in the sales array and use that index to access the product name in the products array).

Explain how you would change this program to track sports scores, for example, to keep track of the number of points each soccer player scores over the season?

Explanation / Answer

Hi,
here is the complete code with comments

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Arrays;

import java.util.Collections;

import java.util.Scanner;

public class Main {

public static void main(String[] args) throws IOException {

try {

String [] products = { "Lemon bar", "Snickerdoodle", "Angel cake",

"Cupcake", "Cherry pie", "Eclair",

"Cream puff", "Roll", "Rye bread",

"Sticky bun", "Bagel", "Biscuit",

"Brownie", "Pumpkin pie", "Flatbread" };//defining the products array

int[] sales= new int[15];

Scanner scanner = new Scanner(new File("/Users/Downloads/myfile.txt"));

while (scanner.hasNextLine()) {//looping till EOF

String product=scanner.nextLine();//reading product name first

if(Arrays.asList(products).contains(product))//if product is present

{

int index = Arrays.asList(products).indexOf(product);//getting index

int sale=scanner.nextInt();//getting sales of the product

sales[index]=sale;//setting the sales with index obtained

}

}

scanner.close();

for(int i=0;i<products.length;i++)

{

System.out.println("Product: "+products[i]+" Sales: "+sales[i]);//printing all sales of all products

}

int maxIndex = Arrays.asList(products).indexOf(Collections.max(Arrays.asList(products)));//getting max element index

System.out.println("Product with max sales is: "+products[maxIndex]);//printing

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}


Thumbs up if this was helpful, otherwise let me know in comments