Create an application (ShoppingCart.java) that works like a shopping cart system
ID: 3820285 • Letter: C
Question
Create an application (ShoppingCart.java) that works like a shopping cart system for a book store. Consider a file titled “book-prices.txt”. A sample template of the file is given below:
book-prices.txt
I Did It Your Way;11.95
The History of Scotland;14.50
Learn Calculus in One Day;29.95
Feel the Stress;18.50
Great Poems;12.95
Europe on a Shoestring;10.95
The Life of Mozart;14.50
Each line in the file contains the names and prices of various books delimited by a “;”. When your application begins execution, it should look for “book-prices.txt” (use the exact file name) file in the project directory. It should read the contents of the file and store the book titles in a list component. The user should be able to select a title from the list and add it to a shopping cart, which is simply another list component. The application should have buttons or menu items that allow the user to remove items from the shopping cart, clear the shopping cart of all items, and also check out. When the user checks out, the application should calculate and display the subtotal of all the books in the shopping cart, the sales tax (6% of the subtotal), and the total.
Explanation / Answer
public class Book {
private String titale;
private double price;
public Book(){
}
public Book(String t,double p){
titale = t;
price = p;
}
public String getTitale() {
return titale;
}
public void setTitale(String titale) {
this.titale = titale;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString(){
return ""+titale+" "+price+" ";
}
}
//=======================================================================
public class ShoppingCart {
private java.util.List<Book> list;
public ShoppingCart(){
list = new java.util.ArrayList<Book>();
}
public java.util.List<Book> getList() {
return list;
}
public void setList(java.util.List<Book> list) {
this.list = list;
}
public void add(Book book){
list.add(book);
}
public boolean delete(int i){
if(i>list.size())return false;
list.remove(i);
return true;
}
public boolean delete(Book book){
if(!list.contains(book))return false;
list.remove(book);
return true;
}
public double getTotal(){
double total =0.0d;
for(int i=0;i<list.size();i++){
total += list.get(i).getPrice();
}
return total;
}
public void clear(){
for(int i=0;i<list.size();i++)
list.remove(i);
}
public String toString(){
String str = " ================ Cart ================ ";
for(int i=0;i<list.size();i++){
str+= "#"+(i+1)+" "+list.get(i).toString();
}
str += " ================================== ";
return str;
}
}
//=============================================================================================
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ShoppingCartApp {
public static void main(String[] args) {
java.util.List<Book> allBooks = new java.util.ArrayList<Book>();
ShoppingCart cart = new ShoppingCart();
Scanner scanner = null;
try{
scanner = new Scanner(new java.io.File("book-prices.txt"));
}catch(FileNotFoundException e){
e.printStackTrace();
}
while(scanner.hasNextLine()){
String[] data = scanner.nextLine().split(";");
allBooks.add(new Book(data[0], Double.parseDouble(data[1])));
}
scanner.close();
scanner = new Scanner(System.in);
boolean quit = false;
while(!quit){
System.out.println("1. Show all Books");
System.out.println("2. Show Cart");
System.out.println("3. Add to Cart");
System.out.println("4. Remove from Cart");
System.out.println("5. Clear Cart");
System.out.println("6. Check Out");
System.out.println("7. Exit");
System.out.println("Enter Your Choice :");
int ch = scanner.nextInt();
int n;
switch(ch){
case 1:
pirntBooks(allBooks);
break;
case 2:
System.out.println(cart);
break;
case 3:
System.out.println("Enter Book # from all books:");
n = scanner.nextInt()-1;
cart.add(allBooks.get(n));
break;
case 4:
System.out.println(cart);
System.out.println("Enter Book # from all cart:");
n = scanner.nextInt()-1;
cart.delete(n);
break;
case 5:
cart.clear();
System.out.println("Cart is Cleared");
break;
case 6:
System.out.println(cart);
System.out.println("Sub Total = "+cart.getTotal());
System.out.println("Sales Tax = 6%");
System.out.println("Grand Total = "+(cart.getTotal()+(cart.getTotal()/100.0d)*6.0d));
System.out.println("=======================================");
break;
case 7:
quit = true;
break;
}
}
}
public static void pirntBooks(java.util.List<Book> list){
System.out.println("============== All Books =============");
for(int i=0;i<list.size();i++){
System.out.println("# "+(i+1)+" "+list.get(i));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.