某种方法的位置

时间:2014-04-10 04:42:28

标签: java

所以我有这个程序本质上是产品库存/商店/购物车。用户输入产品编号,然后将其添加到购物车中,这就是它。问题是我的驱动程序包含很大比例的功能代码。我希望下面的部分在另一个课程中,但我不知道如何去做。有什么帮助吗?

if (entry == P0N) { //initial product number...must be as many as there are products
    System.out.println ("HOW MANY?");
    int cart = Keyboard.readInt();
    if (cart > P0Q) {
        System.out.println ("EXCEEDS AVAILABLE QUANITITY!\nPLEASE TRY AGAIN!"); //will send user to the main menu
    } else if (cart <= P0Q) {
        P0C += cart;
        P0Q -= cart;
        MyCart.add ("Gloves\t\t\t"+cart);
    }
}

1 个答案:

答案 0 :(得分:0)

你不应该在这么简单的程序上使用Keyboard类,从基本的Java API(ahem * Scanner * ahem)开始。

除此之外,你的小“程序”并不复杂也不分开;我不明白为什么你需要将你的代码放在不同的类中,你甚至不需要除了主要类之外的任何类。这样做:

public class Inventory {
      public static void main(String[] args) {
    //declare your variables
    if (entry == P0N) { //initial product number...must be as many as there are products
    System.out.println ("HOW MANY?");
    int cart = Keyboard.readInt();
    if (cart > P0Q) {
        System.out.println ("EXCEEDS AVAILABLE QUANITITY!\nPLEASE TRY AGAIN!"); //will send user to the main menu
    } else if (cart <= P0Q) {
        P0C += cart;
        P0Q -= cart;
        MyCart.add ("Gloves\t\t\t"+cart);
    }

    }

}