modifier()方法和remove()方法不起作用

时间:2018-03-10 03:23:43

标签: java

我有一个学校的章节作业。我一直在努力让代码工作。除了我的modify()和remove()方法之外,代码完美地运行。我一直在尝试我所知道的所有尝试,似乎没有任何工作。如果你能帮助我,我将不胜感激。

这是ItemToPurchase.java的代码

import java.util.Scanner;
public class ItemToPurchase{

private String itemName;
private int itemPrice;
private int itemQuantity;
private int Total;
private String itemDescription;   


public ItemToPurchase(){
  itemName = "none"; 
  itemPrice = 0; 
  itemQuantity = 0; 
  itemDescription = "none"; 
}

public ItemToPurchase(String itemName, String itemDescription,    
  int itemPrice, int itemQuantity){
  this.itemDescription = itemDescription;
  this.itemName = itemName;
  this.itemPrice = itemPrice;
  this.itemQuantity = itemQuantity;
}
public void setDescription(String itemDescription) {
  this.itemDescription = itemDescription;
}

public void setName(String itemName){
  this.itemName = itemName; 
}

public void setPrice(int itemPrice){
  this.itemPrice = itemPrice; 
}

public void setQuantity(int itemQuantity){
  this.itemQuantity = itemQuantity; 
}

public String getDescription() {
  return itemDescription;
}

public String getName(){
  return itemName; 
}

public int getPrice() {
  return itemPrice; 
}

public int getQuantity(){ 
  return itemQuantity;
}

public int getTotal(){
  Total = itemPrice * itemQuantity;
  return Total; 
}

public void printItemCost(){
  System.out.println(itemName + " " + itemQuantity + " @ $" + itemPrice  
  + "=  $" + itemPrice*itemQuantity);  

}
public void printItemDescription() {
  System.out.println(itemName + ":" + itemDescription);
} 

}

这是我的ShoppingCart.java代码

import java.util.Scanner;
import java.util.ArrayList;
public class ShoppingCart{


private String customerName;
private String currentDate;
private int Total;
private ArrayList <ItemToPurchase> cartItems = new ArrayList<ItemToPurchase> 
();

public ShoppingCart(){
  customerName = "none";
  currentDate = "January 1, 2016";
}

public ShoppingCart(String customerName, String currentDate){
  this.customerName = customerName;
  this.currentDate = currentDate;
}

public String getCustomerName(){
  return customerName;
}

public String getDate() {
  return currentDate;
}

public void addItem(ItemToPurchase item){
  cartItems.add(item);

  //Adds an item to cartItems array. Has parameter ItemToPurchase. Does not 
  return anything.
}

public void removeItem(String name){ //this does not work.
  while (!cartItems.isEmpty()){
    for (int i = 0; i < cartItems.size(); ++i){
      if (cartItems.get(i).getName().equals(name)){
        cartItems.remove(i);
      } else {
        System.out.println("Item not found in cart. Nothing removed.");
        System.out.println("");
        break;
      }
    }
    break;
  }

  //Removes item from cartItems array. Has a string (an item's name) 
parameter. Does not return anything.
  //If item name cannot be found, output this message: Item not found in cart. 
  Nothing removed.
}

public void modifyItem(ItemToPurchase item) { //this does not work.
  boolean found = false;;
  for (ItemToPurchase i: cartItems){
    if (i.getName().equals(item.getName())) {
      if (i.getQuantity()!=0){
        i.setQuantity(item.getQuantity());
      }
      found = true;
      break;
    }
  }
  if (!found){
    System.out.println("Item not found in cart. Nothing modified.");
    System.out.println("");
  }
}

public int getNumItemsInCart(){
  int sum = 0;
  for (ItemToPurchase i: cartItems){
    sum = sum + i.getQuantity();
  }
  return sum;
  //Returns quantity of all items in cart. Has no parameters
}

public int getCostOfCart() {
  int sum = 0;
  for (ItemToPurchase i: cartItems){
    sum = sum + i.getPrice()*i.getQuantity();
  }
  System.out.println("");
  System.out.println("Total: $" + sum);
  System.out.println("");
  return sum;

  //Determines and returns the total cost of items in cart. Has no parameters
}

public void printTotal() {
  int Total = 0;
  if (cartItems != null && cartItems.size() > 0) {
    System.out.println(customerName + "'s Shopping Cart - " + currentDate);
    for (ItemToPurchase item: cartItems){
      Total = Total + item.getQuantity();
    }
    System.out.println("Number of Items: " + Total);

    System.out.println();

    for (ItemToPurchase i: cartItems){
      i.printItemCost();
    }
    getCostOfCart();
  } else {

  System.out.println(customerName + "'s Shopping Cart - " + currentDate);

  System.out.println("Number of Items: 0");

  System.out.println();
  System.out.println("SHOPPING CART IS EMPTY");
  System.out.println("");
  System.out.println("Total: $0");
  System.out.println("");
}

  //Outputs total of objects in cart.
  //If cart is empty, output this message: SHOPPING CART IS EMPTY
}

public void printDescriptions() {

  System.out.println(customerName + "'s Shopping Cart - " + currentDate);

  System.out.println("");
  System.out.println("Item Descriptions");
  for (ItemToPurchase i: cartItems){
    i.printItemDescription();
    //Outputs each item's description.
  }
  System.out.println("");
}

}

这是我的ShoppingCartManager.java代码

import java.util.Scanner;
public class ShoppingCartManager{

public static void printMenu(ShoppingCart shoppingCart, Scanner scnr){

  String Input;
  int q = 0;
  int skipMenu = 0;
  do {

    if (skipMenu == 0){
      System.out.println("MENU");
      System.out.println("a - Add item to cart");
      System.out.println("d - Remove item from cart");
      System.out.println("c - Change item quantity");
      System.out.println("i - Output items' descriptions");
      System.out.println("o - Output shopping cart");
      System.out.println("q - Quit\n");
      System.out.println("Choose an option:");

    }

    Input = scnr.next();

    if (Input.equals("a")){
      scnr.nextLine();
      System.out.println("ADD ITEM TO CART");

      System.out.println("Enter the item name:");      

      String name = scnr.nextLine();

      System.out.println("Enter the item description:");

      String itemDescription = scnr.nextLine();

      System.out.println("Enter the item price:");

      int itemPrice = scnr.nextInt();

      System.out.println("Enter the item quantity:");

      int quantity = scnr.nextInt();
      System.out.println("");
      scnr.nextLine();

      ItemToPurchase item = new ItemToPurchase(name, itemDescription, itemPrice, quantity);

      shoppingCart.addItem(item);
      skipMenu = 0;

    } else if (Input.equals("d")) {
      scnr.nextLine();
      System.out.println("REMOVE ITEM FROM CART");

      System.out.println("Enter name of item to remove:");

      String name = scnr.nextLine();
      shoppingCart.removeItem(name);
      skipMenu = 0;

    } else if (Input.equals("c")) {
      scnr.nextLine();
      System.out.println("CHANGE ITEM QUANTITY");

      System.out.println("Enter the item name:");

      String name = scnr.nextLine();

      System.out.println("Enter the new quantity:");

      int quantity = scnr.nextInt();

      ItemToPurchase item = new ItemToPurchase();

      item.setQuantity(quantity);

      shoppingCart.modifyItem(item);
      skipMenu = 0;
    } else if(Input.equals("i")){
      System.out.println("OUTPUT ITEMS' DESCRIPTIONS");

      shoppingCart.printDescriptions();
      skipMenu = 0;

    } else if (Input.equals("o")){
      System.out.println("OUTPUT SHOPPING CART");

      shoppingCart.printTotal();
      skipMenu = 0;

    } else if (Input.equals("q")) {
      q = 1;
      skipMenu = 0;
    } else {
      System.out.println("Choose an option:");
      skipMenu = 1;
    }
  } while(q != 1);
}

public static void main(String [] args) {

  Scanner scnr = new Scanner(System.in);
  String userChar;

  System.out.println("Enter Customer's Name:");
  String customerName = scnr.nextLine();

  System.out.println("Enter Today's Date:");
  String currentDate = scnr.nextLine();
  System.out.println("");

  ShoppingCart s = new ShoppingCart();
  System.out.println("Customer Name: " + customerName);
  System.out.println("Today's Date: " + currentDate);
  System.out.println("");
  ShoppingCart shoppingCart = new ShoppingCart(customerName, currentDate);
  printMenu(shoppingCart, scnr);

}

}

0 个答案:

没有答案