Question

Order up:: Write a program that will be used to keep track of orders placed at...

Order up::

Write a program that will be used to keep track of orders placed at a donut shop. There are two types of items that can be ordered: coffee or donuts. Your program will have a class for each order type, and an abstract superclass.

Coffee orders are constructed with the following information:

  • quantity (int) - how many coffees are being ordered
  • size (String) - the size of the coffees (all coffees in the order have the same size)

Donut orders are constructed with following information:

  • quantity (int) - how many donuts are being ordered
  • price (double) - price per donut (all donuts in the order have the same price)
  • flavour (String) - the flavour of donut (all donuts in the order have the same flavour)

The two order types need constructors and toString methods. The toString should include in its results the type of order ("Coffee" or "Donut"), plus the value of all instance variables.

The size of a coffee determines its price. There are three sizes, with the following prices:

  • small is $1.39
  • medium is $1.69
  • large is $1.99

You can assume that when you construct a coffee order, one of those three strings will be passed as the size of the coffees in the order.

Your classes should have a method called totalPrice(), which returns the total price of the order. Total price is generally equal to price per unit times the quantity; however, orders of donuts with a quantity of less than 6 should have a 7% tax added to the total price. There is no tax on coffee orders, or donut orders with 6 or more donuts.

Use inheritance to prevent duplicate code. Make all your instance variables private, and write getter/setter methods only when necessary. Do not store total price as an instance variable, as it can be calculated when needed.

Your program will read in orders from a text file, create objects, and store them in a single ArrayList of objects of your classes. Each line of the file starts with the word "Coffee" or "Donut", and then provides values for the instance variables for an order of that type, separated by commas, in the same order given above. There will not be any errors in the file.

Coffee,3,medium
Donut,7,0.89,chocolate

Once your program has read in the values and stored them in the ArrayList, it should do the following:

  • Print the complete contents of the list (i.e. all the orders). Show both the results of the toString() and the total price of that order.
  • Print the total quantity of donuts in all the orders, and the total quantity of coffees in all the orders.
  • Print the total of all the prices of all the orders.

You will continue to refine your "Order up" program from above

The first change will be the addition of two new types of products that can be ordered: sandwiches and pop. This gives you a total of four products. These two new products will need new classes, plus additional modifications to the class hierarchy with one or more new abstract class(es).

Sandwich orders are constructed with following information:

  • quantity (int) - how many sandwiches are being ordered
  • price (double) - price per sandwich (all sandwiches in the order have the same price)
  • filling (String) - what's going in the sandwich (for all in the order)
  • bread (String) - the type of bread to use for the sandwich (for all in the order)

Pop orders are constructed with the following information:

  • quantity (int) - how many drinks are being ordered
  • size (String) - the size of the drinks (all drinks in the order have the same size)
  • brand (String) - the brand of pop being ordered

The new classes will need constructors and toString methods. The toString should include in its results the type of order, plus the value of all instance variables.

The size of a pop order determines its price. There are three sizes, with the following prices:

  • small is $1.79
  • medium is $2.09
  • large is $2.49

You can assume that when you construct a pop order, one of those three strings will be passed as the size of the drinks in the order.

Your classes should have a method called totalPrice(), which returns the total price of the order, calculated as before. Allsandwich orders have 7% tax applied to their price, regardless of the order size. The drinks do not, and the donuts have the same rule as before (7% tax if there are fewer than 6 donuts in the order).

Use inheritance to prevent duplicate code. Make all your instance variables private, and write getter/setter methods only when necessary. Do not store total price as an instance variable, as it can be calculated when needed. The principles of good object-oriented programming will be enforced strictly in this assignment.

The second change will be the use of a collection class to store the data. The collection will still contain an ArrayList, but it should also include instance methods that ensure only your order subclasses can ever be inserted into the collection. You may also wish to put some of the processing in your methods for that class.

The third change will be the addition of a sort() method, which will be an instance method in your collection class. It should sort the items in the collection by total price, in ascending order (smallest to largest price).

Your program will read in orders from a text file, create objects, and store them in the collection. Each line of the file starts with the word "Coffee", "Donut", "Pop", or "Sandwich", and then provides values for the instance variables for an order of that type, separated by commas, in the same order given above. There will not be any errors in the file.

Coffee,3,medium
Donut,7,0.89,chocolate
Pop,5,large,Splat! Cola
Sandwich,1,3.89,mystery meat,37-grain whole wheat

Once your program has read in the values and stored them in the collection, it should do the following:

  • Print the complete contents of the list (i.e. all the orders). Show both the results of the toString() and the total price of that order.
  • Sort the order by calling the sort method.
  • Print the items again, with total price, as you did in the first step.
  • Print the total quantity of donuts in all the orders, all the coffees, all the pops, and all the sandwiches
  • Print the total of all the prices of all the orders.

