OOP Java:创建库存清单程序 [英] OOP Java: Creating a stock inventory program

查看:82
本文介绍了OOP Java:创建库存清单程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对面向对象的编程还很陌生,因此在掌握一些基本概念时仍然遇到一些麻烦.因此,我在这里尝试创建一个基本的库存计划以跟踪库存.每只股票包含几个详细信息:公司名称,股票评级(AAA,AAaa,Aaa,诸如此类),购买价格和股票数量.该程序将要求用户通过命令行提示符输入这些详细信息.用户最多只能输入12种股票.如果用户两次输入股票,它将打印出错误.如果用户两次输入一只股票,它也会打印出一条错误消息.

I am fairly new to object oriented programming, so I am still having some trouble grasping some of the basic concepts. So here I am trying to create a basic inventory program to keep track of stocks. Each stock contains couple details: company name, stock rating (AAA, AAa, Aaa, stuff like that), purchase price and numbers of shares. The program will ask user to input these details through command line prompt. And users can only input at most 12 stocks. If the user enters a stock twice, it will print out an error. And if the user has inputted one stock twice, it will also print out an error message.

这是我到目前为止所做的:库存对象

Here is what I have done so far: Stock object

 public class Stock {

private String companyName;
private String stockRating;
private int price;
private int numberOfShares;

public String getCompanyName() {
    return companyName;
}

public int getStockRating() {
    return stockRating;
}

public String getPrice() {
    return price;
}

public int getNumberOfShares() {
    return numberOfShares;
}

public Stock(String companyName, String stockRating, int price, int numberOfShares) {
    super();
    this.companyName = companyName;
    this.stockRating = stockRating;
    this.price = price;
    this.numberOfShares = numberOfShares;
}

现在,我正在尝试创建库存清单程序

Now, I am trying to create stock inventory program

import java.util.*;

public class StockInvetory {

private static final int INVENTORY_SIZE = 12;
private Stock [] stocks;

public StockInvetory() {
    stocks = new Stock [INVENTORY_SIZE];

}

private static void StockInventory() {
       for (int i = 0; i<INVENTORY_SIZE; i++){
         Scanner console = new Scanner(System.in);

    System.out.println ("Stock's name:");
    String stockName = console.next();

    System.out.println ("Stock's rating");
    String stockRating= console.next();

    System.out.println ("Stock's price:");
    int stockPrice = console.nextInt();

    System.out.println ("Numbers of shares: ");
    int numberShares= console.nextInt();

          stocks [i]= new Stock(stockName, stockRatings, stockPrice, numberShares);
    }

public static void main (String [] args){
    StockInventory();



}

}

所以我的问题如下:

第一个库存对象程序应该可以,我的问题是库存程序. 使用此代码,私有Stock [] stocks,是否意味着库存清单程序会将信息存储到数组中?如果是这样,我如何将与特定股票,公司名称,价格等相关的所有详细信息存储在一起.我是否必须创建一个多维数组以跟踪所有细节?就像第一行包含有关一只股票的所有详细信息,第二行包含有关另一只股票的详细信息一样.为了确定用户是否两次输入一只股票,我是否使用"equalsIgnoreCase"进行比较?我知道我可能需要为库存创建另一种方法.

The first stock object program should be okay, my problem is the stock inventory program. With this code, private Stock [] stocks, does it mean that the stock inventory program will store the info into an array? If it is, how do I store all the details associated with a particular stock, company name, price, etcs together. Do I have to create a multi-dimensional array in order of keep track of all the details? Like one row contains all the details about one stock, and second row contains details about another stock. And to determine if the user has entered one stock twice, do I use "equalsIgnoreCase" to do the comparison? I know I probably need to create another method for the stock Inventory.

非常感谢您的协助.

推荐答案

阅读名称,等级,价格和股数后,您需要在类Stock上调用构造函数以创建该类的实例,并将其分配给stocks []数组中的下一个项目.

Once you read in the name, rating, price, and share count, you need to call the constructor on your class Stock to create an instance of the class and assign it to the next item in your stocks[] array.

像这样:

stocks[0] = new Stock( stockName, stockRating, stockPrice, numberShares);

然后,您需要将要从控制台读取的代码行以及创建新Stock对象的行放入循环,以便可以读取所有12种库存:

Then you'll need to put the lines of code that you're using to read from the console, plus my line that creates the new Stock object into a loop so that you can read in all 12 stocks:

for( int i = 0; i < INVENTORY_SIZE; i++ )
{
    System.out.println ("Stock's name:");
    String stockName = console.next();

    System.out.println ("Stock's rating");
    String stockRating= console.next();

    System.out.println ("Stock's price:");
    int stockPrice = console.nextInt();

    System.out.println ("Numbers of shares: ");
    int numberShares= console.nextInt();

    stocks[i] = new Stock( stockName, stockRating, stockPrice, numberShares);
}

现在,这并不是完美的,因为这将要求用户始终输入全套12种股票,因此您需要弄清楚如何让用户中途退出市场,并且您仍然必须添加所需的错误检查,以确保没有重复输入,但是它应该初始化您的单个对象并将每个对象分配给数组元素.

Now, this isn't perfect, since it will require users to always enter a full set of 12 stocks, so you'll need to figure out how to let the user abort out of the loop if they're done, and you'll still have to add that error checking you want, to ensure that no duplicates are entered, but it should initialize your individual objects and assign eachh one to the array elements.

这篇关于OOP Java:创建库存清单程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