Last active
February 19, 2017 18:50
-
-
Save HDI1234/b0e21e27c74b6ad1ad5f8b5f0acc5a77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class retailItem { | |
private String description; | |
private int unitsOnHand, quantityPurchased; | |
private double price,subtotal,tax,total; | |
retailItem (String description, int units, double p, int quantity) { | |
this.description = description; | |
this.unitsOnHand = units; | |
this.price = p; | |
this.quantityPurchased = quantity; | |
} | |
void setItem (String desciption, int units, double p, int quantity) { | |
this.unitsOnHand = units; | |
this.price = p; | |
this.quantityPurchased = quantity; | |
} | |
void setUnits (int units) { | |
this.unitsOnHand = units; | |
} | |
void setPrice (double p) { | |
this.price = p; | |
} | |
void setQuantity (int quantity) { | |
this.quantityPurchased = quantity; | |
} | |
public String getDesc () { | |
return description; | |
} | |
public int getUnits () { | |
return unitsOnHand; | |
} | |
public double getPrice () { | |
return price; | |
} | |
public int getQuantity () { | |
return quantityPurchased; | |
} | |
public double calcSubtotal (int quantity, double price) { | |
double subtotal = quantity*price; | |
return subtotal; | |
} | |
public double calcTax (double subtotal) { | |
double tax = subtotal*0.06; | |
return tax; | |
} | |
public double calcTotal (double subtotal, double tax) { | |
double total = subtotal + tax; | |
return total; | |
} | |
public String toString (){ | |
return ( | |
"[Item Description]Name: " + description | |
+ "\nUnits Available: " + unitsOnHand | |
+ "\nPrice: RM" + price | |
+ "\nQuantity Purchased: " + quantityPurchased | |
+ "\n------------------\nSubtotal: RM" + calcSubtotal(quantityPurchased,price) | |
+ "\nTax: RM" + calcTax(subtotal) | |
+ "\nTotal: RM" + calcTotal(subtotal,tax) | |
); | |
} | |
public static void main (String[]args) { | |
retailItem item1 = new retailItem ("T-Shirt", 50, 24.95, 8); | |
retailItem item2 = new retailItem ("Denim Jacket", 30, 69.99, 5); | |
item1.setPrice(29.95); | |
item1.toString(); | |
item2.setUnits(35); | |
item2.toString(); | |
retailItem item3 = new retailItem (); | |
Scanner input = new Scanner(System.in); | |
System.out.print("Item Name: "); | |
item3.setDesc(input.nextLine()); | |
System.out.print("Units Available: "); | |
item3.setUnits(Integer.parseInt(input.nextLine())); | |
System.out.print("Price: "); | |
item3.setPrice(Double.parseDouble(input.nextLine())); | |
System.out.print("Units Purchased: "); | |
item3.setQuantity(Integer.parseInt(input.nextLine())); | |
item3.toString(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment