将硬币对象添加到钱包对象

问题描述 投票:0回答:1

我想创建一个程序,您可以在钱包中添加和删除硬币,并显示钱包中的总金额。全部通过 OOP。我创建了一个 while 循环,其中有多个选项可供用户选择。分别是添加金币、显示金币数量、从钱包中删除金币。

我正在努力解决的是如何跟踪这些硬币,例如:如果我添加一毛钱 2 次和四分之一 3 次,我希望程序计算这些次数 + 值并将其添加到钱包中,计算总计。

主课

import java.text.DecimalFormat;
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
     Coin myCoin = new Coin();
    Wallet myWallet = new Wallet();

  while(true) {

    Scanner input = new Scanner(System.in);

    // Options

    System.out.println("Select an option: ");
    System.out.println("1. Enter a coin to add");
    System.out.println("2. Display number of coins");
    System.out.println("3. Remove Coins from Wallet");
    System.out.println("4. Exit Program");
    int choice = input.nextInt();

    // If statements for 1

    if (choice == 1) {
      System.out.println("Enter the coin name: ");
      String coin = input.next();
      if (coin.equals("Dime")) {
        myCoin.setName("Dime");
        System.out.println(myCoin.getName()); }
        
       else if (coin.equals("Quarter")) {
        myCoin.setName("Quarter");
        System.out.println(myCoin.getName());
      }
      else if (coin.equals("Nickel")) {
        myCoin.setName("Nickel");
        System.out.println(myCoin.getName());
      }
      else if (coin.equals("Loonie")) {
        myCoin.setName("Loonie");
        System.out.println(myCoin.getName());
      }

      else if (coin.equals("Toonie")) {
        myCoin.setName("Toonie");
        System.out.println(myCoin.getName());
      }
    }

    

    // If statements for 2

  if (choice == 2) {
    System.out.println("Coins in your wallet: ");
    System.out.println("Toonies: " + myWallet.getToonies());
    System.out.println("Loonies: " + myWallet.getLoonie());
    System.out.println("Quarters: " + myWallet.getQuarter());
    System.out.println("Dimes: " + myWallet.getDimes());
    System.out.println("Nickels: " + myWallet.getNickel());

    System.out.println("Total Money: $" + new DecimalFormat("#0.00").format(myWallet.totalCoins()));
  }

    // If statements for 3
    
    if (choice == 3) {
      System.out.println("Enter coin to remove: ");
      String removeCoin = input.next();
      System.out.println("Enter the amount of coins: ");
      String amountRemoved = input.next();
    }
    
    // If statements for 4
    if (choice == 4) {
     
      System.exit(0);
    }
  }
  }
}

币类

import java.text.DecimalFormat;
class Coin {

private String name;
private double value;



  
  public void setName(String name) { 
  this.name = name;

    //dime 
    if (name.equals("Dime")) {
     this.value = 0.1;
    }
    //quarter
    if (name.equals("Quarter")) {
     this.value = 0.25;
    }
    //nickel
    if (name.equals("Nickel")) {
      this.value = 0.05;
    }
    //loonie
    if (name.equals("Loonie")) {
      this.value = 1.0;
    }
    //toonie
    if (name.equals("Toonie")) {
      this.value = 2.0;
    }
    
  }
    

  public String getName() {
  return "You added a " + this.name + ". Value is $" + new DecimalFormat("#0.00").format(this.value);
  }

}

钱包类

import java.util.ArrayList;

class Wallet {

  private int toonies;
  private int loonie;
  private int quarter;
  private int dimes;
  private int nickel;

// array list

  private ArrayList<String> coins = new ArrayList<>();

  
  //setters

  public void setToonies(int toonies) {
    this.toonies = toonies;
  }
  public void setLoonie(int loonie) {
    this.loonie = loonie;
  }
  public void setQuarter(int quarter) {
    this.quarter = quarter;
  }
  public void setDimes(int dimes) {
    this.dimes = dimes;
  }
  public void setNickel(int nickel) {
    this.nickel = nickel;
  }

  //getters

  public int getToonies() {
    return toonies;
  }
  public int getLoonie() {
    return loonie;
  }
  public int getQuarter() {
    return quarter;
  }
  public int getDimes() {
    return dimes;
  }
  public int getNickel() {
    return nickel;
  }

  // total number of coins

  public int totalCoins() {
    return toonies + loonie + quarter + dimes + nickel;
  }

  
  
}

我尝试使用增量来计算 Coin 类中的不同硬币,但它们对我来说不起作用,我正在尝试使用 ArrayList 因为我很确定我需要它来存储硬币,但我对这种代码不太熟悉,所以我对如何解决这个问题感到困惑。

另外,我不确定我的问题的解释是否连贯,所以请随时要求澄清我所说的任何内容

java object arraylist
1个回答
0
投票

首先,您有一个

Coin
类,并且正在为其创建一个对象,但如果用户选择选项 1,则根本不会将其添加到钱包中。 将
Coin
添加到
Wallet
Wallet
类必须有一个以
Coin
作为参数的构造函数。我对你的代码做了一些修改

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    
  public static void main(String[] args) {
      
      Wallet wallet = new Wallet();

      while(true) {
    
        Scanner input = new Scanner(System.in);
    
        // Options
    
        System.out.println("Select an option: ");
        System.out.println("1. Enter a coin to add");
        System.out.println("2. Display number of coins");
        System.out.println("3. Remove Coins from Wallet");
        System.out.println("4. Exit Program");
        int choice = input.nextInt();
    
        // If statements for 1
    
        if (choice == 1) {
          System.out.println("Enter the coin name: ");
          String coinName = input.next().trim();
          Coin coin = null;
          if(coinName.equals("Dime") || coinName.equals("Nickel") || coinName.equals("Loonie") || coinName.equals("Toonie") || coinName.equals("Quarter")) {
              coin = new Coin(coinName);
          }
          if(coin != null) {
              wallet.addCoin(coin);
              System.out.println("Coin added : " + coin.getName() + " " + coin.getValue());
              System.out.println("Wallet total amount : " + wallet.gettotalAmount());
          }
        }
    
        if (choice == 2) {
          System.out.println("Coins in your wallet: ");
          System.out.println("Toonies: " + wallet.getToonies());
          System.out.println("Loonies: " + wallet.getLoonies());
          System.out.println("Quarters: " + wallet.getQuarters());
          System.out.println("Dimes: " + wallet.getDimes());
          System.out.println("Nickels: " + wallet.getNickels());
    
          System.out.println("Total Money: $" + new DecimalFormat("#0.00").format(wallet.gettotalAmount()));
        }
    
        // If statements for 3
        
        if (choice == 3) {
          System.out.println("Enter coin to remove: ");
          String removeCoin = input.next();
          System.out.println("Enter the amount of coins: ");
          int removeAmount = input.nextInt();
          double amountRemoved = wallet.removeCoins(removeCoin, removeAmount);
          if(amountRemoved == 0.0) {
              System.out.println("You do not have enough coins");
              System.out.println("Wallet total amount : " + wallet.gettotalAmount());
          }
          else {
              System.out.println("Amount removed : $ " + amountRemoved);
          }
        }
        
        // If statements for 4
        if (choice == 4) {
          System.exit(0);
        }
     }
  }
}

class Coin {

private String name;
private double value;

  Coin(String name) { 
      
  this.name = name;

    if (name.equals("Dime")) {
     this.value = 0.1;
    }
    
    if (name.equals("Quarter")) {
     this.value = 0.25;
    }
    
    if (name.equals("Nickel")) {
      this.value = 0.05;
    }
    
    if (name.equals("Loonie")) {
      this.value = 1.0;
    }
    
    if (name.equals("Toonie")) {
      this.value = 2.0;
    }
    
  }
    

  public String getName() {
      return name;
  }
  
  public double getValue() {
      return value;
  }

}


class Wallet {

  private int toonies;
  private int loonies;
  private int quarters;
  private int dimes;
  private int nickels;
  
  private double totalAmount;
  Wallet() {
      toonies = 0;
      loonies = 0;
      quarters = 0;
      dimes = 0;
      nickels = 0;
      totalAmount = 0;
  }
  
  public void addCoin(Coin coin) {
      String name = coin.getName();
      switch(name) {
      case "Dime":
          dimes++;
          totalAmount+=coin.getValue();
          break;
      case "Quarter":
          quarters++;
          totalAmount+=coin.getValue();
          break;
      case "Nickel":
          nickels++;
          totalAmount+=coin.getValue();
          break;
      case "Loonie":
          loonies++;
          totalAmount+=coin.getValue();
          break;
      case "Toonie":
          toonies++;
          totalAmount+=coin.getValue();
          break;
      }
            
  }

  public int getToonies() {
    return toonies;
  }
  public int getLoonies() {
    return loonies;
  }
  public int getQuarters() {
    return quarters;
  }
  public int getDimes() {
    return dimes;
  }
  public int getNickels() {
    return nickels;
  }

  public int gettotalCoins() {
    return toonies + loonies + quarters + dimes + nickels;
  }
  
  public double gettotalAmount() {
     return totalAmount;
  }
  
  public double removeCoins(String name,int amount) {
      int currentAmount = 0;
      switch(name) {
          case "Dime":
              if(dimes - amount >= 0) {
                  dimes-=amount;
                  totalAmount-=amount*0.1;
                  return amount*0.1;
              }
              break;
          case "Quarter":
              if(quarters - amount >= 0) {
                  quarters-=amount;
                  totalAmount-=amount*0.25;
                  return amount*0.25;
              }
              break;
          case "Nickel":
              if(nickels - amount >= 0) {
                  nickels-=amount;
                  totalAmount-=amount*0.05;
                  return amount*0.05;
              }
              break;
          case "Loonie":
              if(loonies - amount >= 0) {
                  loonies-=amount;
                  totalAmount-=amount*1.0;
                  return amount*1.0;
              }
              break;
          case "Toonie":
              if(toonies - amount >= 0) {
                  toonies-=amount;
                  totalAmount-=amount*2.0;
                  return amount*2.0;
              }
              break;
          default:
              break;
      }
      return 0.0;
  }
  
}

一切都几乎是不言自明的,但可以给你一些总体的想法。在从用户获取添加硬币的输入时,如果它与我们的有效类型之一(即 Dime、Nickel 等)匹配,我们将使用提供的名称创建一个硬币对象。

Coin
类中的构造函数负责为其赋值。然后我们将该对象添加到钱包对象中。现在
Wallet
类有一个采用
Coin
的构造函数。 该构造函数将硬币的价值添加到总金额中,并且还增加了硬币的数量。 在删除时,我们提供硬币的名称和数量,如果它小于我们钱包中的数量,则返回 0.0 。否则,我们将从总金额中减去金额并退还扣除的金额。

注意:这不是创建硬币和钱包的最佳方式,我刚刚将您的代码变成了可用的代码。您可以通过查看代码的工作原理找到一种自行改进代码的方法

更好地实现这一点的另一种方法是首先创建一个

Coin
类,然后创建其他类型的硬币,例如 Nickel、Dime 等,作为它的子类。这将为您提供一些额外的灵活性。 例如,

class Coin {
   double value;
}
class Nickel extends Coin{
   Nickel() {
       this.value = 0.05;
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.