1. You were asked to create a program for variety stores. The program should:
1. Input the customer name.
2. Input the products name and it's price. Specifically three products.
3. Get the total price of the 3 products.
4. Input the cash from customer.
5. Out the summary of the sales.
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Purchase {
private String customerName;
private ArrayList<String> productName = new ArrayList<String>();
private ArrayList<Double> productPrice = new ArrayList<Double>();
private double totalPrice;
private double cash;
public void setCustomerName(String customerName){
this.customerName = customerName;
}
public void setProductName(String itemName){
this.productName.add(itemName);
}
public void setProductPrice(double itemPrice){
this.productPrice.add(itemPrice);
}
public void setTotalPrice(){
for (int i = 0; i < this.productPrice.size(); i++){
this.totalPrice += this.productPrice.get(i);
}
}
public void setCash(double amountOfMoney){
this.cash = amountOfMoney;
}
public String getCustomerName(){
return this.customerName;
}
public String getProductName(int i){
return this.productName.get(i);
}
public double getProductPrice(int i){
return this.productPrice.get(i);
}
public double getTotalPrice(){
return this.totalPrice;
}
public double getСash(){
return this.cash;
}
public double getСhange(){
return (this.cash - this.totalPrice);
}
public String[] separateBySpace(String str, String separator){
String[] subStr = str.split(separator);
return subStr;
}
public void sales(){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter customer name: ");
setCustomerName(scanner.nextLine());
System.out.print("Input the products name and its price separated by a space: ");
String[] prodAndPrice = separateBySpace(scanner.nextLine(), " ");
for (int i = 0; i < prodAndPrice.length - 1; i +=2 ){
setProductName(prodAndPrice[i]);
setProductPrice(Double.valueOf(prodAndPrice[i + 1]));
}
setTotalPrice();
}
public void payment(){
DecimalFormat df = new DecimalFormat("#,###,###.##");
Scanner scanner = new Scanner(System.in);
System.out.println("The customer owes: " + df.format(getTotalPrice()));
System.out.println("Enter the cash from customer: ");
setCash(scanner.nextDouble());
System.out.println("Here is your change: " + df.format(getСhange()));
}
public void writeSummary(){
DecimalFormat df = new DecimalFormat("#######.##");
System.out.println("You are purchasing:");
for (int i = 0; i < this.productPrice.size(); i++){
System.out.print(getProductName(i) + " at " + getProductPrice(i) + "; ");
}
System.out.println("\nThe total cost is " + df.format(getTotalPrice()));
System.out.println("The cash from customer: " + getСash());
System.out.println("The change is: " + df.format(getСhange()));
}
public static void main(String args[]){
Purchase purchase = new Purchase();
purchase.sales();
purchase.payment();
purchase.writeSummary();
}
}
Comments
Leave a comment