0

The assignment for my class asks me to create a program that tells a supermarket which customer, on a daily basis, has spent the most money in the store. The program must find this customer and display their name.

Goals of assignment - To work with multiple classes, work with ArrayLists and apply the knowledge gained.

My question:

  1. How should I loop my two output statements in my main class? Is that right in my main method? I need it to loop until the sentinel is used.
  2. How is this going to affect my sentinel?
  3. What type of questions should I be asking myself when dealing with loops? I'd like to think I'm overthinking this portion.

I really want to understand what I am doing here, so any help in the right direction would be appreciated! Thanks, in advance, for taking the time to help me out!

import java.util.Scanner;

public class main {

public static void main(String[] args) {

    System.out.println("* * * * * THE SUPERMARKET * * * * *");
    System.out.println("       Written by Nate Irwin");
    System.out.println();

    double finalTotal = -1;
    String anAccountName;

    Scanner input = new Scanner(System.in);
    Store store = new Store();

    do {
        System.out.println("Enter the customer name: ");
        if(input.hasNextLine()){
            anAccountName = input.nextLine();
            System.out.println("Enter customer total price, hit 0 to QUIT: ");
            finalTotal = input.nextDouble();
            store.addAccount(anAccountName, finalTotal);
            System.out.println();
        }
    } while (finalTotal != 0);

    System.out.println(store.getHighestCustomerTotal() + " has spent the most with us today!");
}
}

Store class:

import java.util.ArrayList;

public class Store {
    // Creates an ArrayList.
    private ArrayList<CustomerAccount> accounts = new    ArrayList<CustomerAccount>();

    //
    public void addAccount(String anAccountName, double finalTotal) {
        accounts.add(new CustomerAccount(anAccountName, finalTotal));
    }

    // Gets the HIGHEST customer total.
    public String getHighestCustomerTotal() {

        CustomerAccount highest = accounts.get(0);

        for (int i = 1; i < accounts.size(); i++) {
            if (accounts.get(i).getTotal() > highest.getTotal())
            {
                highest = accounts.get(i);
            }
        }

        return highest.getAccountName();
    }

}

CustomerAccount class:

public class CustomerAccount {
    // Variables defined to this class.
    private String accountName;
    private double total;

    // Constructor.
    public CustomerAccount(String anAccountName, double finalTotal) {
        accountName = anAccountName;
        total = finalTotal;
    }

    // Gets total from each customer.
    public double getTotal() {
        return total;
    }

