Ex OOP Revisited Coding
Ex OOP Revisited Coding
//normal constructor
public Clothes(String size,double price,double percent)
{ this.size=size;
this.price=price;
discPercent=percent;
}
//setter/mutator method
public void setClothes(String s,double p,double percent)
{ this.size=s;
price=p;
discPercent=percent;
}
//retriever/accessor method
public String getSize(){return size;}
//retriever/accessor method
public double getPrice(){return price;}
//retriever/accessor method
public double getDiscPercent(){return discPercent;}
//display/retriever method
public String toString()
{return ("\nSize :"+size+"\tPrice:"+price+"\tPercent Discount:"+discPercent);}
//display/retriever method
public void display()
{ System.out.println("\nSize :"+size+"\tPrice:"+price+"\tPercent Discount:"+discPercent);}
}
clothApp1 coding
String sz;
double price,disc;
Scanner input=new Scanner(System.in);
Scanner inputText=new Scanner(System.in);
for(int i=0;i<3;i++)
{ //input data
System.out.println("\nCloth "+(i+1)+":");
System.out.println("Enter Size:");
sz=inputText.next();
System.out.println("Enter Price:");
price=input.nextDouble();
System.out.println("Enter Percent Discount(Eg:0.05 represent 5%):");
disc=input.nextDouble();
cloth[i]=new Clothes(); //call default constructor
cloth[i].setClothes(sz,price,disc); //set/send the value into an array
}
for(int i=0;i<3;i++)
{ //Calculate the price after discount for each cloth
nPrice[i]=cloth[i].calcNewPrice();
}
//Display the detail of each cloth(using toString() including price after discount)
System.out.println("\nDetail for each cloth:");
for(int i=0;i<3;i++)
{ System.out.println("\nCloth "+(i+1)+":");
System.out.println(cloth[i].toString()+"Price after discount:"+nPrice[i]);
//cloth[i].toString(); this is WRONG
}
}
}
clothApp2 coding:
String sz;
double price,disc;
Scanner input=new Scanner(System.in);
Scanner inputText=new Scanner(System.in);
for(int i=0;i<3;i++)
{ //input data
System.out.println("\nCloth "+(i+1)+":");
System.out.println("Enter Size:");
sz=inputText.nextLine();
System.out.println("Enter Price:");
price=input.nextDouble();
System.out.println("Enter Percent Discount(Eg:0.05 represent 5%):");
disc=input.nextDouble();
cloth[i]=new Clothes(sz,price,disc); //call normal constructor
//cloth[i].setClothes(sz,price,disc); //if used normal constructor,DO NOT call the setter
method
}
//for(int i=0;i<3;i++)
//{
// nPrice[i]=cloth[i].calcNewPrice();
//}
//the process will be done during display the information
//Display the detail of each cloth(using display() method including price after discount.
System.out.println("\nDetail for each cloth:");
for(int i=0;i<3;i++)
{ System.out.println("\nCloth:"+(i+1)+":");
cloth[i].display();
System.out.println("Price after discount:"+cloth[i].calcNewPrice());
}
}
}