In: Computer Science
CP2 Major Project #1:
Design a complete system according to the below requirements.. It Includes:1) A Class called CandyOrder which stores information about the variety of the Candy (such as Snickers), the price per bag/box (such as 2.99), and the amount of bags/boxes (such as 2) ordered. The CandyOrder class should contain a constructor and a default constructor (sets variety to "Snickers", prices to $2.99, and box amount to 1). Accessor and Mutator methods for all instance variables. A toString method returning all instance variable values for thisobject.An equals method which returns true if variety and price are the same.---------------------------------------------------------------------------------------------------------------------
2) A Class called MasterOrder:
private CandyOrder[] orders; //required instance var /** Constructs a new MasterOrder object.
*/ public MasterOrder(int len) {//Constructs array orders (above) with len for length }
public boolean addOrder(int spot, String variety, int boxes, double price) {//create a new CandyOrder object and place in position spot in array orders//validate that spot in orders is null and that spot is not >= length of array orders//if validation fails, return false -- else return true}
public int getTotalBoxes() {//returns the total of amount of boxes in CandyOrder}
public double getTotalCost() {//returns the total cost of all of boxes in CandyOrder}
public CandyOrder[] removeVariety(String candyVar) { //removes any object from CandyOrder that has a variety of candyVar, new possible decreased array of CandyOrder (minus candyVars) returned }
public String printOutInfo() {//returns a combined string of all outputs in array orders using toString method in CandyOrder -- seperate each object by a newline---------------------------------------------------------------------------------------------------------------------
3) Main Driver Class:
a) prompt user for the length of the CandyOrder Array desired.
b) create a MasterOrder object passing the length from item a above
c) enter the amount of CandyOrders objects required in length via the addOrder method
d) call getTotalBoxes to printout total of all boxes
e) call getTotalCost to printout total cost of all CandyOrders in array
f) call removeVariety on a variety you have in the CandyOrder array in MasterOrder
g) call printOutInfo to printout all info in MasterOrder
public class CandyOrder {
String variety;
double price;
int amount;
public CandyOrder(){
this.variety = "Snickers";
this.price = 2.99;
this.amount = 1;
}
public CandyOrder(String variety, double price, int amount){
this.variety = variety;
this.price = price;
this.amount = amount;
}
String getVariety(){
return this.variety;
}
double getPrice(){
return this.price;
}
int getAmount(){
return this.amount;
}
void setVariety(String variety){
this.variety = variety;
}
void setPrice(double price){
this.price = price;
}
void setAmount(int amount){
this.amount = amount;
}
boolean equals(CandyOrder obj){
if (this.variety.equals(obj.variety)) {
if (this.price == obj.price)
return true;
else
return false;
}
else
return false;
}
String tostring(){
return "Variety " + this.variety + ". Has price (per bag/box) " +
this.price + ". Has amount (bags/boxes) " + this.amount;
}
}
public class MasterOrder {
private CandyOrder[] orders;
public MasterOrder(int len){
orders = new CandyOrder[len];
for (int i = 0; i <len; i++)
orders[i] = null;
}
public boolean addOrder(int spot, String variety, int boxes,
double price){
if (spot >= orders.length || spot < 0)
return false;
else if (orders[spot] != null)
return false;
else {
CandyOrder or = new CandyOrder(variety, price, boxes);
this.orders[spot] = or;
return true;
}
}
public int getTotalBoxes(){
int total = 0;
for (int i = 0; i < this.orders.length; i++){
total += this.orders[i].amount;
}
return total;
}
public double getTotalCost(){
double cost = 0;
for (int i = 0; i < this.orders.length; i++){
cost += this.orders[i].price * this.orders[i].amount;
}
return cost;
}
public CandyOrder[] removeVariety(String candyVar){
CandyOrder[] newOrder;
int no = 0;
// Number of elements with variety candyVar
for (int i = 0; i < this.orders.length; i++){
if (this.orders[i].variety.equals(candyVar))
no++;
}
newOrder = new CandyOrder[this.orders.length - no];
int k =0;
for (int i = 0; i < this.orders.length; i++){
if (this.orders[i].variety.equals(candyVar))
continue;
else
newOrder[k++] = this.orders[i];
}
return newOrder;
}
public String printOutInfo(){
String master = "";
for (int i = 0; i < this.orders.length; i++){
master += this.orders[i].tostring() + "\n";
}
return master;
}
}
import java.util.Scanner;
public class Main{
public static void main(){
System.out.println("Enter the length of CandyOrder Array: ");
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
MasterOrder mo = new MasterOrder(len);
for (int i = 0; i< len; i++) {
System.out.println("\nEnter order for %d th index: ");
System.out.println("Enter Variety:");
String variety = sc.next();
System.out.println("Enter Price(Pex bag/box): ");
double price = sc.nextDouble();
System.out.println("Enter Amount of bags/boxes: ");
int amount = sc.nextInt();
mo.addOrder(i, variety, amount, price);
}
mo.getTotalBoxes();
mo.getTotalCost();
System.out.println("Enter the variety to be removed: ");
String var = sc.next();
mo.removeVariety(var);
mo.printOutInfo();
}