Thread: Java Program

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Java Program


    Hi there. I'm wondering if anyone can help me here. I believe I've got the basics... but can't figure out how to complete it. Here's what it SHOULD do:

    Write a Java program to collect the sales data from three stores of a given company. The program will ask the user to enter the number of items sold at each store. The program should display a bar chart to illustrate the sales at each store. A bar chart is a row of printed asterisks -- each asterisk represents the sale of one item. To make the bar chart easier to read, every tenth asterisk is replaced by an 'x'. Here is a sample run of the program:

    Enter today's sales for store 1: 27
    Enter today's sales for store 2: 15
    Enter today's sales for store 3: 34

    SALES BAR CHART

    Store 1: *********x*********x*******
    Store 2: *********x*****
    Store 3: *********x*********x*********x****
    Your program should validate the data entered for each store. The store data should be a value zero or greater (i.e., reject negative values). Here is a sample run of the program in which the user attempted to enter invalid data for store 2 -- eventually the user entered the valid value of 23:
    Enter today's sales for store 1: 9
    Enter today's sales for store 2: -2
    Invalid input - enter a value of zero or greater: -1
    Invalid input - enter a value of zero or greater: 23
    Enter today's sales for store 3: 16

    SALES BAR CHART

    Store 1: *********
    Store 2: *********x*********x***
    Store 3: *********x******
    Start with the following code -- add your code to the main method:

    import java.util.Scanner;

    public class BarChart
    {
    public static void main(String [] args)
    {

    }
    }



    Here is what I have:

    import java.util.Scanner;

    public class BarChart
    {
    public static void main(String [] args)
    {

    int store1;
    int store2;
    int store3;
    int bar1;
    int bar2;
    int bar3;



    // Create a Scanner object to read input.
    Scanner keyboard = new Scanner(System.in);



    System.out.print("Enter today's sales for store 1: ");
    store1 = keyboard.nextInt();

    while (store1 < 0)
    {
    System.out.print("Invalid input - enter a value of zero or greater: "); // Completion of WHILE loop for less than 0.
    store1 = keyboard.nextInt();
    }


    System.out.print("Enter today's sales for store 2: ");
    store2 = keyboard.nextInt();

    while (store2 < 0)
    {
    System.out.print("Invalid input - enter a value of zero or greater: "); // Completion of WHILE loop for less than 0.
    store2 = keyboard.nextInt();
    }

    System.out.print("Enter today's sales for store 3: ");
    store3 = keyboard.nextInt();

    while (store3 < 0)
    {
    System.out.print("Invalid input - enter a value of zero or greater: "); // Completion of WHILE loop for less than 0.
    store3 = keyboard.nextInt();
    }




    System.out.println("\n\nSALES BAR CHART\n\n");
    bar1 = store1/100;
    System.out.println("store 1 : ");
    for(int i = 1; i <= bar1; i++)
    System.out.println("*");

    bar2 = store2/100;
    System.out.println("\nStore 2 : ");
    for(int i = 1; i <= bar2; i++)
    System.out.println("*");

    bar3 = store3/100;
    System.out.println("\nStore 3 : ");
    for(int i = 1; i <= bar3; i++)
    System.out.println("*");

    }
    }


    Everything works, but this is what I get on a run:


    Enter today's sales for store 1: 12
    Enter today's sales for store 2: 15
    Enter today's sales for store 3: 22


    SALES BAR CHART


    store 1 :

    Store 2 :

    Store 3 :


    I'm thoroughly lost on the asterick/plus part. If anyone could offer advice, or point me in the right direction, I'd really really appreciate it.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Okay, a little bit closer.. Issue is revolving around the asterick bar chart.

    updated code:

    System.out.println("\n\nSALES BAR CHART\n\n");
    bar1 = store1;
    System.out.print("store 1 : ");
    for(int i = 1; i <= bar1; i++)
    System.out.print("*");

    bar2 = store2;
    System.out.print("\nStore 2 : ");
    for(int i = 1; i <= bar2; i++)
    System.out.print("*");

    bar3 = store3;
    System.out.print("\nStore 3 : ");
    for(int i = 1; i <= bar3; i++)
    System.out.print("*");



    Ouput:


    Enter today's sales for store 1: 22
    Enter today's sales for store 2: 45
    Enter today's sales for store 3: 7


    SALES BAR CHART


    store 1 : **********************
    Store 2 : *********************************************
    Store 3 : *******
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    It seems this is factored off maybe modulus? i%10 to break down, then everytime i=0 print an x? That works in my mind.. but I don't know what process to do to actually make this do what I want. Any suggestions?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Figured it out.. Don't think it's the CLEANEST way.. but it gets the job done.. nested FOR loop with an IF/ELSE clause:

    System.out.println("\n\nSALES BAR CHART\n\n");
    bar1 = store1;
    System.out.print("store 1 : ");
    for(int i = 1; i <= bar1; i++)

    if (i%10 >=1)
    System.out.print("*");
    else
    System.out.print("x");


    bar2 = store2;
    System.out.print("\nStore 2 : ");
    for(int i = 1; i <= bar2; i++)

    if (i%10 >=1)
    System.out.print("*");
    else
    System.out.print("x");


    bar3 = store3;
    System.out.print("\nStore 3 : ");
    for(int i = 1; i <= bar3; i++)

    if (i%10 >=1)
    System.out.print("*");
    else
    System.out.print("x");


    Completed:
    Enter today's sales for store 1: -5
    Invalid input - enter a value of zero or greater: -4
    Invalid input - enter a value of zero or greater: 12
    Enter today's sales for store 2: -45
    Invalid input - enter a value of zero or greater: -5
    Invalid input - enter a value of zero or greater: 33
    Enter today's sales for store 3: -4
    Invalid input - enter a value of zero or greater: 106


    SALES BAR CHART


    store 1 : *********x**
    Store 2 : *********x*********x*********x***
    Store 3 : *********x*********x*********x*********x*********x*********x*********x*********x*********x*********x ******
    ----jGRASP: operation complete.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    London, ON
    Posts
    322
    Rep Power
    214
    For the record, using code tags will make everything much prettier

    Code:
    System.out.println("\n\nSALES BAR CHART\n\n");
    bar1 = store1;
    System.out.print("store 1 : ");
    for(int i = 1; i <= bar1; i++)	
    
    if (i%10 >=1)
    System.out.print("*");
    else
    System.out.print("x");
    
    
    bar2 = store2;
    System.out.print("\nStore 2 : ");
    for(int i = 1; i <= bar2; i++)
    
    if (i%10 >=1)
       System.out.print("*");
    else
       System.out.print("x");
    
    
    bar3 = store3;
    System.out.print("\nStore 3 : ");
    for(int i = 1; i <= bar3; i++)
    
    if (i%10 >=1)
       System.out.print("*");
    else
       System.out.print("x");
    etc. If you copy & paste directly from your editor into the code tags it'll preserve formatting
    <Tetrad> the program I just wrote 1) compiled the first time without any errors and 2) worked like it was supposed to
    <Tetrad> I don't know whether to be proud or scared to death
    Originally Posted by DaWei_M
    That covers a multitude of your sins, right there.

IMN logo majestic logo threadwatch logo seochat tools logo