package file;
import java.io.*;
import java.util.Scanner;
// Class Soda
class Soda
{
// Instance variables to store soda information
String itemName;
int bottlesSold;
double costBottle;
double retailBottle;
double profitBrand;
double percentSold;
// Parameterized constructor to assign parameter values to instance variables
Soda(String in, int bs, double cb, double rb)
{
itemName = in;
bottlesSold = bs;
costBottle = cb;
retailBottle = rb;
}// End of Parameterized constructor
// Method to calculate profit and percentage
void calculate()
{
profitBrand = (bottlesSold * (retailBottle - costBottle));
percentSold = (profitBrand * retailBottle) / 150.0;
}// End of method
}// End of class
// Driver class SodaSale definition
public class SodaSale
{
// Creates an array of object of class Soda
static Soda sd[] = new Soda[20];
// Counter to keep track of number of records
static int counter = 0;
// Method to read file contents and stores it in Soda class array of object
static void readFile()
{
// Scanner class object declared
Scanner readF = null;
// To store data read from file
String na;
int bs;
double cb, rb;
// try block begins
try
{
// Opens the file for reading
readF = new Scanner(new File("SodaSlaes.txt"));
// Loops till end of file
while(readF.hasNext())
{
// Reads item name
na = readF.nextLine();
// Reads bottle sole
bs = readF.nextInt();
// Reads product cost
cb = readF.nextDouble();
// Reads retail cost
rb = readF.nextDouble();
// Reads new line character
readF.nextLine();
// Creates an object of class Soda using parameterized constructor
// Assigns the object at counter index position
sd[counter] = new Soda(na, bs, cb, rb);
// Calls the method to calculate profit
sd[counter].calculate();
// Increase the counter by one
counter++;
}// End of while loop
}// End of try block
// Catch block to handle FileNotFoundException exception
catch(FileNotFoundException fe)
{
System.err.println("Unable to open the file fore reading.");
}// End of catch block
}// End of method
// Method to sort the records based on item name (ascending order)
static void sort()
{
// Loops till number of records
for(int x = 0; x < counter; x++)
{
// Loops till number of records minus outer loop variable
// Minus outer loop variable because after each iteration one record sorted
// Minus one because names are compared in pair
for(int y = 0; y < counter - x - 1; y++)
{
// Checks if current item name is greater than
// next item name
if(sd[y].itemName.compareTo(sd[y + 1].itemName) > 0)
{
// Swapping process
Soda temp = sd[y];
sd[y] = sd[y + 1];
sd[y + 1] = temp;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of method
// Method to show details
static void show()
{
// To store totals
int totalBottle = 0;
double avgCost = 0.0, avgPrice = 0.0, totProfit = 0.0;
// Display heading
System.out.printf("%50s\n", "SodaSales");
System.out.printf("%28s %10s %10s %10s %11s\n",
"Bottles", "Cost", "Retail", "Profit", "Percent");
System.out.printf("%-20s %-13s %-8s %-10s %-10s %-14s\n",
"Soda", "Sold", "Bottle", "Bottle", "Brand", "Sold of 150");
// Loops till number of records
for(int x = 0; x < counter; x++)
{
// Displays each record information
System.out.printf("\n%-20s %-13d %-8.2f %-10.2f %-10.2f %-15.2f",
sd[x].itemName, sd[x].bottlesSold,
sd[x].costBottle, sd[x].retailBottle,
sd[x].profitBrand, sd[x].percentSold);
// Calculates total
totalBottle += sd[x].bottlesSold;
avgCost += sd[x].costBottle;
avgPrice += sd[x].retailBottle;
totProfit += sd[x].profitBrand;
}// End of for loop
// Displays totals
System.out.printf("\n\n\n Total number of bottle sold: %d", totalBottle);
System.out.printf("\n Average Cost: %.2f", (avgCost / totalBottle));
System.out.printf("\n Average Price: %.2f", (avgPrice / totalBottle));
System.out.printf("\n Total Profit: %.2f", totProfit);
}// End of method
// main method definition
public static void main(String ss[])
{
// Calls the method to read file
readFile();
// Calls the method to sort the records
sort();
// Calls the method display details
show();
}// End of main method
}// End of driver class
Sample Output:
SidaSales.txt 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
i waan that contain loop and method.. NO DECOMPOSITION WORKSHEET REQUIRED FOR THIS LAB! For this...
Read about Cokes strategy in Africa in the article below and discuss the ethics of selling soft drinks to very poor people. Is this an issue that a company like Coke should consider? Africa: Coke's Last Frontier Sales are flat in developed countries. For Coke to keep growing, Africa is it By Duane Stanford Piles of trash are burning outside the Mamakamau Shop in Uthiru, a suburb of Nairobi, Kenya. Sewage trickles by in an open trench. Across the street,...