    // Gets a customer's name.
    public String getAccountName() {
        return accountName;
    }
}
Nate Irwin
  • 11
  • 4
  • I'll leave the question to someone more familiar with Java, but just wanted to comment on the question: "What type of questions should I be asking myself when I deal with loops?". When it comes to most general programming topics, try not to look for strict sets of rules to follow or guide you. Programming logic should (eventually) feel just like writing in your native language. Writing loops is something you will only learn through practice, and soon you won't even need to think about them at all. – Steve May 26 '16 at 23:41
  • Thanks for the reply. I guess I'll just have to keep working at it. The loops themselves aren't confusing, however, implementing them and having them work together is the tricky part. – Nate Irwin May 26 '16 at 23:51
  • Is the loop supposed to check that user input is valid or just collect data until zero is entered? – Paul May 27 '16 at 00:06
  • 3
    Please note that your `anAccountName` variable is limited to the `if` scope. Declare it outside of this block to use it elsewhere. – Scary Wombat May 27 '16 at 00:10
  • Scary Wombat - Thanks a ton! That will help when it comes time to OUTPUT my HighestCustomerTotal – Nate Irwin May 27 '16 at 00:21
  • Jonny Henly - I just fixed it! Thanks – Nate Irwin May 27 '16 at 00:22
  • Have a look at the answer to [this question](http://stackoverflow.com/questions/6850380/are-whiletrue-loops-so-bad) for a discussion on ways to structure an input loop. – Paul May 27 '16 at 00:31
  • I think this should be posted at [code review](http://codereview.stackexchange.com/). Also, your `main` class should be called `Main`, not `main` - proper java convention. – Luke Melaia May 27 '16 at 01:11

2 Answers2

0

Your loop in main:

  • Doesn't really use the data you type in... One would expect this data to be used to create CustomerAccount instances
  • Has a completely unnecessary while(Condition) test at the end. This kind of loop is normally done with a While True and some test in the loop breaks out of the loop.

In getHighestCustomerTotal()

  • you can use a more "modern" form of the for() where you iterate elements of the list directly instead of iterating the index.
xenoid
  • 8,396
  • 3
  • 23
  • 49
  • 1. If you are referring to the program output, you're completely right. I still have yet to produce any OUTPUT as far as the HighestCustomerTotal is concerned. 2. I only implemented the WHILE, because I knew the DO loop requires it. 3. I guess I just need to look for a more modern one then? – Nate Irwin May 27 '16 at 00:08
  • There's nothing *wrong* with your do-while loop, xenoid is referring to the for loop in your highest customer method. By *more "modern" form of the for()* he's talking about using a for-each loop, which uses iterators rather than incrementing a variable. Remember that *modern* does not always mean better. The regular for loop can do everything the *modern* one can and more. @NateIrwin – Jonny Henly May 27 '16 at 00:26
  • When you say "modern form of the 'for()'", are you talking about the [foreach](http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) loop? – Luke Melaia May 27 '16 at 01:14
  • I believe that is what he is saying – Nate Irwin May 27 '16 at 02:14
0

I think your approach is fine, it gets the job done.

I'm not too sure what you're asking by saying how should you loop the two output statements, followed by if it should be in the main method. From what I understand, and by looking at your code, running this input loop is perfectly fine from the main class. The do-while is fine although I'd move the first 'introductory' output outside the loop so you don't see it every time the loop reiterates. Also, I notice you're not actually calling/instantiating the Store class in your main method, there's no data being added to the Store class for when it iterates through the accounts ArrayList.

As far as the answer that stated a more "modern" approach, I think the for loop you used is fine. I think the person was referring to the for-each loop. It doesn't really matter how you loop through it with the little amount of data you have.

There's some error in the logic for that loop. The getHighestCustomerTotal() is referencing an empty accounts ArrayList. You declared an ArrayList within the Store class and tried to loop through it but it's empty unless you called the addAccount() method from your main method at some point, so you'd need some error checking on that.

mastrgamr
  • 631
  • 1
  • 11
  • 21
  • That is exactly what I want. I want the input to say...... Enter customer name: "THE USER INPUTS NAME" Enter total price, hit 0 to QUIT: "THE USER ENTERS 100"... This continues until the sales clerk hits 0 causing the program to finish and displaying the highest customer total. – Nate Irwin May 27 '16 at 01:38
  • Everyone keeps telling me to store the data that is input. I added Store store = new Store(); Now how do I use that below in my code? What I really mean is how would that look in my code? anAccountName = store.addAcounts();? – Nate Irwin May 27 '16 at 01:39
  • Followed by `store.addAccount(anAccountName, finalTotal);`? Because that would actually add the items to the list contained inside the `Store` class. Then you can call `System.out.println(store.getHighestCustomerTotal());` in the main class and it should return something – mastrgamr May 27 '16 at 01:46
  • Thank you mastrgamr, I'm updating the code now! Fingers crossed – Nate Irwin May 27 '16 at 02:08
  • So I boiled the final issue down to my IF statement in my Main method. I'm assuming I'm asking the wrong thing? It's just a matter of getting the loop to output correctly. – Nate Irwin May 28 '16 at 04:54
  • So I ran your code, the program just asks for one customer's name and some prices. So the final output from the store is always going to be correct (since there's only one customer that exists). You might not be asking the right question – mastrgamr May 30 '16 at 00:38