creating ArrayList of objects to access via user input vending machine

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2833 Views - Last Post: 21 July 2015 - 03:34 PM Rate Topic: -----

#1 b.netana   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 11-September 11

creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 12:10 PM

I am currently trying to create a vending machine simulation program, pretty standard stuff, but keep getting errors on my ArrayList object when I try to add the objects, here is the snippet that I am working with, and need to know the best way to allow the user to pick their soda out from the list

        class Soda{
		
		ArrayList< Soda > aList = new ArrayList< Soda >();
		
		// add the items to the arrayList
		
		aList.add(new Soda("Cola", 1.00, 20));
		aList.add(new Soda("Sprite", 1.00, 20));
		aList.add(new Soda("Dr. Pib", 1.00, 20));
		aList.add(new Soda("Squirt", 1.00, 20));
		aList.add(new Soda("Lemon", 1.00, 20));
		
	
			System.out.println("Select a Drink!");
			System.out.println(sodaList);
		
        
        
}

This post has been edited by b.netana: 20 July 2015 - 12:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: creating ArrayList of objects to access via user input vending machine

#2 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 12:44 PM

Please remove the spaces ArrayList< Soda > follow the Java code conventions :)

It seems OK from here, except that you don't have a variable named sodaList
What exceptions / compile errors are you getting?
Was This Post Helpful? 1
  • +
  • -

#3 b.netana   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 11-September 11

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 12:52 PM

I am working in eclipse, and am receiving these errors in the construction of my ArrayList on line 7 of my code:

Multiple markers at this line
- Syntax error, insert ";" to complete MethodDeclaration
- Syntax error, insert "AssignmentOperator Expression" to complete
Expression
- Syntax error, insert "Identifier (" to complete MethodHeaderName
- Syntax error, insert "}" to complete ClassBody
- Syntax error, insert ")" to complete MethodDeclaration

I also need to work through how to display the items and allow a user to choose one of the items that is displayed. I know that I am on the right track with the ArrayList and using the index to access it, but the method of completion is slipping by me.

I suspect that these errors will repeat for the following lines once I have cleared whatever is happening with line 7's code.

This post has been edited by b.netana: 20 July 2015 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 12:55 PM

You cannot invoke methods outside of methods (except in variable declarations). Basically, if you aren't declaring something, it belongs in a method. So pretty much, lines 7-15.
Was This Post Helpful? 1
  • +
  • -

#5 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 12:58 PM

Aha haha, totally missed you wrote your code outside any constructor or method body

Your code needs to be encapsulated inside a constructor or a method of the class, like so:
class Soda{
	public Soda() {
		ArrayList< Soda > aList = new ArrayList< Soda >();

		// add the items to the arrayList

		aList.add(new Soda("Cola", 1.00, 20));
		aList.add(new Soda("Sprite", 1.00, 20));
		aList.add(new Soda("Dr. Pib", 1.00, 20));
		aList.add(new Soda("Squirt", 1.00, 20));
		aList.add(new Soda("Lemon", 1.00, 20));
		System.out.println("Select a Drink!");
		System.out.println(sodaList);
	}
}


class Soda{
	public void fooMethod() {
		ArrayList< Soda > aList = new ArrayList< Soda >();

		// add the items to the arrayList

		aList.add(new Soda("Cola", 1.00, 20));
		aList.add(new Soda("Sprite", 1.00, 20));
		aList.add(new Soda("Dr. Pib", 1.00, 20));
		aList.add(new Soda("Squirt", 1.00, 20));
		aList.add(new Soda("Lemon", 1.00, 20));
		System.out.println("Select a Drink!");
		System.out.println(sodaList);
	}
}


The following will run the code as part of main. Main is the first method that gets executed when your program starts
class Soda{
	public static void main(String[] args) {
		ArrayList< Soda > aList = new ArrayList< Soda >();

		// add the items to the arrayList

		aList.add(new Soda("Cola", 1.00, 20));
		aList.add(new Soda("Sprite", 1.00, 20));
		aList.add(new Soda("Dr. Pib", 1.00, 20));
		aList.add(new Soda("Squirt", 1.00, 20));
		aList.add(new Soda("Lemon", 1.00, 20));
		System.out.println("Select a Drink!");
		System.out.println(sodaList);
	}
}


Was This Post Helpful? 2
  • +
  • -

