Question

In: Computer Science

Can you please take this code and just rewrite it so it can be easily be...

Can you please take this code and just rewrite it so it can be easily be able to put into a complier. in java

Implementation of above program in java:

import java.util.HashMap;
import java.util.Scanner;
import java.util.Map.Entry;

public class Shopping_cart {
  
   static Scanner s= new Scanner (System.in);
  
//   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases
   static HashMap<String,Item> map= new HashMap<>();

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       getupdates();
   }
  
   static void getupdates() {
      
       System.out.println("Welcome to Shopping Cart. Enter:");
       System.out.println("1 : Add an Item ");
       System.out.println("2 : Add multiples Quantities of Given Item ");
       System.out.println("3 : Remove an unspecified Item ");
       System.out.println("4 : CheckOut");
       System.out.println("5 : Checkbudget");
       System.out.print("Choice: ");
       int selection= s.nextInt();
      
      
       switch(selection) {
       case 1:
          add_new_item();
          getupdates();
            
          case 2:
              Add_multiples_items();
              getupdates();
                
          case 3:
              remove_unspecified_data();
              getupdates();
                
          case 4:
                  checkout();
                  getupdates();
                 
          case 5:
                
              checkbudget();
              getupdates();
                    
          case 6:
              System.out.println("Stay Safe");
              break;
                
              default:
                  System.out.println("Invalid choice");
                   System.out.println("Please enter the valid choice");
                   getupdates();
                    
       }
   }

   public static int total_price() {
      
       int total_price=0;
       for (Entry<String, Item> entry : map.entrySet()) {
                  
                   Item it=map.get(entry.getKey());
                  
                   total_price+=it.getPrice()*it.get_quantity();
                  
               }
         
       return total_price;
      
   }
  
  
     
  
   private static void checkbudget() {
      
       System.out.print("Enter your Total Budget : ");
       int budget=s.nextInt();
      
       int totalprice=0;
  


System.out.println("Total Amount = "+total_price());

while(budget<total_price()) {
   totalprice = remove_unspecified_data();
     
     
     
   if(budget>=totalprice) {
       break;
   }
}


System.out.println();

System.out.println("Now updates cart : ");

checkout();

System.out.println();
      
   }

   private static void checkout() {
       System.out.println(" Here list of Items in Your Cart");
      
       int i=1;
       int total_price=0;
       System.out.println(" S.no Name of item Quantity price ");
       System.out.println(" ");
       for (Entry<String, Item> entry : map.entrySet()) {
          
           Item it=map.get(entry.getKey());
          
           System.out.println(" "+i+" "+entry.getKey()+" "+it.get_quantity()+" $"+it.getPrice()*it.get_quantity());
           total_price+=it.getPrice()*it.get_quantity();
           i++;
       }
      
       System.out.println();
      
       System.out.println("Total Amount = "+total_price);
      
       System.out.println();
       System.out.println();
   }

   private static int remove_unspecified_data() {
      
       System.out.print("Enter the item you want to remove from your cart : ");
       String name=s.next();
      
       Item it1=map.get(name);
      
       int quant=it1.get_quantity();
      
       if(quant>1) {
          
       quant--;
       it1.set_quantity(-1);
      
       map.put(name, it1);
       }
         
       else {
           map.remove(name, it1);
       }
      
       int total_price=total_price();
         
       total_price-=it1.getPrice();
         
       System.out.println();
       System.out.println();
      
       return total_price;
      
   }

   private static void Add_multiples_items() {
       System.out.print("Enter the item you want to add in your cart : ");
       String name=s.next();
      
System.out.println("Enter price of Item : ");
      
       int price=s.nextInt();
      
       System.out.print("Enter the quantity of that item you want to add : ");
       int quan=s.nextInt();
      
       if(map.containsKey(name)) {
           Item it1=map.get(name);
           it1.set_quantity(quan);
           map.put(name, it1);
       }
       else {
           Item it= new Item(name,price,quan);
           map.put(name, it);
       }
      
       System.out.println();
       System.out.println();
      
   }

   private static void add_new_item() {
       System.out.print("Enter the item you want to add in your cart : ");
      
       String name=s.next();
      
       System.out.println("Enter price of Item : ");
      
       int price=s.nextInt();
      
      
      
       if(map.containsKey(name)) {
           Item it1=map.get(name);
           it1.set_quantity(1);
           map.put(name, it1);
       }
       else {
           Item it= new Item(name,price,1);
           map.put(name, it);
       }
      
       System.out.println();
       System.out.println();
      
   }
  
  

}

class Item
{   
private String name;
private int price;
private int quantity; //in cents
  
//Constructor
public Item(String n, int p,int quantity)
{
name = n;
price = p;
this. quantity=quantity;
}
  
public boolean equals(Item other)
{
return this.name.equals(other.name) && this.price == other.price;
}
  
//displays name of item and price in properly formatted manner
public String toString()
{
return name + ", price: $" + price/100 + "." + price%100;
}
  
//Getter methods
public int getPrice()
{
return price;
}
  
public int get_quantity()
{
return quantity;
}
  
public void set_quantity(int qa)
{
   quantity+=qa;
}
  
public String getName()
{
return name;
}
}

Solutions

Expert Solution

import java.util.HashMap;
import java.util.Scanner;
import java.util.Map.Entry;

public class Shopping_cart {
  
   static Scanner s= new Scanner (System.in);
  
//   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases
   static HashMap<String,Item> map= new HashMap<>();

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       getupdates();
   }
  
   static void getupdates() {
      
       System.out.println("Welcome to Shopping Cart. Enter:");
       System.out.println("1 : Add an Item ");
       System.out.println("2 : Add multiples Quantities of Given Item ");
       System.out.println("3 : Remove an unspecified Item ");
       System.out.println("4 : CheckOut");
       System.out.println("5 : Checkbudget");
       System.out.print("Choice: ");
       int selection= s.nextInt();
      
      
       switch(selection) {
       case 1:
          add_new_item();
          getupdates();
            
          case 2:
              Add_multiples_items();
              getupdates();
                
          case 3:
              remove_unspecified_data();
              getupdates();
                
          case 4:
                  checkout();
                  getupdates();
                 
          case 5:
                
              checkbudget();
              getupdates();
                    
          case 6:
              System.out.println("Stay Safe");
              break;
                
              default:
                  System.out.println("Invalid choice");
                   System.out.println("Please enter the valid choice");
                   getupdates();
                    
       }
   }

   public static int total_price() {
      
       int total_price=0;
       for (Entry<String, Item> entry : map.entrySet()) {
                  
                   Item it=map.get(entry.getKey());
                  
                   total_price+=it.getPrice()*it.get_quantity();
                  
               }
         
       return total_price;
      
   }
  
  
     
  
   private static void checkbudget() {
      
       System.out.print("Enter your Total Budget : ");
       int budget=s.nextInt();
      
       int totalprice=0;
  


System.out.println("Total Amount = "+total_price());

while(budget<total_price()) {
   totalprice = remove_unspecified_data();
     
     
     
   if(budget>=totalprice) {
       break;
   }
}


System.out.println();

System.out.println("Now updates cart : ");

checkout();

System.out.println();
      
   }

   private static void checkout() {
       System.out.println(" Here list of Items in Your Cart");
      
       int i=1;
       int total_price=0;
       System.out.println(" S.no Name of item Quantity price ");
       System.out.println(" ");
       for (Entry<String, Item> entry : map.entrySet()) {
          
           Item it=map.get(entry.getKey());
          
           System.out.println(" "+i+" "+entry.getKey()+" "+it.get_quantity()+" $"+it.getPrice()*it.get_quantity());
           total_price+=it.getPrice()*it.get_quantity();
           i++;
       }
      
       System.out.println();
      
       System.out.println("Total Amount = "+total_price);
      
       System.out.println();
       System.out.println();
   }

   private static int remove_unspecified_data() {
      
       System.out.print("Enter the item you want to remove from your cart : ");
       String name=s.next();
      
       Item it1=map.get(name);
      
       int quant=it1.get_quantity();
      
       if(quant>1) {
          
       quant--;
       it1.set_quantity(-1);
      
       map.put(name, it1);
       }
         
       else {
           map.remove(name, it1);
       }
      
       int total_price=total_price();
         
       total_price-=it1.getPrice();
         
       System.out.println();
       System.out.println();
      
       return total_price;
      
   }

   private static void Add_multiples_items() {
       System.out.print("Enter the item you want to add in your cart : ");
       String name=s.next();
      
System.out.println("Enter price of Item : ");
      
       int price=s.nextInt();
      
       System.out.print("Enter the quantity of that item you want to add : ");
       int quan=s.nextInt();
      
       if(map.containsKey(name)) {
           Item it1=map.get(name);
           it1.set_quantity(quan);
           map.put(name, it1);
       }
       else {
           Item it= new Item(name,price,quan);
           map.put(name, it);
       }
      
       System.out.println();
       System.out.println();
      
   }

   private static void add_new_item() {
       System.out.print("Enter the item you want to add in your cart : ");
      
       String name=s.next();
      
       System.out.println("Enter price of Item : ");
      
       int price=s.nextInt();
      
      
      
       if(map.containsKey(name)) {
           Item it1=map.get(name);
           it1.set_quantity(1);
           map.put(name, it1);
       }
       else {
           Item it= new Item(name,price,1);
           map.put(name, it);
       }
      
       System.out.println();
       System.out.println();
      
   }
  
  

}

class Item
{   
private String name;
private int price;
private int quantity; //in cents
  
//Constructor
public Item(String n, int p,int quantity)
{
name = n;
price = p;
this. quantity=quantity;
}
  
public boolean equals(Item other)
{
return this.name.equals(other.name) && this.price == other.price;
}
  
//displays name of item and price in properly formatted manner
public String toString()
{
return name + ", price: $" + price/100 + "." + price%100;
}
  
//Getter methods
public int getPrice()
{
return price;
}
  
public int get_quantity()
{
return quantity;
}
  
public void set_quantity(int qa)
{
   quantity+=qa;
}
  
public String getName()
{
return name;
}
}


Related Solutions

Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import...
Can you please rewrite this Java code so it can be better understood. import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public static void main(String[] args) {        // TODO Auto-generated method stub        getupdates();    }...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with...
Note- can you please rewrite the code in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius
Rewrite this code of a game of Moropinzee so that it works as intended without the...
Rewrite this code of a game of Moropinzee so that it works as intended without the "break;" in the last few lines of the code. Code: import java.util.*; public class Moropinzee { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true) { System.out.println("Player 1 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:"); int p1 = sc.nextInt(); while(p1<1 || p1>5) { System.out.println("Invalid choice, Player 1. Enter a number 1-5:"); p1 = sc.nextInt(); } System.out.println("Player 2...
1. What is critical thinking, and can you define it easily, and if so, why not?...
1. What is critical thinking, and can you define it easily, and if so, why not? 2. 'If you want to think critically, knowing what critical thinking is half the battle': discuss.
Please, read the code below so you can figure out what is the steps that are...
Please, read the code below so you can figure out what is the steps that are used. (Answer in words) import java.util.*; import java.io.*; / class WordHelper{ public static String vowels = "aeiouyAEIOUY";    //method to check whether a given character is vowel or not public static String[] sortByVowels(String[] words){ Integer[] noOfVowels = new Integer[words.length]; String[] newArray = new String[words.length];    newArray[i] = words[i]; int cnt = 0; for(int j = 0; j < words[i].length(); j++){ String temp = Character.toString(words[i].charAt(j));...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
please right make it so that it can run on jGRASP and java code please thank...
please right make it so that it can run on jGRASP and java code please thank you Debug Problem 1: As an intern for NASA, you have been instructed to debug a java program that calculates the speed that sound travels in water. Details about the formulas and correct results appear in the comments area at the top of the program Here is the code to debug: importjava.util.Scanner; /**    This program demonstrates a solution to the    The Speed of Sound...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
NEED TO REWRITE THIS IN OOP MODE ######## HOMEWORK 19 ###### Rewrite the code in the...
NEED TO REWRITE THIS IN OOP MODE ######## HOMEWORK 19 ###### Rewrite the code in the OOP mode (class mode). ########################### lambda trick ##### first: changing label properties: after you've created a label, you ##### may want to change something about it. To do that, use configure method: ## label.configure(text = 'Yes') ## label.configure(bg = 'white', fg = 'black') ### that change the properties of a label called label. ################################################### ## from tkinter import * abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def callback(x):...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT