In: Computer Science
Program: Java (using eclipse)
Write a program that asks the user to enter today’s sales for five stores. The program should then display a bar chart comparing each store’s sales. Create each bar in the bar chart by displaying a row or asterisks. Each asterisk should represent $100 of sales. You must use a loop to print the bar chart!!!!
If the user enters a negative value for the sales amount, the program will keep asking the user to enter the sales amount until a positive amount is entered,
You must use methods, loops and arrays for this assignment!!!!!
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
import java.util.Scanner;
public class BarChart {
// function to print the barchart for a given store number and sise
public static void printBar(int storeNum,int size)
{
System.out.print("Store: "+storeNum+" ");
for(int j = 0; j < size; j++)
{
System.out.print("* ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// take an array of size 5
int[] barArray = new int[5];
int count=0;
while(count<5)
{
// ask for input
System.out.print("Enter sale for store "+(count+1)+": ");
// read number
int value= in.nextInt();
if(value<0)
{
// negative value!!
System.out.println("Invalid. Please enter a positive number.");
}
else
{
// Positive value, add it to teh array.
barArray[count]=value;
// increase the count
count++;
}
}
// print the bars
for(int i =0; i < 5; i++)
{
printBar(i+1,barArray[i]/100);
}
}
}
============


SAMPLE RUNS

