nmohamma's Profile User Rating: -----

Reputation: 4 Apprentice
Group:
Members
Active Posts:
26 (0.04 per day)
Joined:
13-June 11
Profile Views:
1,483
Last Active:
User is offline Sep 02 2011 02:35 PM
Currently:
Offline

Previous Fields

Country:
BD
OS Preference:
Windows
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
Playstation
Your Car:
Mazda
Dream Kudos:
0
Icon   nmohamma Wanna-be-something??

Posts I've Made

  1. In Topic: How to Store my Inventory Items in an Array?

    Posted 18 Jun 2011

    View PostITStudent02, on 19 June 2011 - 12:44 AM, said:

    Thank you, I have looked at your code, and I sort of understand what was happening however, my main concern is that you have more than one "main" is that even possible? Also, in this line of code:

    public static void main ( String [] args) {
    	         Item [] inventory = new Item [10];
    	         inventory[0] = new Item ("pliers", 2671, 5348.56);
    	         inventory[1] = new Item ("hammer", 531, 876.44);
    	         inventory[2] = new Item ("screw driver", 888, 1348.82);
    	         for (int i = 0; i< 3; ++i)
    	            System.out.println (inventory[i]);
    


    I was wondering If I deleted my whole inventory class and used this logic (the one you did shown above)it might work better to calculate all of my items and send them out in ascending order? Thank I will try this? ;)


    Yes/No...LOL
    No: in a sense that one class can't have more than one main()
    Yes: different classes can have their own single main(), it does not cause any problem

    See you figured this out... post your final draft and i'm sure people will be able to help you out.

    Take care
  2. In Topic: How to Store my Inventory Items in an Array?

    Posted 18 Jun 2011

    OOPS!! my bad, see what happens to newbies?? LOL...no worries

    I forgot to include the most important file and that is the items.txt, the file that the program reads from

    Here it is: Attached File  items.txt (111bytes)
    Number of downloads: 48

    As I have mentioned earlier put all files first in a folder named lab4 except the app.java file and items.txt file and keep these two files just outside the lab4 folder and then try compiling and run.

    Hope it would work.

    By the way, the code is provided so you can work around and understand the way the problem can be handled.

    I do understand your situation and sorry that I could not be of more help as I said that I am not a professional programmer in JAVA either, still a learner.

    anyways, good luck !!!
  3. In Topic: How to Store my Inventory Items in an Array?

    Posted 18 Jun 2011

    There is a very similar code that I did while in my 12th grade, I'm sharing with you so that you can make adjustments with your requirements. Is this the format your instructor wants exactly?

    it is always a good idea to break your entire code into smaller manageable chunks of code and store them in a file by themselves.Like the ones provided.

    Note: keep all of the files into a folder named lab04 except the app.java
    //This is App.java
    //Lab4                                     Niaz Mohammad
    //
    //CS211-lab section 203
    //
    //Instructor: MVIEIRA
    //
    //Prints out the names,quantity and profits gain from some hardware tools in formatted String
    
       import java.io.*;
       import java.util.*;
       import lab4.*;
    
        public class App {
           public static void main (String [] args) {
             Scanner infile = null;
             Inventory storeA = new Inventory();
          
            //open file
             try {
                infile = new Scanner (new File("items.txt"));
             }
                 catch (FileNotFoundException e) {
                   System.out.println ("Problems opening file");
                   System.exit (1);
                }
          
          
          // readInventory
             String name = null; int amount =0; double profit = 0;
             while (infile.hasNext()) {
                try{
                   name = infile.next();
                   amount = infile.nextInt();
                   profit = infile.nextDouble ();
                   storeA.add(new Item(name,amount,profit));
                }
                    catch (NoSuchElementException e) {
                      System.out.println ("failed to add "+ name);
                   }
             }
          
          //showInventory
             System.out.println (storeA);
            
            // close file
             if (infile != null)
                infile.close();
            
          }           
       }
    	      
            
    
    


    //the following files goes to lab4 folder

    //Inventory.java

    //Lab4                                     Niaz Mohammad
    //
    //CS211-lab section 203
    //
    //Instructor: MVIEIRA
    //
    //Prints out the names,quantity and profits gain from some hardware tools in formatted String
    
       package lab4;
    
       import java.text.NumberFormat;
        public class Inventory {
          Item [] items;
          int count;
       
           public Inventory() {
             items = new Item[2];
             count=0;
          }
       
           public void add(Item item){
             items [count] = item;
             count++;
             if(count==items.length)
                increaseSize();
          }
           private void increaseSize()
          {
             Item[]temp=new Item[items.length*2];
             for(int c=0;c<items.length;c++)
                temp[c]=items[c];
          
             items=temp;
          
          }
       
           public String toString () {
             StringBuffer buf = new StringBuffer ();
             buf.append (String.format 
                ("%-15s%7s%15s\n","Name","Count","ItemProfit"));
             for (int i =0; i< count; ++i){
                buf.append(items [i]);
                buf.append ("\n");
             }
             return buf.toString();
          }
       
           public static void main (String [] args) {
             Inventory storeA = new Inventory ();
             storeA.add (new Item ("pliers", 2671, 5348.56));
             storeA.add (new Item ("hammer", 531, -876.44));
             storeA.add (new Item ("screw driver", 888, 1348.82));
             System.out.println (storeA);
          }
       }
    
    


    //this is Item.java

    //Lab4                                     Niaz Mohammad
    //
    //CS211-lab section 203
    //
    //Instructor: MVIEIRA
    //
    //Prints out the names,quantity and profits gain from some hardware tools in formatted String
    	
       package lab4;
    
    
        public class Item {
          String name;
          int quantity;
          double profitTotal;
       
           public Item (String name, int amount, double profit) 
          {
             this.name = name;
             quantity = amount;
             profitTotal = profit;
          }
       
           public String toString () 
          {
             String result=String.format("%-15s%,7d%(,15.2f", name,quantity,profitTotal/quantity);
             return result;
            /*results printout in fields of width
             15,7,15 respectively.  Use flags on 
             double field to print commas, have precision 2,
             and show negatives with surrounding () */ 
          }
       
           public static void main ( String [] args) {
             Item [] inventory = new Item [10];
             inventory[0] = new Item ("pliers", 2671, 5348.56);
             inventory[1] = new Item ("hammer", 531, 876.44);
             inventory[2] = new Item ("screw driver", 888, 1348.82);
             for (int i = 0; i< 3; ++i)
                System.out.println (inventory[i]);
          }
       }
    
          
    
    


    After compiling all, run the App.java

    Sorry, it was taking a whole lot of brain sugar for me to put things into order from your given code.
  4. In Topic: How to Store my Inventory Items in an Array?

    Posted 18 Jun 2011

    Please provide your codes in code tag, since it is very hard to understand it other ways, I am a newbie as I assume you are too, and have learned it the hard way...LOL
  5. In Topic: How to delete my blog

    Posted 15 Jun 2011

    The two blogs are still showing under the current blog settings, however, they are not accessible with a message "unauthorized to access" or something like that, if that's what you are referring to as being deleted, then so it is cause I don't know anything about it.

    So, am I safe to open a new blog now??

    Thanks by the way, for doing it for us for free...LOL

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Location:
Dhaka, Bangladesh
Interests:
C, C++, JAVA, C#, Web Design and Development
Full Name:
Niaz Mohammad
Years Programming:
4
Programming Languages:
C, C++, C#, JAVA, HTML, CSS, PHP, javascript, Maple, Matlab

Contact Information

E-mail:
Click here to e-mail me
Website URL:
Website URL  http://www.liveintheplanetgreen.wordpress.com

Comments

Page 1 of 1
  1. Photo

    nmohamma Icon

    26 Jun 2011 - 05:02
    LOL...sorta i guess, thanks @ahamedirshad123
  2. Photo

    ahamedirshad123 Icon

    23 Jun 2011 - 05:28
    you look like shakib-ul-Hassan
Page 1 of 1