Kulas_Code20

PExercises2

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