Instructions
Objective
Write a java homework program to create shopping system.
Requirements and Specifications
Source Code
ELECTRONIC ITEM
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package shopping;
/**
*
* @author Eduardo
*/
public class ElectronicItem extends ShoppingItem {
private int storage;
public ElectronicItem(String name, int quantity, double price, int storage)
{
super(name, quantity, price);
this.storage = storage;
}
public int getStorage() {return storage;}
public void setStorage(int newStorage) {storage = newStorage;}
@Override
public String toString(){
return "(Electronics) " + super.toString() + String.format(", Storage: %d GB", getStorage());
}
}
FOOD ITEM
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package shopping;
/**
*
* @author Eduardo
*/
public class FoodItem extends ShoppingItem {
private int calories;
private double proteins;
public FoodItem(String name, int quantity, double price, int calories, double proteins)
{
super(name, quantity, price);
this.calories = calories;
this.proteins = proteins;
}
public int getCalories() {return calories;}
public double getProteins() {return proteins;}
public void setCalories(int newCalories) {calories = newCalories;}
public void setProteins(double newProteins) {proteins = newProteins;}
@Override
public String toString(){
return "(Food) " + super.toString() + String.format(", Calories: %d, Proteins: %.1f%%", getCalories(), getProteins()*100.0);
}
}
SHOPPING JAVA
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package shopping;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Ken
*/
public class Shopping {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create Scanner
Scanner sc = new Scanner(System.in);
ShoppingBag myBag = new ShoppingBag(); //this is myBag. I can buy things to put in it
// Create a list of all available items in the shop
List stock = new ArrayList();
// add some items as examples, like food and electronics
stock.add(new FoodItem("Bag of 10x oranges", 10, 0.499, 100, 0.05));
stock.add(new FoodItem("Bread", 1, 0.5, 140, 0.1));
stock.add(new ElectronicItem("microSD Kingston", 1, 59.99, 512));
stock.add(new ElectronicItem("Crucial SSD", 1, 69.99, 1000));
// start program
while(true)
{
// dsplay menu
displayMainMenu();
System.out.println("Please enter an option: ");
// ask for main menu
int option = getMenuOption(sc, 1, 3);
System.out.println("");
if(option == 1) // buy item
{
// display items
displayAvailableItems(stock);
System.out.println("Please enter an option: ");
// ask for item id
int index = getMenuOption(sc, 1, stock.size());
System.out.println("");
// get item by index
ShoppingItem item = stock.get(index-1);
// add item to the bag
myBag.buyItem(item);
// display item info
System.out.println("The following item has been added to your bag: " + item.toString());
}
else if(option == 2) // list items
{
myBag.printBag();
}
else if(option == 3) // print bag and leave
{
System.out.println("Here is a summary of your orders:");
myBag.printBag();
break;
}
}
sc.close();
}
// method to display the menu
public static void displayMainMenu()
{
System.out.println("1) Buy Item");
System.out.println("2) List all items in bag");
System.out.println("3) Leave Shop");
}
public static void displayAvailableItems(List items)
{
// get number of items
int N = items.size();
// assign one id to each item
int i = 1;
for(ShoppingItem item: items)
{
System.out.println(String.format("%d) %s", i, item.toString()));
i++;
}
}
public static int getMenuOption(Scanner sc, int lb, int ub)
{
/*
This function asks to user for a menu option (int) that is between lb and ub
If the user enters a wrong option, the function will keep requesting for the correct one
*/
while(true)
{
int option = Integer.valueOf(sc.nextLine());
if(option >= lb && option <= ub)
return option;
else
System.out.println(String.format("Please enter an optionn between %d and %d.", lb, ub));
}
}
}
Related Samples
At ProgrammingHomeworkHelp.com, we understand the challenges students face with Java assignments. Our website offers a dedicated section for related Java samples, providing detailed, expertly crafted examples to guide you through complex programming tasks. Whether you're struggling with syntax, object-oriented principles, or advanced Java concepts, our samples are designed to help you grasp key ideas and improve your coding skills. With our comprehensive assignment support, you can confidently tackle your Java coursework and achieve academic success. Explore our Java samples today and enhance your programming proficiency.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java