Text File:

Coffee,3,medium
Donut,7,0.89,chocolate
Donut,3,1.19,eclair
Coffee,1,large
Coffee,2,large
Coffee,7,small
Donut,6,0.89,baker's choice
Donut,1,1.10,lemon buster
Donut,120,0.15,petite oeufs
Coffee,10,medium
Pop,5,large,Splat! Cola
Sandwich,1,3.89,mystery meat,37-grain whole wheat
Sandwich,3,2.99,squeeze cheese,wonderful bread
Donut,1,0.15,petite oeufs
Pop,2,small,Sugar Water
Coffee,3,medium
Donut,7,0.89,chocolate
Pop,1,medium,Splat! Cola
Donut,3,1.19,eclair
Coffee,1,large
Sandwich,1,5.99,club,rye
Coffee,2,large
Coffee,7,small
Sandwich,1,4.59,chicken of the sea,seaweed
Donut,6,0.89,baker's choice
Donut,1,1.10,lemon buster
Sandwich,2,4.99,peanut butter and bacon,bagel
Donut,120,0.15,petite oeufs
Pop,3,medium,Fresssh
Coffee,10,medium

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// MAIN TEST CLASS
//==============================
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Maintests {

   public static void main(String[] args) throws IOException {
       // TODO Auto-generated method stub
       // calling the file order
       File fi = new File("order.txt");
       // setting up the items
       Collection item = new Collection();
       item.addItems(fi);
       // Showing up the items with total prices
       for(int i=0;i<item.list.size();i++) {
           System.out.println("Item N"+i+": "+item.list.get(i).toString() + " total price: "+ item.list.get(i).totalprice());
       }
   }

}

// ==================
// FOOD ABSTRACT CLASS


public abstract class Food {
   // ABSTRACT FOOD CLASS
   public abstract double totalprice();
}


//=======================
// DONUTS CLASS

public class Donut extends Food{
   private int quantity; // quantity of ordered donuts
   private double price; // donuts price
   String flavour; // donuts flavour
   Donut(int q,double p, String f){
       this.quantity = q;
       this.price = p;
       this.flavour = f;
   }
   @Override
   public String toString() {
       return quantity+ "Donut," + ",price/unit " + price + "," + flavour;
   }
   @Override
   public double totalprice() {
       //calculates the total price based on coffee size
       if(quantity<6) return quantity*price*1.07; // we multiply by 1.07 to add the 7% tax
       return quantity*price;
   }
  
}
//=======================
// COFFEE CLASS

public class Coffee extends Food{
   private int quantity; // (int) - how many coffees are being ordered
   private String size; // coffee size
   Coffee(int q,String s){
       this.quantity = q;
       this.size = s;
   }
   @Override
   public String toString() {
       return quantity + "Coffee, " + ", " + size;
   }
   @Override
   public double totalprice() {
       //calculates the total price based on coffee size
       if(size.equals("small")) return quantity*1.39;
       if(size.equals("medium")) return quantity*1.69;
       if(size.equals("large")) return quantity*1.99;
       return 0;
   }
  
}

//=======================
// SANDWICHES CLASS

public class Sandwich extends Food {
   private int quantity; // quantity of ordered donuts
   private double price; // donuts price
   private String filling;
   private String Bread;
   Sandwich(int q,double p, String f,String Bread){
       this.quantity = q;
       this.price = p;
       this.filling = f;
       this.Bread = Bread;
   }
   @Override
   public String toString() {
       return quantity + "Sandwich" + ", price/unit=" + price + "," + filling + ", " + Bread;
   }
   @Override
   public double totalprice() {
       // TODO Auto-generated method stub
       return price*quantity*1.07;
   }
}

//=======================
// POP CLASS

public class Pop extends Food {
   private int quantity; // quantity of ordered donuts
   private String size; // donuts price
   private String brand;
   Pop(int q,String p, String b){
       this.quantity = q;
       this.size = p;
       this.brand = b;
   }
   @Override
   public String toString() {
       return quantity + "Pop" + ", " + size + ", brand=" + brand;
   }
   @Override
   public double totalprice() {
       //calculates the total price based on coffee size
       if(size.equals("small")) return quantity*1.79;
       if(size.equals("medium")) return quantity*2.09;
       if(size.equals("large")) return quantity*2.49;
       return 0;
   }
}

//=======================
// COLLECTION CLASS
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Collection {

   ArrayList<Food> list = new ArrayList<Food>();
   ArrayList<Double> prices = new ArrayList<Double>();
   public void addItems(File fi) throws IOException {
       // OPENING THE FILE
       BufferedReader Lg = new BufferedReader(new FileReader(fi));
       String Ligne = ""; // to read lines
       String [] Read; // to split lines
       do {
           Ligne = Lg.readLine(); // reading lines
           if(Ligne!=null) {
               Read = Ligne.split(","); // splitting lines
               // each case scenario
               if(Read[0].equalsIgnoreCase("coffee")){
                   list.add(new Coffee(Integer.parseInt(Read[1]),Read[2]));
               }
               else if(Read[0].equalsIgnoreCase("donut")){
                   list.add(new Donut(Integer.parseInt(Read[1]),Double.parseDouble(Read[2]),Read[3]));
               }
               else if(Read[0].equalsIgnoreCase("sandwich")){
                   list.add(new Sandwich(Integer.parseInt(Read[1]),Double.parseDouble(Read[2]),Read[3],Read[4]));
               }
               else if(Read[0].equalsIgnoreCase("pop")){
                   list.add(new Pop(Integer.parseInt(Read[1]),Read[2],Read[3]));
               }
           }
       }while(Ligne!= null); // while there is stuff to read from the file
       // implement calculatePrice method to fill price list
       calculatePrice();
   }
   public void calculatePrice() {
       for(int i=0;i<list.size();i++) {
           // Add price of item on position i, to position i on list of prices
           prices.add(list.get(i).totalprice());
       }
   }
   // calculte min meant for price list (to be used in SORT)
   public static int min(ArrayList<Double> p) {
       int m=0;
       for(int i=0;i<p.size();i++) {
           if(p.get(m)>p.get(i)) m = i;
       }
       return m;
   }
   // cloning list to not have memory conflicts
   public static ArrayList<Double> cloneList(ArrayList<Double> L){
       ArrayList<Double> result = new ArrayList<Double>();
       for(int i=0;i<L.size();i++) {
           result.add(L.get(i));
       }
       return result;
   }
   // SORT LIST FUNCTION
   public static ArrayList<Food> Sort(ArrayList<Food> L, ArrayList<Double> Pr){
       ArrayList<Food> result = new ArrayList<Food>();
       ArrayList<Double> P = cloneList(Pr);
       int m = 0;
       for(int i=0;i<L.size();i++) {
           m = min(P);
           result.add(L.get(m));
           P.remove(m);
       }
       return result;
   }
}

OUTPUT:

Item N0: 3Coffee, , medium total price: 5.07
Item N1: 7Donut,,price/unit 0.89,chocolate total price: 6.23
Item N2: 3Donut,,price/unit 1.19,eclair total price: 3.8199
Item N3: 1Coffee, , large total price: 1.99
Item N4: 2Coffee, , large total price: 3.98
Item N5: 7Coffee, , small total price: 9.729999999999999
Item N6: 6Donut,,price/unit 0.89,baker's choice total price: 5.34
Item N7: 1Donut,,price/unit 1.1,lemon buster total price: 1.1770000000000003
Item N8: 120Donut,,price/unit 0.15,petite oeufs total price: 18.0
Item N9: 10Coffee, , medium total price: 16.9
Item N10: 5Pop, large, brand=Splat! Cola total price: 12.450000000000001
Item N11: 1Sandwich, price/unit=3.89,mystery meat, 37-grain whole wheat total price: 4.1623
Item N12: 3Sandwich, price/unit=2.99,squeeze cheese, wonderful bread total price: 9.597900000000001
Item N13: 1Donut,,price/unit 0.15,petite oeufs total price: 0.1605
Item N14: 2Pop, small, brand=Sugar Water total price: 3.58
Item N15: 3Coffee, , medium total price: 5.07
Item N16: 7Donut,,price/unit 0.89,chocolate total price: 6.23
Item N17: 1Pop, medium, brand=Splat! Cola total price: 2.09
Item N18: 3Donut,,price/unit 1.19,eclair total price: 3.8199
Item N19: 1Coffee, , large total price: 1.99
Item N20: 1Sandwich, price/unit=5.99,club, rye total price: 6.409300000000001
Item N21: 2Coffee, , large total price: 3.98
Item N22: 7Coffee, , small total price: 9.729999999999999
Item N23: 1Sandwich, price/unit=4.59,chicken of the sea, seaweed total price: 4.9113
Item N24: 6Donut,,price/unit 0.89,baker's choice total price: 5.34
Item N25: 1Donut,,price/unit 1.1,lemon buster total price: 1.1770000000000003
Item N26: 2Sandwich, price/unit=4.99,peanut butter and bacon, bagel total price: 10.678600000000001
Item N27: 120Donut,,price/unit 0.15,petite oeufs total price: 18.0
Item N28: 3Pop, medium, brand=Fresssh total price: 6.27
Item N29: 10Coffee, , medium total price: 16.9

COMMENT DOWN FOR ANY QUERIES AND,

LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.

Add a comment
Know the answer?
Add Answer to:
Order up:: Write a program that will be used to keep track of orders placed at...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are going to write a java application that will be used in a coffee shop...

    You are going to write a java application that will be used in a coffee shop to take customer orders and print a simple on screen order summary. Your coffee shop only sells one type and one size coffee for $5 dollars. However, your customers have the option of adding whipped cream and chocolate each for $1 dollar. Your application takes in customer name, the number of coffees being ordered and whether or not the customer wants whipped cream and/or...

  • In Java Pls Class Design: Donut (Suggested Time Spent: < 15 minutes) The Donut class is intended to be an abstract a...

    In Java Pls Class Design: Donut (Suggested Time Spent: < 15 minutes) The Donut class is intended to be an abstract and simplified representation of a yummy edible donut Class Properties (MUST be private) Name of the donut Type of donut Price of the donut Class Invariants Donut name must not be empty (Default: "Classic") Type of donut can only be one of the following: "Plain," "Filled," or "Glazed" (Default: "Plain") Class Components Public Getter and Private Setter for name,...

  • Sample output: Total number of shirts ordered: 10 Total price of all shirts: $173.00 Total number of pants ordered: 10...

    Sample output: Total number of shirts ordered: 10 Total price of all shirts: $173.00 Total number of pants ordered: 10 Total price of all pants: $500.00 Average waist size: 42.0 Average inseam size: 32.0 please don't copy and paste an already posted solution This is a program that calculates information about orders of shirts and pants. All orders have a quantity and a color. Write a Clothing class that matches this UML: The no-argument constructor will set the quantity to...

  • Using C++ Mr. Manager wants you to process orders for the Fast Greasy Food Shop. His...

    Using C++ Mr. Manager wants you to process orders for the Fast Greasy Food Shop. His customers have a few choices in what they order. The can order a hamburger for 1.00. It they want cheese it is an addition .50 and if they want bacon the additional amount is .75. They can also order a small or large drink for 1.10 and 1.60 respectively and small or large French fries for 1.20 or 1.75 respectively.    The user can only...

  • CSC 143 Weekly Exercise: Satisfying Slurps For this assignment, imagine you have been hired to create...

    CSC 143 Weekly Exercise: Satisfying Slurps For this assignment, imagine you have been hired to create a program to manage drink orders at a local coffee shop. The coffee shop is known for changing their menu frequently, and people come to the shop in anticipation of getting unusual, but delicious, drinks. Since the drinks change so frequently, your program will need to have the flexibility to handle this. Accordingly, you decide to create classes that can be used to price...

  • C Language Program. PLEASE USE DYNAMIC MEMORY AND FOLLOW DIRECTIONS GIVEN. USE FUNCTIONS GIVEN ALSO. Thank...

    C Language Program. PLEASE USE DYNAMIC MEMORY AND FOLLOW DIRECTIONS GIVEN. USE FUNCTIONS GIVEN ALSO. Thank you. NO FFLUSH. Problem 5 (40 points)-Write a program Submit orders.c (Note: you wil be dynamically allocating memory for this problem. The second argument on the command line will let you know how much memory you will be allocating. O points if you do not dynamically allocate) Chef Bartolomeo owns a very busy Italian restaurant. He has recently started accepting orders online and every...

  • Write a JAVA program to monitor the flow of an item into an out of a...

    Write a JAVA program to monitor the flow of an item into an out of a warehouse. The warehouse has numerous deliveries and shipments for this item (a widget) during the time period covered. A shipment out (an order) is billed at a profit of 50% over the cost of the widget. Unfortunately each incoming shipment may have a different cost associated with it. The accountants of the firm have instituted a last-in, first out system for filling orders. This...

  • What if you had to write a program that would keep track of a list of...

    What if you had to write a program that would keep track of a list of rectangles? This might be for a house painter to use in calculating square footage of walls that need paint, or for an advertising agency to keep track of the space available on billboards. The first step would be to define a class where one object represents one rectangle's length and width. Once we have class Rectangle, then we can make as many objects of...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • Write a program in java. You are tasked with writing an application that will keep track...

    Write a program in java. You are tasked with writing an application that will keep track of Insurance Policies. Create a Policy class called InsurancePolicies.java that will model an insurance policy for a single person. Use the following guidelines to create the Policy class: • An insurance policy has the following attributes: o Policy Number o Provider Name o Policyholder’s First Name o Policyholder’s Last Name o Policyholder’s Age o Policyholder’s Smoking Status (will be “smoker” or “non-smoker”) o Policyholder’s...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT