jeudi 20 avril 2017

Trouble Taking Receving Array from other Class Into Method Without Re-Prompting

Having a really hard time with this problem. I've tried searching to find a similar situation but I can't seem to find anything that helps. I feel like I've got it 99% of the way and it's probably something very simple but I can't for the life of me figure it out.

I have two classes. ClassA and Restaurant.

The problem is that in my method call for printRestaurant() in the main method of ClassA, some of the methods need to receive the array with restaurant names but when I use the methods, it reprompts for the restaurant names each time it hits the next method instead of only prompting once and then moving to execute the next method.

ClassA:

import java.util.Scanner;
public class ClassA
{

  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    String ownerName = "";

    System.out.printf("%nWhat is the name of the owner?");
    ownerName = input.nextLine();
    Restaurant rest = new Restaurant(ownerName);
    rest.arraySize();
    rest.printRestaurant(ownerName,rest.setRestaurants(),rest.setCustomers(rest.setRestaurants()),rest.calcAvgDailyRev(rest.setRestaurants()));

    input.close();
    System.exit(0);

  }//END main()

}//END Application Class ClassA

Restaurants:

public class Restaurant
{
  //Fields
  String ownerName = "";
  int size = 0;
  Scanner input = new Scanner(System.in);

  public Restaurant()
  {

  }//END Restaurant()

  public Restaurant(String name)
  {
  ownerName = name;
  }//END Restaurant(String ownerName)  

  public void arraySize()
  {
    System.out.printf("%n%s, how many restaurants do you own?  ",ownerName);
    size = input.nextInt();
  }//END arraySize()

  public String[] setRestaurants()
  {
    String[] restNames = new String[size];

          input.nextLine(); // clears buffer
    for(int row = 0;row < restNames.length; row++)
    {

      System.out.printf("%nEnter restaurant %d",row+1);
      restNames[row] = input.nextLine();

    }//END for col < restNames.length

    return restNames;

  }//END setRestaurants()

  public int[] setCustomers(String array[])
  {
    int[] noCustomers = new int[size];
    for(int i = 0;i<noCustomers.length;i++)
    {
    System.out.printf("%nEnter the average number of daily customers for %s: ",array[i]);
    noCustomers[i] = input.nextInt();
    }//END for i < size
    return noCustomers;
  }//END setCustomers()

  public double[] calcAvgDailyRev(String array[])
  {
    double[] avgBill = new double[size];
    input.nextLine(); //Clears buffer

    for(int i = 0;i<avgBill.length;i++)
    {
      System.out.printf("%nEnter the average bill for %s: ",array[i]);
      avgBill[i] = input.nextDouble();

    }//end for i < size
    return avgBill;

  }//END calcAvgDailyRev(String array)

  public void printRestaurant(String name, String restName[], int customers[], double dailyAvg[])
  {
    System.out.printf("%n%n%S's RESTAURANTS",name);


    for(int i=0;i<size;i++)
    {
      double avgRevenue = customers[i]*dailyAvg[i];

      System.out.printf("%n%nRestaurant:  %s"
                       +"%nAverage No of Daily Customers: %d"
                       +"%nAverage Bill Per Customer:  $%,.2f"
                       +"%nAverage Daily Revenue:  $%,.2f",restName[i],customers[i],dailyAvg[i],avgRevenue);
    }//END for i < size

  }//END printRestaurant()







}//END Restaurant

Here is an example of the desired output:

What is the name of the owner? Cliff Cliff, how many restaurants do you own? 2 Enter restaurant 1: Eggs Enter restaurant 2: Bacon Enter the average number of daily customers for Eggs: 250 Enter the average number of daily customers for Bacon: 200 Enter the average bill per customer for Eggs: 12 Enter the average bill per customer for Bacon: 15

CLIFF’s RESTAURANT

Restaurant: Eggs Average No of Daily Customers: 250 Average Bill per Customer: $12.00 Average Daily Revenue: $3,000.00

Restaurant: Bacon Average No of Daily Customers: 200 Average Bill per Customer: $15.00 Average Daily Revenue: $3,000.00

If you need any additional information please let me know. Any help would be GREATLY appreciated.






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire