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

لدي صنف منتجات Product وأريد عرض بيانات المنتج بعد البحث عنه بواسطة id

RAA

السؤال

طباعة البيانات التي تعيدها دالة البحث SelectProducts في الدالة الرئيسية

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.in);

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

        int ProductID = input.nextInt();

        while (ProductID != 0) {
            Product p = Product.SelectProducts(list, ProductID);

            p.toString();

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

    }

}


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 Product SelectProducts(ArrayList<Product> list, int productsID) {

        for (int i = 0; i < list.size(); i++) {

            Product currentProduct = list.get(i);
            int currentProductID = currentProduct.getproductsID();

            if (productsID == currentProductID) {
                System.out.println("informations related to this product ID:%d =" + currentProductID);
                return currentProduct;
            }
        }
        System.out.println("Sorry but this product ID is wrong");
        return null;

    }

}

 

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

Recommended Posts

  • 0

قمت بتعديل بسيط لدالة البحث لتصبح:

    public static Product SelectProducts(ArrayList<Product> list, int productsID) {

        for (int i = 0; i < list.size(); i++) {

            Product currentProduct = list.get(i);
            int currentProductID = currentProduct.getproductsID();

            if (productsID == currentProductID) {
                System.out.println("informations related to this product ID:%d =" + currentProductID);
                return currentProduct;
            }
        }
        System.out.println("Sorry but this product ID is wrong");
        return null;

    }

ثم لطباعة البيانات ضمن الدالة الرئيسية،

