Skip to content

Instantly share code, notes, and snippets.

@schleary
Created August 13, 2014 17:26
Show Gist options
  • Save schleary/43d526d2a6e510ff38bf to your computer and use it in GitHub Desktop.
Save schleary/43d526d2a6e510ff38bf to your computer and use it in GitHub Desktop.
Amazon Customer Product Simulation
import java.util.Scanner;
/**
* @author Holly Leary
*
* Customer class - Extra credit part one completed
*
*/
public class Customer {
private String last;
private String first;
private String street;
private String city;
private String state;
private String zip;
private double tax;
/**
* Customer constructor
* sets customer information
*
*/
public Customer(Scanner inFile) {
readNextCustomerInfo(inFile);
}
/**
*
* @return last name
*/
public String getLast(){
return this.last;
}
/**
*
* @param String last
* set last name
*/
public void setLast(String last){
this.last = last;
}
/**
*
* @return first name
*/
public String getFirst(){
return this.first;
}
/**
*
* @param first name (set)
*/
public void setFirst(String first){
this.first = first;
}
/**
*
* @return city
*/
public String getCity(){
return this.city;
}
/**
*
* @param city (set)
*/
public void setCity(String city){
this.city = city;
}
/**
*
* @return street
*/
public String getStreet(){
return this.street;
}
/**
*
* @param street (set)
*/
public void setStreet(String street){
this.street = street;
}
/**
*
* @return state
*/
public String getState(){
return this.state;
}
/**
*
* @param state (set)
*/
public void setState(String state){
this.state = state;
}
/**
*
* @return zip
*/
public String getZip(){
return this.zip;
}
/**
*
* @param zip (set)
*/
public void setZip(String zip){
this.zip = zip;
}
/**
*
* @return tax
*/
public double getTax(){
return this.tax;
}
/**
*
* @param tax (set)
*/
public void setTax(double tax){
// The sales tax member variable should be input constrained to not drop below
// zero and not be allowed to be set greater than 1.0.
if ((tax >=0) && (tax <= 1)){
this.tax = tax;
} else {
throw new IllegalArgumentException("Sales tax must be between 0 and 1.0, exclusive.");
}
}
/**
*
* @param Scanner input
* set customer member variables
* @return boolean false if lines remain
*/
public boolean readNextCustomerInfo(Scanner input) {
int i = 0;
while (input.hasNextLine()) {
if (i > 6) {
return false;
}
String line = input.nextLine().trim();
if (line.length() <=0){
System.out.println("Error: empty customer field(s) in text file. Exiting program.");
System.exit(0);
}
if (i == 0) {
setLast(line);
} else if (i == 1) {
setFirst(line);
} else if (i == 2) {
setStreet(line);
} else if (i == 3) {
setCity(line);
} else if (i == 4) {
setState(line);
} else if (i == 5) {
setZip(line);
} else if (i == 6){
// convert string to double
try{
double tax = Double.parseDouble(line);
setTax(tax);
} catch (Exception e) {
System.out.println("Error: invalid text entry. Tax must be in numeric format. Exiting system.");
System.exit(0);
}
}
i++;
}
return true;
}
}
Fakename
Bob
123 Fake Street
Fake City
FS
99999
.07
The Shawshank Redemption
DVD
1.95
1
true
The Shawshank Redemption
DVD
1.95
1
true
Dracula
Book
.76
1
false
The Dark Knight
DVD
.95
1
true
Lego Ultimate Building Set
Toy
2.95
3
false
The Girl With The Dragon Tattoo
Book
1.95
1
true
Iron Man
DVD
1.95
1
false
Iron Man
DVD
1.95
1
false
Under The Dome
Book
1.95
1
true
/**
* @author Holly Leary
*
* lab04 class - Extra credit part one completed
*
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class lab04 {
private Customer customer;
private ArrayList<SimpleProduct> productList = new ArrayList<SimpleProduct>();
private SimpleProduct product;
private Queue<SimpleProduct> stockedQueue = new LinkedList<SimpleProduct>();
private Stack<SimpleProduct> unstockedStack = new Stack<SimpleProduct>();
private double subtotal;
private double preShipTotal;
private double shipTotal;
private double shippingPercent;
private boolean safeToAdd;
/**
* Customer constructor
* gets file information and distributes reports appropriately
*
*/
public lab04() {
//Get file from user
String fileName = getUserFile();
//place file contents in correct data structures
allocateFileContents(fileName);
//print reports
reportOne();
reportTwo();
}
/**
* @return String file name that contains order database.
*/
private String getUserFile(){
System.out.println("Please enter database file name? ");
Scanner inFile = new Scanner(System.in);
String fileName = inFile.nextLine();
return fileName;
}
/**
* @param fileName
* appropriately allocates contents of file to customer info, stocked queue, and unstocked stack
*/
private void allocateFileContents(String fileName){
Scanner input = null;
int i = 0;
try {
input = new Scanner(new File(fileName));
if((i == 0) && (! input.hasNext())){
System.out.println("Error: Empty text file. Exiting system.");
System.exit(0);
}
while (input.hasNextLine()) {
//if scanner has read past customer info
if (i >= 7) {
product = new SimpleProduct(input);
this.safeToAdd = true;
for (int p = 0; p < productList.size(); p++){
if (product.equals(productList.get(p).getName(), productList.get(p).getType())){
this.safeToAdd = false;
}
}
if (this.safeToAdd == true){
productList.add(product);
// in stock?
if (product.getInStock() == true) {
// add to FIFO queue
stockedQueue.add(product);
} else {
// add to stack
unstockedStack.push(product);
}
i = i + 5;
}
//if customer info
} else {
customer = new Customer(input);
i = i + 7;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Error: file not found. Exiting System. ");
System.exit(0);
}
}
/**
* prints report with in-stock items to be shipped
*/
private void reportOne(){
System.out.println("");
System.out.println("-------------------------------------------------");
System.out.println("");
System.out.println("Shipping to:");
//print address
generateAddress();
System.out.println("-------------------------------------------------");
System.out.println("");
//print queue info
printQueueInfo();
System.out.println("");
System.out.println("-------------------------------------------------");
System.out.println("");
System.out.format("Subtotal: $%.2f%n", this.subtotal);
System.out.format("Sales tax (%.2f): $%.2f%n", customer.getTax(),
(customer.getTax() * this.subtotal));
//get shipping
calculateShippingTotal();
System.out.format("Shipping: $%.2f%n", this.shipTotal);
System.out.println("");
System.out.println("-------------------------------------------------");
System.out.println("");
System.out.format("Total: $%.2f%n", (this.shipTotal + this.preShipTotal));
System.out.println("");
System.out.println("-------------------------------------------------");
}
/**
* Print queue product information and
* @return subtotal
*/
public double printQueueInfo(){
this.subtotal = 0;
int stockedQueueSize = stockedQueue.size();
SimpleProduct current;
for (int i = 0; i < stockedQueueSize; i++) {
//pop from queue, assign local variable, and get info
current = stockedQueue.remove();
double sub = current.getQuantity() * current.getPrice();
System.out.println(current.getQuantity() + " x "
+ current.getName() + " (" + current.getType() + ") "
+ current.getPrice());
this.subtotal = sub + this.subtotal;
}
return this.subtotal;
}
/**
* calculate total cost with shipping
*/
public void calculateShippingTotal(){
this.preShipTotal = (this.subtotal * customer.getTax()) + this.subtotal;
if (this.preShipTotal > 25) {
this.shippingPercent = 0;
} else if (this.preShipTotal > 10) {
this.shippingPercent = .05;
} else {
this.shippingPercent = .15;
}
this.shipTotal = this.shippingPercent * this.preShipTotal;
}
/**
* prints report with items that are on hold, along with outstanding balance
*/
private void reportTwo(){
System.out.println("");
System.out.println("Orders Outstanding For:");
//print address
generateAddress();
System.out.println("-------------------------------------------------");
System.out.println("");
//print stack info
printStackInfo();
System.out.println("");
System.out.println("-------------------------------------------------");
System.out.println("");
System.out.format("Outstanding balance: $%.2f%n", this.subtotal);
System.out.println("");
System.out.println("-------------------------------------------------");
System.out.println("");
}
/**
* prints on-hold items with product info
*/
public void printStackInfo(){
this.subtotal = 0;
int unstockedStackSize = unstockedStack.size();
SimpleProduct unstocked;
double sub;
for (int i = 0; i < unstockedStackSize; i++) {
unstocked = unstockedStack.pop();
sub = unstocked.getQuantity() * unstocked.getPrice();
System.out.println(unstocked.getQuantity() + " x "
+ unstocked.getName() + " (" + unstocked.getType() + ") "
+ unstocked.getPrice());
this.subtotal = sub + this.subtotal;
}
}
/**
* prints out customer address
*/
public void generateAddress(){
System.out.println("");
System.out.println(customer.getFirst() + " " + customer.getLast());
System.out.println(customer.getStreet());
System.out.println(customer.getCity() + ", " + customer.getState()
+ " " + customer.getZip());
System.out.println("");
}
public static void main(String[] args) {
lab04 lab = new lab04();
}
}
/**
* Product
*
* A simple interface for a possible family of Product
* classes.
*
* Extra credit part one (Customer Class) completed
*
* @author Jeremy Morris
* @version 20120928
*/
import java.util.Scanner;
public interface Product {
/*
* setName
* @param name - new name for the product
*/
public void setName(String name);
/*
* getName
* @return the name of the product
*/
public String getName();
/*
* setType
* @param type - the type of the product
*/
public void setType(String type);
/*
* getType
* @return - the product type
*/
public String getType();
/*
* setPrice
* @param price - the price of the product
*/
public void setPrice(double price);
/*
* getPrice
* @return the price of the product
*/
public double getPrice();
/*
* setQuantity
* @param quantity - the number of this product in inventory
*/
public void setQuantity(int quantity);
/*
* getQuantity
* @return the number of this product in inventory
*/
public int getQuantity();
/*
* setInStock
* @param inStock - true if this product is in stock
*/
public void setInStock(boolean inStock);
/*
* getQuantity
* @return true if this product is in stock
*/
public boolean getInStock();
/*
* readNextProduct
* @param inFile - a Scanner containing product entries
* @return false if the product cannot be completely read,
* true otherwise
*/
public boolean readNextProduct(Scanner inFile);
}
/**
* @author Holly Leary
* SimpleProduct class
*
* Project04.java
* Extra credit part one (Customer Class) completed
*/
import java.util.Scanner;
public class SimpleProduct implements Product {
// private member variables
private String name;
private String type;
private double price;
private int quantity;
private boolean inStock;
// constructor
public SimpleProduct(Scanner inFile) {
readNextProduct(inFile);
}
/*
* setName
*
* @param name - new name for the product
*/
public void setName(String name) {
this.name = name;
}
/*
* getName
*
* @return the name of the product
*/
public String getName() {
return this.name;
}
/*
* setType
*
* @param type - the type of the product
*/
public void setType(String type) {
this.type = type;
}
/*
* getType
*
* @return - the product type
*/
public String getType() {
return this.type;
}
/*
* setPrice
*
* @param price - the price of the product
*/
public void setPrice(double price) {
this.price = price;
}
/*
* getPrice
*
* @return the price of the product
*/
public double getPrice() {
return this.price;
}
/*
* setQuantity
*
* @param quantity - the number of this product in inventory
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}
/*
* getQuantity
*
* @return the number of this product in inventory
*/
public int getQuantity() {
return this.quantity;
}
/*
* setInStock
*
* @param inStock - true if this product is in stock
*/
public void setInStock(boolean inStock) {
this.inStock = inStock;
}
/*
* getQuantity
*
* @return true if this product is in stock
*/
public boolean getInStock() {
return this.inStock;
}
/*
* readNextProduct
*
* @param inFile - a Scanner containing product entries
*
* @return false if the product cannot be completely read, true otherwise
*/
public boolean readNextProduct(Scanner input) {
int i = 0;
while (input.hasNextLine()) {
if (i > 4) {
return false;
}
String line = input.nextLine().trim();
if (line.length() <=0){
System.out.println("Error: empty product field(s) in text file. Exiting program.");
System.exit(0);
}
if (i == 0) {
setName(line);
} else if (i == 1) {
setType(line);
} else if (i == 2) {
// convert string to double
try{
double price = Double.parseDouble(line);
setPrice(price);
} catch (Exception e) {
System.out.println("Error: invalid text entry. Prices must be in numeric format. Exiting system.");
System.exit(0);
}
} else if (i == 3) {
// convert string to int
try{
int quantity = Integer.parseInt(line);
setQuantity(quantity);
} catch (Exception e) {
System.out.println("Error: invalid text entry. Quantities must be entered as integers. Exiting system.");
System.exit(0);
}
} else if (i == 4) {
// convert string to boolean
if (line.equalsIgnoreCase("true")
|| line.equalsIgnoreCase("false")) {
setInStock(Boolean.valueOf(line));
} else {
System.out.println("Error: invalid text file entry. Products must be listed as stocked(true) or not(false). Exiting system.");
System.exit(0);
}
}
i++;
}
return true;
}
/**
* @param obj
* @return true value if the name and type of the Product are the same.
*/
public boolean equals(String name, String type) {
return (this.name.equals(name) && this.type.equals(type));
}
/**
* @return string that contains the name of the product, the type of the
* product, the price and the quantity of the product as a
* comma-separated list enclosed in parentheses.
*/
public String toString() {
return ("(" + getName() + ", " + getType() + ", " + getPrice() + ", " + getQuantity()+ ")");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment