项目名
水果摊售货系统
项目描述
该系统类似于超市收银系统,由收银员 *** 作,包括添加商品进购物车,结账以及找零
控制台界面
具体功能
- 查看水果列表
- 购买水果
可以是输入水果名,也可以是输入水果编号
- 查看购物车
- 结账
- 退出
要求
使用数组或集合完成数据存储
使用IO流实现数据的读入和写出
独立完成,不会可以请教一下其他人
父类:水果类
public class Fruit {
private String name;
private double price;
private int num;
Fruit(){
}
public Fruit(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Fruit{" +
"name='" + name + '\'' +
", price=" + price +
", num=" + num +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
几种水果的子类,继承于水果类
public class apple extends Fruit{
public apple(String name, double price) {
super(name, price);
}
//子类应该不用添加get和set方法吧。
}
class pear extends Fruit{
public pear(String name, double price) {
super(name, price);
}
}
class banana extends Fruit{
public banana(String name,double price) {
super(name, price);
}
}
class putao extends Fruit{
public putao(String name, double price) {
super(name, price);
}
}
购物,结账,查看购物车:
import java.util.Scanner; public class BuyCar { Scanner g = new Scanner(System.in); static int p = 0; static double s = 0; static Fruit[] F = new Fruit[4];//这是对象数组,储存对象 public static void bu(Fruit fruit) { F[p++] = fruit; }//用这个方法来添加水果 public void sum() { int sumPrice=0; for (int i = 0; i < p; i++) { sumPrice += F[i].getPrice() * F[i].getNum();//用这个循环来算买水果花了多少钱。 System.out.println("您这次一共消费了" + sumPrice + "元"); } Scanner scanner = new Scanner(System.in); System.out.println("请支付:"); int i = scanner.nextInt(); int sum=i-sumPrice; if(sum >=0){ System.out.println("找零:"+sum+"元"); }else { System.out.println("还需支付"+sum+"元"); }//这个是最后的支付环节的 *** 作 Fivechoose fivechoose = new Fivechoose(); fivechoose.ty(); fivechoose.opo(); } public void see() { System.out.println("您的购物车如下:"); for (int j = 0; j < p; j++) { System.out.println(F[j].toString());//查看购物车,调用tostring方法,来看水果的具体信息。 } } }
实现用户点击按键实现相应需求:
import java.util.Scanner;
public class Fivechoose {
public void ty(){
System.out.println("请选择业务类型");
System.out.println("1.查看水果列表,2.购买水果,3.查看购物车,4.结账,5.退出");
}
public void opo() {
shopping n = new shopping();
BuyCar b = new BuyCar();
FruitList l = new FruitList();
// ty();
Scanner v = new Scanner(System.in);
switch (v.nextInt()) {
case 1:
l.ma();
System.out.println("======================");
ty();
opo();
break;
case 2:
ty();
l.ma();
n.main();
break;
case 3:
b.see();
ty();
opo();
break;
case 4:
b.sum();
break;
case 5:
n.exit();
break;
default:
System.out.println("菜单无此选项,请重新选择");
opo();
}
}}
业务列表:
class business{
public static void ty(){
System.out.println("请选择业务类型");
System.out.println("1.查看水果列表,2.购买水果,3.查看购物车,4.结账,5.退出");
}
}
录入水果信息:
import java.util.Scanner;
public class FruitList {
public void ma() {
Scanner s=new Scanner(System.in);
Fruit a=new apple("苹果",5.0);//5.6.7.10
Fruit pe=new apple("梨",6.0);//5.6.7.10
Fruit ba=new apple("香蕉",7.0);//5.6.7.10
Fruit pu=new apple("葡萄",10.0);//5.6.7.10
System.out.println("水果名:"+a.getName()+"单价:"+a.getPrice()+"/斤");
System.out.println("水果名:"+ pe.getName()+"单价:"+pe.getPrice()+"/斤");
System.out.println("水果名:"+ba.getName()+"单价:"+ba.getPrice()+"/斤");
System.out.println("水果名:"+pu.getName()+"单价:"+pu.getPrice()+"/斤");
}
}
对于输入错误情况的处理:
import java.util.Scanner;
public class InputEorr {
shopping up=new shopping();
Scanner sc =new Scanner(System.in);
int mi=sc.nextInt();
public void i2(){
switch (mi){
case 1:
new Fivechoose().ty();
up.main();
break;
case 0:
Fivechoose i2=new Fivechoose();
i2.opo();
break;
default:
System.out.println("输入有误,请重新输入");
}
}
}
判断输入的是数字还是字符从而跳转到相应 *** 作:
import java.util.Scanner;
public class Jurge {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入数值:");
String s = input.next();
if (s != null && s.matches("^[0.0-9.0]+$")) {// [0-9]没办法识别小数,[0.0-9.0]可以识别小数和整数
System.out.println("输入是数字!");
double num = Double.parseDouble(s);
System.out.println(num);
} else {
System.out.println("输入值为字符!");
}
}
}
购物选项及其 *** 作:
import java.util.Scanner;
public class shopping {
Scanner sc = new Scanner(System.in);
BuyCar io = new BuyCar();
Fivechoose i8 = new Fivechoose();
public void main() {
System.out.println("--------------------");
System.out.println("请选择需要购买的商品");
Scanner input = new Scanner(System.in);
String s = input.nextLine();
if (s != null && s.matches("^[1.0-4.0]+$")) {
int sd = Integer.parseInt(s);
switch (sd) {
case 1:
Fruit a = new apple("苹果", 5.0);
io.bu(a);
System.out.println("请输入需要购买的数量");
a.setNum(sc.nextInt());
System.out.println("继续购物请输入1,返回请输入0");
int mi = sc.nextInt();
if (mi == 1) {
main();
} else if (mi == 0) {
new Fivechoose().ty();
i8.opo();
} else {
System.out.println("抱歉,您输入的选项不存在");
}
break;
case 2:
Fruit pe = new apple("梨", 6.0);
io.bu(pe);
System.out.println("请输入需要购买的数量");
pe.setNum(sc.nextInt());
System.out.println("继续购物请输入1,返回请输入0");
int y = sc.nextInt();
switch (y) {
case 1:
new Fivechoose().ty();
main();
break;
case 0:
new Fivechoose().ty();
i8.opo();
break;
default:
System.out.println("抱歉,您输入的选项不存在");
}
if (sc.nextInt() == 1) {
new Fivechoose().ty();
main();
} else if (sc.nextInt() == 0) {
new Fivechoose().ty();
}
break;
case 3:
Fruit ba = new apple("香蕉", 7.0);
io.bu(ba);
System.out.println("请输入需要购买的数量");
ba.setNum(sc.nextInt());
System.out.println("继续购物请输入1,返回请输入0");
int o = sc.nextInt();
switch (o) {
case 1:
new Fivechoose().ty();
main();
break;
case 0:
new Fivechoose().ty();
Fivechoose i2 = new Fivechoose();
i2.opo();
break;
default:
System.out.println("抱歉,您输入的选项不存在");
}
break;
case 4:
Fruit pu = new apple("葡萄", 10.0);
io.bu(pu);
System.out.println("请输入需要购买的数量");
pu.setNum(sc.nextInt());
System.out.println("继续购物请输入1,返回请输入0");
int m = sc.nextInt();
switch (m) {
case 1:
new Fivechoose().ty();
main();
break;
case 0:
new Fivechoose().ty();
Fivechoose i2 = new Fivechoose();
i2.opo();
break;
default:
System.out.println("抱歉,您输入的选项不存在");
}
break;
default:
System.out.println("您的输入有误,请重新输入");
main();
}
} else {
if (s.equals("苹果")) {
Fruit a = new apple("苹果", 5.0);
io.bu(a);
System.out.println("请输入需要购买的数量");
a.setNum(sc.nextInt());
System.out.println("继续购物请输入1,返回请输入0");
int m = sc.nextInt();
switch (m) {
case 1:
new Fivechoose().ty();
main();
break;
case 0:
new Fivechoose().ty();
Fivechoose i2 = new Fivechoose();
i2.opo();
break;
default:
System.out.println("抱歉,您输入的选项不存在");
}
} else if (s.equals("梨")) {
Fruit pe = new apple("梨", 6.0);
io.bu(pe);
System.out.println("请输入需要购买的数量");
pe.setNum(sc.nextInt());
System.out.println("继续购物请输入1,返回请输入0");
int m = sc.nextInt();
switch (m) {
case 1:
new Fivechoose().ty();
main();
break;
case 0:
new Fivechoose().ty();
Fivechoose i2 = new Fivechoose();
i2.opo();
break;
default:
System.out.println("抱歉,您输入的选项不存在");
}
} else if (s.equals("香蕉")) {
Fruit ba = new apple("香蕉", 7.0);
io.bu(ba);
System.out.println("请输入需要购买的数量");
ba.setNum(sc.nextInt());
System.out.println("继续购物请输入1,返回请输入0");
int u = sc.nextInt();
switch (u) {
case 1:
new Fivechoose().ty();
main();
break;
case 0:
new Fivechoose().ty();
Fivechoose i2 = new Fivechoose();
i2.opo();
break;
default:
System.out.println("抱歉,您输入的选项不存在");
}
} else if (s.equals("葡萄")) {
Fruit pu = new apple("葡萄", 10.0);
io.bu(pu);
System.out.println("请输入需要购买的数量");
pu.setNum(sc.nextInt());
System.out.println("继续购物请输入1,返回请输入0");
int m = sc.nextInt();
switch (m) {
case 1:
new Fivechoose().ty();
main();
break;
case 0:
new Fivechoose().ty();
Fivechoose i2 = new Fivechoose();
i2.opo();
break;
default:
System.out.println("抱歉,您输入的选项不存在");
}
} else {
System.out.println("您的输入有误,请重新输入");
main();
}
}
}
public void exit() {
System.exit(0);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)