Last active
November 22, 2023 08:43
-
-
Save yaswanthrajyadiki/f9b2ec5fbbc1368a54de to your computer and use it in GitHub Desktop.
This is Shopping Cart Code
This file contains 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
class Item { | |
private String productName; | |
private int quantity; | |
private double unitPrice; | |
Item(String productName, int quantity, double unitPrice) { | |
this.productName = productName; | |
this.quantity = quantity; | |
this.unitPrice = unitPrice; | |
} | |
public String toString() { | |
String s = this.productName + ":"; | |
s = s + this.quantity + "\n"; | |
return s; | |
} | |
public String getProductName() { | |
return this.productName; | |
} | |
public int getQuantity() { | |
return this.quantity; | |
} | |
public double getUnitPrice() { | |
return this.unitPrice; | |
} | |
} |
This file contains 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
/** | |
* | |
* ShoppingCartTest has a main method and you cannot make changes to it. | |
* You have to implement Item and ShoppingCart classes for the main method to work | |
* Item class should have a constructor with 3 arguments. (product name, quantity, unit price) | |
* Shopping Cart should hold all the items the user wants to buy | |
* It should implement addToCart, removeFromCart, printInvoice, applyCoupon methods. | |
* It should also implement getTotalAmount, getPayableAmount | |
* Sales tax is 14% of the total amount and it should be printed on the invoice | |
* On applying coupon IND10, 10% discount should be applied to the cart | |
* No other copon codes are allowed | |
* | |
*/ | |
public class ShoppingCartTest | |
{ | |
public static void main(String[] args) | |
{ | |
// creates new items with product name, quantity and unit price | |
Item i1 = new Item("Olive Oil 1l", 3, 100.0); | |
Item i2 = new Item("Cheese Slices", 2, 50.0); | |
Item i3 = new Item("Bread", 1, 75.0); | |
Item i4 = new Item("Eggs", 50, 10.0); | |
Item i5 = new Item("Chicken Salami", 2, 100.0); | |
ShoppingCart cart = new ShoppingCart(); | |
/* add the item to the cart */ | |
cart.addToCart(i1); | |
cart.addToCart(i2); | |
cart.addToCart(i3); | |
cart.addToCart(i4); | |
cart.addToCart(i5); | |
/* | |
* shows the list of items in the cart with quantity | |
* Expected Output to the console: | |
* Olive Oil 1l: 3 | |
* Cheese Slices: 2 | |
* Bread: 1 | |
* Eggs: 50 | |
* Chicken Salami: 2 | |
*/ | |
cart.showCart(); | |
/* remove the item sent as argument from the cart */ | |
cart.removeFromCart(i3); | |
/* | |
* shows the list of items in the cart with quantity | |
* Expected Output to the console: | |
* Olive Oil 1l: 3 | |
* Cheese Slices: 2 | |
* Eggs: 50 | |
* Chicken Salami: 2 | |
*/ | |
cart.showCart(); | |
/* | |
* gets the total amount for the items in the cart | |
* Expected Output: | |
* 1100 | |
*/ | |
double totalAmount = cart.getTotalAmount(); | |
System.out.println(totalAmount); | |
/* | |
* Gets the payable amount, | |
* it deduct discount and add tax to the total amount of items in cart | |
* Expected Output: | |
* 1254 | |
*/ | |
double payableAmount = cart.getPayableAmount(); | |
System.out.println(payableAmount); | |
/* apply a coupon code */ | |
cart.applyCoupon("IND50"); | |
/* | |
* print the items with the quanity, unit price, total amount | |
* apply coupon if valid | |
* add tax to the total amount after discount | |
* Expected Output to the console: | |
* Olive Oil 1l 3 100.00 300.00 | |
* Cheese Slices 2 50.00 100.00 | |
* Eggs 50 10.00 500.00 | |
* Chicken Salami 2 100.00 200.00 | |
* Total: 1100.00 | |
* Disc%: 0.00 | |
* Tax: 154.00 | |
* Total: 1254.00 | |
* | |
*/ | |
cart.printInvoice(); | |
cart.applyCoupon("IND10"); | |
/* | |
* print the items with the quanity, unit price, total amount | |
* apply coupon if valid | |
* add tax to the total amount after discount | |
* Expected Output to the console: | |
* Olive Oil 1l 3 100.00 300.00 | |
* Cheese Slices 2 50.00 100.00 | |
* Eggs 50 10.00 500.00 | |
* Chicken Salami 2 100.00 200.00 | |
* Total: 1100.00 | |
* Disc%: 110.00 | |
* Tax: 154.00 | |
* Total: 1128.60 | |
* | |
*/ | |
cart.printInvoice(); | |
cart.addToCart(new Item("Milk", 5, 30.00)); | |
/* | |
* print the items with the quanity, unit price, total amount | |
* apply coupon if valid | |
* add tax to the total amount after discount | |
* Expected Output to the console: | |
* Olive Oil 1l 3 100.00 300.00 | |
* Cheese Slices 2 50.00 100.00 | |
* Eggs 50 10.00 500.00 | |
* Chicken Salami 2 100.00 200.00 | |
* Milk 5 30.00 150.00 | |
* Total: 1250.00 | |
* Disc%: 125.00 | |
* Tax: 157.50 | |
* Total: 1282.50 | |
* | |
*/ | |
cart.printInvoice(); | |
} | |
} |
This file contains 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.ArrayList; | |
import java.util.ListIterator; | |
class ShoppingCart { | |
ArrayList<Item> item; | |
double totalAmount; | |
double payableAmount; | |
double discount; | |
double tax; | |
String coupon; | |
ShoppingCart() { | |
this.item = new ArrayList<Item>(); | |
this.coupon = ""; | |
this.totalAmount = 0; | |
this.payableAmount = 0; | |
this.discount = 0; | |
this.tax = 0; | |
} | |
public void addToCart(Item item) { | |
this.item.add(item); | |
} | |
public void showCart() { | |
ListIterator<Item> iterator = item.listIterator(); | |
while(iterator.hasNext()) { | |
Item item1 = iterator.next(); | |
System.out.println(item1); | |
} | |
} | |
public void removeFromCart(Item i) { | |
ListIterator<Item> iterator1 = item.listIterator(); | |
while(iterator1.hasNext()) { | |
Item item2 = iterator1.next(); | |
if (item2.getProductName().equals(i.getProductName())) { | |
this.item.remove(i); | |
break; | |
} | |
} | |
} | |
public double getTotalAmount() { | |
ListIterator<Item> iterator2 = item.listIterator(); | |
this.totalAmount = 0; | |
while(iterator2.hasNext()) { | |
Item item3 = iterator2.next(); | |
this.totalAmount = this.totalAmount + (item3.getUnitPrice() * item3.getQuantity()); | |
} | |
return this.totalAmount; | |
} | |
public void applyCoupon(String coupon) { | |
this.coupon = coupon; | |
if (coupon.equals("IND10")) { | |
this.discount = this.getTotalAmount() * (0.1); | |
this.totalAmount = this.totalAmount - this.discount; | |
} else { | |
this.totalAmount = this.totalAmount; | |
} | |
} | |
public double getPayableAmount() { | |
this.payableAmount = 0; | |
this.tax = this.totalAmount * (0.14); | |
this.payableAmount = this.totalAmount + this.tax; | |
return this.payableAmount; | |
} | |
public void printInvoice() { | |
ListIterator<Item> iterator3 = item.listIterator(); | |
while(iterator3.hasNext()) { | |
Item item4 = iterator3.next(); | |
System.out.print(item4.getProductName() + "\t"); | |
System.out.print(item4.getQuantity() + "\t"); | |
System.out.print(item4.getUnitPrice() + "\t"); | |
System.out.println(item4.getUnitPrice() * item4.getQuantity()); | |
} | |
System.out.println("\t\t\t" + "Total : " + this.getTotalAmount()); | |
this.applyCoupon(this.coupon); | |
System.out.println("\t\t\t" + "Discount : " + this.discount); | |
this.getPayableAmount(); | |
System.out.println("\t\t\t" + "Tax : " + this.tax); | |
System.out.println("\t\t\t" + "Total : " + this.getPayableAmount()); | |
} | |
} |
How do i run this files as they are all separated
How do i run this files as they are all separated
- put all files in one directory.
- navigate to that directory in a terminal
- compile with javac *.java
- run with java ShoppingCartTest
How do i run this files as they are all separated
Or you can re-write so you can familiarize the code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very good...
but why you don't show Item class