简体   繁体   中英

How do you write a main method to start a java program?

I'm a beginner programmer and am tasked to write an inventory program. I have only programmed using BlueJay so far, but am about to learn how to use the vim editor. When programming with BlueJay, you didn't need to write a main method. I'm so lost about how to write the main method and everything I researched over the internet didn't seem to explain or help much.

I have already started the design of the program where I have an Inventory class and an Item class. How would I go about starting this project? Like what do I need to do with the main method and how would that work?

Thanks

Here is the code I have so far.

import java.util.*;

public class Inventory
{
    private ArrayList<Item>inventory;

    /**
     * Constructor for objects of class Inventory
     */
    public Inventory()
    {
        inventory = new ArrayList<Item>();
    }

    /**
     * Adds an Item to the Inventory.
     */
    public void addItem(String name, int amount, double price, int location)
    {
        boolean done = false;
        if(inventory.size() == 0)
        {
            inventory.add(new Item(name, amount, price, location));
        }
        else
        {
            for(int i = 0; (!done)&&(i < inventory.size()); i++)
            {
                if(inventory.get(i).getName().equals(name))
                {
                    System.out.println("Item name in use. Please use another name.");
                    done = true;
                }
                else
                {
                    inventory.add(new Item(name, amount, price, location));
                    done = true;
                }
            }
        }
    }

    /**
     * Deletes an Item from the Inventory.
     */
    public void deleteItem(String name)
    {
        ...........
    }

    /**
     * Search for an Item.
     */
    public void searchItem(String name)
    {
        ...........
    }
}

There are answers for this everywhere in Java documentation. But here it is.

public class App {

    public static void main(String [] args)
    {
        //start here
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM