以货币格式显示最终价格并从java文件中读取
我几乎完成了一个为学校期末作业写了很长时间的程序。它的99%完成,但我想添加一些东西,使它尽可能完美,需要帮助 我想以货币格式显示订单的最终价格,但只有在我的tostring方法中找到了一种将其格式化为货币的方法 如果我创建了一个从文件创建订单的方法,那么这也有额外的好处。(例如:订购1块里斯饼干、6块糖饼干、1块巧克力饼干和1勺焦糖冰淇淋) 这是我的密码。感谢您的帮助以货币格式显示最终价格并从java文件中读取,java,Java,我几乎完成了一个为学校期末作业写了很长时间的程序。它的99%完成,但我想添加一些东西,使它尽可能完美,需要帮助 我想以货币格式显示订单的最终价格,但只有在我的tostring方法中找到了一种将其格式化为货币的方法 如果我创建了一个从文件创建订单的方法,那么这也有额外的好处。(例如:订购1块里斯饼干、6块糖饼干、1块巧克力饼干和1勺焦糖冰淇淋) 这是我的密码。感谢您的帮助 package dessertshop; import java.text.NumberFormat; impo
package dessertshop;
import java.text.NumberFormat;
import java.util.Scanner;
abstract class DessertItem
{
protected String name;
public DessertItem()
{
this.name = "";
}
public DessertItem( String name )
{
this.name = name;
}
public final String getName()
{
return name;
}
public abstract double getCost(int number);
}
class Candy extends DessertItem
{
private double pricePerPound;
public Candy( String name, double unitPrice )
{
super( name );
this.pricePerPound = unitPrice;
}
@Override
public double getCost(int amount)
{
return( amount * pricePerPound );
}
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( "Candy\t" + name + "\t @ " + formatter.format( this.pricePerPound ) + " per pound");
}
}
class Cookie extends DessertItem
{
private double pricePerDozen;
public Cookie(String name, double pricePerDozen)
{
super(name);
this.pricePerDozen=pricePerDozen;
}
@Override
public double getCost(int amount)
{
return(amount * pricePerDozen)/12;
}
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( "Cookie\t" + name + "\t @ " + formatter.format( this.pricePerDozen) + " per dozen");
}
}
class IceCream extends DessertItem
{
private double cost;
public IceCream(String name, double cost)
{
super(name);
this.cost=cost;
}
@Override
public double getCost(int amount)
{
return(amount * cost);
}
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( "Ice Cream\t" + name + "\t @ " + formatter.format( this.cost ) + " per scoop");
}
}
public class DessertShop
{
private String name = "";
private DessertItem[] menu;
private int numberOfItems = 0;
public DessertShop(String name)
{
this.name = name;
menu = new DessertItem[200];
}
public DessertShop()
{
menu = new DessertItem[200];
}
public void addToMenu(DessertItem item)
{
menu[numberOfItems++] = item;
}
public void printMenu()
{
System.out.println(this.toString());
for (int i = 0; i < numberOfItems; i++)
{
System.out.print((i + 1) + ": " + menu[i].toString());
System.out.println("");
}
}
public double createNewOrder()
{
double totalCost = 0;
Scanner input = new Scanner(System.in);
while (true)
{
this.printMenu();
System.out.print("What would you like to purchase? (0 to checkout) > ");
int choice = input.nextInt();
if (choice == 0)
{
break;
}
System.out.print("How many (lbs. or amount) > ");
int amount = input.nextInt();
totalCost += menu[(choice - 1)].getCost(amount);
}
input.close();
return totalCost;
}
public String toString()
{
return ("Welcome to " + name);
}
public static void main( String[] args )
{
DessertShop shop01 = new DessertShop("Chuck D's Dessert Depot");
Candy candy01 = new Candy("Reece's Pieces", 3.99);
Candy candy02 = new Candy("Chocolate Covered Raisins", 4.99);
Cookie cookie01 = new Cookie("Peanut Butter", 5.99);
Cookie cookie02 = new Cookie("Chocolate Chip", 4.99);
Cookie cookie03 = new Cookie("Sugar", 4.50);
IceCream icecream01 = new IceCream("Cookie Dough", 3.00);
IceCream icecream02 = new IceCream("Vanilla", 2.00);
IceCream icecream03 = new IceCream("Caramel", 3.50);
IceCream icecream04 = new IceCream("Rocky Road", 2.99);
IceCream icecream05 = new IceCream("Mint Chocolate Chip", 3.99);
shop01.addToMenu(candy01);
shop01.addToMenu(candy02);
shop01.addToMenu(cookie01);
shop01.addToMenu(cookie02);
shop01.addToMenu(cookie03);
shop01.addToMenu(icecream01);
shop01.addToMenu(icecream02);
shop01.addToMenu(icecream03);
shop01.addToMenu(icecream04);
shop01.addToMenu(icecream05);
double frankOrder = shop01.createNewOrder();
System.out.println(frankOrder);
}
}
包装甜品店;
导入java.text.NumberFormat;
导入java.util.Scanner;
抽象类甜点
{
受保护的字符串名称;
公共甜点()
{
this.name=“”;
}
公共甜点项目(字符串名称)
{
this.name=名称;
}
公共最终字符串getName()
{
返回名称;
}
公开摘要(整数);
}
甜点类
{
私人双价每磅;
公共糖果(字符串名称,双倍单价)
{
超级(姓名);
这是每磅价格=单价;
}
@凌驾
公共双重成本(整数金额)
{
退货(金额*每磅价格);
}
公共字符串toString()
{
NumberFormat formatter=NumberFormat.getCurrencyInstance();
返回(“Candy\t”+name+“\t@”+formatter.format(this.pricePerPound)+“perpound”);
}
}
类Cookie扩展了DessertItem
{
私人双倍价格;
公共Cookie(字符串名称,双倍价格/打)
{
超级(姓名);
这个。价格每打=价格每打;
}
@凌驾
公共双重成本(整数金额)
{
退货(金额*每打价格)/12;
}
公共字符串toString()
{
NumberFormat formatter=NumberFormat.getCurrencyInstance();
返回(“Cookie\t”+name+“\t@”+formatter.format(this.pricepertide)+“每打”);
}
}
高级冰淇淋和甜点
{
私人双重成本;
公共冰淇淋(字符串名称,双倍成本)
{
超级(姓名);
成本=成本;
}
@凌驾
公共双重成本(整数金额)
{
退货(金额*成本);
}
公共字符串toString()
{
NumberFormat formatter=NumberFormat.getCurrencyInstance();
返回(“冰淇淋\t”+name+“\t@”+formatter.format(this.cost)+“每勺”);
}
}
公营甜品店
{
私有字符串名称=”;
私人甜点[]菜单;
私有int numberOfItems=0;
公共甜品店(字符串名称)
{
this.name=名称;
菜单=新的甜点项目[200];
}
公共甜品店()
{
菜单=新的甜点项目[200];
}
公共无效添加菜单(甜品项)
{
菜单[numberOfItems++]=项目;
}
公共无效打印菜单()
{
System.out.println(this.toString());
对于(int i=0;i”;
int choice=input.nextInt();
如果(选项==0)
{
打破
}
系统输出打印(“多少(磅或金额)>”;
int amount=input.nextInt();
totalCost+=菜单[(选项-1)]。获取成本(金额);
}
input.close();
返回总成本;
}
公共字符串toString()
{
返回(“欢迎使用”+姓名);
}
公共静态void main(字符串[]args)
{
甜品店shop01=新甜品店(“Chuck D’s甜品店”);
Candy candy01=新糖果(“Reece's Pieces”,3.99);
Candy candy02=新糖果(“巧克力葡萄干”,4.99);
Cookie cookie01=新饼干(“花生酱”,5.99);
Cookie cookie02=新饼干(“巧克力片”,4.99);
Cookie cookie03=新饼干(“糖”,4.50);
冰淇淋冰淇淋01=新冰淇淋(“饼干面团”,3.00);
冰淇淋冰淇淋02=新冰淇淋(“香草”,2.00);
冰淇淋冰淇淋03=新冰淇淋(“焦糖”,3.50);
冰淇淋冰淇淋04=新冰淇淋(“洛基路”,2.99);
冰淇淋冰淇淋05=新冰淇淋(“薄荷巧克力片”,3.99);
shop01.addToMenu(candy01);
shop01.addToMenu(candy02);
shop01.addToMenu(cookie01);
shop01.addToMenu(cookie02);
shop01.addToMenu(cookie03);
shop01.addToMenu(冰淇淋01);
shop01.addToMenu(冰淇淋02);
shop01.addToMenu(冰淇淋03);
shop01.addToMenu(冰淇淋04);
shop01.addToMenu(冰淇淋05);
double frankOrder=shop01.createNewOrder();
系统输出打印项次(法兰克福订单);
}
}
根据您的需求,您可以在最后一行代码中设置价格格式:
系统输出打印项次(法兰克福订单)
看一看:
关于另一个问题,从文件读入,我建议您看看csv文件:以及如何读取它们:如果您不想太麻烦,您可以使用printf获得不错的结果:
System.out.printf("Price is: €%,d", 12342353563623L);
产量为:价格为:123423563623欧元
在“国旗”上:
结果将包括特定于区域设置的分组分隔符
完美的知道了