Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The DonutBox class is intended to be an abstract and simplified representation o

ID: 3857215 • Letter: T

Question

The DonutBox class is intended to be an abstract and simplified representation of a box of donuts. Among other things, this object should be able to list the price of the donuts. class Properties (MUST be private) Donuts (Donut[]) - an array of donuts Count - number of donuts in the box Class Invariants A box can have at most a dozen donuts Class Components Public getter ONLY for count public void addDonut(Donut order) {} public double getPrice[) {} Returns the total price (before tax) of the donuts in the box, with applicable discount: Under 6 donuts, no discount 6 to 12 donuts, 10% discount 12 donuts exactly, 20% discount public String toString() {} - returns a String containing all the donuts in the box An enum for box type (Small, Medium, large) A public method that returns the sue of the box as an enum constant. Small box fits exactly one donut, medium fits up to six donuts, large fits up to 12. The DonutBoxDriver class is intended to be a simple driver to test the Donut and DonutBox objects and should include a main!) method as well as other helper methods. At minimum, the driver class should: Create a box with only one donut Create a box of up to six donuts Create a box of more than six but less than twelve donuts Create a box of dozen donuts For each box created

Explanation / Answer

package chegg;

public class DonutBoxDriver {

   public static void main(String[] args) {

       DonutsBox box = new DonutsBox();
       try {
           box.addDonut(new Donut(10));
           box.addDonut(new Donut(20));
           box.addDonut(new Donut(30));
           box.addDonut(new Donut(40));
           box.addDonut(new Donut(50));
           box.addDonut(new Donut(60));
           box.addDonut(new Donut(70));
           box.addDonut(new Donut(80));
           System.out.println(box);
       } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }

   }
}

public class DonutsBox {

   private Donut[] donuts = null;
   private int count = 0;
   private BoxType size = BoxType.Large;

   public DonutsBox() {
       this.donuts = new Donut[12];

   }

   public DonutsBox(Donut[] donuts) throws Exception {
       this.donuts = donuts;
       if (donuts != null) {
           if (donuts.length > 12) {
               throw new Exception("Box cannot have more than 12 donuts");
           } else {
               this.donuts = donuts;
               this.count = donuts.length;
               if (this.donuts.length == 1) {
                   this.size = BoxType.SMALL;
               } else if (this.donuts.length <= 6) {
                   this.size = BoxType.Mediumn;
               } else if (this.donuts.length > 6) {
                   this.size = BoxType.Large;
               }
           }
       }
   }

   public int getCount() {
       return this.count;
   }

   public void addDonut(Donut order) throws Exception {
       if (order == null)
           throw new Exception("Order cannot be null");
       if (this.donuts != null && this.donuts.length == this.count) {
           this.donuts[0] = order;
           this.count = 1;
           this.size = BoxType.SMALL;
       } else {
           this.donuts[this.count] = order;
           this.count++;
           if (this.count == 1) {
               this.size = BoxType.SMALL;
           } else if (this.count <= 6) {
               this.size = BoxType.Mediumn;
           } else if (this.count > 6) {
               this.size = BoxType.Large;
           }
       }
   }

   public double getPrice() {
       double price = 0;
       if (this.donuts != null && this.count > 1) {
           if (this.count == 1) {
               return this.donuts[0].getPrice();
           } else if (this.count <= 6) {
               double sum = 0;
               for (Donut donut : donuts) {
                   sum = sum + donut.getPrice();
               }
               sum = (sum - ((sum * 10) / 100));
               price = sum;
           } else if (this.count > 6) {
               double sum = 0;
               for (Donut donut : donuts) {
                   sum = sum + donut.getPrice();
               }
               sum = (sum - ((sum * 20) / 100));
               price = sum;
           }
       }
       return price;
   }

   public BoxType getSize() {
       return size;
   }

   @Override
   public String toString() {
       return "DonutsBox [count=" + count + ", size=" + size + "]";
   }

}

public class Donut {

   private double price;

   public Donut(double price) {
       super();
       this.price = price;
   }

   public double getPrice() {
       return price;
   }

   public void setPrice(double price) {
       this.price = price;
   }
}

public enum BoxType {
   SMALL, Mediumn, Large;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote