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

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

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

import javautilArrayList public class Customer private String SOCNum private double totalSpending public CustomerString SOCNum thisSOCNum SOCNum thist... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!