Question

In: Computer Science

public class GroceryShopping {    //declared variable    private String vegetableName;    private String fruitName;   ...

public class GroceryShopping
{
   //declared variable
   private String vegetableName;
   private String fruitName;
   private double vegetablePrice;
   private double fruitPrice;
   private double vegetableOrdered;
   private double fruitOrdered;
  
   //declared constants
   public static final double SERVICE_RATE =0.035;
   public static final double DELIVERY_FEE=5;
  
   public GroceryShopping( String vegetableName, String fruitName, double vegetablePrice, double fruitPrice)
   {
       this.vegetableName = vegetableName;
       this.fruitName = fruitName;
       this.vegetablePrice = vegetablePrice;
       this.fruitPrice = fruitPrice;
       this.vegetableOrdered = vegetableOrdered;
       this.fruitOrdered=fruitOrdered;
   }
   public String getVegetableName()
   {
       return vegetableName;
   }
  
   public void setVegetableName(String vegetableName)
   {
       this.vegetableName = vegetableName;
   }
  
   public String getFruitName()
   {
       return fruitName;
   }
  
   public void setFruitName(String fruitName)
   {
       this.fruitName = fruitName;
   }
  
   public double getVegetablePrice()
   {
       return vegetablePrice;
   }
  
   public void setVegetablePrice(double vegetablePrice)
   {
       this.vegetablePrice = vegetablePrice;
   }
  
   public double getFruitPrice()
   {
       return fruitPrice;
   }
  
   public void setFruitPrice(double fruitPrice)
   {
       this.fruitPrice = fruitPrice;
   }
  
   public double getVegetableOrdered()
   {
       return vegetableOrdered;
   }
  
   public void setVegetableOrdered(double vegetableOrdered)
   {
       this.vegetableOrdered = vegetableOrdered;
   }
  
   public double getFruitOrdered()
   {
       return fruitOrdered;
   }
  
   public void setFruitOrdered(double fruitOrdered)
   {
       this.fruitOrdered = fruitOrdered;
   }
  
  
   public double calculateSubtotal()
   {
       double calculateSubtotal=(vegetablePrice*vegetableOrdered)+(fruitPrice*fruitOrdered);
       return calculateSubtotal;
   }
  
   public double calculateAdditionalFee()
   {
       double calculateAdditionalFee=calculateSubtotal()* SERVICE_RATE + DELIVERY_FEE;
       return calculateAdditionalFee;
   }
  
   public void displayOrderSummary()
   {
       double totalBill=calculateSubtotal()+calculateAdditionalFee();
       System.out.println("--------------------------------------");
       System.out.println("Grocery Shopping Order Summary");
       System.out.println("\nName"+"\t\t\tPrice Per Pound");
       System.out.println("Sub-total:"+"\t\t$"+calculateSubtotal());
       System.out.println("Additional Fee:"+"\t\t$"+calculateAdditionalFee());
       System.out.println("Total Bill:"+"\t\t$"+totalBill);
       System.out.println("--------------------------------------");
      
   }
  
  
  
  
  
  
  
  
  
}

public class GroceryShoppingApp
{

  
   /**
   * @param args
   */
   public static void main(String[] args)
   {
       Scanner input=new Scanner(System.in);
      
  
       displayTable1();
       System.out.println("\nPlease select the vegetable from Table 1: ");
       String vegetableName=input.next();
       System.out.println("please enter the price of the selected vegetable: ");
       double vegetablePrice=input.nextDouble();
      
       displayTable2();
       System.out.println("\nPlease select the fruit from Table 2");
       String fruitName =input.next();
       System.out.println("Please enter the price of the selected fruit:");
       double fruitPrice =input.nextDouble();
       GroceryShopping gs =new GroceryShopping(vegetableName, fruitName, vegetablePrice, fruitPrice);
      
      
      
       System.out.println("\n--------------------------------------");
       System.out.println("Grocery Shopping Menu");
       System.out.println("\nName"+"\t\tPrice Per Pound");
       System.out.println(gs.getVegetableName()+"\t"+gs.getVegetablePrice());
       System.out.println(gs.getFruitName()+"\t\t"+gs.getFruitPrice());
       System.out.println("--------------------------------------\n");
       System.out.println("\nEnter the pounds of "+ gs.getVegetableName()+" ordered: ");
       double vegetableOrdered=input.nextDouble();
       System.out.println("Enter the pounds of "+ gs.getFruitName()+" ordered: ");
       double fruitOrdered=input.nextDouble();      
  
       System.out.println("\n\n\n--------------------------------------");
       System.out.println("Grocery Shopping Order Summary");
       System.out.println("\nName"+"\t\tPrice Per Pound");
       System.out.println(vegetableName+"\t"+vegetablePrice);
       System.out.println(fruitName+"\t\t"+fruitPrice);
      
       gs.displayOrderSummary();
      
       input.close();
      

      
      

   }

