Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need help with displaying items with my program

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    My Mood
    Devilish
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with displaying items with my program

    nid help when displaying added items in my program
    i've been working with this 2 days now and i cant make it to run properly


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     *
     * @author Gasendo
     */
    public class Book{
        private String bookTitle;
        private int bookYear;
     
        public String getBookTitle() {
            return bookTitle;
        }
     
        public void setBookTitle(String bookTitle) {
            this.bookTitle = bookTitle;
        }
     
        public int getBookYear() {
            return bookYear;
        }
     
        public void setBookYear(int bookYear) {
            this.bookYear = bookYear;
        }
        public String toString(){
            String output = "";
            output = "" + this.getBookTitle();
            output += ", " + this.getBookYear();
     
            return output;       
     
        }
     
        }

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    import java.util.*;
    /**
     *
     * @author Gasendo
     */
    public class Accessory{
        private String accName;
         private String accColor;
        private double price;
     
        public String getAccColor() {
            return accColor;
        }
     
        public void setAccColor(String accColor) {
            this.accColor = accColor;
        }
     
        public String getAccName() {
            return accName;
        }
     
        public void setAccName(String accName) {
            this.accName = accName;
        }
     
        public double getPrice() {
            return price;
        }
     
        public void setPrice(double price) {
            this.price = price;
        }
     
         public String toString(){
            String output = "";
            output = "" + this.getAccName();
            output += "\n" + this.getAccColor();
            output += "\n" +this.getPrice();
     
            return output;
     
         }   
     
    }
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Gasendo
     */
    public class Apparel {
        private String appName;
        private String appColor;
     
        public String getAppColor() {
            return appColor;
        }
     
        public void setAppColor(String appColor) {
            this.appColor = appColor;
        }
     
        public String getAppName() {
            return appName;
        }
     
        public void setAppName(String appName) {
            this.appName = appName;
        }
         public String toString(){
            String output = "";
            output = "" + this.getAppName();
            output += ", " + this.getAppColor();
     
            return output;
         }
    }

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    import java.util.*;
    /**
     *
     * @author Gasendo
     */
    public class Cabinet{
        public static void main(String[]args){
            Scanner mike = new Scanner(System.in);
     
            boolean done = false;
     
            while(!done){
     
            System.out.println("Please choose an option from the given menu below. Just type the number. ");
            System.out.println("[1] Add Book\n[2] Add Apparel\n[3] Add Accessory\n[4] Display Books\n[5] Display Apparel\n[6] Display Accessory\n[7] Exit");
     
     
            System.out.print("What do you want to do? ");
            int m = mike.nextInt();
     
     
     
            if(m==1){
     
                Book book = new Book();
                System.out.println("You choose to add a book.");
                System.out.print("Book name/title: ");
                String title = mike.next();
                book.setBookTitle(title); 
                System.out.print("Publication Year: ");
                int year = mike.nextInt();
                book.setBookYear(year);
                System.out.println("Book added successfully! ");
     
     
            }
     
              if (m==2){
     
                Apparel apparel = new Apparel();
                System.out.println("You choose to add apparel.");
                System.out.print("Apparel Name: ");
                String appname = mike.next();
                apparel.setAppName(appname);
                System.out.print("Color: ");
                String appcolor = mike.next();
                apparel.setAppColor(appcolor);
                System.out.println("Apparel added successfully! ");
     
              }
               if (m==3){
     
                Accessory accessory = new Accessory();
                System.out.println("You choose to add accessory.");
                System.out.print("Acessory Name: ");
                String accname = mike.next();
                accessory.setAccName(accname);
                System.out.print("Color: ");
                String acccolor = mike.next();
                accessory.setAccColor(acccolor);
                System.out.print("Price: ");
                double price = mike.nextDouble();
                accessory.setPrice(price);
                System.out.println("Accessory added successfully! ");
               }
                if(m==7){
     
                    System.exit(0);
                }
            } 
        }
    }

    my desired output is this:
    PHP Code:
    Please choose an option from the given menu belowJust type the number
    [
    1Add Book
    [2Add Apparel
    [3Add Accessory
    [4Display Books 
    [5Display Apparel
    [6Display Accessory
    [7] Exit

    What do you want to do? 1
    You choose to add a book
    .

    Book name/title Java Book
    Publication Year
    2009 

    Book added successfully
    !

    Please choose an option from the given menu belowJust type the number.
    [
    1Add Book
    [2Add Apparel
    [3Add Accessory
    [4Display Books 
    [5Display Apparel
    [6Display Accessory
    [7] Exit

    What do you want to do? 2
    You choose to add apparel
    .

    Apparel NamePants
    Color       
    Blue 

    Apparel added successfully
    !

    Please choose an option from the given menu belowJust type the number.
    [
    1Add Book
    [2Add Apparel
    [3Add Accessory
    [4Display Books 
    [5Display Apparel
    [6Display Accessory
    [7] Exit

    What do you want to do? 3
    You choose to add accessory
    .

    Accessory NameEarring
    Color         
    Silver 
    Price          
    5000.00
    Accessory added successfully

    a little hint will do buddies.
    Last edited by mike101290; July 21st, 2011 at 03:54 AM.


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Need help with displaying items with my program

    you need to save your Books and other class to some container. in your case ArrayList will be sufficient.
    For example : List<Book> myBooks = new ArrayList<Book>();
    Last edited by remigio; July 21st, 2011 at 04:50 AM.

  3. The Following User Says Thank You to remigio For This Useful Post:

    mike101290 (July 21st, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    My Mood
    Devilish
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with displaying items with my program

    you mean i need to create another class for arraylist?
    sorry for my ignorance its just my 2nd semester in java.

  5. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Need help with displaying items with my program

    no, no... You must store your elements from (for example) Book class in Arraylist. create ArrayList in your main class.

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    My Mood
    Devilish
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with displaying items with my program

    i know how to use pre-defined values for arrayList but when it comes to displaying user defined values im screwed.

  7. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    My Mood
    Devilish
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with displaying items with my program

    i used case statement and researched on arraylist user defined method, thanks to you remigio! theres not much u can get in this forum unless you will go to liveperson.com which is not free, nyc marketing strategy though. Gud luck!

Similar Threads

  1. my High profitable items: 0 Bonus : $0.00 why??
    By chonch in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 3rd, 2011, 10:31 PM
  2. Getting items from a table/checkboxes
    By beth in forum AWT / Java Swing
    Replies: 2
    Last Post: January 5th, 2011, 01:29 PM
  3. misalignment of list items
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: March 15th, 2010, 08:33 AM
  4. How to get a sum value from items listed in array ?
    By makarov in forum Loops & Control Statements
    Replies: 0
    Last Post: January 6th, 2010, 06:11 PM
  5. Different operation on Array
    By jempot in forum Collections and Generics
    Replies: 4
    Last Post: January 27th, 2009, 06:07 AM