OOP Assignment #2

 avatar
chamanEiqbal
java
a year ago
11 kB
19
Indexable
Never
package assignment2;

import java.util.ArrayList;
import java.util.Scanner;


public class Bill {
    // Bill Items ArrayLists
    static int bItems = 0;
    static int LabourItems = 0;
    static int PartsItems = 0;
        static ArrayList<String> typeList = new ArrayList<>();
        static ArrayList<String> nameList = new ArrayList<>();
        static ArrayList<Float> priceList = new ArrayList<>();
        static ArrayList<Float> quantityList = new ArrayList<>();
        static ArrayList<Float> discountList = new ArrayList<>();
        /* 
        ------------------
         Methods of Taking Information and Setting the information to Bill Item ArrayLists: 
        ------------------*/
    public static void takeInput(Parts p) {                
        Scanner sc = new Scanner(System.in);
        System.out.printf("%50s\n","Enter Part Name: ");
        p.setName(sc.nextLine());
        System.out.printf("%50s\n","Enter Part Price: ");
        p.setPrice(sc.nextFloat());
        System.out.printf("%50s\n","Enter Part Quantity: ");
        p.setQuantity(sc.nextFloat());
        System.out.printf("%50s\n","Enter Discount: ");
        p.setDiscount(sc.nextFloat());
         bItems++;
         PartsItems++;
    }
    
    public static void setBill(Parts p) {  
        typeList.add("Parts");
        nameList.add(p.getName());
        priceList.add(p.getPrice());
        quantityList.add(p.getQuantity());
        discountList.add(p.getDiscount());
    }
    
    public static void takeInput(Labour l) {
        Scanner sc = new Scanner(System.in);
        System.out.printf("%50s\n","Enter Labour Description:");
        l.setName(sc.nextLine());
        System.out.printf("%50s\n","Enter Labor Price: ");
        l.setPrice(Float.parseFloat(sc.nextLine()));
        System.out.printf("%50s\n","Enter Labor Quantity: ");
        l.setQuantity(Float.parseFloat(sc.nextLine()));
        bItems++;
        LabourItems++;
    }
    public static void setBill(Labour l) {
        typeList.add("Labour");
        nameList.add(l.getName());
        priceList.add(l.getPrice());
        quantityList.add(l.getQuantity());
    }
    
    public static void setCustomer(Customer c) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter name of customer:");
        c.setName(sc.nextLine());
        System.out.println("Enter contact No. of customer");
        c.setContactNo(sc.nextLine());
    }
    
    public static void setVeh(Vehicle v) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Vehicle No.");
        v.setVehNo(sc.nextLine());
        System.out.println("Enter Model: ");
        v.setModel(sc.nextLine());
    }
    
public static void printReceipt(Customer customer, Vehicle vehicle) {
    System.out.println("\n\n\n");
    System.out.println("******************************************************************************************************************");
    System.out.printf("%75s","KARACHI AUTOSERVICES PVT. LTD.\n");
    System.out.println("******************************************************************************************************************");
    System.out.printf("Customer Name: %-40s", customer.getName());
    System.out.printf("%40s\n", "Vehicle No.: " + vehicle.getVehNo());
    System.out.printf("Contact No.: %-40s", customer.getContactNo());
    System.out.printf("%40s\n", "Vehicle Model: " + vehicle.getModel());
    System.out.println("******************************************************************************************************************");
    System.out.printf("%65s", "BILL DESCRIPTION: \n");
    System.out.println("******************************************************************************************************************");
    System.out.printf("%-20s%-40s%-20s%-20s%-20s\n", "Type", "Name", "Price", "Quantity", "Discount");
    System.out.println("------------------------------------------------------------------------------------------------------------------");
    float totalAmount = 0.0f;
    float totalPartsAmount = 0.0f;
    float totalLabourAmount = 0.0f;
    for(int i = 0; i < PartsItems; i++) {
        float partsTotal = (priceList.get(i) * quantityList.get(i) - discountList.get(i));
        totalPartsAmount += partsTotal;
    }
    for(int i = PartsItems; i < bItems; i++) {
        float labourTotal = priceList.get(i) * quantityList.get(i);
        totalLabourAmount  += labourTotal;
    }
        for(int i=0;i<bItems;i++) {
        if(typeList.get(i).equalsIgnoreCase("Labour")) {
        System.out.printf("%-20s%-40s%15.2f%15.2f%15.2s\n", typeList.get(i), nameList.get(i), priceList.get(i), quantityList.get(i), "-");
        }
        else {
         System.out.printf("%-20s%-40s%15.2f%15.2f%15.2f\n", typeList.get(i), nameList.get(i), priceList.get(i), quantityList.get(i), discountList.get(i));
        }
        }
        totalAmount = totalPartsAmount + totalLabourAmount;
     
    System.out.println("------------------------------------------------------------------------------------------------------------------");
    System.out.printf("%-80s%20.2f\n", "Parts Total:", totalPartsAmount);
    System.out.printf("%-80s%20.2f\n", "Service Tax (5%):", totalPartsAmount * 0.05);
    System.out.printf("%-80s%20.2f\n", "Labour Total:", totalLabourAmount);
    System.out.printf("%-80s%20.2f\n", "Grand Total:", totalAmount - (totalPartsAmount * 0.05));
    
    System.out.println("******************************************************************************************************************");
}

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String mainCont;
        Customer customer = new Customer();
        Vehicle veh = new Vehicle();
        Parts parts = new Parts();
        Labour labour = new Labour();
        
        
        //setting customer and vehicle info
        System.out.printf("%50s\n", "Enter Customer Details:");
        setCustomer(customer);
        System.out.printf("%50s\n", "Enter Vehicle Details");
        setVeh(veh);
        customer.hasTheVeh(veh);
        int choice;
        do {
        System.out.printf("%50s\n","Enter 1 to  add Part in Bill:");
        System.out.printf("%50s\n","Enter 2 to  add Labour in Bill:");
        System.out.printf("%50s\n","Enter 3 to print results");
        choice = Integer.parseInt(sc.nextLine());
 
        switch(choice) {
        // taking parts input
            case 1:
            System.out.printf("%50s\n","Enter Parts Details:");
         takeInput(parts);
          setBill(parts);
          break;
        
            case 2:
       // taking labour input:
         takeInput(labour);
          setBill(labour);
          break;
            case 3:
               printReceipt(customer,veh);
               break;
               
            default:
                System.out.println("Wrong input");
                break;
    }
            System.out.println("\n \nDo you want to continue?");
            mainCont = sc.nextLine();
        }  while(mainCont.equalsIgnoreCase("yes") || mainCont.startsWith("y"));
    }
}

