Java gui code
Okay. I'll try and explain what this code is doing for you.
First thing you need to understand is your "gui" is what is known as "Standard Out" and "Standard In", this is how Java outputs text to the console, and how it inputs text written by a user from the console. A function like "System.out.println([some string])" will print out that line to the console.
Second thing is the idea of classes. A class can be thought of like a code "blueprint". You write the blueprint, and then you create individual "objects" based on that class in your code. While each object is independant, and has it's own properties, functions, and its own spot in memory, it is based on the same code.
If I made a class called, say, "Shape", and I gave it two properties, "Size" and "Color", then I could in my program make lots of different "Shape" objects and they would each have their own independant sizes and colors. Yet, if I made a function in the class called "Draw" which magically drew it on the screen in the appropriate size and color that I had specified, I could call "Draw" for each object, and each shape would be drawn on teh screen. The same code is being executed for each shape, but with different values for the size and color, since each shape object is its own unique entity with its own size and color.
The whole point of classes is so that you don't rewrite code. If you needed to have 1000 shapes on the screen, it would be silly to write the drawing code out 1000 times, one for each shape. It's much easier to write a single blueprint that applies to all shapes.
That said, your code above has a number of "classes" at work.
The "Products" class is a class which stores information about a "Product". In your case, this info looks like it is the name of the product, it's price, how many are in stock etc.
The "Inventory" class is a class who's purpose it is to keep track of an array of products. There are a bunch of functions inside this class. I'll describe what they do below
The buildinventory() function appears to build up a list of products for use in the inventory. Notice how it calls the line:
product[0] = new Product(1, "socket set", 3, 19.99);
filling up the "product" array? Each call there created a new "Product" object which was based on the "Product" class. So each object has it's own name, inventory number, stock, price etc, but they all have the same set of functions to do things like, say, get the price of the product (the getPrice() function), or get the name of the product (the getName() function). It's the same function, as defined in the Product class, but when called on a different product object, it will return different results. This works well for an inventory program.
The "DisplayInventory" function loops through all of the products that the inventory class has created, and outputs information about each one to the screen. By the looks of it it is also keeping track of the total value of the inventory, in a variable called "totalPrice". Every time it loops through a product, it adds the product's price onto the total. The idea is once that while loop has finished, that variable will hold the final "total" of the inventory. After the loop has finished you can do a System.out.println() and use that variable to display the final total. This is probably part of what your project is asking you to do.
The "SortInventoryByName" function simply sorts the list of products that the Inventory class has created based on the name of the product. It does this using a build in Java class that you don't need to worry about.
You'll notice a function that is just called "Inventory()" near the top. This is called the "constructor". When you create a class, the class sometimes has to do a bit of initialization work before it is usable. This function is called as soon as the class is instantiated as an object somewhere in your code. So in this case, when you eventually create an inventory object, that function will be called and the inventory will be built (by calling buildInventory()), sorted (by calling sortInventoryByName()), and displayed (by calling displayInventory()).
------------
So it looks like you have this great Inventory class that does everything you need it to do. So now what? It still doesn't do anything! The reason is because it itself is just a class. It's a BLUEPRINT for an inventory. You need a Java program to USE the inventory class to actually do something useful.
You need to make a java program with a sub main() in it (just like every program you've ever made in this class). And inside there initialize a new inventory object, just like how the inventory object made product objects.
e.g.
Inventory myInventory = new Inventory();
Remember the Inventory() constructor! It will get called here, and the inventory class will go ahead and build an inventory, sort it, and display it on the screen.
So hopefully this will help you to understand your class a bit better. Again, real programmers don't just take classes and use them, they need to understand how they work. It's true that occasionally you can find something that does what you need it to do, and you don't need to "Reinvent" the wheel, but if you are unable to understand what it is doing, you can never modify it or tweak it, and if your problem isn't perfectly solved using the class, the class is useless to you. Programmers write their own modules and re-use them as they see fit, and occasionally they use someone else's code as a template. This is why you are learning how to modify an existing class.
×