package HomeWork;
public class SonyProduct
{
private String name;
private int price;
public SonyProduct()
{
name="no name";
price=0;
}
public SonyProduct(String na,int pr)
{
this.name=na;
this.price=pr;
}
public SonyProduct(SonyProduct pr)
{
name = pr.name;
price = pr.price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public void display()
{
System.out.println("name : "+ name);
System.out.println("price : "+ price);
}
}public class SonyGroup
{
private SonyProduct s[];
private int nb;
public SonyGroup(int size)
{
s=new SonyProduct[size];
nb=0;
}
public void add(SonyProduct SS)
{
if(nb<s.length)
{
s[nb]=new SonyProduct(SS);
nb++;
}
else{
System.out.println("المصفوفة ممتلئة ");
}
}
public int search(int d)
{
int q=0;
for(int i=0;i<nb;i++)
{
if(s.getPrice()==d)
{
q=i;
}
return q;
}
return -1;
}
public String delet(String d)
{
for(int i=0;i<nb;i++)
{
if(s.getName().equals(d))
{
s=s[nb-1];
nb--;
s[nb]=null;
return "تم الحذف";
}
}
return "آسف هذا الإسم غير موجود";
}
public SonyProduct max()
{
int a=0;
int max=s[0].getPrice();
for(int i=1 ;i<nb;i++)
{
if(s.getPrice()>max)
{
max=s.getPrice();
a=i;
}
return s[a];
}
return null;
}
public SonyProduct min()
{
int a=0;
int min=s[0].getPrice();
for(int i=1;i<nb;i++)
{
if(s.getPrice()<min)
{
min=s.getPrice();
a=i;
}
return s[a];
}
return null;
}
public void average()
{
int total=0;
int a=0;
double aver=0;
for(int i=0;i<nb;i++)
{
total+=s.getPrice();
}
aver=total/nb;
System.out.println("averag:"+aver);
}
public void output()
{
for(int i=0;i<nb;i++)
{
s.display();
}
}
}package HomeWork;
;import java.util.Scanner;
public class test {
public static void main(String []args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter Size area");
int size =input.nextInt();
String q ;
int z,n;
SonyGroup S=new SonyGroup(size);
SonyProduct p=new SonyProduct();
SonyProduct res;
SonyProduct pe;
do{
System.out.println("1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display");
System.out.println("Please enter choice:");
n=input.nextInt();
if(n==1)
{
System.out.println("Enter name");
q=input.next();
System.out.println("Enter price");
z=input.nextInt();
p.setName(q);
p.setPrice(z);
S.add(p);
}
if(n==2)
{
System.out.println("Enter name");
q=input.next();
S.delet(q);
}
if(n==3)
{
System.out.println("Enter price");
z=input.nextInt();
S.search(z);
S.output();
}
if(n==4)
{
res =S.min();
res.display();
}
if(n==5){
res =S.max();
res.display();
}
if(n==6)
{
S.average();
}
if(n==7)
{
S.output();
}
} while(n<=7);
if(n>8)
{
System.out.println("شكرًا");
}
}
}و هذه هي المخرجات :
Enter Size area 100 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 1 Enter name a Enter price 10 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 1 Enter name b Enter price 20 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 1 Enter name c Enter price 30 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 1 Enter name d Enter price 40 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 7 name : a price : 10 name : b price : 20 name : c price : 30 name : d price : 40 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 2 Enter name a 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 7 name : d price : 40 name : b price : 20 name : c price : 30 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 4 name : b price : 20 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 6 averag:30.0 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 5 name : d price : 40 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice 3 Enter price 20 name : d price : 40 name : b price : 20 name : c price : 30 1-add SonyGroup*2-delete in SonyProducts*3-search*4-min*5-max*6-Average*7-display Please enter choice: 8
أنا عارف إني طولت عليكم :P بس سؤالي لاحظوا في المخرجات إذا كتبت الرقم 3 و هو يدل على search يخرج جميع ما أضفت و هذا يعني أن search لا يعمل جيدًا فكيف أتجاوز الخطأ