Java超市找零

「已注销」 2020-03-08 06:02:06
有大神救救小白吗! 怎么用java编写超市找零问题啊 要求:1 购买三样以上商品,浮点数 2 顾客付款为整数 3 有合计(float) 收取(int)找零(int) 简单点的 现在刚开始学
...全文
279 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2020-03-09
  • 打赏
  • 举报
回复
引用 1 楼 King*的回复:
你是要思路还是直接来代码? 顺便再问下,你目前学到面向对象这里来了吗?
你混了好多其他的 只需要有要求那几条就好 你加这么多我好晕啊
「已注销」 2020-03-09
  • 打赏
  • 举报
回复
引用 6 楼 King*的回复:
[quote=引用 4 楼 Gerlias的回复:][quote=引用 3 楼 King* 的回复:]
import java.util.*;

public class Test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Customer cus = new Customer(2000);
		Cashier cas = new Cashier();
		cus.getCar().add(new Article("西瓜", (float) 2.5, 8));
		cus.getCar().add(new Article("牛奶", (float) 32.5, 2));
		cus.getCar().add(new Article("菠萝", (float) 7.5, 2));
		cus.getCar().add(new Article("面包", (float) 1.5, 2));
		cas.print(cus);
	}

}

class Article {
	private String name;// 商品名称
	private float price;// 商品单价
	private int counts;// 购买数量

	public Article(String name, float price, int counts) {
		this.name = name;
		this.price = price;
		this.counts = counts;
	}

	public int getCounts() {
		return counts;
	}

	public void setCounts(int counts) {
		this.counts = counts;
	}

	public String getName() {
		return name;
	}

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

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

}

class ShoppingCar {
	public List<Article> car;

	public ShoppingCar() {
		car = new ArrayList<Article>();
	}

	/**
	 * 往购物车放商品
	 * 
	 * @param article商品
	 */
	public void add(Article article) {
		this.car.add(article);
	}

	/**
	 * 从购物车删除商品
	 * 
	 * @param article商品
	 */
	public void remove(Article article) {
		this.car.remove(article);
	}

	/**
	 * 算出购物车商品总价
	 * 
	 * @return商品总价格
	 */
	public float total() {
		float total = 0;
		for (int i = 0; i < car.size(); i++) {
			Article temp = car.get(i);
			total += temp.getPrice() * temp.getCounts();
		}
		return total;
	}
}

class Customer {
	private float money;// 顾客带的总共的钱
	private ShoppingCar sc;// 顾客的购物车

	public float getMoney() {
		return money;
	}

	public void setMoney(float money) {
		this.money = money;
	}

	public Customer(float money) {
		this.money = money;
		sc = new ShoppingCar();
	}

	/**
	 * 往这个顾客的购物车中添加商品
	 * 
	 * @param article
	 */
	public void add(Article article) {
		sc.car.add(article);
	}

	/**
	 * 往这个顾客的购物车里删除商品
	 * 
	 * @param article
	 */
	public void remove(Article article) {
		sc.car.remove(article);
	}

	public List<Article> getCar() {
		return sc.car;
	}

	public ShoppingCar getSc() {
		return sc;
	}
}

class Cashier {

	/**
	 * 打印收银凭条
	 * 
	 * @param cs顾客
	 */
	public void print(Customer cs) {
		System.out.println("打印凭条:");
		System.out.println("商品名称" + "	" + "商品价格" + "	" + "商品数量" + "	");
		for (int i = 0; i < cs.getCar().size(); i++) {
			Article temp = cs.getCar().get(i);
			System.out.println(temp.getName() + "		" + temp.getPrice() + "		" + temp.getCounts());
		}
		if (cs.getMoney() - cs.getSc().total() > 0) {
			System.out.println("应付:" + cs.getSc().total());
			System.out.println("实付:" + cs.getMoney());
			System.out.println("找零:" + (cs.getMoney() - cs.getSc().total()));
		} else {
			System.out.println("您的钱不够!");
		}
	}
}
我给你写了一个,不知道你看不看得懂,拿去可以直接运行
如果要求买到的东西最多,请问代码应该怎么写?[/quote] 买到的东西最多?不太明白你的意思。我这个程序里,有哪些商品,单价多少,买的数量多少,都是通过创建对象时传参进去决定的,自己可以随便改。这个不涉及最值问题呀[/quote] 太复杂了这个 看不懂
King* 2020-03-08
  • 打赏
  • 举报
回复
引用 4 楼 Gerlias的回复:
[quote=引用 3 楼 King* 的回复:]
import java.util.*;

public class Test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Customer cus = new Customer(2000);
		Cashier cas = new Cashier();
		cus.getCar().add(new Article("西瓜", (float) 2.5, 8));
		cus.getCar().add(new Article("牛奶", (float) 32.5, 2));
		cus.getCar().add(new Article("菠萝", (float) 7.5, 2));
		cus.getCar().add(new Article("面包", (float) 1.5, 2));
		cas.print(cus);
	}

}

class Article {
	private String name;// 商品名称
	private float price;// 商品单价
	private int counts;// 购买数量

	public Article(String name, float price, int counts) {
		this.name = name;
		this.price = price;
		this.counts = counts;
	}

	public int getCounts() {
		return counts;
	}

	public void setCounts(int counts) {
		this.counts = counts;
	}

	public String getName() {
		return name;
	}

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

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

}

class ShoppingCar {
	public List<Article> car;

	public ShoppingCar() {
		car = new ArrayList<Article>();
	}

	/**
	 * 往购物车放商品
	 * 
	 * @param article商品
	 */
	public void add(Article article) {
		this.car.add(article);
	}

	/**
	 * 从购物车删除商品
	 * 
	 * @param article商品
	 */
	public void remove(Article article) {
		this.car.remove(article);
	}

	/**
	 * 算出购物车商品总价
	 * 
	 * @return商品总价格
	 */
	public float total() {
		float total = 0;
		for (int i = 0; i < car.size(); i++) {
			Article temp = car.get(i);
			total += temp.getPrice() * temp.getCounts();
		}
		return total;
	}
}

class Customer {
	private float money;// 顾客带的总共的钱
	private ShoppingCar sc;// 顾客的购物车

	public float getMoney() {
		return money;
	}

	public void setMoney(float money) {
		this.money = money;
	}

	public Customer(float money) {
		this.money = money;
		sc = new ShoppingCar();
	}

	/**
	 * 往这个顾客的购物车中添加商品
	 * 
	 * @param article
	 */
	public void add(Article article) {
		sc.car.add(article);
	}

	/**
	 * 往这个顾客的购物车里删除商品
	 * 
	 * @param article
	 */
	public void remove(Article article) {
		sc.car.remove(article);
	}

	public List<Article> getCar() {
		return sc.car;
	}

	public ShoppingCar getSc() {
		return sc;
	}
}

class Cashier {

	/**
	 * 打印收银凭条
	 * 
	 * @param cs顾客
	 */
	public void print(Customer cs) {
		System.out.println("打印凭条:");
		System.out.println("商品名称" + "	" + "商品价格" + "	" + "商品数量" + "	");
		for (int i = 0; i < cs.getCar().size(); i++) {
			Article temp = cs.getCar().get(i);
			System.out.println(temp.getName() + "		" + temp.getPrice() + "		" + temp.getCounts());
		}
		if (cs.getMoney() - cs.getSc().total() > 0) {
			System.out.println("应付:" + cs.getSc().total());
			System.out.println("实付:" + cs.getMoney());
			System.out.println("找零:" + (cs.getMoney() - cs.getSc().total()));
		} else {
			System.out.println("您的钱不够!");
		}
	}
}
我给你写了一个,不知道你看不看得懂,拿去可以直接运行
如果要求买到的东西最多,请问代码应该怎么写?[/quote] 买到的东西最多?不太明白你的意思。我这个程序里,有哪些商品,单价多少,买的数量多少,都是通过创建对象时传参进去决定的,自己可以随便改。这个不涉及最值问题呀
King* 2020-03-08
  • 打赏
  • 举报
回复
引用 4 楼 Gerlias的回复:
[quote=引用 3 楼 King* 的回复:]
import java.util.*;

public class Test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Customer cus = new Customer(2000);
		Cashier cas = new Cashier();
		cus.getCar().add(new Article("西瓜", (float) 2.5, 8));
		cus.getCar().add(new Article("牛奶", (float) 32.5, 2));
		cus.getCar().add(new Article("菠萝", (float) 7.5, 2));
		cus.getCar().add(new Article("面包", (float) 1.5, 2));
		cas.print(cus);
	}

}

class Article {
	private String name;// 商品名称
	private float price;// 商品单价
	private int counts;// 购买数量

	public Article(String name, float price, int counts) {
		this.name = name;
		this.price = price;
		this.counts = counts;
	}

	public int getCounts() {
		return counts;
	}

	public void setCounts(int counts) {
		this.counts = counts;
	}

	public String getName() {
		return name;
	}

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

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

}

class ShoppingCar {
	public List<Article> car;

	public ShoppingCar() {
		car = new ArrayList<Article>();
	}

	/**
	 * 往购物车放商品
	 * 
	 * @param article商品
	 */
	public void add(Article article) {
		this.car.add(article);
	}

	/**
	 * 从购物车删除商品
	 * 
	 * @param article商品
	 */
	public void remove(Article article) {
		this.car.remove(article);
	}

