السلام عليكم ورحمة الله وبركاته
قمت بعمل مشروع لادارة المخازن يحتوي على العديد من الدوال وفيه العديد من الكلاسات التي تربطها علاقة ال Composition
ولكن هناك العديد من الاخطاء في البرنامج كما اني اواجه صعوبة في انهاء كلاس test الذي يحتوي على دالة ال main
والذي تتركز أخطائي فيه
اليكم الاكواد مع العلم ان كلاس Item وكلاس Category لم اواجه فيهما اي مشاكل
ارجو منكم متابعة كل كلاس على حدا ومساعدتي في انها البرنامج بسرعة :S
لان مناقشة المشروع يوم الاحد :cry: :cry:
اولا كود العنصر المباع
class Item {
private int no ;
private String name ;
private int quantity ;
private float price ;
public Item(int no,String name,int quantity,float price){
this.no=no;
this.name=name;
this.quantity=quantity;
this.price=price;
}
public void changeQuantity(int amount){
quantity = amount + quantity;
}
public void changePrice(float newPrice){
price = newPrice + price ;
}
public int getNo(){
return no;
}
public String getName(){
return name ;
}
public float getPrice(){
return price;
}
public int getQuantity(){
return quantity ;
}
public void setNo(int no){
this.no=no;
}
public void setName(String name){
this.name=name ;
}
public void setQua(int quantity){
this.quantity=quantity ;
}
public void print(){
System.out.print("the item no is"+no);
System.out.print("the item name is"+name);
System.out.print("the item quantity is"+quantity);
System.out.print("the item price is"+price);
}
}ثانيا كلاس الصنف
import javax.swing.*;
class Category {
private String CategoryName ;
private Item items[] ;
private int size ;
private int count ;
public Category(String CategoryName,int size ){
this.CategoryName=CategoryName;
this.size=size;
items =new Item[size];
count=0;
}
public String getCategoryName(){
return CategoryName ;
}
public int pos(int no){
for(int i=0;i<count;i++)
if(items.getNo()==no)
return i;
return -1;
}
public void addItem(){
int no ;
String s ;
int quantity ;
float price ;
if(count==size){
JOptionPane.showMessageDialog(null,"the Category is full");
return ;
}
String name=JOptionPane.showInputDialog("enter the Item name");
s=JOptionPane.showInputDialog("enter the Item number");
no=Integer.parseInt(s);
s=JOptionPane.showInputDialog("enter the Item quantity");
quantity=Integer.parseInt(s);
int p=pos(no);
if(p==-1){
s=JOptionPane.showInputDialog("enter the Item price");
price=Float.parseFloat(s);
items[count]=new Item(no,name,quantity,price) ;
count++;
JOptionPane.showMessageDialog(null,"new Item added");
}
else{
itemsبسم الله الرحمن الرحيم.changeQuantity(quantity);
JOptionPane.showMessageDialog(null,"Item found and Quantity changed");
}
}
public void moveItem(int no){
int p;
p=pos(no);
if(p==-1)
JOptionPane.showMessageDialog(null,"item is not found");
else{
itemsبسم الله الرحمن الرحيم=items[count-1];
items[count-1]=null;
}
}
public void showItems(JTextArea output){
output.setText("ItemName\tItemNO\tItemPrice\tQuantity\n");
for (int i=0;i<count;i++)
output.append(items.getName()+"\t"+items.getNo()+"\t"+items.getPrice()+"\t"+items.getQuantity());
JOptionPane.showMessageDialog(null,output);
}
public void sortItemsByNo(){
for(int i=0;i<count;i++){
for(int j=i+1;j<count;j++){
if(items.getNo() > items[j].getNo()){
Item temp=items;
items=items[j];
items[j]=temp;
}
}
}
}
public void sortItemsByName(){
for(int i=0;i<count;i++){
for(int j=i+1;j<count;j++){
if(items.getName().compareTo( items[j].getName())>=1){
Item temp=items;
items=items[j];
items[j]=temp;
}
}
}
}
}ثالثا كلاس المخزن
وعندي فقط 2 error فيه :wink:
import javax.swing.*;
class Store {
private String storeName ;
private Category categories[] ;
private int size ;
private int count ;
public Store(String name,int size){
storeName=name ;
this.size=size ;
categories=new Category[size];
count = 0 ;
}
public int pos(String name){
for(int i=0;i<count;i++)
if(categories.getCategoryName()==name)
return i;
return -1;
}
public void addCategory(){
String name;
int size;
String s;
if(count==size){
JOptionPane.showMessageDialog(null,"new category added");
return ;
}
else if(count<size){
name=JOptionPane.showInputDialog("Enter the category name");
s=JOptionPane.showInputDialog("Enter the category size");
size=Integer.parseInt(s);
int p=pos(name);
if(p==-1){
categories[count]=new Category(name,size);
count++ ;
JOptionPane.showMessageDialog(null,"new category added");
}
else
JOptionPane.showMessageDialog(null,"Category is found ");
}}
public void moveCategory(){
String s;
String name ;
int p;
if (count==0){
JOptionPane.showMessageDialog(null,"the store is empty");
return ;}
s=JOptionPane.showInputDialog("Enter the Category name");
p=pos(name);
if(p==-1){
JOptionPane.showMessageDialog(null,"the Category not found");
return ;
}
else{
categoriesبسم الله الرحمن الرحيم=categories[count-1];
categories[count-1]=null;
}
}
//???? ???? ???? ??????? ????? ???????
public void showAllItems(){
JTextArea output = new JTextArea();
for(int i=0;i<count;i++){
output.append("Category name : "+categories.getCategoryName());
categories.showItems(output);
}
}
public void showItemsInCategory(String name){
int p ;
p=pos(name);
if(p==-1){
JOptionPane.showMessageDialog(null,"the Category not found");
return ;
}
JTextArea output = new JTextArea();
categoriesبسم الله الرحمن الرحيم.showItems(output);
}
//???? import ????? sell
//??? ??????? ?? ?????remove ;add
public void sellItem(){
String s;
String name ;
int no,p,quantity;
if (count==0){
JOptionPane.showMessageDialog(null,"the category is empty");
return ;
}
s=JOptionPane.showInputDialog("Enter the item No");
no=Integer.parseInt(s);
p=categories[size].pos(no);
if(p==-1){
JOptionPane.showMessageDialog(null,"item not found");
return ;
}
s=JOptionPane.showInputDialog("Enter the item Quantity to sell");
quantity=Integer.parseInt(s);
itemsبسم الله الرحمن الرحيم.changeQuantity(-quantity);
}
public void importItem(){
String s;
String name ;
int no,p,quantity;
if (count==0){
JOptionPane.showMessageDialog(null,"the category is empty");
return ;
}
s=JOptionPane.showInputDialog("Enter the item No");
no=Integer.parseInt(s);
p=categories[size].pos(no);
if(p==-1){
JOptionPane.showMessageDialog(null,"item not found");
return ;
}
s=JOptionPane.showInputDialog("Enter the item Quantity to sell");
quantity=Integer.parseInt(s);
itemsبسم الله الرحمن الرحيم.changeQuantity(quantity);
}
}واخيرا كلاس ال test وهو عقدتي
import java.util.*;
class test{
static Scanner input=new Scanner(System.in);
static Store s = new Store () ;
public static void main (String[] args) {
mainmenu();
}
public static void mainmenu () {
System.out.println ("1.Manage item ");
System.out.println ("2.Import item ");
System.out.println ("3.Sell item");
System.out.println ("4.Exit");
int choice=input.nextInt();
do{
switch(choice){
case 1:
submenu();
break;
case 2:
s.importItem();
break;
case 3:
s.sellItem();
break;
case 4:
break;
default :
System.out.println ("Error");
}}
while(choice!=4);
}
public static void submenu(){
Category cat=new Category();
System.out.println ("1. Add new category \n.2. Delete category.\n3. Add new Item.\n 4. Delete Item.\n5. Change price \n6. Search for item.\n7. Show All items for All categories.\n8. Show All items for particular category.\n9. Sort item by item no \n10. Sort item by item name.\n 11. return to the main menu");
int choice2=input.nextInt();
switch(choice2){
case 1:
System.out.println ("Enter the name of category");
String name =input.next();
System.out.println ("Enter the size of category");
int size =input.nextInt();
Category f=new Category(name , size);
s.addCategory(f);
break;
case 2:
s.moveCategory();
break;
case 3:
s.importItem();
break;
case 4:
cat.moveItem();
break;
case 5:
System.out.println ("Enter the Catugery name");
String name4=input.next();
int indext=s.pos(name4);
if(indext!=-1){
System.out.println ("Enter the item number ");
int itemnu=input.nextInt();
int index2=s.categories[indext].pos(itemnu);
if(index2!=-1){
System.out.println ("enter new price");
float newprice=input.nextFloat();
s.categories[indext].item[itemnu].changePrice(newprice);
} System.out.println ("The item is not Found ");
}
else
System.out.println ("The Catugry is not Found ");
break;
case 6 :
System.out.println ("Enter the Catugery name");
String name5=input.next();
int index1=s.findCategory(name5);
if(index1!=-1){
System.out.println ("Enter the name of item");
int name6=input.nextInt();
int index3=s.categories[index1].findItem(name6);
if (index3!=-1){
System.out.println ("the item found");
s.categories[index1].item[s.categories[index1].findItem(index3)].print();
}
System.out.println ("the item not found");
}
else
System.out.println ("the catagory not found");
break;
case 7:
for(int i=0;i<=s.getcount();i++){
System.out.println ("the category name is "+s.categories.getCtName());
s.showAllItems();
}
break;
case 8:
System.out.println ("enter the catagory name ");
String namect=input.next();
int indexg=s.findCategory(namect);
if(indexg!=-1){
for(int i=0;i<=s.getcount();i++){
if(s.categories.getCtName().equals(namect))
s.categories.showItems();}}
else
System.out.println ("the catagory not found");
break;
case 9:
cat.sortItemsByNo();
break;
case 10:
cat.sortItemsByName();
break;
case 11:
mainmenu();
break;
default :
System.out.println ("Error");
}
}
}