يتوجب استقبال ما تعيده دالة البحث , ثم تطبيق الدالة toString على الناتج

        while (ProductID != 0) {
            Product p = Product.SelectProducts(list, ProductID);
   // ^^^^^^^^^^^^^^
            p.toString(); // هنا

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

 

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

  • 0
بتاريخ 17 ساعات قال Wael Aljamal:

قمت بتعديل بسيط لدالة البحث لتصبح:


    public static Product SelectProducts(ArrayList<Product> list, int productsID) {

        for (int i = 0; i < list.size(); i++) {

            Product currentProduct = list.get(i);
            int currentProductID = currentProduct.getproductsID();

            if (productsID == currentProductID) {
                System.out.println("informations related to this product ID:%d =" + currentProductID);
                return currentProduct;
            }
        }
        System.out.println("Sorry but this product ID is wrong");
        return null;

    }

ثم لطباعة البيانات ضمن الدالة الرئيسية،

يتوجب استقبال ما تعيده دالة البحث , ثم تطبيق الدالة toString على الناتج


        while (ProductID != 0) {
            Product p = Product.SelectProducts(list, ProductID);
   // ^^^^^^^^^^^^^^
            p.toString(); // هنا

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

 

في حالة ابا اعمل في كلاس الفاتورة ميثود تستقبل المنتجات التي اختارها العميل ومعلوماته والمجموع الكلي للسعر كيف ممكن تتعمل؟

public class Invoices {
    
    public double total;
    private Customer customer;
    private Product product;
    
    public Invoices (double total0,Product product0,Customer customer0){
    this.total=total0;
    this.product = new Product(product0);
    this.customer= new Customer(customer0);
    }
    public Invoices(){
        this.total=0;
        this.product=null;
        this.customer=null;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public Product getProduct() {
        return new Product (product);
    }

    public void setProduct(Product product) {
        this.product = new Product (product);
    }

    public Customer getCustomer() {
        return new Customer (customer);
    }

    public void setCustomer(Customer customer) {
        this.customer = new Customer (customer);
    }
    public String toString(){
        String text="""
                    Invoices information :
                    Customer information :
                    """+this.customer+
                "\nThe products :\n"+this.product+"\nThe total price :" +this.total;
        return text;
       
        
    }

   
}

 

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

  • 0
بتاريخ 1 ساعة قال RAA:

في حالة ابا اعمل في كلاس الفاتورة ميثود تستقبل المنتجات التي اختارها العميل ومعلوماته والمجموع الكلي للسعر كيف ممكن تتعمل؟

نمرر قائمة ب id للأغراض التي يطلبها المستخدم (ندخلهم ضمن حلقة مثلا) ثم نحسب مجموع قيمتهم بالمرور على قائمة المنتجات و جلب خاصية السعر و عمل مجموع تراكمي..

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

  • 0
بتاريخ 6 ساعات قال Wael Aljamal:

نمرر قائمة ب id للأغراض التي يطلبها المستخدم (ندخلهم ضمن حلقة مثلا) ثم نحسب مجموع قيمتهم بالمرور على قائمة المنتجات و جلب خاصية السعر و عمل مجموع تراكمي..

طيب بالنسبة لمعلومات العميل المدخلة كيف ممكن تنطبع معاهم؟

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

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

طيب بالنسبة لمعلومات العميل المدخلة كيف ممكن تنطبع معاهم؟

إن كان للفاتورة Invoices زبون، يتوجب تعريف صنف جديد Class Customer مثلاً و نعرف خاصية للفاتورة من زبون و نضيف بيانات الزبون للفاتورة مثل بيانات منتج

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

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

إن كان للفاتورة Invoices زبون، يتوجب تعريف صنف جديد Class Customer مثلاً و نعرف خاصية للفاتورة من زبون و نضيف بيانات الزبون للفاتورة مثل بيانات منتج

ايوا انا عملت كلاس المنتجات وكلاس للعملاء واحتاج ارسل المعلومات تبعهم الاثنين لكلاس الفاتورة

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

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

ايوا انا عملت كلاس المنتجات وكلاس للعملاء واحتاج ارسل المعلومات تبعهم الاثنين لكلاس الفاتورة

نقوم بتمريرهم مع الدالة البانية

  • إنشاء غرض منتج
  • إنشاء غرض عميل 
Product p = new Product (... data)

Customer c = new Customer (... data)


Invoices i = new Invoices ( p , c)

 

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

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

نقوم بتمريرهم مع الدالة البانية

  • إنشاء غرض منتج
  • إنشاء غرض عميل 

Product p = new Product (... data)

Customer c = new Customer (... data)


Invoices i = new Invoices ( p , c)

 

public static void creatInoices(ArrayList<Product> list, int productsID) {
        double total=0;
        for (int i = 0; i < list.size(); i++) {

            Product currentProduct = list.get(i);
            int currentProductID = currentProduct.getproductsID();

            if (productsID == currentProductID) 
            {
                total+=list.get(i).getproductsPrice();
                return ;
            }
        }
        System.out.println("Thank you");
        

    }

صحيح؟؟

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

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

صحيح؟؟

ما فائدة الدالة؟ حساب قيمة فاتورة؟ 

يتوجب تمرير قائمة أرقام منتجات productsID تصبح قائمة فيها أرقام منتجات مختارة من الزبون وليس رقم واحد..

ثم لكل منتج من هذه القائمة، نبحث عنه في القائمة العامة list (التي فيها جميع المنتجات) و نجمع قيمة المنتجات

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

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

ما فائدة الدالة؟ حساب قيمة فاتورة؟ 

يتوجب تمرير قائمة أرقام منتجات productsID تصبح قائمة فيها أرقام منتجات مختارة من الزبون وليس رقم واحد..

ثم لكل منتج من هذه القائمة، نبحث عنه في القائمة العامة list (التي فيها جميع المنتجات) و نجمع قيمة المنتجات

الدالة تطبع المنتجات اللي اختارها العميل والمعلومات تبعتو والسعر الاجمالي 

//class customer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Customer {

    
    
    String name;
    int number;
    String email;
  
    private String nameOfCustomer;
    private int phoneNumber;
    private String emailCustomer;

    public Customer() {
        this.nameOfCustomer="";
        this.phoneNumber=0;
        this.emailCustomer="";
        
    }

    public Customer(String nameOfCustomer, int phoneNumber, String emailCustomer) {
        this.nameOfCustomer = nameOfCustomer;
        this.phoneNumber = phoneNumber;
        this.emailCustomer = emailCustomer;
    }

    Customer(Customer customer0) {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    public String getNameOfCustomer() {
        return nameOfCustomer;
    }

    public int getPhoneNumber() {
        return phoneNumber;
    }

    public String getEmailCustomer() {
        return emailCustomer;
    }

    public void setNameOfCustomer(String nameOfCustomer) {
        this.nameOfCustomer = nameOfCustomer;
    }

    public void setPhoneNumber(int phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setEmailCustomer(String emailCustomer) {
        this.emailCustomer = emailCustomer;
    }
    
    public String toString(){
        String data;
        data="Customer name : "+this.nameOfCustomer+"\nCustomer phone number : "+this.phoneNumber+
                "\nCustomer email : "+this.emailCustomer;
        return data;
    }
    public void WritingInFile() throws IOException{
        FileWriter myFile= new FileWriter("Customer information.txt",true);
        PrintWriter write= new PrintWriter(myFile);
        
        write.print(this.nameOfCustomer+"\t");
        write.print(this.phoneNumber+"\t");
        write.print(this.emailCustomer+"\t");
        
        write.close(); 
    }
    
    public void displayInformation() throws FileNotFoundException{
        
        System.out.println("Customer name\t phone number\t Customer email");
        File file = new File("Customer information.txt");
        Scanner readIn = new Scanner(file);
        
        while(readIn.hasNext()){
            String line =readIn.nextLine();
            System.out.println(line);
        }
        readIn.close();
    }
}





//class invoices
import java.util.ArrayList;

public class Invoices {
    
    public double total;
    private Customer customer;
    private Product product;
    
    public Invoices (double total0,Product product0,Customer customer0){
    this.total=total0;
    this.product = new Product(product0);
    this.customer= new Customer(customer0);
    }
    public Invoices(){
        this.total=0;
        this.product=null;
        this.customer=null;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public Product getProduct() {
        return new Product (product);
    }

    public void setProduct(Product product) {
        this.product = new Product (product);
    }

    public Customer getCustomer() {
        return new Customer (customer);
    }

    public void setCustomer(Customer customer) {
        this.customer = new Customer (customer);
    }
    public String toString(){
        String text="""
                    Invoices information :
                    Customer information :
                    """+this.customer+
                "\nThe products :\n"+this.product+"\nThe total price :" +this.total;
        return text;
       
        
    }

   public static void creatInoices(ArrayList<Product> list, int productsID) {
        double total=0;
        for (int i = 0; i < list.size(); i++) {

            Product currentProduct = list.get(i);
            int currentProductID = currentProduct.getproductsID();

            if (productsID == currentProductID) 
            {
                total+=list.get(i).getproductsPrice();
                return ;
            }
        }
        System.out.println("Thank you");
        

    }
}

الكود تبع الكلاسات ممكن توضح الفكرة اكثر كيف ممكن اعملها؟

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

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

الكود تبع الكلاسات ممكن توضح الفكرة اكثر كيف ممكن اعملها؟

ليس من السهل العمل بدون ملفات المشروع..

العمل يتم ضمن الدالة main 

  • عليك إنشاء زبون افتراضي( مثل المنتجات) ثم ربطه مع الفاتورة من خلال تمريره مع مصفوفة المنتجات

في صنف الفاتورة

public class Invoices {
    
    public double total;
    private Customer customer;
    private ArrayList<Product> products;
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

حساب سعر أكثر من منتج ضمن فاتورة

  • لدينا قائمتين
  • كل قائمة نمر عليها بحلقة
  • عند تقابل رقم منتج من التي اشتراها الزبون مع منتج موجود نجمع السعر

في حال تمرير قائمتين من نوع منتج:

public static void creatInoices(ArrayList<Product> list1, ArrayList<Product> list2) {
  double total=0;
  
  for (int i = 0; i < list1.size(); i++) {

    int currentProductID1 = list1.get(i).getproductsID();

    for (int j = 0; j < list2.size(); j++) {


      int currentProductID2 = list2.get(j).getproductsID();

      if (currentProductID1 == currentProductID2) 
      {
        total += list1.get(i).getproductsPrice();
        break ;
      }
    }

  }
  System.out.println("total is: " + total);


}

 

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

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

ليس من السهل العمل بدون ملفات المشروع..

العمل يتم ضمن الدالة main 

  • عليك إنشاء زبون افتراضي( مثل المنتجات) ثم ربطه مع الفاتورة من خلال تمريره مع مصفوفة المنتجات

في صنف الفاتورة


public class Invoices {
    
    public double total;
    private Customer customer;
    private ArrayList<Product> products;
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

حساب سعر أكثر من منتج ضمن فاتورة

  • لدينا قائمتين
  • كل قائمة نمر عليها بحلقة
  • عند تقابل رقم منتج من التي اشتراها الزبون مع منتج موجود نجمع السعر

في حال تمرير قائمتين من نوع منتج:


public static void creatInoices(ArrayList<Product> list1, ArrayList<Product> list2) {
  double total=0;
  
  for (int i = 0; i < list1.size(); i++) {

    int currentProductID1 = list1.get(i).getproductsID();

    for (int j = 0; j < list2.size(); j++) {


      int currentProductID2 = list2.get(j).getproductsID();

      if (currentProductID1 == currentProductID2) 
      {
        total += list1.get(i).getproductsPrice();
        break ;
      }
    }

  }
  System.out.println("total is: " + total);


}

 

//main class
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
 



public class SuperMarket {

   

    
    public static void main(String[] args) throws IOException 
    {
        
          Scanner input = new Scanner (System.in);
       
       
        
        Scanner readIn = new Scanner (System.in);
        
        ArrayList<Customer> customers = new ArrayList<>();
        Customer cr= new Customer();
        int choice;
        
        while (true){
            System.out.println("1- Add new Customer :");
            
            String name = null;
                int number = 0;
                String email = null;
            
            
            choice=readIn.nextInt();
            if (choice==1){
                
                readIn.nextLine();
                System.out.println("Enter Customer name : ");
                 name = readIn.nextLine();
                System.out.println("Enter Customer number : ");
                number = readIn.nextInt();
                readIn.nextLine();
                System.out.println("Enter Customer email : ");
                email = readIn.nextLine();
                cr = new Customer(name,number,email);
                
                cr.WritingInFile();
            }
        
        


        
        
        
       
      //Product and invoice class part
        
      ArrayList<Product> list = new ArrayList<>();
      Product p1 = new Product("Apple","Fruits and Vegetables section",340,15.5);
      Product p2 = new Product("Saudi coffee","Roaster section",451,35.25);
      Product p3 = new Product("Chocolate","Sweets section",562,40);
      Product p4 = new Product("Spanish Nuts Pie","Bakery section",673,70);
      Product p5 = new Product("Rasberry jam","Canned section",784,28);
      Product p6 = new Product("carrot","Fruits and Vegetables section",341,20);
      Product p7 = new Product("Nuts","Roaster section",452,25);
      Product p8 = new Product("Candies","Sweets section",563,45);
      Product p9 = new Product("French bakery","Bakery section",674,60);
      Product p10 = new Product("Mushrooms","Canned section",785,35);
      Product p11 = new Product("watermelon","Fruits and Vegetables section",342,10);
      Product p12 = new Product("Roasted coffee","Roaster section",453,70);
      Product p13 = new Product("Biscuit","Sweets section",564,30);
      Product p14 = new Product("Corn bread","Bakery section",675,39);
      Product p15 = new Product("White honey","Canned section",786,58.75);
      
      System.out.println();
      list.add(p1);
      list.add(p2);
      list.add(p3);
      list.add(p4);
      list.add(p5);
      list.add(p6);
      list.add(p7);
      list.add(p8);
      list.add(p9);
      list.add(p10);
      list.add(p11);
      list.add(p12);
      list.add(p13);
      list.add(p14);
      list.add(p15);
       
       int select = 0;
      
    
        
        do{
            System.out.println("To display the list of all products, please select the number -1-");
            System.out.println("To search for a product and its availability, please choose the number -2-");
            System.out.println("To select specific products, please choose the number -3-");
            System.out.println("To complete the order and purchase please choose the number -4-");
            System.out.println("To exit please choose the number -5-");
            System.out.print(">>>>>");
            select = input.nextInt();
            switch(select){
                case 1:
                    System.out.println("-----Products List-----\n"+list.toString());
                    break;
                case 2:
                    System.out.println("Dear customer, enter the name of the product you want to search for, and upon completion, enter the word Done");

        String ProductName = input.next();

        while (!"Done".equals(ProductName)) {
            String p = Product.Search(list, ProductName);

            

            System.out.println("Dear customer, enter the name of the product you want to search for, and upon completion, enter the word done");
            ProductName = input.next();
        }
                  break;
                case 3:
                    System.out.println("Dear customer, enter the ID of the product you want to choose, and when finished, enter the number 0");

        int ProductID = input.nextInt();

        while (ProductID != 0) {
            Product p = Product.SelectProducts(list, ProductID);

            

            System.out.println("Dear customer, enter the ID of the product you want to choose, and when finished, enter the number 0");
            ProductID = input.nextInt();
        }
                  break;
                case 4:
                    
                    break;
               
                    
       


            }
            System.out.println("----------------------------");
        }while (select!=5);
        }
}
    
}
                          //invoices class
                          
  import java.util.ArrayList;

public class Invoices {

    private static int ArrayList;
    int total=0;
    private Customer customer;
    private Product product;
    
    public Invoices (String bill0,Product product0,int total0,Customer customer0){
    
    this.product = new Product(product0);
    this.total=total;
    this.customer= new Customer(customer0);
    }
    public Invoices(){
        
        this.product=null;
        this.total=0;
        this.customer=null;
    }

   

    public Product getProduct() {
        return new Product (product);
    }

    public void setProduct(Product product) {
        this.product = new Product (product);
    }
    public int gettotal() {
        return total;
    }
    public void setint(int total) {
        this.total = total;
    }
    
    public Customer getCustomer() {
        return new Customer (customer);
    }

    public void setCustomer(Customer customer) {
        this.customer = new Customer (customer);
    }
    public String toString(){
        String text="""
                    Invoices information :
                    Customer information :
                    """+this.customer+
                "\nThe products :\n"+this.product+"\nThe total price :" +this.total;
        return text;
       
        
    }

   public static void creatInoices(ArrayList<Product> list1, ArrayList<Product> list2) {
  double total=0;
  
  for (int i = 0; i < list1.size(); i++) {

    int currentProductID1 = list1.get(i).getproductsID();

    for (int j = 0; j < list2.size(); j++) {


      int currentProductID2 = list2.get(j).getproductsID();

      if (currentProductID1 == currentProductID2) 
      {
        total += list1.get(i).getproductsPrice();
        break ;
      }
    }

  }
  System.out.println("total is: " + total);


   }
     
        

    
}
                        
                          
   
    
 

 

تم التعديل في بواسطة RAA
رابط هذا التعليق
شارك على الشبكات الإجتماعية

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...