Write a java program implementing Encapsulation concept, for showing the busines
ID: 3680047 • Letter: W
Question
Write a java program implementing Encapsulation concept, for showing the business logic for Amazon shopping website:
-Create the array variables for ItemID, ItemName, Price and generate the getters and setters.
- create a double variable discount and generate the getters and setters.
-Implement the Scanner class to implement the 3 values for each as shown in the output.
-And finally, create the method checkout to show the final checkout price after getting the discount deducted.
OUTPUT:
Please enter 3 item ids
32
65
85
Please enter items you want to buy
HeadPhone
GeoPro
SelifeStick
Please enter 3 items price
257.34
150.45
20
Enter Discount Percentage
40
Amazon Shopping List
------------------------------------------------
The item Ids you chose [32,65,85]
The items you are buying: [HeadPhones, GeoPro, SelfieStick]
The Price of Items you chose: [257.34,150.45,20.0]
The discount price being applied 40%
------------------------------------------------
The Discount Percentage being applied : 256.674
Explanation / Answer
Please find the below classes implemented for said requirement.
Items.java:
public class Items {
private int itemID[];
private String itemName[];
private double price[];
private double discount;
public int[] getItemID() {
return itemID;
}
public void setItemID(int[] itemID) {
this.itemID = itemID;
}
public String[] getItemName() {
return itemName;
}
public void setItemName(String[] itemName) {
this.itemName = itemName;
}
public double[] getPrice() {
return price;
}
public void setPrice(double[] price) {
this.price = price;
}
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
}
FinalBill.java :
public class FinalBill {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int itemID[] = new int[3];
double price[] = new double[3];
double discount;
String itemName[] = new String[3];
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please enter 3 item ids");
itemID[0] = in.nextInt();
itemID[1] = in.nextInt();
itemID[2] = in.nextInt();
System.out.println("Please enter items you want to buy");
itemName[0] = in.next();
itemName[1] = in.next();
itemName[2] = in.next();
System.out.println("Please enter 3 item prices");
price[0] = in.nextDouble();
price[1] = in.nextDouble();
price[2] = in.nextDouble();
System.out.println("Please enter Discount percentage");
discount = in.nextDouble();
Items items = new Items();
items.setDiscount(discount);
items.setItemID(itemID);
items.setItemName(itemName);
items.setPrice(price);
PurchaseItems pi = new PurchaseItems();
pi.checkOut(items); //calling for chekOut items
}
}
PurchaseItems.java:
public class PurchaseItems {
public boolean checkOut(Items items){
System.out.println("Amazon Shopping List");
System.out.println("-------------------------------------------");
String tempItems = "";
for(int i=0;i<items.getItemID().length; i++){
if(i != items.getItemID().length - 1)
tempItems = tempItems + items.getItemID()[i] + ",";
else
tempItems = tempItems + items.getItemID()[i];
}
System.out.println("The item Ids you chose ["+tempItems+"]");
String tempItemNames = "";
for(int i=0;i<items.getItemName().length; i++){
if(i != items.getItemName().length - 1)
tempItemNames = tempItemNames + items.getItemName()[i]+ ",";
else
tempItemNames = tempItemNames + items.getItemName()[i];
}
System.out.println("The items you are buying: ["+tempItemNames+"]");
String tempPrices = "";
double totalprice = 0;
for(int i=0;i<items.getPrice().length; i++){
if(i != items.getPrice().length - 1){
tempPrices = tempPrices + items.getPrice()[i]+ ",";
totalprice = totalprice + items.getPrice()[i];
}
else{
tempPrices = tempPrices + items.getPrice()[i];
totalprice = totalprice + items.getPrice()[i];
}
}
System.out.println("The Price of Items you chose: ["+tempPrices+"]");
System.out.println("The discount price being applied "+items.getDiscount()+" %");
System.out.println("-------------------------------------------");
double finalpriceafterdiscount = totalprice - (totalprice * items.getDiscount())/100;
System.out.println("The Discount Percentage being applied : ["+finalpriceafterdiscount+"]");
return true;
}
}
Output:
Please enter 3 item ids
111
222
333
Please enter items you want to buy
Telephone
mobile
TV
Please enter 3 item prices
1150
15000.50
5999.99
Please enter Discount percentage
22
Amazon Shopping List
----------------------------------------------------------------------------
The item Ids you chose [111,222,333]
The items you are buying: [Telephone,mobile,TV]
The Price of Items you chose: [1150.0,15000.5,5999.99]
The discount price being applied 22.0 %
----------------------------------------------------------------------------
The Discount Percentage being applied : [17277.3822]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.