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

1. You will implement two public classes: the Mothclass, and the MothTester clas

ID: 3803313 • Letter: 1

Question

1. You will implement two public classes: the Mothclass, and the MothTester class. The MothTester class contains the main method.

2. You will implement two public classes: the Message class, and the MessageTester class. The MessageTester class contains the main method. The Message class uses an ArrayList<String> to hold the lines of the message body

3. You will implement two public classes: the Mailbox class, and the MailboxTester class. You will also need the Message class from Programming Assignment 4. The MailboxTester class contains the main method. The Mailbox class uses an ArrayList<Message> to hold the messages in a mailbox.

4. Implement the following interface and classes:

        •IDiscountPolicy

        •BulkDiscount

          •BuyNItemsGetOneFree

          •CombinedDiscount

          •DiscountTester

IDiscountPolicy is an interface. IDiscountPolicy has singe method computeDiscount. computeDiscount returns a double. The double represents the discount amount for a given number of items. computeDiscount has two parameters. int count indicates the number of items. double itemCost is the cost of a single item.

BulkDiscount implements the IDiscountPolicy interface. The constructor has two parameters. int minimum specifies the minimum number of items need to get the discount. double percent is the discount percent. The computeDiscount method computes the discount amount as follows:

if count < minimum then discount = 0;

if count >= minimum then discount = count * itemCost * percent

BuyNItemsGetOneFree implements the IDiscountPolicy interface. The constructor has one parameter: int n. n represents the numbers of items needed to get one item free. For example when n is 3, when buying 3 items one is free, when buying 6 items 2 are free. The

computeDiscount method computes the discount as follows:

discount = number of free items * itemCost

CombinedDiscount implements the IDiscountPolicy interface. The constructor has two parameters of type IDiscountPolicy. The computeDiscount method computes the discount amount using the two IDiscountPolicy objects and returns the largest discount amount.

The DiscountTester class tests the BulkDiscount, BuyNItemsGetOneFree and CombinedDiscount classes.

Assignment Guidelines:

1. Runyour programs with enough test cases to prove that the program works.Hand in the output for each test run.

2. For each source file include a comment that contains your name, assignment number and problem number.

3. Hand in a source code listing ( print out of your Java source

files) and the output produced by your program. Use Courier New

font.

4. Proper indentation.

5. Use Java Language Coding Guidelines (Appendix L) for variable

names, method names, class names and constant names.

Explanation / Answer

//Please Copy every class or interface in seperate file with that class name or interface name

import java.util.ArrayList;
public class Mailbox {
   ArrayList<Message> msgs;
   public Mailbox(){
       msgs = new ArrayList<>();
   }
   public void addMessage(Message msg){
       msgs.add(msg);
   }
   public String toString(){
       String str =" ";
       for(int i=0;i<msgs.size();i++){
           str += "Message "+(i+1)+" : "+msgs.get(i)+" ";
       }
       return str;
   }
}

public class MailboxTester {

   public static void main(String[] args) {
       Mailbox mailBox = new Mailbox();
      
       Message msg = new Message();
       msg.addLine("hi rohit");
       msg.addLine("today i am out of office");
       msg.addLine("Thanks");
       msg.addLine("Regards");
       msg.addLine("Srathasf dfas");
       mailBox.addMessage(msg);
      
       msg = new Message();
       msg.addLine("hi rohit");
       msg.addLine("I am not comming to office");
       msg.addLine("Thanks");
       msg.addLine("Regards");
       msg.addLine("Srathasf dfas");
       mailBox.addMessage(msg);
      
       System.out.println("MailBox : "+mailBox);
   }

}

import java.util.ArrayList;
public class Message {
   ArrayList<String> msgLines;
   public Message(){
       msgLines = new ArrayList<>();
   }
   public void addLine(String line){
       msgLines.add(line);
   }
   public String toString(){
       String str =" ";
       for(int i=0;i<msgLines.size();i++){
           str += msgLines.get(i)+" ";
       }
       return str;
   }
}

public class MessageTester {

   public static void main(String[] args) {
       Message msg = new Message();
       msg.addLine("hi rohit");
       msg.addLine("today i am out of office");
       msg.addLine("Thanks");
       msg.addLine("Regards");
       msg.addLine("Srathasf dfas");
       System.out.println("Message :" +msg);
   }

}

public interface IDiscountPolicy {
   public double computeDiscount(int count,double itemCost );
}

public class BulkDiscount implements IDiscountPolicy{

   int minimum;
   double discountPercent;
   public BulkDiscount(int minimum,double discountPercent){
       this.minimum = minimum;
       this.discountPercent = discountPercent;
   }
   @Override
   public double computeDiscount(int count, double itemCost) {
       if(count<minimum)
           return 0;
       return ((double)count*discountPercent*itemCost);
   }
}

public class BuyNItemsGetOneFree implements IDiscountPolicy {
   int n;
   public BuyNItemsGetOneFree(int n){
       this.n = n;
   }
   @Override
   public double computeDiscount(int count, double itemCost) {
       int freeItems = count/n;
       return (double)freeItems*itemCost;
   }
}

public class CombinedDiscount implements IDiscountPolicy{

   IDiscountPolicy dp1;
   IDiscountPolicy dp2;
  
   public CombinedDiscount(IDiscountPolicy p1,IDiscountPolicy p2){
       dp1=p1;
       dp2=p2;
   }
   @Override
   public double computeDiscount(int count, double itemCost) {
       return dp1.computeDiscount(count, itemCost)+dp2.computeDiscount(count, itemCost);
   }
}

public class DiscountTester {

   public static void main(String[] args) {
       BulkDiscount bd = new BulkDiscount(4, 30);
       BuyNItemsGetOneFree buyN = new BuyNItemsGetOneFree(10);
       CombinedDiscount combinedDis = new CombinedDiscount(bd, buyN);
       System.out.println("Combined Discount : "+combinedDis.computeDiscount(5, 10));
   }
}