Kulas_Code20

Programming_Exercise2

Feb 21st, 2022
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. package com.project;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PE2Proj {
  6.  
  7.     String itemName;
  8.     double itemPrice, amountDue;
  9.     int itemQuantity;
  10.    
  11.     PE2Proj(){
  12.         //Default constructor
  13.     }
  14.    
  15.     public void setItemName(String newItemName) {
  16.        
  17.     }
  18.    
  19.     public void setTotalCost(int quantity, double price) {
  20.        
  21.     }
  22.    
  23.     public String getItemName() {
  24.         return itemName;
  25.     }
  26.    
  27.     public double getTotalCost() {
  28.         return amountDue = itemQuantity * itemPrice;
  29.     }
  30.    
  31.     public void readInput() {
  32.         Scanner sc = new Scanner(System.in);
  33.        
  34.         System.out.println("Enter the name of the item you are purchasing:");
  35.         itemName = sc.nextLine();
  36.        
  37.         System.out.println("Enter the quantity and price separated by space:");
  38.         itemQuantity = sc.nextInt();
  39.         itemPrice = sc.nextDouble();
  40.        
  41.         setItemName(itemName);
  42.         setTotalCost(itemQuantity, itemPrice);
  43.     }
  44.    
  45.     public void writeOutput() {
  46.         System.out.printf("You are purchasing %d %s(s) at %.2f each.", itemQuantity, itemName, itemPrice);
  47.         System.out.printf("\nAmount due is %.2f", getTotalCost());
  48.     }
  49.    
  50.     public static void main(String[] args) {
  51.         PE2Proj proj = new PE2Proj();
  52.        
  53.         proj.readInput();
  54.         proj.writeOutput();
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment