Java Code Part A: Order up ( Please write coments in program) Write a program th
ID: 3914884 • Letter: J
Question
Java Code
Part A: Order up ( Please write coments in program)
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.
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.
Use the data file a4a.txt to test your program.
a4a.txt:
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
// Abstract base class Items defined
abstract class Items
{
// How many items are being ordered
int quantity;
// Price per item
double price;
}// End of class
// Class Coffee derived from Items
class Coffee extends Items
{
// The size of the coffees (all coffees in the order have the same size)
String size;
// Parameterized constructor
Coffee(int qty, String size)
{
// Assigns quantity to base class instance variable
quantity = qty;
// Checks if the size is "small" set the base class instance variable price to 1.39
if(size.equalsIgnoreCase("small"))
price = 1.39;
// Otherwise checks if the size is "medium" set the base class instance variable price to 1.69
else if(size.equalsIgnoreCase("medium"))
price = 1.69;
// Otherwise size is "large" set the base class instance variable price to 1.99
else
price = 1.99;
}// End of constructor
// Method to calculate and return total price
double totalPrice()
{
return (quantity * price);
}// End of method
// Overrides toString method
public String toString()
{
String res = "";
res += " Item Type: " + this.getClass().getName() + " Quantity: " + quantity +
" Price: " + price + " Amount: " + totalPrice();
return res;
}// End of method
}// End of class
// Class Donut derived from Items
class Donut extends Items
{
// The flavour of donut (all donuts in the order have the same flavour)
String flavour;
Donut(int qty, double pr, String fla)
{
// Assigns quantity to base class instance variable
quantity = qty;
price = pr;
flavour = fla;
}// End of constructor
// Method to calculate and return total price
double totalPrice()
{
double amount = quantity * price;
if(quantity < 6)
return (amount += amount * 0.7);
else
return amount;
}// End of method
// Overrides toString method
public String toString()
{
String res = "";
res += " Item Type: " + this.getClass().getName() + " Quantity: " + quantity + " Price: " + price +
" Flavour: " + flavour + " Amount: " + totalPrice();
return res;
}// End of method
}// End of class
// Class Sandwich derived from Items
class Sandwich extends Items
{
// What's going in the sandwich (for all in the order)
String filling;
// The type of bread to use for the sandwich (for all in the order)
String bread;
Sandwich(int qty, double pr, String filling, String bred)
{
// Assigns quantity to base class instance variable
quantity = qty;
price = pr;
this.filling = filling;
this.bread = bread;
}// End of constructor
// Method to calculate and return total price
double totalPrice()
{
return (quantity * price);
}// End of method
// Overrides toString method
public String toString()
{
String res = "";
res += " Item Type: " + this.getClass().getName() + " Quantity: " + quantity + " Price: " + price
+ " Filling: " + filling + " Bread: " + bread + " Amount: " + totalPrice();
return res;
}// End of method
}// End of class
// Class Pop derived from Items
class Pop extends Items
{
// The size of the drinks (all drinks in the order have the same size)
String size;
// The brand of pop being ordered
String brand;
Pop(int qty, String size, String brand)
{
// Assigns quantity to base class instance variable
quantity = qty;
this.brand = brand;
// Checks if the size is "small" set the base class instance variable price to 1.79
if(size.equalsIgnoreCase("small"))
price = 1.79;
// Otherwise checks if the size is "medium" set the base class instance variable price to 2.09
else if(size.equalsIgnoreCase("medium"))
price = 2.09;
// Otherwise size is "large" set the base class instance variable price to 2.49
else
price = 2.49;
}// End of constructor
// Method to calculate and return total price
double totalPrice()
{
return (quantity * price);
}// End of method
// Overrides toString method
public String toString()
{
String res = "";
res += " Item Type: " + this.getClass().getName() + " Quantity: " + quantity + " Price: " + price
+ " Size: " + size + " Brand: " + brand + " Amount: " + totalPrice();
return res;
}// End of method
}// End of class
// Driver class ItemsDriver definition
public class ItemsDriver
{
// Creates an array list to store objects of Items
static ArrayList <Items> ItemObjects;
//Method to red file contents
static void readFile(String fileName)
{
// Initializes the array list object
ItemObjects = new ArrayList<Items>();
// Try block
try
{
// File Reader object created to read data from given file name
File reader = new File(fileName);
// Scanner class object created
Scanner sc = new Scanner(reader);
// Loops till data available
while(sc.hasNextLine())
{
// Reads a line of string from the file and stores it in data
String data = sc.nextLine();
// Split the contents of the string by comma and stores it in obj array
String []obj = data.split(",");
// Checks if the starting index position of the obj is "Coffee"
if(obj[0].equalsIgnoreCase("Coffee"))
// Creates an instance of Coffee class by calling parameterized constructor and adds it to ItemObjects
ItemObjects.add(new Coffee(Integer.parseInt(obj[1]), obj[2]));
// Checks if the starting index position of the obj is "Donut"
else if(obj[0].equalsIgnoreCase("Donut"))
// Creates an instance of Donut class by calling parameterized constructor and adds it to ItemObjects
ItemObjects.add(new Donut(Integer.parseInt(obj[1]), Double.parseDouble(obj[2]), obj[3]));
// Checks if the starting index position of the obj is "Pop"
else if(obj[0].equalsIgnoreCase("Pop"))
// Creates an instance of Pop class by calling parameterized constructor and adds it to ItemObjects
ItemObjects.add(new Pop(Integer.parseInt(obj[1]), obj[2], obj[3]));
// Checks if the starting index position of the obj is "Sandwich"
else if(obj[0].equalsIgnoreCase("Sandwich"))
// Creates an instance of Sandwifh class by calling parameterized constructor and adds it to ItemObjects
ItemObjects.add(new Sandwich(Integer.parseInt(obj[1]), Double.parseDouble(obj[2]), obj[3], obj[4]));
}//End of while
//Closer the file
sc.close();
} //End of try block
//Catch block to handle file not found exception
catch(FileNotFoundException e)
{
System.out.println("File Not found");
e.printStackTrace();
}//End of catch
}//End of method
// main method definition
public static void main(String[] args)
{
// Calls the method to read the file contents and store it in ItemObject array list
readFile("Items.txt");
// Displays the contents of the objects using toString override method
for(Items i : ItemObjects)
System.out.println(i);
}// End of main method
}// End of class
Sample Run:
Item Type: Coffee Quantity: 3 Price: 1.69 Amount: 5.07
Item Type: Donut Quantity: 7 Price: 0.89 Flavour: chocolate Amount: 6.23
Item Type: Donut Quantity: 3 Price: 1.19 Flavour: eclair Amount: 6.068999999999999
Item Type: Coffee Quantity: 1 Price: 1.99 Amount: 1.99
Item Type: Coffee Quantity: 2 Price: 1.99 Amount: 3.98
Item Type: Coffee Quantity: 7 Price: 1.39 Amount: 9.729999999999999
Item Type: Donut Quantity: 6 Price: 0.89 Flavour: baker's choice Amount: 5.34
Item Type: Donut Quantity: 1 Price: 1.1 Flavour: lemon buster Amount: 1.87
Item Type: Donut Quantity: 120 Price: 0.15 Flavour: petite oeufs Amount: 18.0
Item Type: Coffee Quantity: 10 Price: 1.69 Amount: 16.9
Item Type: Pop Quantity: 5 Price: 2.49 Size: null Brand: Splat! Cola Amount: 12.450000000000001
Item Type: Sandwich Quantity: 1 Price: 3.89 Filling: mystery meat Bread: null Amount: 3.89
Item Type: Sandwich Quantity: 3 Price: 2.99 Filling: squeeze cheese Bread: null Amount: 8.97
Item Type: Donut Quantity: 1 Price: 0.15 Flavour: petite oeufs Amount: 0.255
Item Type: Pop Quantity: 2 Price: 1.79 Size: null Brand: Sugar Water Amount: 3.58
Item Type: Coffee Quantity: 3 Price: 1.69 Amount: 5.07
Item Type: Donut Quantity: 7 Price: 0.89 Flavour: chocolate Amount: 6.23
Item Type: Pop Quantity: 1 Price: 2.09 Size: null Brand: Splat! Cola Amount: 2.09
Item Type: Donut Quantity: 3 Price: 1.19 Flavour: eclair Amount: 6.068999999999999
Item Type: Coffee Quantity: 1 Price: 1.99 Amount: 1.99
Item Type: Sandwich Quantity: 1 Price: 5.99 Filling: club Bread: null Amount: 5.99
Item Type: Coffee Quantity: 2 Price: 1.99 Amount: 3.98
Item Type: Coffee Quantity: 7 Price: 1.39 Amount: 9.729999999999999
Item Type: Sandwich Quantity: 1 Price: 4.59 Filling: chicken of the sea Bread: null Amount: 4.59
Item Type: Donut Quantity: 6 Price: 0.89 Flavour: baker's choice Amount: 5.34
Item Type: Donut Quantity: 1 Price: 1.1 Flavour: lemon buster Amount: 1.87
Item Type: Sandwich Quantity: 2 Price: 4.99 Filling: peanut butter and bacon Bread: null Amount: 9.98
Item Type: Donut Quantity: 120 Price: 0.15 Flavour: petite oeufs Amount: 18.0
Item Type: Pop Quantity: 3 Price: 2.09 Size: null Brand: Fresssh Amount: 6.27
Item Type: Coffee Quantity: 10 Price: 1.69 Amount: 16.9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.