0

I am working on a program that mimics a vending machine, it needs to pull data from a .txt file and use that information in the program. I want it to ask the user to input their amount of money and give them change as well. I want to store the prices in 2d arrays as well as the item names then have the user pick an item to buy. I am not sure how to go about this!! I really need some help, this is what I have so far.

 Code:

    import java.util.*;
    import java.io.File;
    import java.io.FileNotFoundException;

    public class Vending
    {
      public static void main(String[] args) throws FileNotFoundException
      {
          System.out.print("Enter your food selection file: ");      // User inputs file
          Scanner input = new Scanner(System.in);                       // Keyboard input from user
          Scanner fs = new Scanner(new File(input.nextLine()));         // Scans in the file that was inputed
      }         
            double price = 0;
            while(fs.hasNextLine()){
              fs.next();
                 price = fs.nextDouble();

                 System.out.print(price);
        }
      }

Item list file (Food.txt):

1        Honey roasted peanuts
1.50     Cheetos
1.50     Bugles
2        Synder’s Pretzels
1        Snickers
1        Twix
1.25     M n Ms
.75      Life savers
1        Twizzlers
1        Nutter Butters
1        Butter Fingers
1.50     King Size Kit Kats
1.25     Carrot sticks
.50      Juicy Fruit
.50      Spearmint Gum
.50      Five gum
3.50     Pepperoni
1.75     Cheez-Its
.25      Slim Jim
1.50     Lays Barbeque Chips
Cliff
  • 11
  • 1
  • 5
  • Do you have to use a 2D array, or can you create a simple `Item` class that holds both the `itemName` and `itemPrice`? – Keith Oct 16 '15 at 02:44
  • Hi and welcome to Stack Overflow! The question you're asking is quite broad (you'd like to know how to store the prices, how to handle user input, and so on.) It might be worth making a start, perhaps by reading the prices in as you've started to do, then adding them to a data structure and outputting to the screen. Other questions like http://stackoverflow.com/questions/19844649/java-read-file-and-store-text-in-an-array might give you some pointers. – Rich Churcher Oct 16 '15 at 02:51

1 Answers1

1

Here are some thoughts:

  • Instead of a 2-D array, create a simple Item POJO that holds the item's name and price.
  • Read in each line of the input file and create one Item for each.
  • Store each of those in an array.
  • Rather than having the user input the precise name of the item they want, emulate a real vending machine and assign each item a number. E.g., iterate over the array and let each index represent the "item number" and then print out the item price and name.
  • Now you can have your application's main function:
    • Read the file and create the array
    • Print out the options
    • Ask user to select an option
    • Ask user to provide money
    • "Return" the product (i.e. print "here's your item")
    • Calculate and "return" the change (i.e. print "here's your $x.xx back")

Here's a pseudocode for the change calculation using an Item object:

double calculateChange(Item item, double money) {
    double price = item.getPrice();
    // if assignment needs to handle "money < price", do so here
    return money - price;
}
Keith
  • 3,079
  • 2
  • 17
  • 26