	/**
	 * 算出购物车商品总价
	 * 
	 * @return商品总价格
	 */
	public float total() {
		float total = 0;
		for (int i = 0; i < car.size(); i++) {
			Article temp = car.get(i);
			total += temp.getPrice() * temp.getCounts();
		}
		return total;
	}
}

class Customer {
	private float money;// 顾客带的总共的钱
	private ShoppingCar sc;// 顾客的购物车

	public float getMoney() {
		return money;
	}

	public void setMoney(float money) {
		this.money = money;
	}

	public Customer(float money) {
		this.money = money;
		sc = new ShoppingCar();
	}

	/**
	 * 往这个顾客的购物车中添加商品
	 * 
	 * @param article
	 */
	public void add(Article article) {
		sc.car.add(article);
	}

	/**
	 * 往这个顾客的购物车里删除商品
	 * 
	 * @param article
	 */
	public void remove(Article article) {
		sc.car.remove(article);
	}

	public List<Article> getCar() {
		return sc.car;
	}

	public ShoppingCar getSc() {
		return sc;
	}
}

class Cashier {

	/**
	 * 打印收银凭条
	 * 
	 * @param cs顾客
	 */
	public void print(Customer cs) {
		System.out.println("打印凭条:");
		System.out.println("商品名称" + "	" + "商品价格" + "	" + "商品数量" + "	");
		for (int i = 0; i < cs.getCar().size(); i++) {
			Article temp = cs.getCar().get(i);
			System.out.println(temp.getName() + "		" + temp.getPrice() + "		" + temp.getCounts());
		}
		if (cs.getMoney() - cs.getSc().total() > 0) {
			System.out.println("应付:" + cs.getSc().total());
			System.out.println("实付:" + cs.getMoney());
			System.out.println("找零:" + (cs.getMoney() - cs.getSc().total()));
		} else {
			System.out.println("您的钱不够!");
		}
	}
}
我给你写了一个,不知道你看不看得懂,拿去可以直接运行
如果要求买到的东西最多,请问代码应该怎么写?[/quote] 买到的东西最多?不太明白你的意思。我这个程序里,有哪些商品,单价多少,买的数量多少,都是通过创建对象时传参进去决定的,自己可以随便改。这个不涉及最值问题呀
Gerlias 2020-03-08
  • 打赏
  • 举报
回复
引用 3 楼 King* 的回复:
import java.util.*;

public class Test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Customer cus = new Customer(2000);
		Cashier cas = new Cashier();
		cus.getCar().add(new Article("西瓜", (float) 2.5, 8));
		cus.getCar().add(new Article("牛奶", (float) 32.5, 2));
		cus.getCar().add(new Article("菠萝", (float) 7.5, 2));
		cus.getCar().add(new Article("面包", (float) 1.5, 2));
		cas.print(cus);
	}

}

class Article {
	private String name;// 商品名称
	private float price;// 商品单价
	private int counts;// 购买数量

	public Article(String name, float price, int counts) {
		this.name = name;
		this.price = price;
		this.counts = counts;
	}

	public int getCounts() {
		return counts;
	}

	public void setCounts(int counts) {
		this.counts = counts;
	}

	public String getName() {
		return name;
	}

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

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

}

class ShoppingCar {
	public List<Article> car;

	public ShoppingCar() {
		car = new ArrayList<Article>();
	}

	/**
	 * 往购物车放商品
	 * 
	 * @param article商品
	 */
	public void add(Article article) {
		this.car.add(article);
	}

	/**
	 * 从购物车删除商品
	 * 
	 * @param article商品
	 */
	public void remove(Article article) {
		this.car.remove(article);
	}

	/**
	 * 算出购物车商品总价
	 * 
	 * @return商品总价格
	 */
	public float total() {
		float total = 0;
		for (int i = 0; i < car.size(); i++) {
			Article temp = car.get(i);
			total += temp.getPrice() * temp.getCounts();
		}
		return total;
	}
}

class Customer {
	private float money;// 顾客带的总共的钱
	private ShoppingCar sc;// 顾客的购物车

	public float getMoney() {
		return money;
	}

	public void setMoney(float money) {
		this.money = money;
	}

	public Customer(float money) {
		this.money = money;
		sc = new ShoppingCar();
	}

	/**
	 * 往这个顾客的购物车中添加商品
	 * 
	 * @param article
	 */
	public void add(Article article) {
		sc.car.add(article);
	}

	/**
	 * 往这个顾客的购物车里删除商品
	 * 
	 * @param article
	 */
	public void remove(Article article) {
		sc.car.remove(article);
	}

	public List<Article> getCar() {
		return sc.car;
	}

	public ShoppingCar getSc() {
		return sc;
	}
}

class Cashier {

	/**
	 * 打印收银凭条
	 * 
	 * @param cs顾客
	 */
	public void print(Customer cs) {
		System.out.println("打印凭条:");
		System.out.println("商品名称" + "	" + "商品价格" + "	" + "商品数量" + "	");
		for (int i = 0; i < cs.getCar().size(); i++) {
			Article temp = cs.getCar().get(i);
			System.out.println(temp.getName() + "		" + temp.getPrice() + "		" + temp.getCounts());
		}
		if (cs.getMoney() - cs.getSc().total() > 0) {
			System.out.println("应付:" + cs.getSc().total());
			System.out.println("实付:" + cs.getMoney());
			System.out.println("找零:" + (cs.getMoney() - cs.getSc().total()));
		} else {
			System.out.println("您的钱不够!");
		}
	}
}
我给你写了一个,不知道你看不看得懂,拿去可以直接运行
如果要求买到的东西最多,请问代码应该怎么写?
King* 2020-03-08
  • 打赏
  • 举报
回复
import java.util.*;

public class Test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Customer cus = new Customer(2000);
		Cashier cas = new Cashier();
		cus.getCar().add(new Article("西瓜", (float) 2.5, 8));
		cus.getCar().add(new Article("牛奶", (float) 32.5, 2));
		cus.getCar().add(new Article("菠萝", (float) 7.5, 2));
		cus.getCar().add(new Article("面包", (float) 1.5, 2));
		cas.print(cus);
	}

}

class Article {
	private String name;// 商品名称
	private float price;// 商品单价
	private int counts;// 购买数量

	public Article(String name, float price, int counts) {
		this.name = name;
		this.price = price;
		this.counts = counts;
	}

	public int getCounts() {
		return counts;
	}

	public void setCounts(int counts) {
		this.counts = counts;
	}

	public String getName() {
		return name;
	}

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

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

}

class ShoppingCar {
	public List<Article> car;

	public ShoppingCar() {
		car = new ArrayList<Article>();
	}

	/**
	 * 往购物车放商品
	 * 
	 * @param article商品
	 */
	public void add(Article article) {
		this.car.add(article);
	}

	/**
	 * 从购物车删除商品
	 * 
	 * @param article商品
	 */
	public void remove(Article article) {
		this.car.remove(article);
	}

	/**
	 * 算出购物车商品总价
	 * 
	 * @return商品总价格
	 */
	public float total() {
		float total = 0;
		for (int i = 0; i < car.size(); i++) {
			Article temp = car.get(i);
			total += temp.getPrice() * temp.getCounts();
		}
		return total;
	}
}

class Customer {
	private float money;// 顾客带的总共的钱
	private ShoppingCar sc;// 顾客的购物车

	public float getMoney() {
		return money;
	}

	public void setMoney(float money) {
		this.money = money;
	}

	public Customer(float money) {
		this.money = money;
		sc = new ShoppingCar();
	}

	/**
	 * 往这个顾客的购物车中添加商品
	 * 
	 * @param article
	 */
	public void add(Article article) {
		sc.car.add(article);
	}

	/**
	 * 往这个顾客的购物车里删除商品
	 * 
	 * @param article
	 */
	public void remove(Article article) {
		sc.car.remove(article);
	}

	public List<Article> getCar() {
		return sc.car;
	}

	public ShoppingCar getSc() {
		return sc;
	}
}

class Cashier {

	/**
	 * 打印收银凭条
	 * 
	 * @param cs顾客
	 */
	public void print(Customer cs) {
		System.out.println("打印凭条:");
		System.out.println("商品名称" + "	" + "商品价格" + "	" + "商品数量" + "	");
		for (int i = 0; i < cs.getCar().size(); i++) {
			Article temp = cs.getCar().get(i);
			System.out.println(temp.getName() + "		" + temp.getPrice() + "		" + temp.getCounts());
		}
		if (cs.getMoney() - cs.getSc().total() > 0) {
			System.out.println("应付:" + cs.getSc().total());
			System.out.println("实付:" + cs.getMoney());
			System.out.println("找零:" + (cs.getMoney() - cs.getSc().total()));
		} else {
			System.out.println("您的钱不够!");
		}
	}
}
我给你写了一个,不知道你看不看得懂,拿去可以直接运行
「已注销」 2020-03-08
  • 打赏
  • 举报
回复
引用 1 楼 King*的回复:
你是要思路还是直接来代码? 顺便再问下,你目前学到面向对象这里来了吗?
代码 对啊 面向对象程序设计
King* 2020-03-08
  • 打赏
  • 举报
回复
你是要思路还是直接来代码? 顺便再问下,你目前学到面向对象这里来了吗?

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