In: Computer Science
The solution should be written in Java.
Your POSmain program should take three file names from command
line arguments. The first file contains a list of products and
their prices; the second and third files are lists of items in two
shopping carts of two customers. The POSmain program should first
read the price file, then read each of the cart files to load a
list of items in a shopping cart and store them in a ShoppingCart
objects. The price file may contain a variable number of products
and the cart files may contain a variable number of items.
POSmain then will create a CashRegister object by passing the price
list to it. The POSmain program then will use the CashRegister
object to scan items in a cart and print a receipt for each
shopping cart one by one. At last, POSmain will use the
CashRegister object to print a report for the day.
The students of CSIT111 and CSIT811 will print a different report
for the day, which requires different design of your CashRegister
class.
The output should be like this:-
One customer is checking out ...
========================================
Product Price Qty Subtotal
----------------------------------------
Bed $499.99 2 $999.98
Char $45.49 4 $181.96
TV $999.99 1 $999.99
Table $199.0 2 $398.0
-------------------------
Total $2579.93
========================================
One customer is checking out ...
========================================
Product Price Qty Subtotal
----------------------------------------
Bread $1.75 2 $3.5
Butter $2.84 1 $2.84
Ham $2.5 1 $2.5
Lettuce $1.0 1 $1.0
Milk $3.0 2 $6.0
Onions $0.54 3 $1.62
Tomato $0.76 5 $3.8
-------------------------
Total $21.26
========================================
Report for the day
========================================
Number of customers: 2
Total sale: $2601.19
List of products sold:
----------------------------------------
Product Qty
----------------------------------------
Bed 2
Bread 2
Butter 1
Char 4
Ham 1
Lettuce 1
Programming Fundamentals - 3/4 -
Milk 2
Onions 3
TV 1
Table 2
Tomato 5
class Global{
public String[] productArr=new
String[100];
public int[] quantityArr=new int[100];
public int tempSize=0;
Global()
{
for(int
i=0;i<100;i++)
{
quantityArr[i]=0;
}
tempSize=0;
}
};
class Product
{
private String product;
private float price;
public Product()
{
price=0;
product="";
}
String getProduct()
{
return product;
}
float getPrice()
{
return price;
}
void setProduct(String product_)
{
product=product_;
}
void setPrice(float price_)
{
price=price_;
}
};
class Cart
{
private Product[] pList=new Product[100];
private int[] qty=new int[100];
private int size;
public Cart()
{
size=0;
}
void addProduct(String name,float price,int
qty_)
{
if(size<100)
{
pList[size]=new Product();
pList[size].setProduct(name);
pList[size].setPrice(price);
qty[size]=qty_;
size++;
System.out.println("\nProduct Added into cart");
}
else
{
System.out.println("\nCart is full you have to checkout...");
}
}
public Product getProductByName(String
Name)
{
for(int
i=0;i<=size;i++)
{
if(pList[i].getProduct()==Name)
{
return pList[i];
}
}
System.out.println("\nNo
product found");
return null;
}
public Product getProductByIndex(int
index)
{
if(index<=size&&index>=0)
{
return pList[index];
}
System.out.println("\nNo
product found");
return null;
}
public int getSize()
{
return size;
}
public void setSize(int size_)
{
size=size_;
}
public int getQtyByName(String Name)
{
for(int
i=0;i<=size;i++)
{
if(pList[i].getProduct()==Name)
{
return qty[i];
}
}
System.out.println("\nNo
product found");
return -1;
}
public int getQtyByIndex(int index)
{
if(index<=size&&index>=0)
{
return qty[index];
}
System.out.println("\nNo
Quantity for product (at this index) found");
return 0;
}
public float checkOut(Global obj)
{
float sum=0;
System.out.println("\nOne customer is checking out...");
System.out.println("\nProduct\t\tPrice\tQuantity\tSubTotal\n----------------------------------------------------------");
for(int
i=0;i<size;i++)
{
boolean flag=true;
System.out.println(""+pList[i].getProduct()+"\t"+pList[i].getPrice()+"\t"+qty[i]+"\t\t\t$"+(pList[i].getPrice()*qty[i]));
sum+=pList[i].getPrice()*qty[i];
for(int j=0;j<obj.tempSize;j++)
{
if(pList[i].getProduct()==obj.productArr[i])
{
obj.quantityArr[i]+=qty[i];
flag=false;
break;
}
}
if(flag==true)
{
obj.productArr[obj.tempSize]=pList[i].getProduct();
obj.quantityArr[obj.tempSize]=qty[i];
obj.tempSize++;
}
}
System.out.println("Total &"+sum);
return sum;
}
};
class Manage
{
public Cart[] customer=new Cart[10];
public int size;
int customerNumber;
float sum;
public Manage()
{
size=0;
sum=0;
}
public void addCustomer()
{
if(size<100)
{
customer[size]=new Cart();
size++;
customerNumber=size;
}
}
public void addProduct(String name,float
price,int qty,int index)
{
if(index>=0&&index<=size)
{
customer[index-1].addProduct(name,price,qty);
}
else
{
System.out.println("\nInvalid index");
}
}
public void checkoutCustomer(Global obj,int
index)
{
if(index>=0&&index<=size)
{
sum+=customer[index-1].checkOut(obj);
customer[index-1]=customer[size-1];
size--;
}
else
{
System.out.println("\nInvalid index");
}
}
public void dailyReport(Global obj)
{
System.out.println("\nDaily Report\n--------------------");
System.out.println("\nNumber of customer:"+customerNumber);
System.out.println("\nTotal Sale:$"+sum);
System.out.println("\nList of product
sold:\n---------------------");
System.out.println("\nProduct\tQuantity");
System.out.println("\n---------------------");
for(int
i=0;i<obj.tempSize;i++)
{
System.out.println(""+obj.productArr[i]+"\t"+obj.quantityArr[i]);
}
}
};
public class MyClass
{
public static void main(String[] args)
{
Global global_obj=new
Global();
Manage obj=new
Manage();
obj.addCustomer();
obj.addProduct("Product-1",10,2,1);
obj.addProduct("Product-2",10,2,1);
obj.addProduct("Product-3",10,2,1);
obj.checkoutCustomer(global_obj,1);
obj.addCustomer();
obj.addProduct("Product-1",10,2,1);
obj.addProduct("Product-2",10,2,1);
obj.addProduct("Product-3",10,2,1);
obj.checkoutCustomer(global_obj,1);
obj.dailyReport(global_obj);
}
}