Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,291 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 947 people online right now. Registration is fast and FREE... Join Now!




Java inventory program 2

 
Reply to this topicStart new topic

Java inventory program 2, Need help with loop

evak77
18 Dec, 2008 - 03:14 PM
Post #1

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 9


My Contributions
I need help with a loop for the inventory program

CODE

public class Inventory2 {
    
    public static void main(String[] args) {    
        
        CD cd;
        Inventory inventory = new Inventory();    
        
        cd = new CD(001, "Master of Puppets", 2, 27.98f);
        inventory.add(cd);  
        
        cd = new CD(022, "Heaven and Hell", 1, 9.99f);
        inventory.add(cd);  
        
        cd = new CD(013, "Throwing Copper", 10, 17.95f);
        inventory.add(cd);  
        
        cd = new CD(004, "Back in Black", 4, 8.99f);
        inventory.add(cd);  
        
        inventory.display();      
        
        System.out.println("\nEnd CD inventory\n");
    }

} // end class CD


CODE

class CD {
    private int partID;
    private String name;
    private int copies;        
    private float price;
    
    CD(int partID, String name, int copies, float price) {
        this.partID = partID;
        this.name   = name;
        this.copies = copies;
        this.price  = price;
    }
    
    
    public String getName() {
        return name;
    }
    
    public float value() {
        return copies * price;
    }


    public int compareTo(Object o) {
        CD cd = (CD) o;
        return getName().compareTo(cd.getName());
    }

    public String toString() {
        return String.format("  %03d    %-22s  %3d     $%6.2f     $%7.2f",
                             partID, name, copies, price, value());
    }
        
} // end class CD


CODE

class Inventory {
    private CD[] cds;
    private int numCDs;
        private int valCDs;
        private int namCDs;
        private int priCDs;
    
    ///Construct a new inventory for CDs
    Inventory() {
        cds = new CD[25];
        numCDs = 0;
    }
    
    
    public void add(CD cd) {
        cds[numCDs] = cd;
        ++numCDs;
    }
    
    public void display() {
        System.out.println("\nCD Inventory\n");
        
        System.out.println("PartID   Name                   Copies     Price        Value");
      
    }


} // end class Inventory


*Edited to add the [ code] tags to make the code readable. Please code.gif

This post has been edited by pbl: 18 Dec, 2008 - 04:31 PM

User is offlineProfile CardPM
+Quote Post


BigAnt
RE: Java Inventory Program 2
18 Dec, 2008 - 03:17 PM
Post #2

May Your Swords Stay Sharp
Group Icon

Joined: 16 Aug, 2008
Posts: 2,384



Thanked: 95 times
Dream Kudos: 75
My Contributions
What loop do you need help with?

Also code.gif
User is offlineProfile CardPM
+Quote Post

evak77
RE: Java Inventory Program 2
18 Dec, 2008 - 03:51 PM
Post #3

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 9


My Contributions
QUOTE(BigAnt @ 18 Dec, 2008 - 03:17 PM) *

What loop do you need help with?

Also code.gif



I need to loop thru the inventory, displaying each CD. I am really horrible at programming and it is my last class for my degree. My area of expertise in in graphics and design.
User is offlineProfile CardPM
+Quote Post

evak77
RE: Java Inventory Program 2
18 Dec, 2008 - 04:05 PM
Post #4

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 9


My Contributions
Soory that did not go through, I need to write a loop that will loop through and show all of the inventory.



QUOTE(BigAnt @ 18 Dec, 2008 - 03:17 PM) *

What loop do you need help with?

Also code.gif


User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Inventory Program 2
18 Dec, 2008 - 04:40 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,951



Thanked: 673 times
Dream Kudos: 200
My Contributions
CODE

  public void display() {
        System.out.println("\nCD Inventory\n");
        for(int i = 0; i < numCDs; i++) {
              System.out.println(CDs[i].getName());
        }
    }

User is offlineProfile CardPM
+Quote Post

evak77
RE: Java Inventory Program 2
20 Dec, 2008 - 03:29 PM
Post #6

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 9


My Contributions
Thanks very much, it helped out alot.



QUOTE(pbl @ 18 Dec, 2008 - 04:40 PM) *

CODE

  public void display() {
        System.out.println("\nCD Inventory\n");
        for(int i = 0; i < numCDs; i++) {
              System.out.println(CDs[i].getName());
        }
    }



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 03:53PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month