In: Computer Science
Write a Java program which asks customer name, id, address and
other personal information, there are two types of customers,
walk-in and credit card. The rates of items are different for both
type of customers. System also asks for customer type. Depending
upon customer type, it calculates total payment.
A credit-card customer will pay 5 % extra the actual price.
Use object-oriented concepts to solve the problem. Define as many
items and prices as you want.
Example Output:
Enter Name of Customer: John
Enter Id: 15
Customer Type: Card
Select items:1 3 6
The total price is 560
Customer.java
//defining a customer class
class Customer{
String name, id, address, email, customerType;
int phone;
//parameterized constructor
public Customer(String _name, String _id, String _address, int _phone, String _email, String _customerType){
name = _name;
id = _id;
address = _address;
phone = _phone;
email = _email;
customerType = _customerType;
}
}
Item.java
class Item{
int id;
double price;
public Item(int _id, double _price){
id = _id;
price = _price;
}
}
Main.java
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
//input user details
String _name, _id, _address, _email;
int _phone;
System.out.print("Name: ");
_name = scn.next();
System.out.print("ID: ");
_id = scn.next();
System.out.print("Address: ");
_address = scn.next();
System.out.print("Phone: ");
_phone = scn.nextInt();
System.out.print("Emial: ");
_email = scn.next();
System.out.print("Customer Type ");
String _customerType = scn.next();
//create a customer object with given details
Customer customer = new Customer(_name, _id, _address, _phone, _email, _customerType);
//add some items to our items catalog
ArrayList<Item> items = new ArrayList<>();
items.add(new Item(1, 20));
items.add(new Item(3, 50));
items.add(new Item(6, 60));
items.add(new Item(4, 90));
System.out.println("Select items");
//create a shoping cart for our customer
ArrayList<Item> cart = new ArrayList<>();
int j = 0;
//item entered by th customer are added to cart if the exist in our cataog
while(j < 3){
int item = scn.nextInt();
for(Item i : items){
if(item == i.id){
cart.add(i);
}
}
j++;
}
//after all items are added to the cart we begin calculating the price
double price = 0;
for(Item i: cart){
price += i.price;
}
//if customer is a credit card customer than he has to pay 5% extra
if(customer.customerType.compareTo("CARD") == 0){
price += price * 0.05;
}
//output the price details
System.out.println("The total price is "+price);
}
}