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: Help cleaning up my code.

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Help cleaning up my code.

    I am pretty proud of myself really. I looked online for the assignment and found the answers and code pretty much done for me, but I decided I was going to give it a whack.

    Here's the assignment:


    Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information about one product at a time, including the item number, the name of the product, the department of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.

    Create a method to calculate the value of the entire inventory.

    Create another method to sort the array items by the name of the product.

    Here's my code:

    //Thomas Harrald
    //IT215 Checkpoint Inventory Program
    //Camera Inventory program
    import java.util.Scanner;
    import java.util.Arrays;
     
    // Stores and then gets info on a Camera
    public class CameraProgram2
    {
    	public static void main(String args[])
    	{
    		//Declare Arrays and Scanner
    		Scanner in = new Scanner(System.in);
    		int[] arrayItem = new int[3];
    		String[] arrayName = new String[3];
    		String[] arrayDept = new String[3];
    		int[] arrayUnits = new int[3];
    		double[] arrayPrice = new double[3];
    		double[] arrayTotal = new double[3];
    		double totalValue = 0;
     
    		//Build the arrays
    		for( int counter =0; counter<=2; ++counter) //For loop to add each part of the array
    		{
    			// input product info
    			System.out.println("Enter the item number:");
    			arrayItem[counter] = Integer.parseInt(in.nextLine());
     
    			System.out.println("Enter the name of the Camera:");
    			arrayName[counter] = in.nextLine();
     
    			System.out.println("Enter the name of the department:");
    			arrayDept[counter] = in.nextLine();
     
    			System.out.println("Enter the units in stock:");
    			arrayUnits[counter] = Integer.parseInt(in.nextLine());
     
    			System.out.println("Enter the price:");
    			arrayPrice[counter] = Double.parseDouble(in.nextLine());
     
    			arrayTotal[counter] = arrayUnits[counter] * arrayPrice[counter]; //Assignment statement for Total Value of the product Units * Price
     
    			totalValue += arrayTotal[counter]; //Adds the arrayTotal value to totalValue
     
    		}
     
                    //For loop to display arrays
    		for( int count =0; count <= 2; ++count)
    		{
    			System.out.printf("\nItem Number:%d\nItem Name:%s\nItem Department:%s\nUnits in Stock:%d\nTotal Value of product:%f\n", 
    				arrayItem[count], arrayName[count], arrayDept[count], arrayUnits[count], arrayPrice[count], arrayTotal[count]);
    		}
     
                    //Sorts array products names and displays them in order
    		Arrays.sort(arrayName);
    		System.out.println(Arrays.toString(arrayName));
     
     
    		System.out.printf("\n%f\n", totalValue);
     
     
     
    	}
    }

    However I don't think the way the displaying of names is exactly what the professor is looking for, however it does not clearly state that the display needs to include all of the products information... so...

    Anyways, any tips to improve this program?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help cleaning up my code.

    Quote Originally Posted by Harrald View Post
    Modify the Inventory Program so the application can handle multiple items.

    Use an array to store the items.

    The output should display the information about one product at a time, including the item number, the name of the product, the department of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product.

    In addition, the output should display the value of the entire inventory.

    Create a method to calculate the value of the entire inventory.

    Create another method to sort the array items by the name of the product.
    If you break the instructions down into parts, I would start with the way I have listed them from your quote.
    Then I would group like items as follows:
    Modify the Inventory Program so the application can handle multiple items. Use an array to store the items.


    Create a method to calculate the value of the entire inventory.


    Create another method to sort the array items by the name of the product.


    The output should display the information about one product at a time, including the item number, the name of the product, the department of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.
    Looking at the instructions somewhat organized, I see 4 parts. Your code does not have any methods at all. So some work should be done in that respect.

    Try to organize your plan before you write code. Get an idea of what you need to do, and how it needs to be done. Go over it a few times and make sure everything seems to be in order. Then start on the code. Right now you have everything stuck inside the main method. Rather than performing all of your tasks in order, build methods which will perform your tasks, and call the methods from main, (in your desired order). The idea is to get those methods outside of main to do all of your work. Suppose the next assignment will be of the same nature, and rely on the methods you build now. That is what happens in many coding environments. Start out with the basics and get it going. Then you can build from there. Before long your guess-my-number from 1-100 program has turned into a magical 8-ball with witty remarks.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Help cleaning up my code.

    Here's the cleaned up code... and I believe it accomplishes what the professor was looking for too.

    //Thomas Harrald
    //IT215 Checkpoint Inventory Program
    //Camera Inventory program
    import java.util.Arrays;
     
    // Stores and then gets info on a Camera
    public class CameraProgram3
    {
     
    	public static void main(String args[])
    	{
     
    		CameraClass[] cameras;
     
    		CameraClass nikonCamera= new CameraClass( 3100, "Nikon", "Electronics", 5, 10.00);
    		CameraClass sonyCamera = new CameraClass( 4500, "Sony", "Electronics", 10, 300.00);
    		CameraClass canonCamera= new CameraClass( 1550, "Canon", "Electronics", 3, 40.00);
     
    		cameras = new CameraClass[ 3 ];
     
    		cameras[ 0 ] = nikonCamera;
    		cameras[ 1 ] = sonyCamera;
    		cameras[ 2 ] = canonCamera;
     
    		System.out.println(cameras[0]);
    		System.out.println(cameras[1]);
    		System.out.println(cameras[2]);
     
     
    		System.out.printf( "\nTotal Value of inventory: %f\n", (nikonCamera.value() + sonyCamera.value() + canonCamera.value()))	;
     
    	}
    }

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Help cleaning up my code.

    Here's tonight's assignment, part 3 of the last one.

     
    //Thomas Harrald
    //Camera Super Class
    //IT215 Inventory Program
     
    public class CameraClass
    {
    	private int item;
    	private String name;
    	private String dept;
    	private int units;
    	private double price;
     
    	// constructor
    	public CameraClass(int classItem, String className, String classDept, int classUnits, double classPrice) 
    	{
    		item = classItem;
    		name = className;
    		dept = classDept;
    		units = classUnits;
    		price = classPrice;
    	}
     
    	// total value
    	public double value() 
    	{
    		return units*price;
    	}
     
    	// getters and setters
    	public int getItem() 
    	{
    		return item;
    	}
     
    	public void setItem(int classItem) 
    	{
    		classItem = item;
    	}
     
    	public String getName() 
    	{
    		return name;
    	}
     
    	public void setName(String className) 
    	{
    		className = name;
    	}
     
    	public int getUnits() 
    	{
    		return units;
    	}
     
    	public void setUnits(int classUnits) 
    	{
    		classUnits = units;
    	}
     
    	public double getPrice() 
    	{
    		return price;
    	}
     
    	public void setPrice(double classPrice) 
    	{
    		classPrice = price;
    	}
     
    	public void setDept(String classDept)
    	{
    		classDept = dept;
    	}
     
    	public String getDept()
    	{
    		return dept;
    	}
     
    	public String toString()
    	{
    		return "Item number: " + item + " Item Name:  " + name + " Dept: " + dept + " Units: " + units + " Price: " + price;
    	}
    }

    //Thomas Harrald
    //Camera Subclass
     
    public class DigitalCamera extends CameraClass
    {
    	private double ImageResolution;
     
    	public DigitalCamera( int classItem, String className, String classDept, int classUnits, 
    				double classPrice, double resolution)
    	{
    		super(classItem, className, classDept, classUnits, classPrice);
    		setResolution( resolution);
    	}
     
    	public void setResolution(double resolution)
    	{
    		ImageResolution=resolution;
    	}
     
    	public double getResolution()
    	{
    		return ImageResolution;
    	}
     
    	@Override
    	public double value()
    	{
    		return getUnits()*getPrice() + (getUnits()*getPrice()*.05);
    	}
     
    	@Override
    	public String toString()
    	{
    		return "Item number: " + getItem() + " Item Name:  " + getName() + " Dept: " + getDept() + 
    			" Units: " + getUnits() + " Price: " + getPrice() + " Image Resolution: " + getResolution();
    	}
    }

    //Thomas Harrald
    //IT215 Checkpoint Inventory Program
    //Camera Inventory program
    import java.util.Arrays;
     
    // Stores and then gets info on a Camera
    public class CameraProgram3
    {
     
    	public static void main(String args[])
    	{
     
    		CameraClass[] cameras;
     
    		CameraClass nikonCamera= new CameraClass( 3100, "Nikon", "Electronics", 5, 10.00);
    		CameraClass sonyCamera = new CameraClass( 4500, "Sony", "Electronics", 10, 300.00);
    		CameraClass canonCamera= new CameraClass( 1550, "Canon", "Electronics", 3, 40.00);
    		DigitalCamera kodakCamera = new DigitalCamera( 3000, "Kodak", "Electronics", 20,5.00, 1080);
     
    		cameras = new CameraClass[ 4 ];
     
    		cameras[ 0 ] = nikonCamera;
    		cameras[ 1 ] = sonyCamera;
    		cameras[ 2 ] = canonCamera;
    		cameras[ 3 ] = kodakCamera;
     
    		System.out.println(cameras[0]);
    		System.out.println(cameras[1]);
    		System.out.println(cameras[2]);
    		System.out.println(cameras[3]);
     
     
    		System.out.printf( "\nTotal Value of inventory: %f\n", (nikonCamera.value() + sonyCamera.value() + canonCamera.value() + kodakCamera.value()))	;
     
    	}
    }

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Help cleaning up my code.

    Totally lacking comments... will work on that.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help cleaning up my code.

    I think you should have a loop or two in there to cut down on some of the repeated code doing the same thing to a different object

Similar Threads

  1. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  2. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM