Hiển thị kết quả từ 1 đến 4 / 4
  1. #1
    Tham gia
    23-05-2007
    Bài viết
    684
    Like
    0
    Thanked 5 Times in 4 Posts

    Ý tưởng mới ! Hỏi về bài tập Object trong Java

    Mình làm 1 bài tập về Object trong Java. Tạo phần nhập xuất đơn giản thông tin 2 khách hàng, sử dụng nhập xuất qua 1 class khác (non - static)
    Nhập xuất khách hàng đầu thì ổn nhưng khách hàng 2 báo lỗi ngay. Mọi người xem giúp mình.

    Nội dung Invoice.class
    ================================================== =====
    import java.util.Scanner;
    public class Invoice
    {
    private int itemNum;
    private int quantity;
    private int price;
    private String name;
    /*
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the item number ");
    itemNum = input.nextInt();
    System.out.print("Enter the quantity ");
    quantity = input.nextInt();
    System.out.print("Enter the price ");
    price = input.nextInt();
    System.out.print("Enter the name ");
    name = input.nextLine();

    displayLine();
    }
    */
    public int getItemNum()
    {
    return itemNum;
    }
    public void setItemNum(int n)
    {
    itemNum = n;
    }

    public int getQua()
    {
    return quantity;
    }
    public void setQua(int n)
    {
    quantity = n;
    }

    public int getPri()
    {
    return price;
    }
    public void setPri(int n)
    {
    price = n;
    }

    public String getName()
    {
    return name;
    }


    public void setName(String Abc)
    {
    name = Abc;
    }
    public int getTotal()
    {
    return (price*quantity);
    }

    public void displayLine()
    {
    System.out.println("Item number " + itemNum + " - "+ quantity + " Name; " + name + " - " + price + " Name: " + getTotal());
    }
    }


    Nội dung TestInvoice.class
    ==========================================
    import java.util.Scanner;
    public class TestInvoice
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    Invoice first = new Invoice();
    Invoice second = new Invoice();
    Invoice third = new Invoice();

    String name;
    int itemNum;
    int qua;
    int price;

    System.out.print("Enter your name ");
    name = input.nextLine();
    first.setName(name);
    itemNum = input.nextInt();
    first.setItemNum(itemNum);
    qua = input.nextInt();
    first.setQua(qua);
    price = input.nextInt();
    first.setPri(price);
    first.displayLine();

    String name2;
    int itemNum2;
    int qua2;
    int price2;
    System.out.print("Enter your name ");
    name2 = input.nextLine();
    second.setName(name2);
    itemNum2 = input.nextInt();
    second.setItemNum(itemNum2);
    qua2 = input.nextInt();
    second.setQua(qua2);
    price2 = input.nextInt();
    second.setPri(price2);
    second.displayLine();

    }
    }

    ==========================================
    Hoặc xem tại:
    http://paste.ideaslabs.com/show/L8ROdSS0xy
    http://paste.ideaslabs.com/show/Hs6ZsOiWzo
    Quote Quote

  2. #2
    Tham gia
    23-05-2007
    Bài viết
    684
    Like
    0
    Thanked 5 Times in 4 Posts
    Không nhập được cái Object thứ 2

    lối là java.util.MissonInput...

  3. #3
    Tham gia
    05-03-2010
    Bài viết
    129
    Like
    2
    Thanked 16 Times in 16 Posts
    Quote Được gửi bởi masterdark116 View Post
    Không nhập được cái Object thứ 2

    lối là java.util.MissonInput...
    Theo mình thì trong class Invoice định nghĩa đối tượng thì bạn nên thêm method input dữ liệu + output dữ liệu để class sau gọi cho chính xác hơn, còn viết như bạn thì xảy ra lỗi của scan rồi vì đối tượng chưa được cấp phát. Thử lại xem bạn, Good luck!

  4. #4
    Tham gia
    05-03-2010
    Bài viết
    129
    Like
    2
    Thanked 16 Times in 16 Posts
    Theo mình thì mô hình đối tượng bạn nên làm như sau: (Mình ví dụ đối tượng sinh viên chẳng hạn)
    Bước 1: Tạo class định nghĩa object sinh viên:
    Code:
    import java.util.Scanner;
    
    public class sv {
    
    	private String hoten;
    	private String gioitinh;
    	private int diem;
    /*
     * 
     * Getters and Setters
     * 
     */
    	public String getHoten() {
    		return hoten;
    	}
    	public void setHoten(String hoten) {
    		this.hoten = hoten;
    	}
    	public String getGioitinh() {
    		return gioitinh;
    	}
    	public void setGioitinh(String gioitinh) {
    		this.gioitinh = gioitinh;
    	}
    	public int getDiem() {
    		return diem;
    	}
    	public void setDiem(int diem) {
    		this.diem = diem;
    	}
    /*
     * 
     * inPut method
     * 
     */
    	public void inPut()
    	{
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Nhap ho ten sv:");
    		hoten = scan.nextLine();
    		System.out.println("Nhap gioi tinh:");
    		gioitinh = scan.nextLine();
    		System.out.println("Nhap diem thi:");
    		diem = scan.nextInt();
    	}
    /*
     * 
     * outPut method
     * 	
     */
    	public void outPut()
    	{
    		System.out.println("Ho va ten:" + hoten);
    		System.out.println("Gioi tinh:" + gioitinh);
    		System.out.println("Diem thi:" + diem);
    	}
    /*
     * 
     * conStructors
     * 
     */
    	public sv()
    	{
    		super();
    	}
    	
    	public sv(String hoten, String gioitinh, int diem)
    	{
    		super();
    		this.hoten = hoten;
    		this.gioitinh = gioitinh;
    		this.diem = diem;
    	}
    	
    }
    Bước 2: Tạo một class xử lý các method mà class sv vừa định nghĩa:
    Code:
    import java.util.Scanner;
    
    public class imPlementSV {
    
    	
    	public static void main(String[] args) {
    		sv[] sinhvien;
    		Scanner scanner = new Scanner(System.in);
    		int numSV,i;
    		System.out.println("Nhap so Sinh Vien:");
    		numSV = scanner.nextInt();
    		sinhvien = new sv[numSV+1];
    		for(i=0;i<numSV;i++)
    		{
    			System.out.printf("\n Nhap thong tin sv thu : %d \n",i+1 );
    			sinhvien[i] = new sv();
    			sinhvien[i].inPut();			
    		}
    		for(i=0;i<numSV;i++)
    		{
    			System.out.printf("\n Thong tin sv thu : %d \n",i+1 );
    			sinhvien[i].outPut();			
    		}
    
    	}
    
    }
    Như vậy ta đã tạo được 1 chương trình nhập xuất đơn giản rồi. Good luck!

Bookmarks

Quy định

  • Bạn không thể tạo chủ đề mới
  • Bạn không thể trả lời bài viết
  • Bạn không thể gửi file đính kèm
  • Bạn không thể sửa bài viết của mình
  •