Prep for next part

This commit is contained in:
Logen Kain 2020-11-11 09:46:43 -05:00
parent 706d225e78
commit 9b0cff092f
2 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,44 @@
/*
* Following Specs:
* Init in default constructor:
*
* String itemName -- init to none
* int itemPrice - init to 0
* int itemQuanity - init to 0
*
* Public member mothods (mutators & accessors)
* setPrice() & getPrice()
*/
public class ItemToPurchase {
private String itemName;
private int itemPrice;
private int itemQuantity;
public ItemToPurchase() {
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
public void setName(String name){
this.itemName = name;
}
public String getName(){
return itemName;
}
public void setQuantity(int q){
this.itemQuantity = q;
}
public int getQuantity(){
return itemQuantity;
}
public void setPrice(int p){
this.itemPrice = p;
}
public int getPrice(){
return itemPrice;
}
}

View File

@ -0,0 +1,74 @@
/*
In main, ask the user for two items and create two objects of the
ItemToPurchase class.
Before asking for the second item, call scnr.nextLine()
to allow the user to put in a new string.
Example:
Item 1
Enter the item name:
Chocolate Chips
Enter the item price:
3
Enter the item quantity:
1
Item 2
Enter the item name:
Bottled Water
Enter the item price:
1
Enter the item quantity:
10
Then add the costs of teh two items together and output
the total cost
Example:
TOTAL COST
Chocolate Chips 1 @ $3 = $3
Bottled Water 10 @ $1 = $10
Total: $13
*/
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
System.out.println("Item 1");
System.out.println("Enter the item name:");
item1.setName(scnr.nextLine());
System.out.println("Enter the item price:");
item1.setPrice(scnr.nextInt());
System.out.println("Enter the item quantity:");
item1.setQuantity(scnr.nextInt());
scnr.nextLine();
System.out.println("\nItem 2");
System.out.println("Enter the item name:");
item2.setName(scnr.nextLine());
System.out.println("Enter the item price:");
item2.setPrice(scnr.nextInt());
System.out.println("Enter the item quantity:");
item2.setQuantity(scnr.nextInt());
System.out.println("\nTOTAL COST");
System.out.println(item1.getName() + " " + item1.getQuantity() +
" @ $" + item1.getPrice() + " = $" +
(item1.getPrice() * item1.getQuantity()));
System.out.println(item2.getName() + " " + item2.getQuantity() +
" @ $" + item2.getPrice() + " = $" +
(item2.getPrice() * item2.getQuantity()));
System.out.println("\nTotal: $" +
((item2.getPrice() * item2.getQuantity()) +
(item1.getPrice() * item1.getQuantity())));
}
}