   private static void displayTable1()
   {
   System.out.println("Vegetable Name\t\t"+"Price Per Pound");
   System.out.println("Broccoli\t\t"+"$3.12");
   System.out.println("Yellow Onion\t\t"+"$1.15");
   System.out.println("Chill Pepper\t\t"+"$4.58");
   System.out.println("Greens Bundle\t\t"+"$2.82");
   System.out.println("--------------------------------------");
   System.out.println("Table 1: Vegetable names with corresponding price per pound");
   }
  
  
   private static void displayTable2()
   {
   System.out.println("\n--------------------------------------");
   System.out.println("Fruit Name\t\t"+"Price Per Pound");
   System.out.println("Apple\t\t\t"+"$1.73");
   System.out.println("Grape\t\t\t"+"$2.15");
   System.out.println("Key Lime\t\t"+"$2.58");
   System.out.println("Navel Orange\t\t"+"$1.86");
   System.out.println("--------------------------------------");
   System.out.println("Table 2: Fruit names with corresponding price per pound ");
   }
  
}
I have two question

1) My Sub-Total fee is always is zero, no matter what i input.

2)When i input the vegetable and fruit name, only the apple , Grape and Broccoli are working. The others always shows error, when i try to input them.

Solutions

Expert Solution


import java.util.Scanner;

class GroceryShopping
{
//declared variable
private String vegetableName;
private String fruitName;
private double vegetablePrice;
private double fruitPrice;
private double vegetableOrdered;
private double fruitOrdered;
  
//declared constants
public static final double SERVICE_RATE =0.035;
public static final double DELIVERY_FEE=5;
  
public GroceryShopping( String vegetableName, String fruitName, double vegetablePrice, double fruitPrice)
{
this.vegetableName = vegetableName;
this.fruitName = fruitName;
this.vegetablePrice = vegetablePrice;
this.fruitPrice = fruitPrice;
this.vegetableOrdered = vegetableOrdered;
this.fruitOrdered=fruitOrdered;
}
public String getVegetableName()
{
return vegetableName;
}
  
public void setVegetableName(String vegetableName)
{
this.vegetableName = vegetableName;
}
  
public String getFruitName()
{
return fruitName;
}
  
public void setFruitName(String fruitName)
{
this.fruitName = fruitName;
}
  
public double getVegetablePrice()
{
return vegetablePrice;
}
  
public void setVegetablePrice(double vegetablePrice)
{
this.vegetablePrice = vegetablePrice;
}
  
public double getFruitPrice()
{
return fruitPrice;
}
  
public void setFruitPrice(double fruitPrice)
{
this.fruitPrice = fruitPrice;
}
  
public double getVegetableOrdered()
{
return vegetableOrdered;
}
  
public void setVegetableOrdered(double vegetableOrdered)
{
this.vegetableOrdered = vegetableOrdered;
}
  
public double getFruitOrdered()
{
return fruitOrdered;
}
  
public void setFruitOrdered(double fruitOrdered)
{
this.fruitOrdered = fruitOrdered;
}
  
  
public double calculateSubtotal()
{
double calculateSubtotal=(vegetablePrice*vegetableOrdered)+(fruitPrice*fruitOrdered);
return calculateSubtotal;
}
  
public double calculateAdditionalFee()
{
double calculateAdditionalFee=calculateSubtotal()* SERVICE_RATE + DELIVERY_FEE;
return calculateAdditionalFee;
}
  
public void displayOrderSummary()
{
double totalBill=calculateSubtotal()+calculateAdditionalFee();
System.out.println("--------------------------------------");
System.out.println("Grocery Shopping Order Summary");
System.out.println("\nName"+"\t\t\tPrice Per Pound");
System.out.println("Sub-total:"+"\t\t$"+calculateSubtotal());
System.out.println("Additional Fee:"+"\t\t$"+calculateAdditionalFee());
System.out.println("Total Bill:"+"\t\t$"+totalBill);
System.out.println("--------------------------------------");
  
}
  
  
  
  
  
  
  
  
  
}

