Results 1 to 5 of 5
  1. #1
    liquidsystem's Avatar
    Join Date
    Sep 2012
    Gender
    male
    Posts
    507
    Reputation
    135
    Thanks
    691
    My Mood
    Happy

    Vending Machine class for my AP Programming class

    I've just been working on this little class that our teacher wanted us to write. We were set with the task of creating our own class, with different constructors, getters, setters, etc... I decided I'd share it here for constructive criticism.

    Code:
    package vending_machine;
    
    import java.util.Arrays;
    
    
    
    public class VendingMachine {
    	
    	public int i=0;
    	public int k=0;
    	String[][] items;
    	double[][] price;
    	double moneyEntered = 0.00;
    	
    	VendingMachine()
    	{
    		items = new String[2][2];
    			//Set up each column in the 0th (technically 1st in real life) row
    			items[0][0] = "Cool Ranch Doritos";
    			items[0][1] = "Sour Cream & Onion Lays";
    			
    			//Set up each column in the 1st (technically 2nd in real life) row
    			items[1][0] = "Extremely Unhealthy Food";
    			items[1][1] = "Totally Radical Healthy Food That's Extremely Small";
    			
    		//End of items
    		price = new double[2][2];
    			//Set up price for items in the 1st row
    			price[0][0] = 2.00;
    			price[0][1] = 2.00;
    			
    			//Set up price for items in the 2nd row
    			price[1][0] = 4.00;
    			price[1][1] = 4.00;
    		//End of price
    		moneyEntered = 0.00;
    	}
    	
    	VendingMachine(int NUM_OF_ROWS, int NUM_OF_COLUMNS)
    	{
    		items = new String[NUM_OF_ROWS][NUM_OF_COLUMNS];
    			//Item defining done here
    		
    		//End of items
    		price = new double[NUM_OF_ROWS][NUM_OF_COLUMNS];
    			//Pricing for each item done here
    		
    		//End of price
    		moneyEntered = 0.00;
    	}
    	
    	
    	//Setters
    	void addOrChangeItem(int row, int column, String itemName, double itemPrice)
    	{
    		if(items[row][column] == null)
    		{
    			System.out.println("This element appears to be empty... Continuing");
    			items[row][column] = itemName;
    			price[row][column] = itemPrice;
    			System.out.println("New item added at "+row+","+column+" with the price of: $"+itemPrice);
    			
    		}
    		else
    		{
    			System.out.println("Replacing " + items[row][column] + " (cost: $" + price[row][column] + ")" + " with " + itemName + " (cost: $"
    					+ itemPrice + ")");
    			items[row][column] = itemName;
    			price[row][column] = itemPrice;
    		}
    		
    	}
    	
    	void changePrice(int row, int column, double itemPrice)
    	{
    		if(items[row][column] != null)
    		{
    			System.out.println("Changing the price of " + items[row][column] + " to $" + itemPrice);
    			price[row][column] = itemPrice;
    		}
    	}
    	
    	void enterMoney(double m)
    	{
    		moneyEntered = m;
    	}
    	
    	void chooseFood(int row, int column)
    	{
    		if(moneyEntered >= price[row][column])
    		{
    			System.out.println("You chose " + items[row][column] + " which has a price of $" + price[row][column]);
    			moneyEntered-= price[row][column];
    			System.out.println("Enjoy!");
    		}
    		else if (moneyEntered < price[row][column])
    		{
    			System.out.println("You don't have enough money for this! The price of this item is " + price[row][column]
    					+ " Current money in the machine: $" + moneyEntered);
    		}
    	}
    	
    	//Getters
    	void getFoodsAndPrices()
    	{
    		for(i = 0; i < 2; i++)
    		{
    			for(k = 0; k < 2; k++)
    			{
    				System.out.println("Price of " + items[i][k] + " is: " + "$" +price[i][k]);
    			}
    		}
    	}
    	
    	void getMoneyEntered()
    	{
    		System.out.println("There is currently $" + moneyEntered + " in this machine.");
    	}
    	
    }
    EDIT: I'm also looking for more things that I should need for a vending machine (setters/getters) I feel like I covered it all though.
    Last edited by liquidsystem; 02-28-2015 at 08:51 AM.
    If you wish to thank me, don't forget to click the button!

    Currently Playing: Osu, OldSchool Runescape (pm for username)


    Bitcoin Address: 1HUdLVM7DnT9gC1i5kNKzcKWekSbFoKNp2


    Steam
    Main Account
    Sales Account

    Feel free to PM me if you have any other questions relating Java, Python, or Physics, or general schoolwork


  2. #2
    aslhax's Avatar
    Join Date
    Aug 2015
    Gender
    male
    Location
    New York
    Posts
    41
    Reputation
    10
    Thanks
    7
    My Mood
    In Love
    I don't really have any criticism at all, this is very nice, organised coding. Nice job.

  3. #3
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    VendingMachine(int NUM_OF_ROWS, int NUM_OF_COLUMNS)
    {
    ...
    }
    //Getters
    void getFoodsAndPrices()
    {
    for(i = 0; i < 2; i++)
    {
    ...
    }
    }
    . .
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  4. #4
    αяgуяσѕ's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    A Box
    Posts
    1,714
    Reputation
    64
    Thanks
    146
    My Mood
    Lurking
    Edit.
    Noticed post was really old.
    Last edited by αяgуяσѕ; 09-03-2015 at 01:28 PM.
    Quote Originally Posted by Dave84311 View Post
    Valid keys, he gave me one himself.

    ____________________________
    Need help? Pm me
    ___________________________
    Please Press The Thanks If I Helped

  5. The Following User Says Thank You to αяgуяσѕ For This Useful Post:

    abuckau907 (09-03-2015)

  6. #5
    Qballl's Avatar
    Join Date
    Dec 2014
    Gender
    male
    Location
    United States
    Posts
    381
    Reputation
    20
    Thanks
    35
    My Mood
    Happy
    If I could suggest one thing it would be to make it a gui

Similar Threads

  1. Second LT 4th class for sale.
    By thebazarr21 in forum Trade Accounts/Keys/Items
    Replies: 11
    Last Post: 07-04-2010, 08:10 PM
  2. 2ND LT 5th class for sale.. legit sale..
    By spizzil in forum Selling Accounts/Keys/Items
    Replies: 7
    Last Post: 05-19-2010, 05:58 AM
  3. [FOR SALE] Segeant First Class
    By Divine in forum Selling Accounts/Keys/Items
    Replies: 7
    Last Post: 03-03-2010, 02:06 PM
  4. Master Sgt 3rd class for sale
    By octogon in forum Trade Accounts/Keys/Items
    Replies: 10
    Last Post: 09-16-2009, 10:56 PM
  5. Replies: 8
    Last Post: 12-22-2008, 12:09 PM