Sue McBee, the CEO of the Hungry Heifer fast food restaurant chain, has ambitiou
ID: 3568919 • Letter: S
Question
Sue McBee, the CEO of the Hungry Heifer fast food restaurant chain, has ambitious expansion plans. She is confident that 3 to 6 more locations can be successfully opened within the next 6 months. Furthermore, she wants the sales report created in project 5 revised. The report now needs to be formatted as follows : HUNGRY HEIFER MONTHY SALES Location Monthly Sales Percent of Total Sales (in $1000s) Pine Ridge : 349 22.00 % Anderson 348 21.95 % Frontier 220 13.88 % Western 220 13.88 % University 187 11.79 % Dyess 159 10.03 % Waco 102 6.43 % Total Sales 1585 Notice that the report must be sorted on the monthly sales amount in descending order. In addition, note the percentages may not add up to exactly 100 % due truncation and rounding, which is acceptable. Since the number of locations is expected to be increasing in the near future, the program must be able to process up to 15 locations. The program must use two arrays; one to store location names and another to store at least the sales amounts for the locations. Also, the program must use at least two functions. One function to obtain and load the data and a second function to output the report. The user is to be prompted for the number of locations and then each location name and the monthly sales of each location.. A sample of the programs user interface for inputting data is as follows : Please enter the number of locations : 7 Enter location name followed by its monthly sales : Frontier 220420 Enter location name followed by its monthly sales : Waco 102003 Enter location name followed by its monthly sales : University 187530 Enter location name followed by its monthly sales : Western 220014 Enter location name followed by its monthly sales : PineRidge 349654 Enter location name followed by its monthly sales : Dyess 159659 Enter location name followed by its monthly sales : Anderson 348584
Explanation / Answer
import java.util.*;
//Sales Class
public class SalesClass
{
String locations;
int monthlySales;
//constructor
SalesClass(String location, int mSales)
{
addData(location,mSales);
}
//addData method
public void addData(String location, int mSales)
{
locations=location;
monthlySales=mSales;
}
//getters
public String getLocations()
{
return locations;
}
public int getmonthlySales()
{
return monthlySales;
}
//display method
public String display()
{
String s="";
s+=""+getLocations()+ " "+getmonthlySales();
return s;
}
}
import java.text.DecimalFormat;
import java.util.*;
//implementation class
public class ImplementationClass
{
//static variables
static ArrayList<SalesClass>sales=new ArrayList<SalesClass>();
public static void main(String args[])
{
//delcare the required variables
String locations[];
int mSales[];
int size;
Scanner input=new Scanner(System.in);
System.out.println("Enter the number of locations: ");
size=input.nextInt();
locations=new String[size];
mSales=new int[size];
int count=0;
//add the data
while(count<size)
{
System.out.println("Enter location name followed by its monthly sales : ");
locations[count]=input.next();
mSales[count]=input.nextInt();
SalesClass sc=new SalesClass(locations[count], mSales[count]);
sales.add(sc);
sc.display();
count++;
}
//sort the array list
Collections.sort(sales, new Comparator<SalesClass>() {
public int compare(SalesClass sc1, SalesClass sc2) {
return (int) (sc1.getmonthlySales() - sc2.getmonthlySales());
}
});
System.out.println("Location Monthly-Sales Percent_of_Total_Sales");
displayData();
}
//compute the total
public static int computeTotal()
{
int total=0;
for(int i=0;i<sales.size();i++)
{
total+=sales.get(i).getmonthlySales();
}
return total;
}
//display the final result
public static void displayData()
{
DecimalFormat df = new DecimalFormat("##.00");
double percentage=0;
int total=computeTotal();
for(int i=0;i<sales.size();i++)
{
percentage=100*(sales.get(i).getmonthlySales()/(double)total);
System.out.println(sales.get(i).display()+" "+df.format(percentage));
}
System.out.println("Total: "+total);
}
}
Sample Output:
Enter the number of locations:
7
Enter location name followed by its monthly sales :
Frontier 220420
Enter location name followed by its monthly sales :
Waco 102003
Enter location name followed by its monthly sales :
University 187530
Enter location name followed by its monthly sales :
Western 220014
Enter location name followed by its monthly sales :
PineRidge 349654
Enter location name followed by its monthly sales :
Dyess 159659
Enter location name followed by its monthly sales :
Anderson 348584
Location Monthly-Sales Percent_of_Total_Sales
Waco 102003 6.42
Dyess 159659 10.05
University 187530 11.81
Western 220014 13.86
Frontier 220420 13.88
Anderson 348584 21.95
PineRidge 349654 22.02
Total: 1587864
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.