473,383 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Java beginner

1
Working on a project for my class and spent too long searching around on the internet trying to find the right answer...

Basically its a simple vending machine program using a hashmap.

So i got this so far...

Expand|Select|Wrap|Line Numbers
  1. public void runSimulator()
  2.     {
  3.         SoftDrink softdrink1 = new SoftDrink("A1","Coke",1.25);
  4.         SoftDrink softdrink2 = new SoftDrink("A2","Pepsi",1.25);
  5.         SoftDrink softdrink3 = new SoftDrink("A3","Sprite",1.25);
  6.         Candy candy1 = new Candy("B1","Snickers",0.75);
  7.         Candy candy2 = new Candy("B2","Crunch",0.75);
  8.         Candy candy3 = new Candy("B3","Butterfinger",0.75);
  9.  
  10.         HashMap<String,Object> vendingMap = new HashMap<String,Object>();
  11.         vendingMap.put("A1", softdrink1);
  12.         vendingMap.put("A2", softdrink2);
  13.         vendingMap.put("A3", softdrink3);
  14.         vendingMap.put("B1", candy1);
  15.         vendingMap.put("B2", candy2);
  16.         vendingMap.put("B3", candy3);
We're suppose to next just print out a list of all the items in the vending machine. But I can't figure out how to print the values of the objects.

Is there some way to just make a System.out.println( ??? (softdrink1)); and have it print out the (key, name, price)?
Nov 27 '07 #1
1 1553
Laharl
849 Expert 512MB
There is a way to do that (sort of). You need to override the Object method toString() in your SoftDrink and Candy classes. That will then print out whatever you use in that string. If you want to print out the key that maps to that value, that's more complicated. You'll need a loop to go through the map and find which key maps to your value and save that.

Eg:
Expand|Select|Wrap|Line Numbers
  1. public class Person {
  2.   private String name;
  3.   public Person(String n){name = n;}
  4.   public String toString(){return name;}//Prototype MUST be public String toString() --No deviations permitted!
  5. };
  6. public static void main(String[] args){
  7.   Person d = new Person("Dave");
  8.   System.out.println(d); //Dave is outputted
  9. }
  10.  
Nov 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: jcnews | last post by:
I am using Windows XP and am learning how to program in Java. Is there any way to get my fonts to be anti-aliased in both editors and in my programs? The non anti-aliased fonts don't look very...
5
by: Thomas G. Marshall | last post by:
This message is sent to these newsgroups because they are no longer valid: comp.lang.java comp.lang.java.api comp.lang.java.bugs comp.lang.java.misc comp.lang.java.setup comp.lang.java.tech ...
6
by: Alan Brown | last post by:
As a newbie I would like to ask what the difference in application (not details) is between C++ and Java. In what situations would you use C++ in preference to Java and vice versa? I am not...
55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
15
by: RAM | last post by:
Hello, I graduated computer science faculty and decided to became a programmer. Please help me to make a decision: Java or Microsoft .NET? What is the future of Java? Thanks! /RAM/
17
by: JD | last post by:
hi, i m new to java, i want to know if there is a good and free Java IDE that i can use that'll help me, i dont care if its complicated , i just want it to be good, i tried netbeans which comes...
6
crystal2005
by: crystal2005 | last post by:
Hello guys, I'm a beginner in Java application programming. I started to write a Java application in which link to MS Access database. I encountered a problem in deletion function. E.g. I would...
1
by: czi02 | last post by:
Im new to java. ANd somebody out there knows the tutorial of java thanx. for beginner. and how to code it???
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.