اذهب إلى المحتوى
  • 0

عمل دالة تبحث عن عنصر من خلال id ضمن قائمة ب جافا

RAA

السؤال

بحتاج من المستخدم انو بيدخل ID تبع المنتج اللي يبا يختاروا وعملت ميثود ولكن لاتعمل ايه التعديل الي اعلموا

public static void SelectProducts(ArrayList<Product> list,int productsID)
{
  System.out.println(   list.toString());
  for(int i =0; i<list.size();i++)
  {
    System.out.println(  "enter the ID number of the product you want, and when you finish selecting orders, please choose 0 ");


    int currentProductID= list.get(i).getproductsID();
    if(productsID==currentProductID)
    {
      return;          

    }
    System.out.println("Sorry but this product ID is wrong");
    if(currentProductID==0)
    {
      break;
    }
  }
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

ضمن حلقة البحث عن منتج حسب رقمه، لا نطلب إدخال قيم جديدة أو 0

بل يتوجب استدعاء دالة ابحث ضمن حلقة خارجية و تمرير مدخلات المستخدم لها

Scanner scan = new Scanner();

System.out.println("enter the ID number of the product you want, 0 to exit");

int ProductID = scan.nextInteger();

while(ProductID != 0) {
  SelectProducts(list,ProductID); // سوف تطبع نتيجة البحث
	
  // نعيد قراءة مدخلات جديدة
  System.out.println("enter the ID number of the product you want, 0 to exit");
  ProductID = scan.nextInteger();
}

وتعديل دالة البحث:

  • مسؤولية الدالة هو التأكد من وجود منتج وحيد فقط
public static void SelectProducts(ArrayList<Product> list,int productsID)
{
  System.out.println(list.toString());
  for(int i =0; i<list.size();i++)
  {
    int currentProductID= list.get(i).getproductsID();
    if(productsID==currentProductID)
    {
     System.out.println("we have this product with id = " + currentProductID);
      return;          
    }
  }
  System.out.println("Sorry but this product ID is wrong");
}

ملاحظة:

يمكن وضع منطق الشيفرة الأولى في دالة منفصلة SelectProductsByIDs

public static void SelectProductsByIDs(ArrayList<Product> list,int productsID)
{
  Scanner scan = new Scanner();

  System.out.println("enter the ID number of the product you want, 0 to exit");
  int ProductID = scan.nextInteger();

  // ينتهي التنفيذ مع قراءة 0
  while(ProductID != 0) {
    // البحث
    SelectProducts(list,ProductID); // سوف تطبع نتيجة البحث

    // نعيد قراءة مدخلات جديدة
    System.out.println("enter the ID number of the product you want, 0 to exit");
    ProductID = scan.nextInteger();
  }
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 59 دقائق مضت قال Wael Aljamal:

ضمن حلقة البحث عن منتج حسب رقمه، لا نطلب إدخال قيم جديدة أو 0

بل يتوجب استدعاء دالة ابحث ضمن حلقة خارجية و تمرير مدخلات المستخدم لها


Scanner scan = new Scanner();

System.out.println("enter the ID number of the product you want, 0 to exit");

int ProductID = scan.nextInteger();

while(ProductID != 0) {
  SelectProducts(list,ProductID); // سوف تطبع نتيجة البحث
	
  // نعيد قراءة مدخلات جديدة
  System.out.println("enter the ID number of the product you want, 0 to exit");
  ProductID = scan.nextInteger();
}

وتعديل دالة البحث:

  • مسؤولية الدالة هو التأكد من وجود منتج وحيد فقط

public static void SelectProducts(ArrayList<Product> list,int productsID)
{
  System.out.println(list.toString());
  for(int i =0; i<list.size();i++)
  {
    int currentProductID= list.get(i).getproductsID();
    if(productsID==currentProductID)
    {
     System.out.println("we have this product with id = " + currentProductID);
      return;          
    }
  }
  System.out.println("Sorry but this product ID is wrong");
}

ملاحظة:

يمكن وضع منطق الشيفرة الأولى في دالة منفصلة SelectProductsByIDs


public static void SelectProductsByIDs(ArrayList<Product> list,int productsID)
{
  Scanner scan = new Scanner();

  System.out.println("enter the ID number of the product you want, 0 to exit");
  int ProductID = scan.nextInteger();

  // ينتهي التنفيذ مع قراءة 0
  while(ProductID != 0) {
    // البحث
    SelectProducts(list,ProductID); // سوف تطبع نتيجة البحث

    // نعيد قراءة مدخلات جديدة
    System.out.println("enter the ID number of the product you want, 0 to exit");
    ProductID = scan.nextInteger();
  }
}

 

package pkgsuper.market;

import java.util.ArrayList;
import java.util.Scanner;

public class SuperMarket {

    
    public static void main(String[] args) 
    {
       
        
        
        
        
        
        
 
      
      ArrayList<Product> list = new ArrayList<>();
      Product p1 = new Product("Apple","fruits",347685,39);
      Product p2 = new Product("carrot","vegetables",354305,39);
      Product p3 = new Product("tea","herbs",389545,39);
      System.out.println();
      list.add(p1);
      list.add(p2);
      list.add(p3);
    
      Scanner input = new Scanner();بيخرج ايرور\\ا

System.out.println("enter the ID number of the product you want, 0 to exit");

int ProductID = input.nextInt();

while(ProductID != 0) {
  Product.SelectProducts(list,ProductID); 
	
  
  System.out.println("enter the ID number of the product you want, 0 to exit");
  ProductID = input.nextInt();
}
      
      
      }

    }



package pkgsuper.market;

import java.util.ArrayList;
import java.util.Scanner;



public class Product 
{

  
    
   private  String productsName;
   private  String productsType;
   private  int productsID;
   private  double productsPrice;
    private Object list;
  

    
    
    public Product()
    {
        productsName= null;
        productsType= null;
        productsID = 0;
        productsPrice = 0.0;
    }
    
    public Product(String productsName,String productsType,int productsID,double productsPrice )
    {
        this.productsName = productsName;
        this.productsType = productsType;
        this.productsID = productsID;
        this.productsPrice = productsPrice;
    }
    
    
    
    public void setproductsName(String productsName)
    {
        this.productsName = productsName;
    }
    
   public void setproductsType(String productsType)
    {
        this.productsType = productsType;
    }
   
    
   public void setproductsID(int productsID)
    {
        this.productsID = productsID;
    } 
   
  public void setproductsPrice(int productsPrice)
    {
        this.productsPrice = productsPrice;
    }  
  
  
  public String getproductsName()
  {
      return this.productsName;
  }
  
   public String getproductsType()
    {
        return this.productsType;
    }
  
public int getproductsID()
  {
      return this.productsID;
  }

public double getproductsPrice()
  {
      return this.productsPrice;
  }


public String toString()

{
  String str = "Products name :\n " + productsName + 
          "\nProducts type: \n" + productsType + 
          "\nProducts ID: " + productsID  +
                  " Products price:\n " + productsPrice;
   return str;
}
public static void Search(ArrayList<Product> list,String productsName)
{
  for(int i =0; i<list.size();i++) 
  {
    String currentProductName = list.get(i).getproductsName();  
    
    if(productsName.equals(currentProductName))
    {
      System.out.println("This product is available");
      
      return;               
    }
  }
  
   System.out.println("Sorry,this product is not available"); 
}
 


public static void SelectProducts(ArrayList<Product> list,int productsID)
{
  System.out.println(list.toString());
  for(int i =0; i<list.size();i++)
  {
    int currentProductID= list.get(i).getproductsID();
    if(productsID==currentProductID)
    {
     System.out.println("we have this product with id = " + currentProductID);
      return;          
    }
  }
  System.out.println("Sorry but this product ID is wrong");
}
}

ايه حل الشمكلة

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 3 دقائق مضت قال RAA:

ايه حل الشمكلة

الخطأ بشكل تعليق ليس واضح، ارجو كتابة نص مباشرة يشير للمشكلة..

يتوجب تمرير مسار الدخل System.in لباني Scanner بالشكل:

Scanner input = new Scanner(System.in);

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 5 دقائق مضت قال Wael Aljamal:

الخطأ بشكل تعليق ليس واضح، ارجو كتابة نص مباشرة يشير للمشكلة..

يتوجب تمرير مسار الدخل System.in لباني Scanner بالشكل:

Scanner input = new Scanner(System.in);

انا ما احتاج انو يبحث موجود ولا لا انا احتاج انو يبحث اذا موجود يظهر لي معلومات هذا المنتج ومن ثم يرسلوا للكلاس الخاصة بال order 

وفي حالة انو ادخل ID مش موجود بيظهر رسالة انو خطأ ويعيد ادخال ID

بتاريخ الآن قال RAA:

انا ما احتاج انو يبحث موجود ولا لا انا احتاج انو يبحث اذا موجود يظهر لي معلومات هذا المنتج ومن ثم يرسلوا للكلاس الخاصة بال order 

وفي حالة انو ادخل ID مش موجود بيظهر رسالة انو خطأ ويعيد ادخال ID

وعذرا على الازعاج 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 9 دقائق مضت قال RAA:

انا ما احتاج انو يبحث موجود ولا لا انا احتاج انو يبحث اذا موجود يظهر لي معلومات هذا المنتج ومن ثم يرسلوا للكلاس الخاصة بال

تمام، ضمن الشرط يمكنك طباعة بيانات المنتح، و لإرجاعه يتوجب جعل الدالة بنمط int مثلا و نعمل return product id أو نحددها بنمط Product و نعيد list.git i بكامله.

 

بتاريخ 12 دقائق مضت قال RAA:

وفي حالة انو ادخل ID مش موجود بيظهر رسالة انو خطأ ويعيد ادخال

الحلقة تضمن إعادة إدخال المعرف لبحث جديد

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 1 دقيقة مضت قال Wael Aljamal:

تمام، ضمن الشرط يمكنك طباعة بيانات المنتح، و لإرجاعه يتوجب جعل الدالة بنمط int مثلا و نعمل return product id أو نحددها بنمط Product و نعيد list.git i بكامله.

 

الحلقة تضمن إعادة إدخال المعرف لبحث جديد

تعديل الشرط حيكون ايه؟

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 3 دقائق مضت قال RAA:

تعديل الشرط حيكون ايه؟

قلتي انك ترغبين بإعادة المنتج من خلال الدالة لمكان استدهدعائها.. 

نعمل return list.get(i) ضمن الشرط if 

و يتوجب أن تعيد الدالة نمط Product.

او يمكنك إعادة فقط رقم المنتج.. index ضمن القائمة return i ونمط الدالة int

بدل void

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 36 دقائق مضت قال Wael Aljamal:

قلتي انك ترغبين بإعادة المنتج من خلال الدالة لمكان استدهدعائها.. 

نعمل return list.get(i) ضمن الشرط if 

و يتوجب أن تعيد الدالة نمط Product.

او يمكنك إعادة فقط رقم المنتج.. index ضمن القائمة return i ونمط الدالة int

بدل void

public static Product SelectProducts(ArrayList<Product> list,int productsID)
{
  
  for(int i =0; i<list.size();i++)
  {
    int currentProductID= list.get(i).getproductsID();
    if(productsID==currentProductID)
    {
     System.out.println("informations related to this product ID:%d =" + currentProductID);
      return list.get(i);
    }
  }
  System.out.println("Sorry but this product ID is wrong");
       return null ;
       
}

برضو المشكلة ذاتها بيانات Id اللي بدخلو ما بتظهر

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 4 دقائق مضت قال RAA:

public static Product SelectProducts(ArrayList<Product> list,int productsID)
{
  
  for(int i =0; i<list.size();i++)
  {
    int currentProductID= list.get(i).getproductsID();
    if(productsID==currentProductID)
    {
     System.out.println("informations related to this product ID:%d =" + currentProductID);
      return list.get(i);
    }
  }
  System.out.println("Sorry but this product ID is wrong");
       return null ;
       
}

برضو المشكلة ذاتها بيانات Id اللي بدخلو ما بتظهر

تأكدي من تمرير الوسطاء لجميع الدوال من بداية البرنامج لنهايته.

يمكنك وضع صور تظهر تقدم التنفيء لحين حصول المشكلة (استخدمي عبارة الطباعة لكل خطوة)..

ان بقي مشكلة أرجو إرفاق الملف كاملا

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...