#6 b.netana   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 11-September 11

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 01:16 PM

Thank you The example is extremely helpful, but now I am getting this error:

Multiple markers at this line
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error, insert "Identifier (" to complete MethodHeaderName
- Syntax error, insert "}" to complete ClassBody
- Syntax error, insert ";" to complete MethodDeclaration
- Syntax error, insert "AssignmentOperator Expression" to complete
Expression
- The constructor Soda(String, double, int) is undefined
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 01:17 PM

Please post your revised code, as well as indicate the specific line numbers where the errors occur.
Was This Post Helpful? 0
  • +
  • -

#8 b.netana   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 11-September 11

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 01:23 PM

sorry about that, here is the revised code following what CasiOo posted:

class Soda{
	public Soda() {
		ArrayList< Soda > aList = new ArrayList< Soda >();

		// add the items to the arrayList

		aList.add(new Soda("Cola", 1.00, 20));
		aList.add(new Soda("Sprite", 1.00, 20));
		aList.add(new Soda("Dr. Pib", 1.00, 20));
		aList.add(new Soda("Squirt", 1.00, 20));
		aList.add(new Soda("Lemon", 1.00, 20));
		System.out.println("Select a Drink!");
		System.out.println(sodaList);
	}
}



I am thinking that I may need to define the values for the Soda object, but am unsure of how to do so with the ArrayList errors on 7-15

This post has been edited by b.netana: 20 July 2015 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 01:26 PM

This error is pretty self explanatory: The constructor Soda(String, double, int) is undefined . By the way- it doesn't make sense to add Soda objects to an ArrayList<Soda> in the Soda constructor. You will get a StackOverflowError. In addition, it's poor design practice as well. A Soda object should model a Soda, not a 12-pack of Sodas.

Which line(s) do the other errors occur at? Please be specific for which error occurs at which line.
Was This Post Helpful? 1
  • +
  • -

#10 b.netana   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 11-September 11

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 01:51 PM

Okay I have fixed the self explanatory one, and here is the new revision of the code

        class Soda{
        	public Soda(){

        ArrayList<Soda> aList = new ArrayList<Soda>();
		
		Soda(String, double, int)
		// add the items to the arrayList
		aList.add(new Soda("Cola", 1.00, 20));
		aList.add(new Soda("Sprite", 1.00, 20));
		aList.add(new Soda("Dr. Pib", 1.00, 20));
		aList.add(new Soda("Squirt", 1.00, 20));
		aList.add(new Soda("Lemon", 1.00, 20));
		
	
			System.out.println("Select a Drink!");
			System.out.println(aList);
		
        
        	
}}}}



I get errors on lines 6 and 8, on 6 the error is:

Multiple markers at this line
- Syntax error, insert ". class" to complete
Expression
- Syntax error on token ",", ( expected

on 8 the error is:

Multiple markers at this line
- Syntax error, insert ")" to complete Expression
- Syntax error, insert ")" to complete MethodDeclaration
- Syntax error, insert "Identifier (" to complete MethodHeaderName
- Syntax error, insert "}" to complete ClassBody
- Syntax error, insert ";" to complete MethodDeclaration
- Syntax error, insert "AssignmentOperator Expression" to complete
Expression
Was This Post Helpful? 0
  • +
  • -

#11 b.netana   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 11-September 11

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 01:56 PM

Quote

By the way- it doesn't make sense to add Soda objects to an ArrayList<Soda> in the Soda constructor. You will get a StackOverflowError. In addition, it's poor design practice as well. A Soda object should model a Soda, not a 12-pack of Sodas.


I was actually trying to get it to store the number of that item in the machine, but now that you have mentioned it, it does not make much sense to have the object hold that information. Thanks for that one, I will need to place that within my vending machine class.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 02:01 PM

There is a lot wrong with your code. First, let's hit the obvious- indentation and formatting. If your code is hard to read, it's hard to maintain and debug. Curly braces are used to denote encapsulation. There should be one curly brace at a given line, and they should be indented to add clarity.

Based on indenting your code, you will see that you have two extra close braces at the end of your code. These will cause errors. Delete them.
        class Soda {
        	public Soda() {

        		ArrayList < Soda > aList = new ArrayList < Soda > ();

        		Soda(String, double, int)
        		// add the items to the arrayList
        		aList.add(new Soda("Cola", 1.00, 20));
        		aList.add(new Soda("Sprite", 1.00, 20));
        		aList.add(new Soda("Dr. Pib", 1.00, 20));
        		aList.add(new Soda("Squirt", 1.00, 20));
        		aList.add(new Soda("Lemon", 1.00, 20));


        		System.out.println("Select a Drink!");
        		System.out.println(aList);



        	}
        }
        }
        }



Now this: Soda(String, double, int) is illegal for a number of reasons. First, it's not a valid method header. The Java error message indicates that you are attempting to use a Soda constructor with parameters of type String, double, and int in that order. You actually have to name these parameters: Soda(String name, double quantity, int price).

The second problem with this Soda(String, double, int) is that it's declared inside your other Soda() constructor. You cannot declare a method inside of a method.

class Soda{

   public Soda(){
      //code
   }

   public Soda(String name, double price, int quantity){
      //code
   }
}



Now I want to reiterate my advice from my previous post:

Quote

By the way- it doesn't make sense to add Soda objects to an ArrayList<Soda> in the Soda constructor. In addition, it's poor design practice as well. A Soda object should model a Soda, not a 12-pack of Sodas.


I would encourage you as well to go through some tutorials on OOP and basic Java syntax.
Was This Post Helpful? 3
  • +
  • -

#13 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 02:32 PM

Hi pemby- I have removed your post, as we are not a site to give our homework solutions. I appreciate your willingness to help, but please avoid giving completed solutions to problems that are academic in nature.
Was This Post Helpful? 1
  • +
  • -

#14 b.netana   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 11-September 11

Re: creating ArrayList of objects to access via user input vending machine

Posted 20 July 2015 - 02:53 PM

I am using a snippet of my code here, that accounts for the second set of brackets, but I do think the OOP tutorials will be helpful either way, but with your advice I have been able to clear the errors, but now have a bit of a logic problem that I think I should fight with a bit more on my own, thank you.
Was This Post Helpful? 0
  • +
  • -

#15 b.netana   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 11-September 11

Re: creating ArrayList of objects to access via user input vending machine

Posted 21 July 2015 - 01:48 AM

I am having some trouble with my vending machine practice problem. I am trying to instantiate my ArrayList that is stored in another local class and display specific elements to the user so that they can then select the item that they want, from there I will be able to handle the rest of the calculations. Here is my code:

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.*;

class Drink{

	public Drink(int index, String sodaName, double price){
	
    ArrayList<Drink> aList = new ArrayList<Drink>();
	

	// add the items to the arrayList
	aList.add(new Drink(1, "Cola", 1.00));
	aList.add(new Drink(2, "Sprite", 1.00));
	aList.add(new Drink(3, "Dr. Pib", 1.00));
	aList.add(new Drink(4, "Squirt", 1.00));
	aList.add(new Drink(5, "Lemon", 1.00));

	return aList;
	}
}
        

public class vendingMachine {

	public static void main(String[] args) {
	    //start the main method
		
		//create keyboard listener
		Scanner keyboard = new Scanner(System.in);
        double balance = 25.00;
        double deposit= 0.00;
        double change = 0.00;
        double sValue = 1.00;
        double dollar = 1.00;
        double quarter = 0.25;
        double dime = 0.10;
        double nickel = 0.05;
        
System.out.println("Insert Money to make a selection");
        do{
        	System.out.println("1. Dollar");
        	System.out.println("2. Quarter");
        	System.out.println("3. Dime");
        	System.out.println("4. Nickel");
        	
		int moneySelect = keyboard.nextInt();
        	
        	if(moneySelect == 1){
        		deposit= dollar + deposit;
        		System.out.println(deposit);
        	}
        	
        	else if(moneySelect == 2){
        		deposit= quarter + deposit;
        		System.out.println(deposit);
        	}
        	
        	else if(moneySelect == 3){
        		deposit= dime + deposit;
        		System.out.println(deposit);
        	}
        	
        	else if(moneySelect == 4){
        		deposit= nickel + deposit;
        		System.out.println(deposit);
        	}
        	
        	else{
        		System.out.println("Please enter a valid amount");
        		System.out.println(deposit);
        	}
        		
        	}
        while(deposit < sValue);
        	
        	System.out.println("Select a soda");
        	System.out.print();

}
}



This post has been edited by b.netana: 21 July 2015 - 02:10 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2