Using JGRASP Please help with ShoppingCart class base off of this Class, please
ID: 3822539 • Letter: U
Question
Using JGRASP Please help with ShoppingCart class base off of this Class,
please do not import anything try to keep it basic for me to understand ty for reading and appreciate the help!!!
//John Ngan's Assignment five
public class Item {
//variables
private String name;
private String vendor;
private double salePrice;
private double costPrice;
private double weight;
private boolean taxable;
//Constructor
public Item(String n,double cp, double sp)
{
name=n;
costPrice=cp;
salePrice=sp;
weight=1;
taxable=true;
}
//toString part 1 Assignment 6
public String toString()
{
String Chair;
Chair = "Name : "+getName()+"Cost Price : "+getCostPrice()+"Sale Price : "+getSalePrice()+"Weight : "+getWeight()+"Taxable : "+getTaxable();
return Chair;
}
//Accessors
public String getName()
{
return name;
}
public String getVendor()
{
return vendor;
}
public double getSalePrice()
{
return salePrice;
}
public double getCostPrice()
{
return costPrice;
}
public double getWeight()
{
return weight;
}
public boolean getTaxable()
{
return taxable;
}
//mutuators
public void setVendor(String v)
{
vendor=v;
}
public void setWeight(double w)
{
weight=w;
}
public void setTaxable(boolean tax)
{
taxable=tax;
}
//increaseCost
public void increaseCost()
{
costPrice=costPrice+1;
}
//profit
public double profit()
{
return salePrice-costPrice;
}
public static void main(String[] args) {
//ItemTester? previous assignment
Item chair = new Item("Desk Chair", 30, 55);
//increase cost price by $3.
chair.increaseCost();
chair.increaseCost();
chair.increaseCost();
//display the profit
System.out.println("The chair's profit is now $" + chair.profit());
//set the chair’s weight to 7 lb
chair.setWeight(7);
Item table = new Item("Picnic Table", 70,88);
table.setWeight(8);
System.out.println("The table's profit is now $" + table.profit());
//part 2 assignment 6
int totalWeight = 0;
Item []arrayItem = new Item[3];
arrayItem[0] = chair;
arrayItem[1] = table;
arrayItem[2] = chair;
for(int i = 0; i<arrayItem.length; i++){
totalWeight += arrayItem[i].getWeight();
}
System.out.println(totalWeight);
}
}
Explanation / Answer
public class Item {
//variables
private String name;
private String vendor;
private double salePrice;
private double costPrice;
private double weight;
private boolean taxable;
//Constructor
public Item(String n,double cp, double sp)
{
name=n;
costPrice=cp;
salePrice=sp;
weight=1;
taxable=true;
}
//toString part 1 Assignment 6
public String toString()
{
String Chair;
Chair = "Name : "+getName()+"Cost Price : "+getCostPrice()+"Sale Price : "+getSalePrice()+"Weight : "+getWeight()+"Taxable : "+getTaxable();
return Chair;
}
//Accessors
public String getName()
{
return name;
}
public String getVendor()
{
return vendor;
}
public double getSalePrice()
{
return salePrice;
}
public double getCostPrice()
{
return costPrice;
}
public double getWeight()
{
return weight;
}
public boolean getTaxable()
{
return taxable;
}
//mutuators
public void setVendor(String v)
{
vendor=v;
}
public void setWeight(double w)
{
weight=w;
}
public void setTaxable(boolean tax)
{
taxable=tax;
}
//increaseCost
public void increaseCost()
{
costPrice=costPrice+1;
}
//profit
public double profit()
{
return salePrice-costPrice;
}
}
class ShopingCart{
private Item[] items;
private int count;
public ShopingCart(){
items = new Item[10];
count = 0;
}
public void addItem(Item item){
items[count] = item;
count++;
}
public double cartTotal(){
double total = 0.0;
for(int i=0; i<count; i++){
total += items[i].getSalePrice();
}
return total;
}
public double cartTaxAmount(double rate){
double total = 0.0;
for(int i=0; i<count; i++){
if(items[i].getTaxable())
total += items[i].getSalePrice();
}
return (total/100.0)*rate;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.