JAVA: If you have two classes below, how would you apply the Aggregation concept into the classes?
Question:
JAVA:
If you have two classes below, how would you apply the Aggregation concept into the classes? Modify the code to demonstrate the concept. (12 points). Moreover, (8 points) create the main( ) that contains an ArrayList which collects three objects of the class that aggregates the other.
public class Customer
{ private String SOCNum; private double Spending;
// You are to create a constructor, and other common methods based //on the questions asked above.
}
public class AmazonItem
{ private String Item; private double SalesPrice;
// You are to create a constructor, and other common methods.
// You are to create a constructor, and other common methods based on // the questions asked above.
}
I NEED A DIFFERENT CODE THEN THE ONE BELOW, THANKS
import java.util.*; class Customer //creating customer class { private String SOCNum; //creating variables private double Spending; Customer(String s,double p) //parameterized constructor { this.SOCNum = s; this.Spending = p; } void addAmount(double d){ //add item price to spendings if (d>0) { Spending+=d; } } void delAmount(double d){ //delete item price from spendings if user want to change item price if (d>0) { Spending-=d; } } public String toString() //returning variables { return "Customer SOCNum : "+SOCNum+" "+"Total Spendings :"+Double.toString(Spending);
}
}
class AmazonItem //creating customer class { private String Item; //creating variables private double SalesPrice;
Customer c; //aggregation
AmazonItem(String i,double s,String soc,double sp) //parameterized constructor { this.Item = i; //initializing variables this.SalesPrice = s; c = new Customer(soc,sp); //passing arguments for aggregate object c.addAmount(s); //adding Item amount to spendings }
void changePrice(double s)//method for if user want to change the price {
if (s>0) { c.delAmount(SalesPrice); //firstly remove previous price SalesPrice= s; //assign new price c.addAmount(SalesPrice);//then change total spendings } } public String toString() //returning variables {
return "Item Name : "+Item+" "+"Price Of Item : "+Double.toString(SalesPrice)+" "+c; }
} public class Main //main class { public static void main(String[] args) { AmazonItem a = new AmazonItem("Mobile",100,"101",100); //creating 3 objects of amazonitem class AmazonItem b = new AmazonItem("FAN",90,"111",202); AmazonItem c = new AmazonItem("TV",210,"121",70); ArrayList obj = new ArrayList(); //creating a arraylist of type amazonitem obj.add(a); //adding objects obj.add(b); obj.add(c); for (int i=0;i
} }
Data Analysis and Decision Making
ISBN: 978-0538476126
4th edition
Authors: Christian Albright, Wayne Winston, Christopher Zappe