简体   繁体   中英

Cannot use arrays or foreach loops to iterate over list of data and print out only certain values

The Prompt: A program that accepts a candy name (for example, “chocolate-covered blueberries”), price per pound, and number of pounds sold in the average month, and displays the item's data only if it is a best-selling item. Best-selling items are those that sell more than 2000 pounds per month. b. A program that accepts candy data continuously until a sentinel value is entered and displays a list of high- priced, best-selling items. Best-selling items are defined in Exercise 2a. High-priced items are those that sell for $10 per pound or more.

Here is an example of a good design in operation:

High-priced, Best-selling Candy
Fudge $12.50 4500 lbs
Vanilla Creme $13.75 2200 lbs.
Fudge, 12.50, 4500 Jawbreakers, 6.50, 5500 Chocolate, 14.00, 790 Butterscotch, 9.50, 4500 Vanilla Creme, 13.75, 2200
Item that sold most pounds: Jawbreakers

but the problem I am having is that my teacher is not letting me use for loops, or arrays. And I do not want to define multiple instances of the same variable because it is finite to a certain amount.... What would be the most efficient way of doing this?

 start

    // Declarations
    num QUIT = "Y";
    final String HEADING = "High Priced, Best Selling Candy" + "\n" + "\n";
    final String HSPS = candyName + " " + candyPrice + " " + candySold + " ";
    final String MOSTSOLD  = "Item that sold the most pounds is "

  while <> QUIT;
  enterCandy();
  printHighPriceBestSelling();
  printSoldMostPounds();
  endwhile;

  stop

entercandy()
  String candyName = "poop";
  double candyPrice = 0.0;
  double candyWeight = 0.0;
  int candySold = 0;
  output "Please enter name of candy.";
  input candyName;
  output "Please enter candy price.";
  input candyPrice;
  output "Please enter pounds sold.";
  input candySold;

printHighPriceBestSelling()
  if(candySold > 2000 && candyPrice > 10)
  {
    output HEADING;
    output HSPS;
  }
  else
  {
    output "There were/are no best selling, high priced candy!"
  }

  printSoldMostPounds();

 //There is no basis for comparison. 

There are only two ways of doing this. Create lots of different, artbitrary, and predefined variables to be filled by the loop until they are overwritten. Lets say 10. Or create an array. I am sure there is an overly complex way of doing it with nested if/switch/while loops, but why teach us/force us to use the ugly inefficient way?

    output "MOSTSOLD ";

I'm assuming that, besides arrays, you're teacher isn't allowing you to use any standard Collection objects.

You could always just build your own LinkedList of entered candy orders--it's ugly, but it would work. A single "link" in the chain would look like this

public class CandyOrderLink {
private String candyName;
private Double candyPrice;
private Double orderAmount;
private CandyOrderLink nextOrderLink;

public CandyOrderLink(String candyName, Double candyPrice, Double orderAmount) {
    this.candyName = candyName;
    this.candyPrice = candyPrice;
    this.orderAmount = orderAmount;
}

public CandyOrderLink getNextLink() {
    return nextOrder;
}

public void setNextLink(CandyOrderLink nextOrderLink) {
    this.nextOrderLink= nextOrderLink;
}

public String getCandyName() {
    return candyName;
}
public Double getCandyPrice() {
    return candyPrice;
}
public Double getOrderAmount() {
    return orderAmount;
}
}

Not sure if I'm quite grasping the point of the assignment, but using a list data-structure to keep track of all orders will work. Just build a link for each entry (candyName, price, amount) and set that link as the next link of the previous one. At the end of input, iterate through the list by repeatedly calling getNextLink() on each link and printing information (if appropriate). Here is Wikipedia's article on linked lists: http://en.wikipedia.org/wiki/Linked_list

From the problem's description , I see no need to store the data entered so that it can be sorted. Both a and b state simple conditions for displaying a candy: greater than 2,000 pounds and at least $10/lb. You can print each entry immediately after it is entered.

However, your example output implies that you must pick the single best-selling candy which contradicts the description. Which is correct?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM