JBTALKS.CC

标题: JAVA高手帮帮忙 [打印本页]

作者: derrick90    时间: 2009-7-3 10:53 PM
标题: JAVA高手帮帮忙
各位高手帮帮忙,这是小弟的作业题目(英文)请别介意。

Task 1
(a) Define the class Item to store the details of an item sold
in the bookshop. Each object of Item should store the
following information:
· Item number
· Item description
· Selling price
· Quantity on hand
Besides the set and get methods, Item class should
also include:
· A no-argument constructor
· A constructor with parameters
· The method equals that returns true if two objects
contain the same information.
(b) Write a test driver to test the various operations of the
class Item.
Task 2
Create an application program that declares an array of 100
elements of type Item. Your program should provide the
following operations which the user may invoke from a menu:
· Add a new item into the array (duplicate entries are not
allowed).
· Amend the details of an item.
· Search for an item by item number.
· Enter a sales transaction (the quantity on hand has to
be reduced accordingly).
· Input stock received (the quantity on hand has to be
updated accordingly).
· List details of all items – one item’s detail per line, with
line numbering.

红字是我要问的,请帮忙,谢谢。

[ 本帖最后由 derrick90 于 2009-7-4 11:09 AM 编辑 ]
作者: Super-Tomato    时间: 2009-7-3 11:05 PM
原帖由 derrick90 于 2009-7-3 10:53 PM 发表
各位楼主帮帮忙,这是小弟的作业题目(英文)请别介意。

Task 1
(a) Define the class Item to store the details of an item sold
in the bookshop. Each object of Item should store the
following in ...



· A no-argument constructor
· A constructor with parameters

constructor 就是主建構函數

class A {
    public function A() {}  //這個 A 就是constructor
}





如何初始化 Array 應該去看你的教學課本, 這是基本上所有課本或教學網站都會教的




Search 就使用循環搜索 Array 中每個 Class 中的 item number 屬性值




更改 Class 屬性更加不用說了


作者: derrick90    时间: 2009-7-7 06:37 PM
请帮帮忙,要交了还是有问题,谢谢。
不懂怎样加  Quantity

public class Item {
    private int itemNumber;
    private String itemDescription;
    private double sellingPrice;
    private static int quantityOnHand;
        
        public Item() {
    }
   
    public Item(int in,String id,double sp){
            itemNumber=in;
            itemDescription=id;
            sellingPrice=sp;
    }
        
        public int getItemNumber(){
                return itemNumber;
        }
        public void setItemNumber(int sin){
                itemNumber=sin;
        }
        public String getItemDescription(){
                return itemDescription;
        }
        public void setItemDescription(String sid){
                itemDescription=sid;
        }
        public double getSellingprice(){
                return sellingPrice;
        }
        public void setSellingprice(double ssp){
                sellingPrice=ssp;
        }
        public static int getQuantityOnHand(){
                return quantityOnHand;
        }
        public static void setQuantityOnHand(int sqoh){
                quantityOnHand = sqoh;
        }   
}





import java.util.Scanner;

public class testItem {

    public static void main(String [] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the number of book:");
            int num = input.nextInt();
            Item[] ItemArray = new Item[num];
            for (int i = 0 ; i < ItemArray.length ; i++){
                    ItemArray = inputItemInfo();
            }

            System.out.println("code   description     price     quantity");
        System.out.println("=========================================");
            for (int i = 0; i < ItemArray.length;i++){
                    System.out.printf("%d   %s     %.2f     \n", ItemArray.getItemNumber(),ItemArray.getItemDescription(),ItemArray.getSellingprice());
            }
    }
    public static Item inputItemInfo(){
   
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the code :");
    int itemNumber = input.nextInt();
    System.out.print("Enter the description :");
    String itemDescription = input.next();
    System.out.print("Enter the selling price :");
    float sellingPrice = input.nextFloat();
   
    Item item = new Item(itemNumber,itemDescription,sellingPrice);
    return item;
    }   
}


[ 本帖最后由 derrick90 于 2009-7-7 07:14 PM 编辑 ]
作者: goodday    时间: 2009-7-7 09:10 PM
下在property 里

private int PQuantity;

public int getSize() { return PQuantity; }
public void setSize(int value) { PQuantity= PQuantity+  value; }
作者: derrick90    时间: 2009-7-7 09:38 PM
解决了,for ..loop 还是不行,谢谢帮忙
作者: Super-Tomato    时间: 2009-7-8 01:58 AM
原帖由 derrick90 于 2009-7-7 09:38 PM 发表
解决了,for ..loop 还是不行,谢谢帮忙



不明白你所說的 for loop 有甚麼不行的
作者: derrick90    时间: 2009-7-15 07:07 PM
帮帮忙,谢谢。。
帮我解决infinite loop的问题


import java.util.*;

public class HandleExceptionDemo {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    boolean continueInput = true;

    do {
      try {
        System.out.print("\nEnter an integer: ");
        int number = scanner.nextInt();

        // Display the result
        System.out.println("\nThe number entered is " + number);

        continueInput = false;
      }
      catch (InputMismatchException ex) {
        System.out.println("Try again. (Incorrect input: an integer is required)");
        
      }
    } while (continueInput);
  }
}




欢迎光临 JBTALKS.CC (https://www.jbtalks.cc/) Powered by Discuz! X2.5