//----------------------------------------------------------------
// DESCRIPTION DETAILS CLASS:
//----------------------------------------------------------------
package assignment2;

public class DescriptionDetails {
    private float price;
    private float quantity;
    
    public DescriptionDetails() {
        // default constructor;
    }
    
    public DescriptionDetails(float price, float quantity) {
         // constructor for linking with Labour Class:
         this.quantity = quantity;
        this.price = price;
    }
    public void setPrice(float price) {this.price = price;}
    public void setQuantity(float quantity) {this.quantity = quantity;}

    public float getPrice() {return price;}
    public float getQuantity(){return quantity;}
   
}

//----------------------------------------------------------------
// LABOUR CLASS:
//----------------------------------------------------------------

package assignment2;

public class Labour extends DescriptionDetails { 
    private String name;
    
    public Labour() {
        //default contsructor
    }
    public Labour(String name, float price, float quantity) {
        super(price, quantity);
        this.name = name;
    }
    
    //setters getters
    public void setName(String name) {this.name = name;}
    public String getName() {return name;}
    
}

//----------------------------------------------------------------
// PARTS CLASS:
//----------------------------------------------------------------

package assignment2;

public class Parts extends DescriptionDetails {
    private String name;
    private float discount;

    
    public Parts(){
        //default constructor;
    }
    public Parts(String name, float price, float quantity, float discount) {
        super(price, quantity);
        this.name = name;
        this.discount = discount;
    }
    
    // setters getters:
    
    public void setName(String name) {this.name = name;}
    public void setDiscount(float discount) {this.discount = discount;}
    public String getName() {return name;}
    public float getDiscount() {return discount;}
    
}

//----------------------------------------------------------------
// CUSTOMER CLASS:
//----------------------------------------------------------------

package assignment2;

import java.util.ArrayList;

public class Customer {
    private String name;
    private String contactNo;
    private ArrayList<Vehicle> vehicles = new ArrayList<>();
    
    public Customer() {
        // default constructor;
    }
    public Customer(String name, String contactNo, Vehicle veh) {
        this.name = name;
        this.contactNo = contactNo;
        vehicles.add(veh);
    }
    
    
    public void setName(String name) {this.name = name;}
    public void setContactNo(String contactNo) {this.contactNo = contactNo;}
    public void hasTheVeh(Vehicle veh) {vehicles.add(veh);}
    
    public String getName() {return name;}
    public String getContactNo() {return contactNo;}
    public ArrayList<Vehicle> getVehicles() {return vehicles;}
}

//----------------------------------------------------------------
// VEHICLE CLASS:
//----------------------------------------------------------------

package assignment2;

public class Vehicle {
    private String vehNo;
    private String model;
    
    
    public Vehicle() {
        //default constructor;
    }
    public Vehicle(String vehNo, String model) {
        this.vehNo = vehNo;
        this.model = model;
    }
    
    public void setVehNo(String vehNo) {this.vehNo = vehNo;}
    public void setModel(String model) {this.model = model;}
    
    public String getVehNo() {return vehNo;}
    public String getModel() {return model;}
 }