public class GroceryShoppingApp
{

  
/**
* @param args
*/
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
  
  
displayTable1();
System.out.println("\nPlease select the vegetable from Table 1: ");
String vegetableName=input.nextLine();
System.out.println("please enter the price of the selected vegetable: ");
double vegetablePrice=input.nextDouble();
  
displayTable2();
System.out.println("\nPlease select the fruit from Table 2");

//issue-2
String fruitName =input.nextLine();
fruitName =input.nextLine();

System.out.println("Please enter the price of the selected fruit:");
double fruitPrice =input.nextDouble();
GroceryShopping gs =new GroceryShopping(vegetableName, fruitName, vegetablePrice, fruitPrice);
  
  
  
System.out.println("\n--------------------------------------");
System.out.println("Grocery Shopping Menu");
System.out.println("\nName"+"\t\tPrice Per Pound");
System.out.println(gs.getVegetableName()+"\t"+gs.getVegetablePrice());
System.out.println(gs.getFruitName()+"\t\t"+gs.getFruitPrice());
System.out.println("--------------------------------------\n");
System.out.println("\nEnter the pounds of "+ gs.getVegetableName()+" ordered: ");
double vegetableOrdered=input.nextDouble();
System.out.println("Enter the pounds of "+ gs.getFruitName()+" ordered: ");
double fruitOrdered=input.nextDouble();   

//issue 1
gs.setVegetableOrdered(vegetableOrdered);
gs.setFruitOrdered(fruitOrdered);

System.out.println("\n\n\n--------------------------------------");
System.out.println("Grocery Shopping Order Summary");
System.out.println("\nName"+"\t\tPrice Per Pound");
System.out.println(vegetableName+"\t"+vegetablePrice);
System.out.println(fruitName+"\t\t"+fruitPrice);
  
gs.displayOrderSummary();
  
input.close();
  

  
  

}

private static void displayTable1()
{
System.out.println("Vegetable Name\t\t"+"Price Per Pound");
System.out.println("Broccoli\t\t"+"$3.12");
System.out.println("Yellow Onion\t\t"+"$1.15");
System.out.println("Chill Pepper\t\t"+"$4.58");
System.out.println("Greens Bundle\t\t"+"$2.82");
System.out.println("--------------------------------------");
System.out.println("Table 1: Vegetable names with corresponding price per pound");
}
  
  
private static void displayTable2()
{
System.out.println("\n--------------------------------------");
System.out.println("Fruit Name\t\t"+"Price Per Pound");
System.out.println("Apple\t\t\t"+"$1.73");
System.out.println("Grape\t\t\t"+"$2.15");
System.out.println("Key Lime\t\t"+"$2.58");
System.out.println("Navel Orange\t\t"+"$1.86");
System.out.println("--------------------------------------");
System.out.println("Table 2: Fruit names with corresponding price per pound ");
}
  
}

// Issue 1: missed setting the quantity so its always taking quantity as 0

// Issue 2: read string with spaces we need to use nextLine() not next that is the reason for you only single work names are working

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList;...
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList; public Postfix(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } public void reset(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } private boolean isOperator(char c) { if(c=='-'||c=='+'||c=='*'||c=='/') return true; return false; } private boolean isParenthesis(char c) { if(c == '(' || c==')') return true;...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>();...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>(); } public void addEdge(String v, String w) { // Put v in w's SET and w in v's SET. if (!st.contains(v)) st.put(v, new SET<String>()); if (!st.contains(w)) st.put(w, new SET<String>()); st.get(v).add(w); st.get(w).add(v); } public Iterable<String> adjacentTo(String v) { return st.get(v); } public Iterable<String> vertices() { return st.keys(); } // See Exercises 4.5.1-4 for V(), E(), degree(), // hasVertex(), and hasEdge(). public static void main(String[] args)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } } package construction; public class ContractorBids{ // assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } } You are doing renovations on your building, and multiple contractors...
public class Person { private String name; public Person() { name = "No name yet"; }...
public class Person { private String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } } 2- Write a Program Patient. Java Class that extends Person to include  Social security Gender  Appropriate construtors, accessors, and mutators....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT